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ů.

205 řádky
7.5 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 14 roky
před 14 roky
  1. %%% File : ibrowse_test_server.erl
  2. %%% Author : Chandrashekhar Mullaparthi <chandrashekhar.mullaparthi@t-mobile.co.uk>
  3. %%% Description : A server to simulate various test scenarios
  4. %%% Created : 17 Oct 2010 by Chandrashekhar Mullaparthi <chandrashekhar.mullaparthi@t-mobile.co.uk>
  5. -module(ibrowse_test_server).
  6. -export([
  7. start_server/2,
  8. stop_server/1
  9. ]).
  10. -record(request, {method, uri, version, headers = [], body = []}).
  11. -define(dec2hex(X), erlang:integer_to_list(X, 16)).
  12. start_server(Port, Sock_type) ->
  13. Fun = fun() ->
  14. register(server_proc_name(Port), self()),
  15. case do_listen(Sock_type, Port, [{active, false},
  16. {reuseaddr, true},
  17. {nodelay, true},
  18. {packet, http}]) of
  19. {ok, Sock} ->
  20. do_trace("Server listening on port: ~p~n", [Port]),
  21. accept_loop(Sock, Sock_type);
  22. Err ->
  23. erlang:error(
  24. lists:flatten(
  25. io_lib:format(
  26. "Failed to start server on port ~p. ~p~n",
  27. [Port, Err]))),
  28. exit({listen_error, Err})
  29. end
  30. end,
  31. spawn_link(Fun).
  32. stop_server(Port) ->
  33. catch exit(whereis(server_proc_name(Port)), kill),
  34. ok.
  35. server_proc_name(Port) ->
  36. list_to_atom("ibrowse_test_server_"++integer_to_list(Port)).
  37. do_listen(tcp, Port, Opts) ->
  38. gen_tcp:listen(Port, Opts);
  39. do_listen(ssl, Port, Opts) ->
  40. application:start(crypto),
  41. application:start(ssl),
  42. ssl:listen(Port, Opts).
  43. do_accept(tcp, Listen_sock) ->
  44. gen_tcp:accept(Listen_sock);
  45. do_accept(ssl, Listen_sock) ->
  46. ssl:ssl_accept(Listen_sock).
  47. accept_loop(Sock, Sock_type) ->
  48. case do_accept(Sock_type, Sock) of
  49. {ok, Conn} ->
  50. Pid = spawn_link(
  51. fun() ->
  52. server_loop(Conn, Sock_type, #request{})
  53. end),
  54. set_controlling_process(Conn, Sock_type, Pid),
  55. Pid ! {setopts, [{active, true}]},
  56. accept_loop(Sock, Sock_type);
  57. Err ->
  58. Err
  59. end.
  60. set_controlling_process(Sock, tcp, Pid) ->
  61. gen_tcp:controlling_process(Sock, Pid);
  62. set_controlling_process(Sock, ssl, Pid) ->
  63. ssl:controlling_process(Sock, Pid).
  64. setopts(Sock, tcp, Opts) ->
  65. inet:setopts(Sock, Opts);
  66. setopts(Sock, ssl, Opts) ->
  67. ssl:setopts(Sock, Opts).
  68. server_loop(Sock, Sock_type, #request{headers = Headers} = Req) ->
  69. receive
  70. {http, Sock, {http_request, HttpMethod, HttpUri, HttpVersion}} ->
  71. server_loop(Sock, Sock_type, Req#request{method = HttpMethod,
  72. uri = HttpUri,
  73. version = HttpVersion});
  74. {http, Sock, {http_header, _, _, _, _} = H} ->
  75. server_loop(Sock, Sock_type, Req#request{headers = [H | Headers]});
  76. {http, Sock, http_eoh} ->
  77. process_request(Sock, Sock_type, Req),
  78. server_loop(Sock, Sock_type, #request{});
  79. {http, Sock, {http_error, Err}} ->
  80. do_trace("Error parsing HTTP request:~n"
  81. "Req so far : ~p~n"
  82. "Err : ", [Req, Err]),
  83. exit({http_error, Err});
  84. {setopts, Opts} ->
  85. setopts(Sock, Sock_type, Opts),
  86. server_loop(Sock, Sock_type, Req);
  87. {tcp_closed, Sock} ->
  88. do_trace("Client closed connection~n", []),
  89. ok;
  90. Other ->
  91. do_trace("Recvd unknown msg: ~p~n", [Other]),
  92. exit({unknown_msg, Other})
  93. after 5000 ->
  94. do_trace("Timing out client connection~n", []),
  95. ok
  96. end.
  97. do_trace(Fmt, Args) ->
  98. do_trace(get(my_trace_flag), Fmt, Args).
  99. do_trace(true, Fmt, Args) ->
  100. io:format("~s -- " ++ Fmt, [ibrowse_lib:printable_date() | Args]);
  101. do_trace(_, _, _) ->
  102. ok.
  103. process_request(Sock, Sock_type,
  104. #request{method='GET',
  105. headers = Headers,
  106. uri = {abs_path, "/ibrowse_stream_once_chunk_pipeline_test"}} = Req) ->
  107. Req_id = case lists:keysearch("X-Ibrowse-Request-Id", 3, Headers) of
  108. false ->
  109. "";
  110. {value, {http_header, _, _, _, Req_id_1}} ->
  111. Req_id_1
  112. end,
  113. Req_id_header = ["x-ibrowse-request-id: ", Req_id, "\r\n"],
  114. do_trace("Recvd req: ~p~n", [Req]),
  115. Body = string:join([integer_to_list(X) || X <- lists:seq(1,100)], "-"),
  116. Chunked_body = chunk_request_body(Body, 50),
  117. Resp_1 = [<<"HTTP/1.1 200 OK\r\n">>,
  118. Req_id_header,
  119. <<"Transfer-Encoding: chunked\r\n\r\n">>],
  120. Resp_2 = Chunked_body,
  121. do_send(Sock, Sock_type, Resp_1),
  122. timer:sleep(100),
  123. do_send(Sock, Sock_type, Resp_2);
  124. process_request(Sock, Sock_type,
  125. #request{method='GET',
  126. headers = _Headers,
  127. uri = {abs_path, "/ibrowse_inac_timeout_test"}} = Req) ->
  128. do_trace("Recvd req: ~p. Sleeping for 30 secs...~n", [Req]),
  129. timer:sleep(30000),
  130. do_trace("...Sending response now.~n", []),
  131. Resp = <<"HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n">>,
  132. do_send(Sock, Sock_type, Resp);
  133. process_request(Sock, Sock_type, Req) ->
  134. do_trace("Recvd req: ~p~n", [Req]),
  135. Resp = <<"HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n">>,
  136. do_send(Sock, Sock_type, Resp).
  137. do_send(Sock, tcp, Resp) ->
  138. gen_tcp:send(Sock, Resp);
  139. do_send(Sock, ssl, Resp) ->
  140. ssl:send(Sock, Resp).
  141. %%------------------------------------------------------------------------------
  142. %% Utility functions
  143. %%------------------------------------------------------------------------------
  144. chunk_request_body(Body, _ChunkSize) when is_tuple(Body) orelse
  145. is_function(Body) ->
  146. Body;
  147. chunk_request_body(Body, ChunkSize) ->
  148. chunk_request_body(Body, ChunkSize, []).
  149. chunk_request_body(Body, _ChunkSize, Acc) when Body == <<>>; Body == [] ->
  150. LastChunk = "0\r\n",
  151. lists:reverse(["\r\n", LastChunk | Acc]);
  152. chunk_request_body(Body, ChunkSize, Acc) when is_binary(Body),
  153. size(Body) >= ChunkSize ->
  154. <<ChunkBody:ChunkSize/binary, Rest/binary>> = Body,
  155. Chunk = [?dec2hex(ChunkSize),"\r\n",
  156. ChunkBody, "\r\n"],
  157. chunk_request_body(Rest, ChunkSize, [Chunk | Acc]);
  158. chunk_request_body(Body, _ChunkSize, Acc) when is_binary(Body) ->
  159. BodySize = size(Body),
  160. Chunk = [?dec2hex(BodySize),"\r\n",
  161. Body, "\r\n"],
  162. LastChunk = "0\r\n",
  163. lists:reverse(["\r\n", LastChunk, Chunk | Acc]);
  164. chunk_request_body(Body, ChunkSize, Acc) when length(Body) >= ChunkSize ->
  165. {ChunkBody, Rest} = split_list_at(Body, ChunkSize),
  166. Chunk = [?dec2hex(ChunkSize),"\r\n",
  167. ChunkBody, "\r\n"],
  168. chunk_request_body(Rest, ChunkSize, [Chunk | Acc]);
  169. chunk_request_body(Body, _ChunkSize, Acc) when is_list(Body) ->
  170. BodySize = length(Body),
  171. Chunk = [?dec2hex(BodySize),"\r\n",
  172. Body, "\r\n"],
  173. LastChunk = "0\r\n",
  174. lists:reverse(["\r\n", LastChunk, Chunk | Acc]).
  175. split_list_at(List, N) ->
  176. split_list_at(List, N, []).
  177. split_list_at([], _, Acc) ->
  178. {lists:reverse(Acc), []};
  179. split_list_at(List2, 0, List1) ->
  180. {lists:reverse(List1), List2};
  181. split_list_at([H | List2], N, List1) ->
  182. split_list_at(List2, N-1, [H | List1]).