hurl icon indicating copy to clipboard operation
hurl copied to clipboard

parse curl's --cookie flag

Open dhth opened this issue 6 months ago • 7 comments

Resolves https://github.com/Orange-OpenSource/hurl/issues/3877.

Had a fundamental question regarding the scope of this feature. Since curl allows the contents of the cookie to be read from a file (and stdin), should hurl support this as well? If yes, any preferences on how this should be done?

From curl's man page:

-b, --cookie <data|filename>
       (HTTP) Pass the data to the HTTP server in the Cookie header. It is supposedly the data previously received from the server in a "Set-Cookie:" line.
       The data should be in the format "NAME1=VALUE1; NAME2=VALUE2". This makes curl use the cookie header with this content explicitly in all outgoing
       request(s). If multiple requests are done due to authentication, followed redirects or similar, they all get this cookie passed on.

       If no '=' symbol is used in the argument, it is instead treated as a filename to read previously stored cookie from. This option also activates the
       cookie engine which makes curl record incoming cookies, which may be handy if you are using this in combination with the -L, --location option or do
       multiple URL transfers on the same invoke.

       If the file name is exactly a minus ("-"), curl instead reads the contents from stdin. If the file name is an empty string ("") and is the only cookie
       input, curl will activate the cookie engine without any cookies.

       The file format of the file to read cookies from should be plain HTTP headers (Set-Cookie style) or the Netscape/Mozilla cookie file format.

       The file specified with -b, --cookie is only used as input. No cookies are written to the file. To store cookies, use the -c, --cookie-jar option.

       If you use the Set-Cookie file format and do not specify a domain then the cookie is not sent since the domain never matches. To address this, set a
       domain in Set-Cookie line (doing that includes subdomains) or preferably: use the Netscape format.

       Users often want to both read cookies from a file and write updated cookies back to a file, so using both -b, --cookie and -c, --cookie-jar in the same
       command line is common.

       If curl is built with PSL (Public Suffix List) support, it detects and discards cookies that are specified for such suffix domains that should not be
       allowed to have cookies. If curl is not built with PSL support, it has no ability to stop super cookies.

       --cookie can be used several times in a command line

       Examples:
        curl -b "" https://example.com
        curl -b cookiefile https://example.com
        curl -b cookiefile -c cookiefile https://example.com

dhth avatar Jun 20 '25 20:06 dhth

Hi @dhth, for a first start, we should only support the first mode of --cookie(NAME=VALUE) and not deals with file

jcamiel avatar Jun 21 '25 08:06 jcamiel

Hi @dhth, for a first start, we should only support the first mode of --cookie(NAME=VALUE) and not deals with file

Sounds good, that keeps it simple.

dhth avatar Jun 21 '25 09:06 dhth

@jcamiel I added validation for the --cookie flag using the crate cookie. Let me know if you'd prefer to not add a dependency for this.

dhth avatar Jun 21 '25 12:06 dhth

Thanks @dhth I would prefer a simple parsing for start (splitting over ;, trimming then resplitting at =) without importing any dependency

jcamiel avatar Jun 21 '25 12:06 jcamiel

@jcamiel I've removed the dependency for parsing cookie values.

dhth avatar Jun 21 '25 13:06 dhth

Thanks for the PR @dhth would you be kind to squash your commits into a single one?

jcamiel avatar Jun 21 '25 17:06 jcamiel

@jcamiel squashed and rebased onto the latest master.

dhth avatar Jun 21 '25 19:06 dhth

Thanks for the PR @dhth

jcamiel avatar Jun 25 '25 08:06 jcamiel

/accept

jcamiel avatar Jun 25 '25 08:06 jcamiel

🕗 /accept is running, please wait for completion.

hurl-bot avatar Jun 25 '25 08:06 hurl-bot

✅ Pull request merged with fast forward by jcamiel..

# List of commits merged from dhth/hurl/parse-curls-cookie-flag branch into Orange-OpenSource/hurl/master branch:

  • 81be6a6c25 parse curl's --cookie flag

hurl-bot avatar Jun 25 '25 08:06 hurl-bot