Skip to content

Commit 912b2ac

Browse files
dborowitzgitster
authored andcommitted
http: add http.savecookies option to write out HTTP cookies
HTTP servers may send Set-Cookie headers in a response and expect them to be set on subsequent requests. By default, libcurl behavior is to store such cookies in memory and reuse them across requests within a single session. However, it may also make sense, depending on the server and the cookies, to store them across sessions. Provide users an option to enable this behavior, writing cookies out to the same file specified in http.cookiefile. Signed-off-by: Dave Borowitz <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6a90778 commit 912b2ac

File tree

4 files changed

+38
-1
lines changed

4 files changed

+38
-1
lines changed

Documentation/config.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1456,7 +1456,11 @@ http.cookiefile::
14561456
of the file to read cookies from should be plain HTTP headers or
14571457
the Netscape/Mozilla cookie file format (see linkgit:curl[1]).
14581458
NOTE that the file specified with http.cookiefile is only used as
1459-
input. No cookies will be stored in the file.
1459+
input unless http.saveCookies is set.
1460+
1461+
http.savecookies::
1462+
If set, store cookies received during requests to the file specified by
1463+
http.cookiefile. Has no effect if http.cookiefile is unset.
14601464

14611465
http.sslVerify::
14621466
Whether to verify the SSL certificate when fetching or pushing

http.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ static long curl_low_speed_time = -1;
4545
static int curl_ftp_no_epsv;
4646
static const char *curl_http_proxy;
4747
static const char *curl_cookie_file;
48+
static int curl_save_cookies;
4849
static struct credential http_auth = CREDENTIAL_INIT;
4950
static int http_proactive_auth;
5051
static const char *user_agent;
@@ -200,6 +201,10 @@ static int http_options(const char *var, const char *value, void *cb)
200201

201202
if (!strcmp("http.cookiefile", var))
202203
return git_config_string(&curl_cookie_file, var, value);
204+
if (!strcmp("http.savecookies", var)) {
205+
curl_save_cookies = git_config_bool(var, value);
206+
return 0;
207+
}
203208

204209
if (!strcmp("http.postbuffer", var)) {
205210
http_post_buffer = git_config_int(var, value);
@@ -513,6 +518,8 @@ struct active_request_slot *get_active_slot(void)
513518
slot->callback_data = NULL;
514519
slot->callback_func = NULL;
515520
curl_easy_setopt(slot->curl, CURLOPT_COOKIEFILE, curl_cookie_file);
521+
if (curl_save_cookies)
522+
curl_easy_setopt(slot->curl, CURLOPT_COOKIEJAR, curl_cookie_file);
516523
curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, pragma_header);
517524
curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, curl_errorstr);
518525
curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, NULL);

t/lib-httpd/apache.conf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ ErrorLog error.log
2222
<IfModule !mod_version.c>
2323
LoadModule version_module modules/mod_version.so
2424
</IfModule>
25+
<IfModule !mod_headers.c>
26+
LoadModule headers_module modules/mod_headers.so
27+
</IfModule>
2528

2629
<IfVersion < 2.4>
2730
LockFile accept.lock
@@ -87,6 +90,11 @@ Alias /auth/dumb/ www/auth/dumb/
8790
SetEnv GIT_HTTP_EXPORT_ALL
8891
SetEnv GIT_NAMESPACE ns
8992
</LocationMatch>
93+
<LocationMatch /smart_cookies/>
94+
SetEnv GIT_EXEC_PATH ${GIT_EXEC_PATH}
95+
SetEnv GIT_HTTP_EXPORT_ALL
96+
Header set Set-Cookie name=value
97+
</LocationMatch>
9098
ScriptAliasMatch /smart_*[^/]*/(.*) ${GIT_EXEC_PATH}/git-http-backend/$1
9199
ScriptAlias /broken_smart/ broken-smart-http.sh/
92100
<Directory ${GIT_EXEC_PATH}>

t/t5551-http-fetch.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,24 @@ test_expect_success 'dumb clone via http-backend respects namespace' '
187187
test_cmp expect actual
188188
'
189189

190+
cat >cookies.txt <<EOF
191+
127.0.0.1 FALSE /smart_cookies/ FALSE 0 othername othervalue
192+
EOF
193+
cat >expect_cookies.txt <<EOF
194+
# Netscape HTTP Cookie File
195+
# http://curl.haxx.se/docs/http-cookies.html
196+
# This file was generated by libcurl! Edit at your own risk.
197+
198+
127.0.0.1 FALSE /smart_cookies/ FALSE 0 othername othervalue
199+
127.0.0.1 FALSE /smart_cookies/repo.git/info/ FALSE 0 name value
200+
EOF
201+
test_expect_success 'cookies stored in http.cookiefile when http.savecookies set' '
202+
git config http.cookiefile cookies.txt &&
203+
git config http.savecookies true &&
204+
git ls-remote $HTTPD_URL/smart_cookies/repo.git master &&
205+
test_cmp expect_cookies.txt cookies.txt
206+
'
207+
190208
test -n "$GIT_TEST_LONG" && test_set_prereq EXPENSIVE
191209

192210
test_expect_success EXPENSIVE 'create 50,000 tags in the repo' '

0 commit comments

Comments
 (0)