Browse Source

Add a private cowboy_req function to create a Req object

Private means you must not used it. It's meant for internal use.
Loïc Hoguin 12 years ago
parent
commit
6dbc1f9ef9
3 changed files with 23 additions and 13 deletions
  1. 2 2
      include/http.hrl
  2. 6 11
      src/cowboy_protocol.erl
  3. 15 0
      src/cowboy_req.erl

+ 2 - 2
include/http.hrl

@@ -28,10 +28,10 @@
 	host       = undefined :: undefined | binary(),
 	host_info  = undefined :: undefined | cowboy_dispatcher:tokens(),
 	port       = undefined :: undefined | inet:port_number(),
-	path       = undefined :: undefined | binary(),
+	path       = undefined :: binary(),
 	path_info  = undefined :: undefined | cowboy_dispatcher:tokens(),
 	qs_vals    = undefined :: undefined | list({binary(), binary() | true}),
-	raw_qs     = undefined :: undefined | binary(),
+	raw_qs     = undefined :: binary(),
 	bindings   = undefined :: undefined | cowboy_dispatcher:bindings(),
 	headers    = []        :: cowboy_http:headers(),
 	p_headers  = []        :: [any()], %% @todo Improve those specs.

+ 6 - 11
src/cowboy_protocol.erl

@@ -139,10 +139,8 @@ request({http_request, Method, {abs_path, AbsPath}, Version},
 	ConnAtom = if Keepalive < MaxKeepalive -> version_to_connection(Version);
 		true -> close
 	end,
-	parse_header(#http_req{socket=Socket, transport=Transport,
-		connection=ConnAtom, pid=self(), method=Method, version=Version,
-		path=RawPath, raw_qs=Qs, onresponse=OnResponse, urldecode=URLDec},
-		State#state{path_tokens=PathTokens});
+	parse_header(cowboy_req:new(Socket, Transport, ConnAtom, Method, Version,
+		RawPath, Qs, OnResponse, URLDec), State#state{path_tokens=PathTokens});
 request({http_request, Method, '*', Version},
 		State=#state{socket=Socket, transport=Transport,
 		req_keepalive=Keepalive, max_keepalive=MaxKeepalive,
@@ -150,10 +148,8 @@ request({http_request, Method, '*', Version},
 	ConnAtom = if Keepalive < MaxKeepalive -> version_to_connection(Version);
 		true -> close
 	end,
-	parse_header(#http_req{socket=Socket, transport=Transport,
-		connection=ConnAtom, pid=self(), method=Method, version=Version,
-		path= <<"*">>, raw_qs= <<>>, onresponse=OnResponse,
-		urldecode=URLDec}, State#state{path_tokens='*'});
+	parse_header(cowboy_req:new(Socket, Transport, ConnAtom, Method, Version,
+		<<"*">>, <<>>, OnResponse, URLDec), State#state{path_tokens='*'});
 request({http_request, _Method, _URI, _Version}, State) ->
 	error_terminate(501, State);
 request({http_error, <<"\r\n">>},
@@ -427,9 +423,8 @@ error_terminate(Code, State=#state{socket=Socket, transport=Transport,
 	receive
 		{cowboy_req, resp_sent} -> ok
 	after 0 ->
-		_ = cowboy_req:reply(Code, #http_req{
-			socket=Socket, transport=Transport, onresponse=OnResponse,
-			connection=close, pid=self(), resp_state=waiting}),
+		_ = cowboy_req:reply(Code, cowboy_req:new(Socket, Transport,
+			close, 'GET', {1, 1}, <<>>, <<>>, OnResponse, undefined)),
 		ok
 	end,
 	terminate(State).

+ 15 - 0
src/cowboy_req.erl

@@ -42,6 +42,7 @@
 -module(cowboy_req).
 
 %% Request API.
+-export([new/9]).
 -export([method/1]).
 -export([version/1]).
 -export([peer/1]).
@@ -115,6 +116,20 @@
 
 %% Request API.
 
+%% @doc Create a new HTTP Req object.
+%%
+%% This function takes care of setting the owner's pid to self().
+%% @private
+-spec new(inet:socket(), module(), keepalive | close,
+	cowboy_http:method(), cowboy_http:version(), binary(), binary(),
+	undefined | fun(), undefined | {fun(), atom()})
+	-> req().
+new(Socket, Transport, Connection, Method, Version, Path, Qs,
+		OnResponse, URLDecode) ->
+	#http_req{socket=Socket, transport=Transport, connection=Connection,
+		pid=self(), method=Method, version=Version, path=Path, raw_qs=Qs,
+		onresponse=OnResponse, urldecode=URLDecode}.
+
 %% @doc Return the HTTP method of the request.
 -spec method(Req) -> {cowboy_http:method(), Req} when Req::req().
 method(Req) ->