Skip to contentSkip to conversation

git "smart http" server vs. http redirects

PATCH In review Miles Bader 5 participants 9 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

The savannah.gnu.org admins are trying out the git "smart http" server, but it doesn't seem to handle http redirects properly... should it? Is this a bug with the git server?

The follow is from Sylvain Beucler;

Quoted 8 lines

Hmmm, actually it works, but it doesn't support a HTTP redirect (namely git.sv.gnu.org -> git.savannah.gnu.org).

$ git clone http://git.sv.gnu.org/r/freedink.git
Initialized empty Git repository in /tmp/freedink/.git/
error: RPC failed; result=22, HTTP code = 405
^C
Quoted 4 lines
$ git clone http://git.savannah.gnu.org/r/freedink.git
Initialized empty Git repository in /tmp/freedink/.git/
remote: Counting objects: 5528, done.
remote: Compressing objects:  11% (174/1578)

This is the response I get from git.sv.gnu.org manually:

  $ telnet git.sv.gnu.org http
  Trying 140.186.70.72...
  Connected to git.sv.gnu.org.
  Escape character is '^]'.
  GET /r/freedink.git HTTP/1.1
  Host: git.sv.gnu.org

HTTP/1.1 301 Moved Permanently
Date: Fri, 17 Sep 2010 06:03:27 GMT
Server: Apache/2.2.9 (Debian) DAV/2 SVN/1.5.1 mod_python/3.2.10 Python/2.4.4 mod_wsgi/2.5
Location: http://git.savannah.gnu.org/r/freedink.git
Content-Length: 389
Content-Type: elided-to-get-past-vger's-filter; charset=iso-8859-1

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>The document has moved <a href="http://git.savannah.gnu.org/r/freedink.git">here</a>.</p>
<hr>
<address>Apache/2.2.9 (Debian) DAV/2 SVN/1.5.1 mod_python/3.2.10 Python/2.4.4 mod_wsgi/2.5 Server at git.sv.gnu.org Port 80</address> </body></html>
Connection closed by foreign host.

Thanks,

-Miles

Signature · 2 lines
XML is like violence.  If it doesn't solve your problem, you're not
using enough of it.
#1 lore raw
Quoted 11 lines · Miles Bader

The savannah.gnu.org admins are trying out the git "smart http" server, but it doesn't seem to handle http redirects properly... should it? Is this a bug with the git server?

The follow is from Sylvain Beucler;

Quoted 6 lines

Hmmm, actually it works, but it doesn't support a HTTP redirect (namely git.sv.gnu.org -> git.savannah.gnu.org).

$ git clone http://git.sv.gnu.org/r/freedink.git
Initialized empty Git repository in /tmp/freedink/.git/
error: RPC failed; result=22, HTTP code = 405

405 (Method not allowed)? Can you see what request it is trying to send and to where (the request that fails with 405 that is)?

-Ilari

#2 lore raw
Quoted 10 lines · Ilari Liusvaara
Quoted 8 lines · Miles Bader

The savannah.gnu.org admins are trying out the git "smart http" server, but it doesn't seem to handle http redirects properly... should it? Is this a bug with the git server?

The follow is from Sylvain Beucler;

Quoted 3 lines

Hmmm, actually it works, but it doesn't support a HTTP redirect (namely git.sv.gnu.org -> git.savannah.gnu.org).

Quoted 6 lines
Quoted 3 lines
Quoted 3 lines
$ git clone http://git.sv.gnu.org/r/freedink.git
Initialized empty Git repository in /tmp/freedink/.git/
error: RPC failed; result=22, HTTP code = 405

405 (Method not allowed)? Can you see what request it is trying to send and to where (the request that fails with 405 that is)?

I think this is a bug in the apache setup at savannnah. It is responding to POST .../git-upload-pack with both 301 and 405. This does not happen if you use the redirected address directly.

Andreas.

Signature · 3 lines
Andreas Schwab, [email protected]
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."
#3 lore raw

PATCH 1/1 smart-http: Don't change POST to GET when following redirect

When the remote HTTP server returns a redirect the default libcurl action is to change a POST request into a GET request while following the redirect, but the remote http backend does not expect that. Tell libcurl to always keep the POST request.

---

Quoted 16 lines · Andreas Schwab
Quoted 14 lines · Ilari Liusvaara
Quoted 11 lines · Miles Bader

The savannah.gnu.org admins are trying out the git "smart http" server, but it doesn't seem to handle http redirects properly... should it? Is this a bug with the git server?

The follow is from Sylvain Beucler;

Quoted 6 lines

Hmmm, actually it works, but it doesn't support a HTTP redirect (namely git.sv.gnu.org -> git.savannah.gnu.org).

$ git clone http://git.sv.gnu.org/r/freedink.git
Initialized empty Git repository in /tmp/freedink/.git/
error: RPC failed; result=22, HTTP code = 405
Quoted 6 lines
Quoted 2 lines

405 (Method not allowed)? Can you see what request it is trying to send and to where (the request that fails with 405 that is)?

I think this is a bug in the apache setup at savannnah. It is responding to POST .../git-upload-pack with both 301 and 405. This does not happen if you use the redirected address directly.

Looking closer, this is actually a bug in git. It does not set the POSTREDIR option, so that libcurl changes the POST into GET when following a redirection.

Andreas.

---
 remote-curl.c |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
remote-curl.c +1 -0
index 04d4813..309b024 100644
@@ -393,6 +393,7 @@ static int post_rpc(struct rpc_state *rpc)
 	curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
 	curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
 	curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "");
+	curl_easy_setopt(slot->curl, CURLOPT_POSTREDIR, CURL_REDIR_POST_ALL);
 
 	headers = curl_slist_append(headers, rpc->hdr_content_type);
 	headers = curl_slist_append(headers, rpc->hdr_accept);
Signature · 7 lines
1.7.2.3


-- 
Andreas Schwab, [email protected]
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."
#4 lore raw

Re: [PATCH] smart-http: Don't change POST to GET when following redirect

Thanks Andreas!

-miles

Signature · 2 lines
Acquaintance, n. A person whom we know well enough to borrow from, but not
well enough to lend to.
#5 lore raw

Re: [PATCH] smart-http: Don't change POST to GET when following redirect

In reply to Andreas Schwab
Quoted 1 line · Andreas Schwab

+       curl_easy_setopt(slot->curl, CURLOPT_POSTREDIR, CURL_REDIR_POST_ALL);

Not sure what git's minimum supported libcurl is, but this define was added in 7.19.1 (November 5 2008).

j.

#6 lore raw

PATCH v2 1/1 smart-http: Don't change POST to GET when following redirect

From: Andreas Schwab <[email protected]>

For a long time (29508e1 "Isolate shared HTTP request functionality", Fri

Nov 18 11:02:58 2005), we've followed HTTP redirects with

CURLOPT_FOLLOWLOCATION.

However, when the remote HTTP server returns a redirect the default libcurl action is to change a POST request into a GET request while following the redirect, but the remote http backend does not expect that.

Fix this by telling libcurl to always keep the request as type POST with CURLOPT_POSTREDIR.

---

Added simple tests and made some changes to the patch message.

Andreas:
shifted the setopt to right after FOLLOWLOCATION, since they're linked closely.

Jay: added the usual hexadecimal version checks.

 http.c                  |    3 +++
 t/lib-httpd/apache.conf |    7 +++++++
 t/t5551-http-fetch.sh   |    8 ++++++++
 3 files changed, 18 insertions(+), 0 deletions(-)
http.c +3 -0
index 1320c50..25f8b45 100644
@@ -275,6 +275,9 @@ static CURL *get_curl_handle(void)
 	}
 
 	curl_easy_setopt(result, CURLOPT_FOLLOWLOCATION, 1);
+#if LIBCURL_VERSION_NUM >= 0x071301
+	curl_easy_setopt(result, CURLOPT_POSTREDIR, CURL_REDIR_POST_ALL);
+#endif
 
 	if (getenv("GIT_CURL_VERBOSE"))
 		curl_easy_setopt(result, CURLOPT_VERBOSE, 1);
t/lib-httpd/apache.conf +7 -0
index 4961505..f41c7c6 100644
@@ -17,6 +17,9 @@ ErrorLog error.log
 <IfModule !mod_env.c>
 	LoadModule env_module modules/mod_env.so
 </IfModule>
+<IfModule !mod_rewrite.c>
+	LoadModule rewrite_module modules/mod_rewrite.so
+</IFModule>
 
 Alias /dumb/ www/
 
@@ -36,6 +39,10 @@ ScriptAlias /smart_noexport/ ${GIT_EXEC_PATH}/git-http-backend/
 	Options ExecCGI
 </Files>
 
+RewriteEngine on
+RewriteRule ^/smart-redir-perm/(.*)$ /smart/$1 [R=301]
+RewriteRule ^/smart-redir-temp/(.*)$ /smart/$1 [R=302]
+
 <IfDefine SSL>
 LoadModule ssl_module modules/mod_ssl.so
 
t/t5551-http-fetch.sh +8 -0
index fd19121..26d3557 100755
@@ -101,5 +101,13 @@ test_expect_success 'used upload-pack service' '
 	test_cmp exp act
 '
 
+test_expect_success 'follow redirects (301)' '
+	git clone $HTTPD_URL/smart-redir-perm/repo.git --quiet repo-p
+'
+
+test_expect_success 'follow redirects (302)' '
+	git clone $HTTPD_URL/smart-redir-temp/repo.git --quiet repo-t
+'
+
 stop_httpd
 test_done

--
1.7.3.67.g2a10b

#7 lore raw

Re: [PATCH v2] smart-http: Don't change POST to GET when following redirect

http.c static CURL *get_curl_handle(void) · line 280

Quoted 11 lines · Tay Ray Chuan
http.c +3 -0
index 1320c50..25f8b45 100644
@@ -275,6 +275,9 @@ static CURL *get_curl_handle(void)
 	}
 
 	curl_easy_setopt(result, CURLOPT_FOLLOWLOCATION, 1);
+#if LIBCURL_VERSION_NUM >= 0x071301
+	curl_easy_setopt(result, CURLOPT_POSTREDIR, CURL_REDIR_POST_ALL);
+#endif

Should this fall back to CURLOPT_POST301 on older versions? (Those won't handle 302 though, I think.)

Andreas.

Signature · 3 lines
Andreas Schwab, [email protected]
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."
#8 lore raw

PATCH v3 1/1 smart-http: Don't change POST to GET when following redirect

For a long time (29508e1 "Isolate shared HTTP request functionality", Fri

Nov 18 11:02:58 2005), we've followed HTTP redirects with

CURLOPT_FOLLOWLOCATION.

However, when the remote HTTP server returns a redirect the default libcurl action is to change a POST request into a GET request while following the redirect, but the remote http backend does not expect that.

Fix this by telling libcurl to always keep the request as type POST with CURLOPT_POSTREDIR.

For users of libcurl older than 7.19.1, use CURLOPT_POST301 instead, which only follows 301s instead of both 301s and 302s.

---

Andreas: added CURLOPT_POST301 alternative. There doesn't seem to be a pretty
constant, the docs just say a non-zero value [1] or 1 [2].

[1] http://github.com/bagder/curl/blob/curl-7_17_1/docs/libcurl/curl_easy_setopt.3#L646
[2] http://github.com/bagder/curl/blob/curl-7_19_0/docs/libcurl/curl_easy_setopt.3#L699

 http.c                  |    5 +++++
 t/lib-httpd/apache.conf |    7 +++++++
 t/t5551-http-fetch.sh   |    8 ++++++++
 3 files changed, 20 insertions(+), 0 deletions(-)
http.c +5 -0
index 1320c50..d10e2f0 100644
@@ -275,6 +275,11 @@ static CURL *get_curl_handle(void)
 	}
 
 	curl_easy_setopt(result, CURLOPT_FOLLOWLOCATION, 1);
+#if LIBCURL_VERSION_NUM >= 0x071301
+	curl_easy_setopt(result, CURLOPT_POSTREDIR, CURL_REDIR_POST_ALL);
+#elif LIBCURL_VERSION_NUM >= 0x071101
+	curl_easy_setopt(result, CURLOPT_POST301, 1);
+#endif
 
 	if (getenv("GIT_CURL_VERBOSE"))
 		curl_easy_setopt(result, CURLOPT_VERBOSE, 1);
t/lib-httpd/apache.conf +7 -0
index 4961505..f41c7c6 100644
@@ -17,6 +17,9 @@ ErrorLog error.log
 <IfModule !mod_env.c>
 	LoadModule env_module modules/mod_env.so
 </IfModule>
+<IfModule !mod_rewrite.c>
+	LoadModule rewrite_module modules/mod_rewrite.so
+</IFModule>
 
 Alias /dumb/ www/
 
@@ -36,6 +39,10 @@ ScriptAlias /smart_noexport/ ${GIT_EXEC_PATH}/git-http-backend/
 	Options ExecCGI
 </Files>
 
+RewriteEngine on
+RewriteRule ^/smart-redir-perm/(.*)$ /smart/$1 [R=301]
+RewriteRule ^/smart-redir-temp/(.*)$ /smart/$1 [R=302]
+
 <IfDefine SSL>
 LoadModule ssl_module modules/mod_ssl.so
 
t/t5551-http-fetch.sh +8 -0
index fd19121..26d3557 100755
@@ -101,5 +101,13 @@ test_expect_success 'used upload-pack service' '
 	test_cmp exp act
 '
 
+test_expect_success 'follow redirects (301)' '
+	git clone $HTTPD_URL/smart-redir-perm/repo.git --quiet repo-p
+'
+
+test_expect_success 'follow redirects (302)' '
+	git clone $HTTPD_URL/smart-redir-temp/repo.git --quiet repo-t
+'
+
 stop_httpd
 test_done

--
1.7.3.67.g2a10b