extend/cowboy — Issue #135: no support for requests which parse with absoluteURI
Author: davidkelly
Created: 2012-02-01 22:31:27+00:00
State: open

Say you run your requests to cowboy through a proxy. The resulting get request could look like:

GET http://proxy.server.uri/some/query/string HTTP 1.1 ...

Then, when cowboy uses decode_packet on it, the resulting parsed request:

{http_request,'GET',{absoluteURI,http,<<"proxy.server.uri">>,undefined,<<"/some/query/string">>},{1,1}}

doesn't match any of the cowboy_http_request:request(...) functions. In the patch below, I just added that support in the simplest way I could:

patch:
============================================================================================

From 6646c9b0cfcab0500f8f4648811c93a1421a3003 Mon Sep 17 00:00:00 2001
From: David Kelly
Date: Wed, 1 Feb 2012 14:08:29 -0800
Subject: [PATCH] added absoluteURI support

If requests go through a proxy, they will have the original uri in the
request, i.e. : GET http://proxy.server.uri/some/query/string HTTP 1.1 ...

That was problematic -- cowboy_http_protocol:request didn't know what to
to with the result of decode_packet applied to this, which would be something
like:

{http_request,'GET',{absoluteURI,http,<<"proxy.server.uri">>,undefined,<<"/some/query/string">>},{1,1}}

So, I just ignore the host, grab the path and pass into

cowboy_http_protocol:request({http_request, Method, {abs_path, Path}, Version}, State)

Seems to do the trick without much effort.
---
src/cowboy_http_protocol.erl | 2 ++
1 files changed, 2 insertions(+), 0 deletions(-)

diff --git a/src/cowboy_http_protocol.erl b/src/cowboy_http_protocol.erl
index baee081..f807937 100644
--- a/src/cowboy_http_protocol.erl
+++ b/src/cowboy_http_protocol.erl
@@ -113,6 +113,8 @@ wait_request(State=#state{socket=Socket, transport=Transport,
request({http_request, _Method, _URI, Version}, State)
when Version =/= {1, 0}, Version =/= {1, 1} ->
error_terminate(505, State);
+request({http_request, Method, {absoluteURI, _Scheme, _Host, _Port, Path}, Version}, State) ->
+ request({http_request, Method, {abs_path, Path}, Version}, State);
request({http_request, Method, {abs_path, AbsPath}, Version},
State=#state{socket=Socket, transport=Transport,
urldecode={URLDecFun, URLDecArg}=URLDec}) ->
--
1.7.4.4