Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

275 řádky
11 KiB

před 14 roky
před 14 roky
před 14 roky
před 14 roky
před 14 roky
před 14 roky
před 14 roky
před 14 roky
před 13 roky
před 14 roky
před 14 roky
před 14 roky
před 14 roky
před 14 roky
před 14 roky
před 14 roky
před 14 roky
před 14 roky
před 13 roky
před 13 roky
před 14 roky
před 14 roky
  1. %%% File : ibrowse_test_server.erl
  2. %%% Author : Chandrashekhar Mullaparthi <chandrashekhar.mullaparthi@t-mobile.co.uk>
  3. %%% Benjamin Lee <yardspoon@gmail.com>
  4. %%% Brian Richards <bmrichards16@gmail.com>
  5. %%% Description : A server to simulate various test scenarios
  6. %%% Created : 17 Oct 2010 by Chandrashekhar Mullaparthi <chandrashekhar.mullaparthi@t-mobile.co.uk>
  7. -module(ibrowse_test_server).
  8. -export([
  9. start_server/2,
  10. stop_server/1,
  11. get_conn_pipeline_depth/0
  12. ]).
  13. -record(request, {method, uri, version, headers = [], body = []}).
  14. -define(dec2hex(X), erlang:integer_to_list(X, 16)).
  15. -define(ACCEPT_TIMEOUT_MS, 1000).
  16. -define(CONN_PIPELINE_DEPTH, conn_pipeline_depth).
  17. start_server(Port, Sock_type) ->
  18. Fun = fun() ->
  19. Name = server_proc_name(Port),
  20. register(Name, self()),
  21. ets:new(?CONN_PIPELINE_DEPTH, [named_table, public, set]),
  22. case do_listen(Sock_type, Port, [{active, false},
  23. {reuseaddr, true},
  24. {nodelay, true},
  25. {packet, http}]) of
  26. {ok, Sock} ->
  27. do_trace("Server listening on port: ~p~n", [Port]),
  28. accept_loop(Sock, Sock_type);
  29. Err ->
  30. erlang:error(
  31. lists:flatten(
  32. io_lib:format(
  33. "Failed to start server on port ~p. ~p~n",
  34. [Port, Err]))),
  35. exit({listen_error, Err})
  36. end,
  37. unregister(Name)
  38. end,
  39. spawn_link(Fun).
  40. stop_server(Port) ->
  41. server_proc_name(Port) ! stop,
  42. timer:sleep(2000), % wait for server to receive msg and unregister
  43. ok.
  44. get_conn_pipeline_depth() ->
  45. ets:tab2list(?CONN_PIPELINE_DEPTH).
  46. server_proc_name(Port) ->
  47. list_to_atom("ibrowse_test_server_"++integer_to_list(Port)).
  48. do_listen(tcp, Port, Opts) ->
  49. gen_tcp:listen(Port, Opts);
  50. do_listen(ssl, Port, Opts) ->
  51. application:start(crypto),
  52. application:start(ssl),
  53. ssl:listen(Port, Opts).
  54. do_accept(tcp, Listen_sock) ->
  55. gen_tcp:accept(Listen_sock, ?ACCEPT_TIMEOUT_MS);
  56. do_accept(ssl, Listen_sock) ->
  57. ssl:ssl_accept(Listen_sock, ?ACCEPT_TIMEOUT_MS).
  58. accept_loop(Sock, Sock_type) ->
  59. case do_accept(Sock_type, Sock) of
  60. {ok, Conn} ->
  61. Pid = spawn_link(
  62. fun() ->
  63. server_loop(Conn, Sock_type, #request{})
  64. end),
  65. ets:insert(?CONN_PIPELINE_DEPTH, {Pid, 0}),
  66. set_controlling_process(Conn, Sock_type, Pid),
  67. Pid ! {setopts, [{active, true}]},
  68. accept_loop(Sock, Sock_type);
  69. {error, timeout} ->
  70. receive
  71. stop ->
  72. ok
  73. after 10 ->
  74. accept_loop(Sock, Sock_type)
  75. end;
  76. Err ->
  77. Err
  78. end.
  79. set_controlling_process(Sock, tcp, Pid) ->
  80. gen_tcp:controlling_process(Sock, Pid);
  81. set_controlling_process(Sock, ssl, Pid) ->
  82. ssl:controlling_process(Sock, Pid).
  83. setopts(Sock, tcp, Opts) ->
  84. inet:setopts(Sock, Opts);
  85. setopts(Sock, ssl, Opts) ->
  86. ssl:setopts(Sock, Opts).
  87. server_loop(Sock, Sock_type, #request{headers = Headers} = Req) ->
  88. receive
  89. {http, Sock, {http_request, HttpMethod, HttpUri, HttpVersion}} ->
  90. server_loop(Sock, Sock_type, Req#request{method = HttpMethod,
  91. uri = HttpUri,
  92. version = HttpVersion});
  93. {http, Sock, {http_header, _, _, _, _} = H} ->
  94. server_loop(Sock, Sock_type, Req#request{headers = [H | Headers]});
  95. {http, Sock, http_eoh} ->
  96. ets:update_counter(?CONN_PIPELINE_DEPTH, self(), 1),
  97. process_request(Sock, Sock_type, Req),
  98. server_loop(Sock, Sock_type, #request{});
  99. {http, Sock, {http_error, Err}} ->
  100. do_trace("Error parsing HTTP request:~n"
  101. "Req so far : ~p~n"
  102. "Err : ", [Req, Err]),
  103. exit({http_error, Err});
  104. {setopts, Opts} ->
  105. setopts(Sock, Sock_type, Opts),
  106. server_loop(Sock, Sock_type, Req);
  107. {tcp_closed, Sock} ->
  108. do_trace("Client closed connection~n", []),
  109. ok;
  110. Other ->
  111. do_trace("Recvd unknown msg: ~p~n", [Other]),
  112. exit({unknown_msg, Other})
  113. after 5000 ->
  114. do_trace("Timing out client connection~n", []),
  115. ok
  116. end.
  117. do_trace(Fmt, Args) ->
  118. do_trace(get(my_trace_flag), Fmt, Args).
  119. do_trace(true, Fmt, Args) ->
  120. io:format("~s -- " ++ Fmt, [ibrowse_lib:printable_date() | Args]);
  121. do_trace(_, _, _) ->
  122. ok.
  123. process_request(Sock, Sock_type,
  124. #request{method='GET',
  125. headers = Headers,
  126. uri = {abs_path, "/ibrowse_stream_once_chunk_pipeline_test"}} = Req) ->
  127. Req_id = case lists:keysearch("X-Ibrowse-Request-Id", 3, Headers) of
  128. false ->
  129. "";
  130. {value, {http_header, _, _, _, Req_id_1}} ->
  131. Req_id_1
  132. end,
  133. Req_id_header = ["x-ibrowse-request-id: ", Req_id, "\r\n"],
  134. do_trace("Recvd req: ~p~n", [Req]),
  135. Body = string:join([integer_to_list(X) || X <- lists:seq(1,100)], "-"),
  136. Chunked_body = chunk_request_body(Body, 50),
  137. Resp_1 = [<<"HTTP/1.1 200 OK\r\n">>,
  138. Req_id_header,
  139. <<"Transfer-Encoding: chunked\r\n\r\n">>],
  140. Resp_2 = Chunked_body,
  141. do_send(Sock, Sock_type, Resp_1),
  142. timer:sleep(100),
  143. do_send(Sock, Sock_type, Resp_2);
  144. process_request(Sock, Sock_type,
  145. #request{method='GET',
  146. headers = _Headers,
  147. uri = {abs_path, "/ibrowse_inac_timeout_test"}} = Req) ->
  148. do_trace("Recvd req: ~p. Sleeping for 30 secs...~n", [Req]),
  149. timer:sleep(30000),
  150. do_trace("...Sending response now.~n", []),
  151. Resp = <<"HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n">>,
  152. do_send(Sock, Sock_type, Resp);
  153. process_request(Sock, Sock_type,
  154. #request{method='HEAD',
  155. headers = _Headers,
  156. uri = {abs_path, "/ibrowse_head_transfer_enc"}}) ->
  157. Resp = <<"HTTP/1.1 400 Bad Request\r\nServer: Apache-Coyote/1.1\r\nContent-Length:5\r\nDate: Wed, 04 Apr 2012 16:53:49 GMT\r\n\r\nabcde">>,
  158. do_send(Sock, Sock_type, Resp);
  159. process_request(Sock, Sock_type,
  160. #request{method='GET',
  161. headers = Headers,
  162. uri = {abs_path, "/ibrowse_echo_header"}}) ->
  163. Tag = "x-binary",
  164. Headers_1 = [{to_lower(X), to_lower(Y)} || {http_header, _, X, _, Y} <- Headers],
  165. X_binary_header_val = case lists:keysearch(Tag, 1, Headers_1) of
  166. false ->
  167. "not_found";
  168. {value, {_, V}} ->
  169. V
  170. end,
  171. Resp = [<<"HTTP/1.1 200 OK\r\n">>,
  172. <<"Server: ibrowse_test\r\n">>,
  173. Tag, ": ", X_binary_header_val, "\r\n",
  174. <<"Content-Length: 0\r\n\r\n">>],
  175. do_send(Sock, Sock_type, Resp);
  176. process_request(Sock, Sock_type,
  177. #request{method='HEAD',
  178. headers = _Headers,
  179. uri = {abs_path, "/ibrowse_head_test"}}) ->
  180. Resp = <<"HTTP/1.1 200 OK\r\nServer: Apache-Coyote/1.1\r\nTransfer-Encoding: chunked\r\nDate: Wed, 04 Apr 2012 16:53:49 GMT\r\nConnection: close\r\n\r\n">>,
  181. do_send(Sock, Sock_type, Resp);
  182. process_request(Sock, Sock_type,
  183. #request{method='POST',
  184. headers = _Headers,
  185. uri = {abs_path, "/ibrowse_303_no_body_test"}}) ->
  186. Resp = <<"HTTP/1.1 303 See Other\r\nLocation: http://example.org\r\n">>,
  187. do_send(Sock, Sock_type, Resp);
  188. process_request(Sock, Sock_type,
  189. #request{method='POST',
  190. headers = _Headers,
  191. uri = {abs_path, "/ibrowse_303_with_body_test"}}) ->
  192. Resp = <<"HTTP/1.1 303 See Other\r\nLocation: http://example.org\r\nContent-Length: 5\r\n\r\nabcde">>,
  193. do_send(Sock, Sock_type, Resp);
  194. process_request(_Sock, _Sock_type, #request{uri = {abs_path, "/never_respond"} } ) ->
  195. noop;
  196. process_request(Sock, Sock_type, Req) ->
  197. do_trace("Recvd req: ~p~n", [Req]),
  198. Resp = <<"HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n">>,
  199. do_send(Sock, Sock_type, Resp).
  200. do_send(Sock, tcp, Resp) ->
  201. gen_tcp:send(Sock, Resp);
  202. do_send(Sock, ssl, Resp) ->
  203. ssl:send(Sock, Resp).
  204. %%------------------------------------------------------------------------------
  205. %% Utility functions
  206. %%------------------------------------------------------------------------------
  207. chunk_request_body(Body, _ChunkSize) when is_tuple(Body) orelse
  208. is_function(Body) ->
  209. Body;
  210. chunk_request_body(Body, ChunkSize) ->
  211. chunk_request_body(Body, ChunkSize, []).
  212. chunk_request_body(Body, _ChunkSize, Acc) when Body == <<>>; Body == [] ->
  213. LastChunk = "0\r\n",
  214. lists:reverse(["\r\n", LastChunk | Acc]);
  215. chunk_request_body(Body, ChunkSize, Acc) when is_binary(Body),
  216. size(Body) >= ChunkSize ->
  217. <<ChunkBody:ChunkSize/binary, Rest/binary>> = Body,
  218. Chunk = [?dec2hex(ChunkSize),"\r\n",
  219. ChunkBody, "\r\n"],
  220. chunk_request_body(Rest, ChunkSize, [Chunk | Acc]);
  221. chunk_request_body(Body, _ChunkSize, Acc) when is_binary(Body) ->
  222. BodySize = size(Body),
  223. Chunk = [?dec2hex(BodySize),"\r\n",
  224. Body, "\r\n"],
  225. LastChunk = "0\r\n",
  226. lists:reverse(["\r\n", LastChunk, Chunk | Acc]);
  227. chunk_request_body(Body, ChunkSize, Acc) when length(Body) >= ChunkSize ->
  228. {ChunkBody, Rest} = split_list_at(Body, ChunkSize),
  229. Chunk = [?dec2hex(ChunkSize),"\r\n",
  230. ChunkBody, "\r\n"],
  231. chunk_request_body(Rest, ChunkSize, [Chunk | Acc]);
  232. chunk_request_body(Body, _ChunkSize, Acc) when is_list(Body) ->
  233. BodySize = length(Body),
  234. Chunk = [?dec2hex(BodySize),"\r\n",
  235. Body, "\r\n"],
  236. LastChunk = "0\r\n",
  237. lists:reverse(["\r\n", LastChunk, Chunk | Acc]).
  238. split_list_at(List, N) ->
  239. split_list_at(List, N, []).
  240. split_list_at([], _, Acc) ->
  241. {lists:reverse(Acc), []};
  242. split_list_at(List2, 0, List1) ->
  243. {lists:reverse(List1), List2};
  244. split_list_at([H | List2], N, List1) ->
  245. split_list_at(List2, N-1, [H | List1]).
  246. to_lower(X) when is_atom(X) ->
  247. list_to_atom(to_lower(atom_to_list(X)));
  248. to_lower(X) when is_list(X) ->
  249. string:to_lower(X).