# [PATCH] http-backend: provide Allow header for 405

4 messages from 2013-09-08 to 2013-09-11. Participants: brian m. carlson, Jonathan Nieder, Jeff King.
Thread: https://gitlist.dev/t/34880

## brian m. carlson, 2013-09-08 18:15

Subject: [PATCH] http-backend: provide Allow header for 405
Message-ID: <1378664106-95936-1-git-send-email-sandals@crustytoothpaste.net>
URL: https://gitlist.dev/e/1378664106-95936-1-git-send-email-sandals%40crustytoothpaste.net

```
The HTTP 1.1 standard requires an Allow header for 405 Method Not Allowed:

  The response MUST include an Allow header containing a list of valid methods
  for the requested resource.

So provide such a header when we return a 405 to the user agent.

Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>
---
 http-backend.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/http-backend.c b/http-backend.c
index 0324417..8c61084 100644
--- a/http-backend.c
+++ b/http-backend.c
@@ -594,8 +594,11 @@ int main(int argc, char **argv)
 
 			if (strcmp(method, c->method)) {
 				const char *proto = getenv("SERVER_PROTOCOL");
-				if (proto && !strcmp(proto, "HTTP/1.1"))
+				if (proto && !strcmp(proto, "HTTP/1.1")) {
 					http_status(405, "Method Not Allowed");
+					hdr_str("Allow", !strcmp("GET", c->method) ?
+						"GET, HEAD" : c->method);
+				}
 				else
 					http_status(400, "Bad Request");
 				hdr_nocache();
-- 
1.8.4.rc3

```

## Jonathan Nieder, 2013-09-09 04:18

Subject: Re: [PATCH] http-backend: provide Allow header for 405
Message-ID: <20130909041812.GA3430@elie.Belkin>
URL: https://gitlist.dev/e/20130909041812.GA3430%40elie.Belkin
In-Reply-To: <1378664106-95936-1-git-send-email-sandals@crustytoothpaste.net>

```
Hi,

brian m. carlson wrote:

> --- a/http-backend.c
> +++ b/http-backend.c
> @@ -594,8 +594,11 @@ int main(int argc, char **argv)
>  
>  			if (strcmp(method, c->method)) {
>  				const char *proto = getenv("SERVER_PROTOCOL");
> -				if (proto && !strcmp(proto, "HTTP/1.1"))
> +				if (proto && !strcmp(proto, "HTTP/1.1")) {
>  					http_status(405, "Method Not Allowed");
> +					hdr_str("Allow", !strcmp("GET", c->method) ?
> +						"GET, HEAD" : c->method);
> +				}
>  				else

Small style nit: the closing brace should go on the same line as the
"else", like so:

				if (proto && ...) {
					...
				} else
					http_status(400, "Bad Request");

Another micronit: the comparison should be !strcmp(c->method, "GET")
--- variable first, then constant it is being compared to.

The functional change looks good.

Thanks,
Jonathan

```

## Jeff King, 2013-09-10 06:24

Subject: Re: [PATCH] http-backend: provide Allow header for 405
Message-ID: <20130910062359.GA19638@sigill.intra.peff.net>
URL: https://gitlist.dev/e/20130910062359.GA19638%40sigill.intra.peff.net
In-Reply-To: <1378664106-95936-1-git-send-email-sandals@crustytoothpaste.net>

```
On Sun, Sep 08, 2013 at 06:15:06PM +0000, brian m. carlson wrote:

> The HTTP 1.1 standard requires an Allow header for 405 Method Not Allowed:
> 
>   The response MUST include an Allow header containing a list of valid methods
>   for the requested resource.
> 
> So provide such a header when we return a 405 to the user agent.

Makes sense.

>  			if (strcmp(method, c->method)) {
>  				const char *proto = getenv("SERVER_PROTOCOL");
> -				if (proto && !strcmp(proto, "HTTP/1.1"))
> +				if (proto && !strcmp(proto, "HTTP/1.1")) {
>  					http_status(405, "Method Not Allowed");
> +					hdr_str("Allow", !strcmp("GET", c->method) ?
> +						"GET, HEAD" : c->method);
> +				}

It took me a minute to figure out what is going on here. But we seem to
convert HEAD requests into GETs elsewhere, so any "GET" service should
be able to do either.

Looks OK to me.

-Peff

```

## brian m. carlson, 2013-09-11 02:14

Subject: Re: [PATCH] http-backend: provide Allow header for 405
Message-ID: <20130911021359.GA118418@vauxhall.crustytoothpaste.net>
URL: https://gitlist.dev/e/20130911021359.GA118418%40vauxhall.crustytoothpaste.net
In-Reply-To: <20130909041812.GA3430@elie.Belkin>

```
On Sun, Sep 08, 2013 at 09:18:12PM -0700, Jonathan Nieder wrote:
> > --- a/http-backend.c
> > +++ b/http-backend.c
> > @@ -594,8 +594,11 @@ int main(int argc, char **argv)
> >  
> >  			if (strcmp(method, c->method)) {
> >  				const char *proto = getenv("SERVER_PROTOCOL");
> > -				if (proto && !strcmp(proto, "HTTP/1.1"))
> > +				if (proto && !strcmp(proto, "HTTP/1.1")) {
> >  					http_status(405, "Method Not Allowed");
> > +					hdr_str("Allow", !strcmp("GET", c->method) ?
> > +						"GET, HEAD" : c->method);
> > +				}
> >  				else
> 
> Small style nit: the closing brace should go on the same line as the
> "else", like so:
> 
> 				if (proto && ...) {
> 					...
> 				} else
> 					http_status(400, "Bad Request");
> 
> Another micronit: the comparison should be !strcmp(c->method, "GET")
> --- variable first, then constant it is being compared to.
> 
> The functional change looks good.

I plan on doing a reroll tomorrow.

-- 
brian m. carlson / brian with sandals: Houston, Texas, US
+1 832 623 2791 | http://www.crustytoothpaste.net/~bmc | My opinion only
OpenPGP: RSA v4 4096b: 88AC E9B2 9196 305B A994 7552 F1BA 225C 0223 B187

```
