Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

implement start_link/1 exports and add {link, false} option to start/1

  • Loading branch information...
commit 7972b2b46353407496787c2e51d818747349cedd 1 parent 497f1fc
@etrepum etrepum authored
View
2  src/mochiweb.erl
@@ -98,7 +98,7 @@ with_server(Transport, ServerFun, ClientFun) ->
ssl ->
ServerOpts0 ++ [{ssl, true}, {ssl_opts, ssl_cert_opts()}]
end,
- {ok, Server} = mochiweb_http:start(ServerOpts),
+ {ok, Server} = mochiweb_http:start_link(ServerOpts),
Port = mochiweb_socket_server:get(Server, port),
Res = (catch ClientFun(Transport, Port)),
mochiweb_http:stop(Server),
View
2  src/mochiweb_acceptor.erl
@@ -35,6 +35,8 @@ init(Server, Listen, Loop) ->
call_loop({M, F}, Socket) ->
M:F(Socket);
+call_loop({M, F, [A1]}, Socket) ->
+ M:F(Socket, A1);
call_loop({M, F, A}, Socket) ->
erlang:apply(M, F, [Socket | A]);
call_loop(Loop, Socket) ->
View
13 src/mochiweb_echo.erl
@@ -11,11 +11,14 @@ stop() ->
mochiweb_socket_server:stop(?MODULE).
start() ->
- mochiweb_socket_server:start([{name, ?MODULE},
- {port, 6789},
- {ip, "127.0.0.1"},
- {max, 1},
- {loop, {?MODULE, loop}}]).
+ mochiweb_socket_server:start([{link, false} | options()]).
+
+options() ->
+ [{name, ?MODULE},
+ {port, 6789},
+ {ip, "127.0.0.1"},
+ {max, 1},
+ {loop, {?MODULE, loop}}].
loop(Socket) ->
case mochiweb_socket:recv(Socket, 0, 30000) of
View
65 src/mochiweb_http.erl
@@ -5,13 +5,13 @@
-module(mochiweb_http).
-author('bob@mochimedia.com').
--export([start/0, start/1, stop/0, stop/1]).
--export([loop/2, default_body/1]).
+-export([start/1, start_link/1, stop/0, stop/1]).
+-export([loop/2]).
-export([after_response/2, reentry/1]).
-export([parse_range_request/1, range_skip_length/2]).
--define(REQUEST_RECV_TIMEOUT, 300000). % timeout waiting for request line
--define(HEADERS_RECV_TIMEOUT, 30000). % timeout waiting for headers
+-define(REQUEST_RECV_TIMEOUT, 300000). %% timeout waiting for request line
+-define(HEADERS_RECV_TIMEOUT, 30000). %% timeout waiting for headers
-define(MAX_HEADERS, 1000).
-define(DEFAULTS, [{name, ?MODULE},
@@ -19,9 +19,7 @@
parse_options(Options) ->
{loop, HttpLoop} = proplists:lookup(loop, Options),
- Loop = fun (S) ->
- ?MODULE:loop(S, HttpLoop)
- end,
+ Loop = {?MODULE, loop, [HttpLoop]},
Options1 = [{loop, Loop} | proplists:delete(loop, Options)],
mochilists:set_defaults(?DEFAULTS, Options1).
@@ -31,15 +29,12 @@ stop() ->
stop(Name) ->
mochiweb_socket_server:stop(Name).
-start() ->
- start([{ip, "127.0.0.1"},
- {loop, {?MODULE, default_body}}]).
-
%% @spec start(Options) -> ServerRet
%% Options = [option()]
%% Option = {name, atom()} | {ip, string() | tuple()} | {backlog, integer()}
%% | {nodelay, boolean()} | {acceptor_pool_size, integer()}
%% | {ssl, boolean()} | {profile_fun, undefined | (Props) -> ok}
+%% | {link, false}
%% @doc Start a mochiweb server.
%% profile_fun is used to profile accept timing.
%% After each accept, if defined, profile_fun is called with a proplist of a subset of the mochiweb_socket_server state and timing information.
@@ -48,52 +43,8 @@ start() ->
start(Options) ->
mochiweb_socket_server:start(parse_options(Options)).
-frm(Body) ->
- ["<html><head></head><body>"
- "<form method=\"POST\">"
- "<input type=\"hidden\" value=\"message\" name=\"hidden\"/>"
- "<input type=\"submit\" value=\"regular POST\">"
- "</form>"
- "<br />"
- "<form method=\"POST\" enctype=\"multipart/form-data\""
- " action=\"/multipart\">"
- "<input type=\"hidden\" value=\"multipart message\" name=\"hidden\"/>"
- "<input type=\"file\" name=\"file\"/>"
- "<input type=\"submit\" value=\"multipart POST\" />"
- "</form>"
- "<pre>", Body, "</pre>"
- "</body></html>"].
-
-default_body(Req, M, "/chunked") when M =:= 'GET'; M =:= 'HEAD' ->
- Res = Req:ok({"text/plain", [], chunked}),
- Res:write_chunk("First chunk\r\n"),
- timer:sleep(5000),
- Res:write_chunk("Last chunk\r\n"),
- Res:write_chunk("");
-default_body(Req, M, _Path) when M =:= 'GET'; M =:= 'HEAD' ->
- Body = io_lib:format("~p~n", [[{parse_qs, Req:parse_qs()},
- {parse_cookie, Req:parse_cookie()},
- Req:dump()]]),
- Req:ok({"text/html",
- [mochiweb_cookies:cookie("mochiweb_http", "test_cookie")],
- frm(Body)});
-default_body(Req, 'POST', "/multipart") ->
- Body = io_lib:format("~p~n", [[{parse_qs, Req:parse_qs()},
- {parse_cookie, Req:parse_cookie()},
- {body, Req:recv_body()},
- Req:dump()]]),
- Req:ok({"text/html", [], frm(Body)});
-default_body(Req, 'POST', _Path) ->
- Body = io_lib:format("~p~n", [[{parse_qs, Req:parse_qs()},
- {parse_cookie, Req:parse_cookie()},
- {parse_post, Req:parse_post()},
- Req:dump()]]),
- Req:ok({"text/html", [], frm(Body)});
-default_body(Req, _Method, _Path) ->
- Req:respond({501, [], []}).
-
-default_body(Req) ->
- default_body(Req, Req:get(method), Req:get(path)).
+start_link(Options) ->
+ mochiweb_socket_server:start_link(parse_options(Options)).
loop(Socket, Body) ->
ok = mochiweb_socket:setopts(Socket, [{packet, http}]),
View
2  src/mochiweb_multipart.erl
@@ -311,7 +311,7 @@ with_socket_server(Transport, ServerFun, ClientFun) ->
ssl ->
ServerOpts0 ++ [{ssl, true}, {ssl_opts, ssl_cert_opts()}]
end,
- {ok, Server} = mochiweb_socket_server:start(ServerOpts),
+ {ok, Server} = mochiweb_socket_server:start_link(ServerOpts),
Port = mochiweb_socket_server:get(Server, port),
ClientOpts = [binary, {active, false}],
{ok, Client} = case Transport of
View
44 src/mochiweb_socket_server.erl
@@ -9,7 +9,7 @@
-include("internal.hrl").
--export([start/1, stop/1]).
+-export([start/1, start_link/1, stop/1]).
-export([init/1, handle_call/3, handle_cast/2, terminate/2, code_change/3,
handle_info/2]).
-export([get/2, set/3]).
@@ -33,10 +33,22 @@
-define(is_old_state(State), not is_record(State, mochiweb_socket_server)).
-start(State=#mochiweb_socket_server{}) ->
- start_server(State);
+start_link(Options) ->
+ start_server(start_link, parse_options(Options)).
+
start(Options) ->
- start(parse_options(Options)).
+ case lists:keytake(link, 1, Options) of
+ {value, {_Key, false}, Options1} ->
+ start_server(start, parse_options(Options1));
+ _ ->
+ %% TODO: https://github.com/mochi/mochiweb/issues/58
+ %% [X] Phase 1: Add new APIs (Sep 2011)
+ %% [_] Phase 2: Add deprecation warning
+ %% [_] Phase 3: Change default to {link, false} and ignore link
+ %% [_] Phase 4: Add deprecation warning for {link, _} option
+ %% [_] Phase 5: Remove support for {link, _} option
+ start_link(Options)
+ end.
get(Name, Property) ->
gen_server:call(Name, {get, Property}).
@@ -61,6 +73,8 @@ stop(Options) ->
%% Internal API
+parse_options(State=#mochiweb_socket_server{}) ->
+ State;
parse_options(Options) ->
parse_options(Options, #mochiweb_socket_server{}).
@@ -116,22 +130,22 @@ parse_options([{profile_fun, ProfileFun} | Rest], State) when is_function(Profil
parse_options(Rest, State#mochiweb_socket_server{profile_fun=ProfileFun}).
-start_server(State=#mochiweb_socket_server{ssl=Ssl, name=Name}) ->
- case Ssl of
- true ->
- ok = mochiweb:ensure_started(crypto),
- ok = mochiweb:ensure_started(public_key),
- ok = mochiweb:ensure_started(ssl);
- false ->
- ok
- end,
+start_server(F, State=#mochiweb_socket_server{ssl=Ssl, name=Name}) ->
+ ok = prep_ssl(Ssl),
case Name of
undefined ->
- gen_server:start_link(?MODULE, State, []);
+ gen_server:F(?MODULE, State, []);
_ ->
- gen_server:start_link(Name, ?MODULE, State, [])
+ gen_server:F(Name, ?MODULE, State, [])
end.
+prep_ssl(true) ->
+ ok = mochiweb:ensure_started(crypto),
+ ok = mochiweb:ensure_started(public_key),
+ ok = mochiweb:ensure_started(ssl);
+prep_ssl(false) ->
+ ok.
+
ensure_int(N) when is_integer(N) ->
N;
ensure_int(S) when is_list(S) ->
Please sign in to comment.
Something went wrong with that request. Please try again.