# git "smart http" server vs. http redirects

9 messages from 2010-09-18 to 2010-09-25. Participants: Miles Bader, Ilari Liusvaara, Andreas Schwab, Jay Soffian, Tay Ray Chuan.
Thread: https://gitlist.dev/t/25138

## Miles Bader, 2010-09-18 04:33

Subject: git "smart http" server vs. http redirects
Message-ID: <877hijvff7.fsf@catnip.gol.com>
URL: https://gitlist.dev/e/877hijvff7.fsf%40catnip.gol.com

```
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;
> 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
>
> $ 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

-- 
XML is like violence.  If it doesn't solve your problem, you're not
using enough of it.

```

## Ilari Liusvaara, 2010-09-18 07:03

Subject: Re: git "smart http" server vs. http redirects
Message-ID: <20100918070315.GA30872@LK-Perkele-V2.elisa-laajakaista.fi>
URL: https://gitlist.dev/e/20100918070315.GA30872%40LK-Perkele-V2.elisa-laajakaista.fi
In-Reply-To: <877hijvff7.fsf@catnip.gol.com>

```
On Sat, Sep 18, 2010 at 01:33:48PM +0900, Miles Bader wrote:

> 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;
> > 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

```

## Andreas Schwab, 2010-09-18 08:16

Subject: Re: git "smart http" server vs. http redirects
Message-ID: <m262y3cvpy.fsf@whitebox.home>
URL: https://gitlist.dev/e/m262y3cvpy.fsf%40whitebox.home
In-Reply-To: <20100918070315.GA30872@LK-Perkele-V2.elisa-laajakaista.fi>

```
Ilari Liusvaara <ilari.liusvaara@elisanet.fi> writes:

> On Sat, Sep 18, 2010 at 01:33:48PM +0900, Miles Bader wrote:
>
>> 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;
>> > 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)?

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.

-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."

```

## Andreas Schwab, 2010-09-18 08:47

Subject: [PATCH] smart-http: Don't change POST to GET when following redirect
Message-ID: <m21v8rcua1.fsf_-_@whitebox.home>
URL: https://gitlist.dev/e/m21v8rcua1.fsf_-_%40whitebox.home
In-Reply-To: <m262y3cvpy.fsf@whitebox.home>

```
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.

Signed-off-by: Andreas Schwab <schwab@linux-m68k.org>
---
Andreas Schwab <schwab@linux-m68k.org> writes:

> Ilari Liusvaara <ilari.liusvaara@elisanet.fi> writes:
>
>> On Sat, Sep 18, 2010 at 01:33:48PM +0900, Miles Bader wrote:
>>
>>> 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;
>>> > 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)?
>
> 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(-)

diff --git a/remote-curl.c b/remote-curl.c
index 04d4813..309b024 100644
--- a/remote-curl.c
+++ b/remote-curl.c
@@ -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);
-- 
1.7.2.3


-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."

```

## Miles Bader, 2010-09-18 14:09

Subject: Re: [PATCH] smart-http: Don't change POST to GET when following redirect
Message-ID: <87vd639m9c.fsf@catnip.gol.com>
URL: https://gitlist.dev/e/87vd639m9c.fsf%40catnip.gol.com
In-Reply-To: <m21v8rcua1.fsf_-_@whitebox.home>

```
Thanks Andreas!

-miles

-- 
Acquaintance, n. A person whom we know well enough to borrow from, but not
well enough to lend to.

```

## Jay Soffian, 2010-09-18 21:00

Subject: Re: [PATCH] smart-http: Don't change POST to GET when following redirect
Message-ID: <AANLkTimwkXTs==+zT=Ue3jFNyRLL+7A1FFhoDuF-5zZ3@mail.gmail.com>
URL: https://gitlist.dev/e/AANLkTimwkXTs%3D%3D%2BzT%3DUe3jFNyRLL%2B7A1FFhoDuF-5zZ3%40mail.gmail.com
In-Reply-To: <m21v8rcua1.fsf_-_@whitebox.home>

```
On Sat, Sep 18, 2010 at 4:47 AM, Andreas Schwab <schwab@linux-m68k.org> wrote:
> +       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.

```

## Tay Ray Chuan, 2010-09-24 06:20

Subject: [PATCH v2] smart-http: Don't change POST to GET when following redirect
Message-ID: <1285309223-4348-1-git-send-email-rctay89@gmail.com>
URL: https://gitlist.dev/e/1285309223-4348-1-git-send-email-rctay89%40gmail.com
In-Reply-To: <AANLkTimwkXTs==+zT=Ue3jFNyRLL+7A1FFhoDuF-5zZ3@mail.gmail.com>

```
From: Andreas Schwab <schwab@linux-m68k.org>

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.

Signed-off-by: Andreas Schwab <schwab@linux-m68k.org>
Signed-off-by: Tay Ray Chuan <rctay89@gmail.com>
---

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(-)

diff --git a/http.c b/http.c
index 1320c50..25f8b45 100644
--- a/http.c
+++ b/http.c
@@ -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);
diff --git a/t/lib-httpd/apache.conf b/t/lib-httpd/apache.conf
index 4961505..f41c7c6 100644
--- a/t/lib-httpd/apache.conf
+++ b/t/lib-httpd/apache.conf
@@ -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

diff --git a/t/t5551-http-fetch.sh b/t/t5551-http-fetch.sh
index fd19121..26d3557 100755
--- a/t/t5551-http-fetch.sh
+++ b/t/t5551-http-fetch.sh
@@ -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

```

## Andreas Schwab, 2010-09-24 18:06

Subject: Re: [PATCH v2] smart-http: Don't change POST to GET when following redirect
Message-ID: <m21v8jaudw.fsf@igel.home>
URL: https://gitlist.dev/e/m21v8jaudw.fsf%40igel.home
In-Reply-To: <1285309223-4348-1-git-send-email-rctay89@gmail.com>

```
Tay Ray Chuan <rctay89@gmail.com> writes:

> diff --git a/http.c b/http.c
> index 1320c50..25f8b45 100644
> --- a/http.c
> +++ b/http.c
> @@ -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.

-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."

```

## Tay Ray Chuan, 2010-09-25 04:20

Subject: [PATCH v3] smart-http: Don't change POST to GET when following redirect
Message-ID: <1285388435-5336-1-git-send-email-rctay89@gmail.com>
URL: https://gitlist.dev/e/1285388435-5336-1-git-send-email-rctay89%40gmail.com
In-Reply-To: <m21v8jaudw.fsf@igel.home>

```
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.

Signed-off-by: Andreas Schwab <schwab@linux-m68k.org>
Signed-off-by: Tay Ray Chuan <rctay89@gmail.com>
---

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(-)

diff --git a/http.c b/http.c
index 1320c50..d10e2f0 100644
--- a/http.c
+++ b/http.c
@@ -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);
diff --git a/t/lib-httpd/apache.conf b/t/lib-httpd/apache.conf
index 4961505..f41c7c6 100644
--- a/t/lib-httpd/apache.conf
+++ b/t/lib-httpd/apache.conf
@@ -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

diff --git a/t/t5551-http-fetch.sh b/t/t5551-http-fetch.sh
index fd19121..26d3557 100755
--- a/t/t5551-http-fetch.sh
+++ b/t/t5551-http-fetch.sh
@@ -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

```
