Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Assume Mongrel2 handles Expect headers and redact where present

to prevent duplicate 100s coming from Webmachine
  • Loading branch information...
commit f55f6868dbebee41901ab5ed7189489e7cde91a4 1 parent 2e4e15b
@seansawyer authored
Showing with 26 additions and 3 deletions.
  1. +1 −1  ebin/m2mw.app
  2. +1 −1  src/m2mw.app.src
  3. +24 −1 src/m2mw_socket.erl
View
2  ebin/m2mw.app
@@ -1,6 +1,6 @@
{application,m2mw,
[{description,[]},
- {vsn,"1"},
+ {vsn,"3.1"},
{registered,[m2mw_sup]},
{applications,[kernel,stdlib]},
{mod,{m2mw_app,[]}},
View
2  src/m2mw.app.src
@@ -1,7 +1,7 @@
{application, m2mw,
[
{description, ""},
- {vsn, "1"},
+ {vsn, "3.1"},
{registered, [m2mw_sup]},
{applications, [kernel, stdlib]},
{mod, {m2mw_app, []}},
View
25 src/m2mw_socket.erl
@@ -22,6 +22,10 @@
accept/2,
reply/2]).
+-ifdef(TEST).
+-include_lib("eunit/include/eunit.hrl").
+-endif.
+
%% State data
-record(state, {listen=null, mw_sock=null, port=null, req=null, zmq_send=null}).
@@ -181,7 +185,10 @@ construct_http_req(#req{headers=Headers, body=Body}) ->
Uri = proplists:get_value(<<"URI">>, HeadersPl),
Vsn = proplists:get_value(<<"VERSION">>, HeadersPl),
RequestLine = io_lib:format("~s ~s ~s\r\n", [Method, Uri, Vsn]),
- HeaderLines = [io_lib:format("~s: ~s\r\n", [K,V]) || {K,V} <- HeadersPl],
+ %% We assume that expect header is handled by Mongrel2, so we redact it here
+ %% to prevent duplicate 100s, etc.
+ HeadersPl1 = proplists:delete(<<"expect">>, HeadersPl),
+ HeaderLines = [io_lib:format("~s: ~s\r\n", [K,V]) || {K,V} <- HeadersPl1],
iolist_to_binary([RequestLine, HeaderLines, "\r\n", Body]).
init_socket(Port) ->
@@ -194,3 +201,19 @@ reset(StateData) ->
ok = m2mw_handler:recv(HandlerPid),
% ok = gen_tcp:close(StateData#state.mw_sock),
StateData#state{mw_sock=null, req=null, zmq_send=null}.
+
+-ifdef(TEST).
+%% ===================================================================
+%% Support function tests
+%% ===================================================================
+
+construct_http_req_test_() ->
+ F = fun construct_http_req/1,
+ ZmqMsgExpect = <<"HSALSelohcnysHSALS-elohcnys-redrawrof 140 /foo/4f4ce03d8a1dc75c580000a6 340:{\"PATH\":\"\/foo\/4f4ce03d8a1dc75c580000a6\",\"x-forwarded-for\":\"10.68.21.199\",\"content-type\":\"application\/json\",\"expect\":\"100-continue\",\"connection\":\"close\",\"accept-encoding\":\"deflate, gzip\",\"content-length\":\"15\",\"accept\":\"*\/*\",\"host\":\"somehost.com\",\"METHOD\":\"PUT\",\"VERSION\":\"HTTP\/1.1\",\"URI\":\"\/foo\/f4ce03d8a1dc75c580000a6\",\"PATTERN\":\"\/pattern\/\"},15:{\"name\":\"m2mw\"},">>,
+ ZmqMsgNoExpect = <<"HSALSelohcnysHSALS-elohcnys-redrawrof 140 /foo/4f4ce03d8a1dc75c580000a6 316:{\"PATH\":\"\/foo\/4f4ce03d8a1dc75c580000a6\",\"x-forwarded-for\":\"10.68.21.199\",\"content-type\":\"application\/json\",\"connection\":\"close\",\"accept-encoding\":\"deflate, gzip\",\"content-length\":\"15\",\"accept\":\"*\/*\",\"host\":\"somehost.com\",\"METHOD\":\"PUT\",\"VERSION\":\"HTTP\/1.1\",\"URI\":\"\/foo\/f4ce03d8a1dc75c580000a6\",\"PATTERN\":\"\/pattern\/\"},15:{\"name\":\"m2mw\"},">>,
+ HttpReq = <<"PUT /foo/f4ce03d8a1dc75c580000a6 HTTP/1.1\r\nPATH: /foo/4f4ce03d8a1dc75c580000a6\r\nx-forwarded-for: 10.68.21.199\r\ncontent-type: application/json\r\nconnection: close\r\naccept-encoding: deflate, gzip\r\ncontent-length: 15\r\naccept: */*\r\nhost: somehost.com\r\nMETHOD: PUT\r\nVERSION: HTTP/1.1\r\nURI: /foo/f4ce03d8a1dc75c580000a6\r\nPATTERN: /pattern/\r\n\r\n{\"name\":\"m2mw\"}">>,
+ [{"constructs a request with the Expect header redacted when present",
+ ?_assertMatch(HttpReq, F(m2mw_util:parse_request(ZmqMsgExpect)))},
+ {"constructs the same request when the Expect header is not present",
+ ?_assertMatch(HttpReq, F(m2mw_util:parse_request(ZmqMsgNoExpect)))}].
+-endif.
Please sign in to comment.
Something went wrong with that request. Please try again.