Permalink
Browse files

Added webmachine c25ec2dfaba2 tip from http://bitbucket.org/justin/we…

…bmachine/

Patched dispatcher of webmachine to use a custom dispatcher.
  • Loading branch information...
1 parent f189630 commit d67e934e2f19b3f846a61e5070f738fe58adf29d @mworrell mworrell committed Dec 15, 2009
Showing with 2,901 additions and 937 deletions.
  1. +2 −2 Makefile
  2. +4 −1 THANKS
  3. +0 −8 include/wm_host_dispatch_list.hrl
  4. +4 −3 include/wm_reqdata.hrl
  5. +9 −0 include/wm_reqstate.hrl
  6. +0 −1 priv/skel/deps/.empty
  7. +0 −1 priv/skel/ebin/.empty
  8. +14 −2 priv/skel/src/skel.erl
  9. +4 −2 priv/skel/src/skel_sup.erl
  10. +3 −1 src/webmachine.app
  11. +21 −2 src/webmachine.erl
  12. +45 −24 src/webmachine_decision_core.erl
  13. +109 −18 src/webmachine_dispatcher.erl
  14. +24 −16 src/webmachine_error_handler.erl
  15. +0 −120 src/webmachine_host.erl
  16. +9 −5 src/webmachine_logger.erl
  17. +58 −66 src/webmachine_mochiweb.erl
  18. +11 −2 src/webmachine_multipart.erl
  19. +1 −1 src/webmachine_perf_logger.erl
  20. +551 −13 src/webmachine_request.erl
  21. +0 −606 src/webmachine_request_srv.erl
  22. +38 −14 src/webmachine_resource.erl
  23. +4 −2 src/webmachine_skel.erl
  24. +10 −11 src/webmachine_sup.erl
  25. +19 −2 src/webmachine_util.erl
  26. +30 −14 src/wrq.erl
  27. +67 −0 www/blogs.html
  28. +48 −0 www/contact.html
  29. +103 −0 www/css/style-1c.css
  30. +103 −0 www/css/style.css
  31. +292 −0 www/debugging.html
  32. +57 −0 www/diagram.html
  33. +121 −0 www/dispatcher.html
  34. +58 −0 www/docs.html
  35. +250 −0 www/example_resources.html
  36. BIN www/favicon.ico
  37. BIN www/images/WM200-crop.png
  38. BIN www/images/basho-landscape.gif
  39. BIN www/images/basic-trace-decision-tab.png
  40. BIN www/images/basic-trace-labeled.png
  41. BIN www/images/basic-trace-request-tab.png
  42. BIN www/images/basic-trace-response-tab.png
  43. BIN www/images/bg.gif
  44. BIN www/images/blankbox.gif
  45. BIN www/images/chash.gif
  46. BIN www/images/easy-ops.gif
  47. BIN www/images/gossip4.gif
  48. BIN www/images/halfblankbox.gif
  49. BIN www/images/http-headers-status-v3.png
  50. BIN www/images/more.gif
  51. BIN www/images/site.gif
  52. BIN www/images/splash250.gif
  53. BIN www/images/vclock.gif
  54. +73 −0 www/index.html
  55. +62 −0 www/intros.html
  56. +108 −0 www/mechanics.html
  57. +77 −0 www/quickstart.html
  58. +52 −0 www/reftrans.html
  59. +143 −0 www/reqdata.html
  60. +141 −0 www/resources.html
  61. +176 −0 www/streambody.html
View
@@ -11,8 +11,8 @@ erl:
@$(ERL) -pa $(EBIN_DIRS) -noinput +B \
-eval 'case make:all() of up_to_date -> halt(0); error -> halt(1) end.'
-docs:
- @erl -noshell -run edoc_run application '$(APP)' '"."' '[]'
+edoc:
+ @$(ERL) -noshell -run edoc_run application '$(APP)' '"."' '[{preprocess, true},{includes, ["."]}]'
clean:
@echo "removing:"
View
5 THANKS
@@ -1,7 +1,8 @@
The following people have contributed to Webmachine:
-Justin Sheehy
Andy Gross
+Justin Sheehy
+John Muellerleile
Robert Ahrens
Jeremy Latt
Bryan Fink
@@ -11,4 +12,6 @@ Marc Worrell
Seth Falcon
Tuncer Ayaz
Martin Scholl
+Paul Mineiro
+Dave Smith
@@ -1,8 +0,0 @@
-%% @doc Dispatch list on a per-host basis.
-%% host = atom specificying the symbolic hostname, also set in the wm_reqdata. Use 'default' for the catch-all host
-%% hostname = the primary hostname, lowercase (eg. "www.example.com")
-%% hostalias = list of accepted aliases, lowercase (eg. [ "example.com", "example.net" ])
-%% redirect = boolean, set to true to redirect GET requests to the main host
-%% dispatch_list = list of {pathspec, resource, args}
-
--record(wm_host_dispatch_list, {host, hostname, hostalias, redirect, dispatch_list}).
View
@@ -1,7 +1,8 @@
--record(wm_reqdata, {method, version, peer, wmreq,
- host, disp_path, path, raw_path, path_info, path_tokens,
+-record(wm_reqdata, {method, version, peer, wm_state,
+ disp_path, path, raw_path, path_info, path_tokens,
app_root,response_code,max_recv_body,
req_cookie, req_qs, req_headers, req_body,
- resp_redirect, resp_headers, resp_body
+ resp_redirect, resp_headers, resp_body,
+ host_tokens, port
}).
View
@@ -0,0 +1,9 @@
+-record(reqstate, {socket=undefined,
+ metadata=dict:new(),
+ range=undefined,
+ peer=undefined,
+ reqdata=undefined,
+ bodyfetch=undefined,
+ log_data=undefined
+ }).
+
View
@@ -1 +0,0 @@
-
View
@@ -1 +0,0 @@
-
View
@@ -5,7 +5,7 @@
-module(skel).
-author('author <author@example.com>').
--export([start/0, stop/0]).
+-export([start/0, start_link/0, stop/0]).
ensure_started(App) ->
case application:start(App) of
@@ -14,12 +14,24 @@ ensure_started(App) ->
{error, {already_started, App}} ->
ok
end.
-
+
+%% @spec start_link() -> {ok,Pid::pid()}
+%% @doc Starts the app for inclusion in a supervisor tree
+start_link() ->
+ skel_deps:ensure(),
+ ensure_started(crypto),
+ application:set_env(webmachine, webmachine_logger_module,
+ webmachine_logger),
+ ensure_started(webmachine),
+ skel_sup:start_link().
+
%% @spec start() -> ok
%% @doc Start the skel server.
start() ->
skel_deps:ensure(),
ensure_started(crypto),
+ application:set_env(webmachine, webmachine_logger_module,
+ webmachine_logger),
ensure_started(webmachine),
application:start(skel).
@@ -41,8 +41,10 @@ upgrade() ->
%% @spec init([]) -> SupervisorTree
%% @doc supervisor callback.
init([]) ->
- Ip = case os:getenv("WEBMACHINE_IP") of false -> "0.0.0.0"; Any -> Any end,
- {ok, Dispatch} = file:consult("priv/dispatch.conf"),
+ Ip = case os:getenv("WEBMACHINE_IP") of false -> "0.0.0.0"; Any -> Any end,
+ {ok, Dispatch} = file:consult(filename:join(
+ [filename:dirname(code:which(?MODULE)),
+ "..", "priv", "dispatch.conf"])),
WebConfig = [
{ip, Ip},
{port, 8000},
View
@@ -1,6 +1,6 @@
{application, webmachine,
[{description, "webmachine"},
- {vsn, "1.3"},
+ {vsn, "1.5"},
{modules, [
webmachine,
webmachine_app,
@@ -13,8 +13,10 @@
webmachine_resource,
webmachine_request,
webmachine_request_srv,
+ webmachine_skel,
webmachine_sup,
webmachine_mochiweb,
+ webmachine_multipart,
webmachine_util,
wrq,
wmtrace_resource
View
@@ -20,6 +20,10 @@
-export([start/0, stop/0]).
-export([new_request/2]).
+-include("webmachine_logger.hrl").
+-include_lib("include/wm_reqstate.hrl").
+-include_lib("include/wm_reqdata.hrl").
+
%% @spec start() -> ok
%% @doc Start the webmachine server.
start() ->
@@ -38,8 +42,23 @@ new_request(mochiweb, Request) ->
RawPath = Request:get(raw_path),
Version = Request:get(version),
Headers = Request:get(headers),
- {ok, Pid} = webmachine_request_srv:start_link(Socket, Method, RawPath, Version, Headers),
- webmachine_request:new(Pid).
+ InitState = #reqstate{socket=Socket,
+ reqdata=wrq:create(Method,Version,RawPath,Headers)},
+ InitReq = {webmachine_request,InitState},
+ {Peer, ReqState} = InitReq:get_peer(),
+ PeerState = ReqState#reqstate{reqdata=wrq:set_peer(Peer,
+ ReqState#reqstate.reqdata)},
+ LogData = #wm_log_data{start_time=now(),
+ method=Method,
+ headers=Headers,
+ peer=PeerState#reqstate.peer,
+ path=RawPath,
+ version=Version,
+ response_code=404,
+ response_length=0},
+ webmachine_request:new(PeerState#reqstate{log_data=LogData}).
+
+
@@ -25,9 +25,9 @@
-export([do_log/1]).
-include("webmachine_logger.hrl").
-handle_request(Req, Resource) ->
- put(req, Req),
+handle_request(Resource, ReqState) ->
put(resource, Resource),
+ put(reqstate, ReqState),
try
d(v3b13)
catch
@@ -36,8 +36,18 @@ handle_request(Req, Resource) ->
end.
wrcall(X) ->
- Req = get(req),
- Req:call(X).
+ RS0 = get(reqstate),
+ Req = webmachine_request:new(RS0),
+ {Response, RS1} = Req:call(X),
+ put(reqstate, RS1),
+ Response.
+
+resource_call(Fun) ->
+ Resource = get(resource),
+ {Reply, NewResource, NewRS} = Resource:do(Fun,get()),
+ put(resource, NewResource),
+ put(reqstate, NewRS),
+ Reply.
get_header_val(H) -> wrcall({get_req_header, H}).
@@ -55,7 +65,9 @@ respond(Code) ->
404 ->
{ok, ErrorHandler} = application:get_env(webmachine, error_handler),
Reason = {none, none, []},
- ErrorHTML = ErrorHandler:render_error(Code, get(req), Reason),
+ {ErrorHTML,ReqState} = ErrorHandler:render_error(
+ Code, {webmachine_request,get(reqstate)}, Reason),
+ put(reqstate, ReqState),
wrcall({set_resp_body, ErrorHTML});
304 ->
wrcall({remove_resp_header, "Content-Type"}),
@@ -81,17 +93,16 @@ respond(Code) ->
LogData = LogData0#wm_log_data{resource_module=RMod,
end_time=EndTime},
spawn(fun() -> do_log(LogData) end),
- Resource:stop(),
- Req = get(req),
- Req:stop().
+ Resource:stop().
respond(Code, Headers) ->
wrcall({set_resp_headers, Headers}),
respond(Code).
error_response(Code, Reason) ->
{ok, ErrorHandler} = application:get_env(webmachine, error_handler),
- ErrorHTML = ErrorHandler:render_error(Code, get(req), Reason),
+ ErrorHTML = ErrorHandler:render_error(
+ Code, {webmachine_request,get(reqstate)}, Reason),
wrcall({set_resp_body, ErrorHTML}),
respond(Code).
error_response(Reason) ->
@@ -115,25 +126,17 @@ decision_flow({ErrCode, Reason}, _TestResult) when is_integer(ErrCode) ->
error_response(ErrCode, Reason).
do_log(LogData) ->
- LoggerModule =
- case application:get_env(webmachine, webmachine_logger_module) of
- {ok, Val} -> Val;
- _ -> webmachine_logger
- end,
- LoggerModule:log_access(LogData),
+ case application:get_env(webmachine, webmachine_logger_module) of
+ {ok, LoggerModule} -> LoggerModule:log_access(LogData);
+ _ -> nop
+ end,
case application:get_env(webmachine, enable_perf_logger) of
{ok, true} ->
webmachine_perf_logger:log(LogData);
_ ->
ignore
end.
-resource_call(Fun) ->
- Resource = get(resource),
- {Reply, NewResource} = Resource:do(Fun,get()),
- put(resource, NewResource),
- Reply.
-
log_decision(DecisionID) ->
Resource = get(resource),
Resource:log_d(DecisionID).
@@ -417,7 +420,9 @@ decision(v3n11) ->
end;
_ ->
case resource_call(process_post) of
- true -> stage1_ok;
+ true ->
+ encode_body_if_set(),
+ stage1_ok;
{halt, Code} -> respond(Code);
Err -> error_response(Err)
end
@@ -539,7 +544,21 @@ accept_helper() ->
[] -> {respond,415};
AcceptedContentList ->
F = hd(AcceptedContentList),
- resource_call(F)
+ case resource_call(F) of
+ true ->
+ encode_body_if_set(),
+ true;
+ Result -> Result
+ end
+ end.
+
+encode_body_if_set() ->
+ case wrcall(has_resp_body) of
+ true ->
+ Body = wrcall(resp_body),
+ wrcall({set_resp_body, encode_body(Body)}),
+ true;
+ _ -> false
end.
encode_body(Body) ->
@@ -555,6 +574,8 @@ encode_body(Body) ->
case Body of
{stream, StreamBody} ->
{stream, make_encoder_stream(Encoder, Charsetter, StreamBody)};
+ {writer, BodyFun} ->
+ {writer, {Encoder, Charsetter, BodyFun}};
_ ->
Encoder(Charsetter(iolist_to_binary(Body)))
end.
@@ -564,7 +585,7 @@ make_encoder_stream(Encoder, Charsetter, {Body, done}) ->
make_encoder_stream(Encoder, Charsetter, {Body, Next}) ->
{Encoder(Charsetter(Body)),
fun() -> make_encoder_stream(Encoder, Charsetter, Next()) end}.
-
+
choose_encoding(AccEncHdr) ->
Encs = [Enc || {Enc,_Fun} <- resource_call(encodings_provided)],
case webmachine_util:choose_encoding(Encs, AccEncHdr) of
Oops, something went wrong.

0 comments on commit d67e934

Please sign in to comment.