Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Custom reason phrase #141

Merged
merged 2 commits into from

2 participants

Steve Vinoski Macneil Shonle
Steve Vinoski

allow apps to supply custom reason phrases for HTTP response codes

Allow applications to return:

{halt, {Code::integer(), ReasonPhrase::iolist()}}

in order to provide custom reason phrases in their responses. The original {halt, Code} also still works.

Also, in a second commit, add the doc directory to .gitignore.

vinoski added some commits
Steve Vinoski vinoski allow apps to supply custom reason phrases for HTTP response codes
Allow applications to return:

{halt, {Code::integer(), ReasonPhrase::iolist()}}

in order to provide custom reason phrases in their responses. The original
{halt, Code} also still works.
dedc573
Steve Vinoski vinoski add doc directory to .gitignore c30f903
Macneil Shonle

+1

Steve Vinoski vinoski merged commit 01f444c into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 25, 2013
  1. Steve Vinoski

    allow apps to supply custom reason phrases for HTTP response codes

    vinoski authored
    Allow applications to return:
    
    {halt, {Code::integer(), ReasonPhrase::iolist()}}
    
    in order to provide custom reason phrases in their responses. The original
    {halt, Code} also still works.
  2. Steve Vinoski
This page is out of date. Refresh to see the latest.
1  .gitignore
View
@@ -2,3 +2,4 @@ deps/*
*.beam
.eunit/*
ebin/*.app
+doc/*
8 src/webmachine_decision_core.erl
View
@@ -59,7 +59,9 @@ d(DecisionID) ->
log_decision(DecisionID),
decision(DecisionID).
-respond(Code) ->
+respond(Code) when is_integer(Code) ->
+ respond({Code, undefined});
+respond({Code, _ReasonPhrase}=CodeAndReason) ->
Resource = get(resource),
EndTime = now(),
case Code of
@@ -86,9 +88,9 @@ respond(Code) ->
_ -> ignore
end,
put(code, Code),
- wrcall({set_response_code, Code}),
+ wrcall({set_response_code, CodeAndReason}),
resource_call(finish_request),
- wrcall({send_response, Code}),
+ wrcall({send_response, CodeAndReason}),
RMod = wrcall({get_metadata, 'resource_module'}),
Notes = wrcall(notes),
LogData0 = wrcall(log_data),
11 src/webmachine_log_handler.erl
View
@@ -1,4 +1,4 @@
-%% Copyright (c) 2011-2012 Basho Technologies, Inc. All Rights Reserved.
+%% Copyright (c) 2011-2013 Basho Technologies, Inc. All Rights Reserved.
%%
%% This file is provided to you under the Apache License,
%% Version 2.0 (the "License"); you may not use this file
@@ -91,7 +91,14 @@ format_req(#wm_log_data{method=Method,
response_length=ResponseLength}) ->
User = "-",
Time = webmachine_log:fmtnow(),
- Status = integer_to_list(ResponseCode),
+ Status = case ResponseCode of
+ {Code, _ReasonPhrase} when is_integer(Code) ->
+ integer_to_list(Code);
+ _ when is_integer(ResponseCode) ->
+ integer_to_list(ResponseCode);
+ _ ->
+ ResponseCode
+ end,
Length = integer_to_list(ResponseLength),
Referer =
case mochiweb_headers:get_value("Referer", Headers) of
11 src/webmachine_perf_log_handler.erl
View
@@ -1,4 +1,4 @@
-%% Copyright (c) 2011-2012 Basho Technologies, Inc. All Rights Reserved.
+%% Copyright (c) 2011-2013 Basho Technologies, Inc. All Rights Reserved.
%%
%% This file is provided to you under the Apache License,
%% Version 2.0 (the "License"); you may not use this file
@@ -93,7 +93,14 @@ format_req(#wm_log_data{resource_module=Mod,
end_time=EndTime,
finish_time=FinishTime}) ->
Time = webmachine_log:fmtnow(),
- Status = integer_to_list(ResponseCode),
+ Status = case ResponseCode of
+ {Code, _ReasonPhrase} when is_integer(Code) ->
+ integer_to_list(Code);
+ _ when is_integer(ResponseCode) ->
+ integer_to_list(ResponseCode);
+ _ ->
+ ResponseCode
+ end,
Length = integer_to_list(ResponseLength),
TTPD = webmachine_util:now_diff_milliseconds(EndTime, StartTime),
TTPS = webmachine_util:now_diff_milliseconds(FinishTime, EndTime),
28 src/webmachine_request.erl
View
@@ -242,13 +242,15 @@ call({set_disp_path, P}, {?MODULE, ReqState}) ->
call(do_redirect, {?MODULE, ReqState}) ->
{ok, ReqState#wm_reqstate{
reqdata=wrq:do_redirect(true, ReqState#wm_reqstate.reqdata)}};
-call({send_response, Code}, Req) ->
+call({send_response, Code}, Req) when is_integer(Code) ->
+ call({send_response, {Code, undefined}}, Req);
+call({send_response, {Code, ReasonPhrase}=CodeAndReason}, Req) when is_integer(Code) ->
{Reply, NewState} =
case Code of
200 ->
- send_ok_response(Req);
+ send_ok_response(ReasonPhrase, Req);
_ ->
- send_response(Code, Req)
+ send_response(CodeAndReason, Req)
end,
LogData = NewState#wm_reqstate.log_data,
NewLogData = LogData#wm_log_data{finish_time=now()},
@@ -347,18 +349,18 @@ send_chunk(Socket, Data) ->
send(Socket, [mochihex:to_hex(Size), <<"\r\n">>, Data, <<"\r\n">>]),
Size.
-send_ok_response({?MODULE, ReqState}=Req) ->
+send_ok_response(ReasonPhrase, {?MODULE, ReqState}=Req) ->
RD0 = ReqState#wm_reqstate.reqdata,
{Range, State} = get_range(Req),
case Range of
X when X =:= undefined; X =:= fail; X =:= ignore ->
- send_response(200, Req);
+ send_response({200, ReasonPhrase}, Req);
Ranges ->
{PartList, Size} = range_parts(RD0, Ranges),
case PartList of
[] -> %% no valid ranges
%% could be 416, for now we'll just return 200
- send_response(200, Req);
+ send_response({200, ReasonPhrase}, Req);
PartList ->
{RangeHeaders, RangeBody} =
parts_to_body(PartList, Size, Req),
@@ -367,7 +369,7 @@ send_ok_response({?MODULE, ReqState}=Req) ->
RespBodyRD = wrq:set_resp_body(
RangeBody, RespHdrsRD),
NewState = State#wm_reqstate{reqdata=RespBodyRD},
- send_response(206, NewState, Req)
+ send_response({206, ReasonPhrase}, NewState, Req)
end
end.
@@ -688,8 +690,12 @@ stream_multipart_part_helper(Fun, Rest, CType, Boundary, Size) ->
end
end.
-make_code(X) when is_integer(X) ->
- [integer_to_list(X), [" " | httpd_util:reason_phrase(X)]];
+make_code({Code, undefined}) when is_integer(Code) ->
+ make_code({Code, httpd_util:reason_phrase(Code)});
+make_code({Code, ReasonPhrase}) when is_integer(Code) ->
+ [integer_to_list(Code), [" " | ReasonPhrase]];
+make_code(Code) when is_integer(Code) ->
+ make_code({Code, httpd_util:reason_phrase(Code)});
make_code(Io) when is_list(Io); is_binary(Io) ->
Io.
@@ -698,7 +704,9 @@ make_version({1, 0}) ->
make_version(_) ->
<<"HTTP/1.1 ">>.
-make_headers(Code, Length, RD) ->
+make_headers({Code, _ReasonPhrase}, Length, RD) ->
+ make_headers(Code, Length, RD);
+make_headers(Code, Length, RD) when is_integer(Code) ->
Hdrs0 = case Code of
304 ->
mochiweb_headers:make(wrq:resp_headers(RD));
2  src/wrq.erl
View
@@ -172,6 +172,8 @@ set_req_body(Body, RD) -> RD#wm_reqdata{req_body=Body}.
set_resp_body(Body, RD) -> RD#wm_reqdata{resp_body=Body}.
+set_response_code({Code, _ReasonPhrase}=CodeAndReason, RD) when is_integer(Code) ->
+ RD#wm_reqdata{response_code=CodeAndReason};
set_response_code(Code, RD) when is_integer(Code) ->
RD#wm_reqdata{response_code=Code}.
1  www/resources.html
View
@@ -59,6 +59,7 @@
<table><tr><th>Result</th><th>Effect</th></tr>
<tr><td class="fwf lhcol">{error, Err::term()}</td><td>Immediately end processing of this request, returning a 500 response code. The response body will contain the <span class="fwf"> Err </span> term.</td></tr>
<tr><td class="fwf lhcol">{halt, Code::integer()}</td><td>Immediately end processing of this request, returning response code Code. It is the responsibility of the resource to ensure that all necessary response header and body elements are filled in <span class="fwf"> ReqData </span> in order to make that reponse code valid.</td></tr>
+<tr><td class="fwf lhcol">{halt, {Code::integer(), ReasonPhrase::iolist()}}</td><td>Same as <span class="fwf"> {halt, Code::integer()} </span> but supply a custom reason phrase for the HTTP status code as well.</td></tr>
</table>
<p>
Something went wrong with that request. Please try again.