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 <dborowitz@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Dave Borowitz committed Jul 23, 2013 at 15:40 UTC 912b2acf2f376e51bee32a486ca42ba4ad764363
4 files changed +38 -1
Documentation/config.txt
+5 -1
@@ -1456,7 +1456,11 @@ http.cookiefile::
1456 of the file to read cookies from should be plain HTTP headers or
1457 the Netscape/Mozilla cookie file format (see linkgit:curl[1]).
1458 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.
1464
1465 http.sslVerify::
1466 Whether to verify the SSL certificate when fetching or pushing
http.c
+7
@@ -45,6 +45,7 @@ static long curl_low_speed_time = -1;
45 static int curl_ftp_no_epsv;
46 static const char *curl_http_proxy;
47 static const char *curl_cookie_file;
48 +static int curl_save_cookies;
49 static struct credential http_auth = CREDENTIAL_INIT;
50 static int http_proactive_auth;
51 static const char *user_agent;
@@ -200,6 +201,10 @@ static int http_options(const char *var, const char *value, void *cb)
201
202 if (!strcmp("http.cookiefile", var))
203 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 + }
208
209 if (!strcmp("http.postbuffer", var)) {
210 http_post_buffer = git_config_int(var, value);
@@ -513,6 +518,8 @@ struct active_request_slot *get_active_slot(void)
518 slot->callback_data = NULL;
519 slot->callback_func = NULL;
520 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);
523 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, pragma_header);
524 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, curl_errorstr);
525 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, NULL);
t/lib-httpd/apache.conf
+8
@@ -22,6 +22,9 @@ ErrorLog error.log
22 <IfModule !mod_version.c>
23 LoadModule version_module modules/mod_version.so
24 </IfModule>
25 +<IfModule !mod_headers.c>
26 + LoadModule headers_module modules/mod_headers.so
27 +</IfModule>
28
29 <IfVersion < 2.4>
30 LockFile accept.lock
@@ -87,6 +90,11 @@ Alias /auth/dumb/ www/auth/dumb/
90 SetEnv GIT_HTTP_EXPORT_ALL
91 SetEnv GIT_NAMESPACE ns
92 </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>
98 ScriptAliasMatch /smart_*[^/]*/(.*) ${GIT_EXEC_PATH}/git-http-backend/$1
99 ScriptAlias /broken_smart/ broken-smart-http.sh/
100 <Directory ${GIT_EXEC_PATH}>
t/t5551-http-fetch.sh
+18
@@ -187,6 +187,24 @@ test_expect_success 'dumb clone via http-backend respects namespace' '
187 test_cmp expect actual
188 '
189
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 +
208 test -n "$GIT_TEST_LONG" && test_set_prereq EXPENSIVE
209
210 test_expect_success EXPENSIVE 'create 50,000 tags in the repo' '