server.d 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006
  1. /**
  2. A HTTP 1.1/1.0 server implementation.
  3. Copyright: © 2012-2017 Sönke Ludwig
  4. License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file.
  5. Authors: Sönke Ludwig, Jan Krüger, Ilya Shipunov
  6. */
  7. module vibe.http.server;
  8. public import vibe.core.net;
  9. public import vibe.http.common;
  10. public import vibe.http.session;
  11. import vibe.container.internal.appender : FixedAppender;
  12. import vibe.container.internal.utilallocator;
  13. import vibe.core.file;
  14. import vibe.core.log;
  15. import vibe.data.json;
  16. import vibe.http.dist;
  17. import vibe.http.log;
  18. import vibe.inet.message;
  19. import vibe.inet.url;
  20. import vibe.inet.webform;
  21. import vibe.internal.interfaceproxy : InterfaceProxy;
  22. import vibe.stream.counting;
  23. import vibe.stream.operations;
  24. import vibe.stream.tls;
  25. import vibe.stream.wrapper : ConnectionProxyStream, createConnectionProxyStream, createConnectionProxyStreamFL;
  26. import vibe.textfilter.urlencode;
  27. import vibe.internal.freelistref;
  28. import vibe.internal.string : formatAlloc, icmp2;
  29. import core.atomic;
  30. import core.vararg;
  31. import diet.traits : SafeFilterCallback, dietTraits;
  32. import std.algorithm : canFind, splitter;
  33. import std.array;
  34. import std.conv;
  35. import std.datetime;
  36. import std.encoding : sanitize;
  37. import std.exception;
  38. import std.format;
  39. import std.functional : toDelegate;
  40. import std.string;
  41. import std.traits : ReturnType;
  42. import std.typecons;
  43. import std.uri;
  44. version (VibeNoSSL) enum HaveNoTLS = true;
  45. else version (Have_botan) enum HaveNoTLS = false;
  46. else version (Have_openssl) enum HaveNoTLS = false;
  47. else enum HaveNoTLS = true;
  48. /**************************************************************************************************/
  49. /* Public functions */
  50. /**************************************************************************************************/
  51. /**
  52. Starts a HTTP server listening on the specified port.
  53. request_handler will be called for each HTTP request that is made. The
  54. res parameter of the callback then has to be filled with the response
  55. data.
  56. request_handler can be either HTTPServerRequestDelegate/HTTPServerRequestFunction
  57. or a class/struct with a member function 'handleRequest' that has the same
  58. signature.
  59. Note that if the application has been started with the --disthost command line
  60. switch, listenHTTP() will automatically listen on the specified VibeDist host
  61. instead of locally. This allows for a seamless switch from single-host to
  62. multi-host scenarios without changing the code. If you need to listen locally,
  63. use listenHTTPPlain() instead.
  64. Params:
  65. settings = Customizes the HTTP servers functionality (host string or HTTPServerSettings object)
  66. request_handler = This callback is invoked for each incoming request and is responsible
  67. for generating the response.
  68. Returns:
  69. A handle is returned that can be used to stop listening for further HTTP
  70. requests with the supplied settings. Another call to `listenHTTP` can be
  71. used afterwards to start listening again.
  72. */
  73. HTTPListener listenHTTP(Settings)(Settings _settings, HTTPServerRequestDelegate request_handler)
  74. @safe
  75. if (is(Settings == string) || is(Settings == HTTPServerSettings)) {
  76. // auto-construct HTTPServerSettings
  77. static if (is(Settings == string))
  78. auto settings = new HTTPServerSettings(_settings);
  79. else
  80. alias settings = _settings;
  81. enforce(settings.bindAddresses.length, "Must provide at least one bind address for a HTTP server.");
  82. // if a VibeDist host was specified on the command line, register there instead of listening
  83. // directly.
  84. if (s_distHost.length && !settings.disableDistHost) {
  85. return listenHTTPDist(settings, request_handler, s_distHost, s_distPort);
  86. } else {
  87. return listenHTTPPlain(settings, request_handler);
  88. }
  89. }
  90. /// ditto
  91. HTTPListener listenHTTP(Settings)(Settings settings, HTTPServerRequestFunction request_handler)
  92. @safe
  93. if (is(Settings == string) || is(Settings == HTTPServerSettings)) {
  94. return listenHTTP(settings, () @trusted { return toDelegate(request_handler); } ());
  95. }
  96. /// ditto
  97. HTTPListener listenHTTP(Settings)(Settings settings, HTTPServerRequestHandler request_handler)
  98. @safe
  99. if (is(Settings == string) || is(Settings == HTTPServerSettings)) {
  100. return listenHTTP(settings, &request_handler.handleRequest);
  101. }
  102. /// ditto
  103. HTTPListener listenHTTP(Settings)(Settings settings, HTTPServerRequestDelegateS request_handler)
  104. @safe
  105. if (is(Settings == string) || is(Settings == HTTPServerSettings)) {
  106. return listenHTTP(settings, cast(HTTPServerRequestDelegate)request_handler);
  107. }
  108. /// ditto
  109. HTTPListener listenHTTP(Settings)(Settings settings, HTTPServerRequestFunctionS request_handler)
  110. @safe
  111. if (is(Settings == string) || is(Settings == HTTPServerSettings)) {
  112. return listenHTTP(settings, () @trusted { return toDelegate(request_handler); } ());
  113. }
  114. /// ditto
  115. HTTPListener listenHTTP(Settings)(Settings settings, HTTPServerRequestHandlerS request_handler)
  116. @safe
  117. if (is(Settings == string) || is(Settings == HTTPServerSettings)) {
  118. return listenHTTP(settings, &request_handler.handleRequest);
  119. }
  120. /// Scheduled for deprecation - use a `@safe` callback instead.
  121. HTTPListener listenHTTP(Settings)(Settings settings, void delegate(HTTPServerRequest, HTTPServerResponse) @system request_handler)
  122. @system
  123. if (is(Settings == string) || is(Settings == HTTPServerSettings)) {
  124. return listenHTTP(settings, (req, res) @trusted => request_handler(req, res));
  125. }
  126. /// ditto
  127. HTTPListener listenHTTP(Settings)(Settings settings, void function(HTTPServerRequest, HTTPServerResponse) @system request_handler)
  128. @system
  129. if (is(Settings == string) || is(Settings == HTTPServerSettings)) {
  130. return listenHTTP(settings, (req, res) @trusted => request_handler(req, res));
  131. }
  132. /// ditto
  133. HTTPListener listenHTTP(Settings)(Settings settings, void delegate(scope HTTPServerRequest, scope HTTPServerResponse) @system request_handler)
  134. @system
  135. if (is(Settings == string) || is(Settings == HTTPServerSettings)) {
  136. return listenHTTP(settings, (scope req, scope res) @trusted => request_handler(req, res));
  137. }
  138. /// ditto
  139. HTTPListener listenHTTP(Settings)(Settings settings, void function(scope HTTPServerRequest, scope HTTPServerResponse) @system request_handler)
  140. @system
  141. if (is(Settings == string) || is(Settings == HTTPServerSettings)) {
  142. return listenHTTP(settings, (scope req, scope res) @trusted => request_handler(req, res));
  143. }
  144. unittest
  145. {
  146. void test()
  147. {
  148. static void testSafeFunction(HTTPServerRequest req, HTTPServerResponse res) @safe {}
  149. listenHTTP("0.0.0.0:8080", &testSafeFunction);
  150. listenHTTP(":8080", new class HTTPServerRequestHandler {
  151. void handleRequest(HTTPServerRequest req, HTTPServerResponse res) @safe {}
  152. });
  153. listenHTTP(":8080", (req, res) {});
  154. static void testSafeFunctionS(scope HTTPServerRequest req, scope HTTPServerResponse res) @safe {}
  155. listenHTTP(":8080", &testSafeFunctionS);
  156. void testSafeDelegateS(scope HTTPServerRequest req, scope HTTPServerResponse res) @safe {}
  157. listenHTTP(":8080", &testSafeDelegateS);
  158. listenHTTP(":8080", new class HTTPServerRequestHandler {
  159. void handleRequest(scope HTTPServerRequest req, scope HTTPServerResponse res) @safe {}
  160. });
  161. listenHTTP(":8080", (scope req, scope res) {});
  162. }
  163. }
  164. /** Treats an existing connection as an HTTP connection and processes incoming
  165. requests.
  166. After all requests have been processed, the connection will be closed and
  167. the function returns to the caller.
  168. Params:
  169. connection = The stream to treat as an incoming HTTP client connection.
  170. context = Information about the incoming listener and available
  171. virtual hosts
  172. */
  173. void handleHTTPConnection(TCPConnection connection, HTTPServerContext context)
  174. @safe {
  175. import vibe.http.internal.http1.server : handleHTTP1Connection;
  176. import vibe.http.internal.http2.server : handleHTTP2Connection;
  177. import vibe.http.internal.http2.settings : HTTP2ServerContext, HTTP2Settings;
  178. static if (HaveNoTLS) {
  179. alias TLSStreamType = Stream;
  180. } else {
  181. alias TLSStreamType = ReturnType!(createTLSStreamFL!(InterfaceProxy!Stream));
  182. }
  183. InterfaceProxy!Stream http_stream;
  184. http_stream = connection;
  185. scope (exit) connection.close();
  186. // check wether the client's address is banned
  187. foreach (ref virtual_host; context.m_virtualHosts)
  188. if ((virtual_host.settings.rejectConnectionPredicate !is null) &&
  189. virtual_host.settings.rejectConnectionPredicate(connection.remoteAddress()))
  190. return;
  191. // Set NODELAY to true, to avoid delays caused by sending the response
  192. // header and body in separate chunks. Note that to avoid other performance
  193. // issues (caused by tiny packets), this requires using an output buffer in
  194. // the event driver, which is the case at least for the legacy libevent
  195. // based driver.
  196. connection.tcpNoDelay = true;
  197. TLSStreamType tls_stream;
  198. if (!connection.waitForData(10.seconds())) {
  199. logDebug("Client didn't send the initial request in a timely manner. Closing connection.");
  200. return;
  201. }
  202. // If this is a HTTPS server, initiate TLS
  203. if (context.tlsContext) {
  204. static if (HaveNoTLS) assert(false, "No TLS support compiled in.");
  205. else {
  206. logDebug("Accept TLS connection: %s", context.tlsContext.kind);
  207. // TODO: reverse DNS lookup for peer_name of the incoming connection for TLS client certificate verification purposes
  208. tls_stream = createTLSStreamFL(http_stream, context.tlsContext, TLSStreamState.accepting, null, connection.remoteAddress);
  209. Nullable!string proto = tls_stream.alpn;
  210. if(!proto.isNull && proto == "h2" && (context.m_virtualHosts[0].settings.options & HTTPServerOption.enableHTTP2)) {
  211. logTrace("Using HTTP/2 as requested per ALPN");
  212. HTTP2Settings settings;
  213. auto h2context = new HTTP2ServerContext(context, settings);
  214. handleHTTP2Connection(tls_stream, connection, h2context);
  215. return;
  216. }
  217. http_stream = tls_stream;
  218. }
  219. }
  220. handleHTTP1Connection(connection, tls_stream, http_stream, context);
  221. logTrace("Done handling connection.");
  222. }
  223. /**
  224. Provides a HTTP request handler that responds with a static Diet template.
  225. */
  226. @property HTTPServerRequestDelegateS staticTemplate(string template_file)()
  227. {
  228. return (scope HTTPServerRequest req, scope HTTPServerResponse res){
  229. res.render!(template_file, req);
  230. };
  231. }
  232. /**
  233. Provides a HTTP request handler that responds with a static redirection to the specified URL.
  234. Params:
  235. url = The URL to redirect to
  236. status = Redirection status to use $(LPAREN)by default this is $(D HTTPStatus.found)$(RPAREN).
  237. Returns:
  238. Returns a $(D HTTPServerRequestDelegate) that performs the redirect
  239. */
  240. HTTPServerRequestDelegate staticRedirect(string url, HTTPStatus status = HTTPStatus.found)
  241. @safe {
  242. return (HTTPServerRequest req, HTTPServerResponse res){
  243. res.redirect(url, status);
  244. };
  245. }
  246. /// ditto
  247. HTTPServerRequestDelegate staticRedirect(URL url, HTTPStatus status = HTTPStatus.found)
  248. @safe {
  249. return (HTTPServerRequest req, HTTPServerResponse res){
  250. res.redirect(url, status);
  251. };
  252. }
  253. ///
  254. unittest {
  255. import vibe.http.router;
  256. void test()
  257. {
  258. auto router = new URLRouter;
  259. router.get("/old_url", staticRedirect("http://example.org/new_url", HTTPStatus.movedPermanently));
  260. listenHTTP(new HTTPServerSettings, router);
  261. }
  262. }
  263. /**
  264. Sets a VibeDist host to register with.
  265. */
  266. void setVibeDistHost(string host, ushort port)
  267. @safe {
  268. s_distHost = host;
  269. s_distPort = port;
  270. }
  271. /**
  272. Renders the given Diet template and makes all ALIASES available to the template.
  273. You can call this function as a pseudo-member of `HTTPServerResponse` using
  274. D's uniform function call syntax.
  275. See_also: `diet.html.compileHTMLDietFile`
  276. Examples:
  277. ---
  278. string title = "Hello, World!";
  279. int pageNumber = 1;
  280. res.render!("mytemplate.dt", title, pageNumber);
  281. ---
  282. */
  283. @property void render(string template_file, ALIASES...)(HTTPServerResponse res)
  284. {
  285. res.contentType = "text/html; charset=UTF-8";
  286. version (VibeUseOldDiet)
  287. pragma(msg, "VibeUseOldDiet is not supported anymore. Please undefine in the package recipe.");
  288. import vibe.stream.wrapper : streamOutputRange;
  289. import diet.html : compileHTMLDietFile;
  290. auto output = streamOutputRange!1024(res.bodyWriter);
  291. compileHTMLDietFile!(template_file, ALIASES, DefaultDietFilters)(output);
  292. }
  293. /**
  294. Provides the default `css`, `javascript`, `markdown` and `htmlescape` filters
  295. */
  296. @dietTraits
  297. struct DefaultDietFilters {
  298. import diet.html : HTMLOutputStyle;
  299. import diet.traits : SafeFilterCallback;
  300. import std.string : splitLines;
  301. version (VibeOutputCompactHTML) enum HTMLOutputStyle htmlOutputStyle = HTMLOutputStyle.compact;
  302. else enum HTMLOutputStyle htmlOutputStyle = HTMLOutputStyle.pretty;
  303. static string filterCss(I)(I text, size_t indent = 0)
  304. {
  305. auto lines = splitLines(text);
  306. string indent_string = "\n";
  307. while (indent-- > 0) indent_string ~= '\t';
  308. string ret = indent_string~"<style><!--";
  309. indent_string = indent_string ~ '\t';
  310. foreach (ln; lines) ret ~= indent_string ~ ln;
  311. indent_string = indent_string[0 .. $-1];
  312. ret ~= indent_string ~ "--></style>";
  313. return ret;
  314. }
  315. static string filterJavascript(I)(I text, size_t indent = 0)
  316. {
  317. auto lines = splitLines(text);
  318. string indent_string = "\n";
  319. while (indent-- > 0) indent_string ~= '\t';
  320. string ret = indent_string~"<script>";
  321. ret ~= indent_string~'\t' ~ "//<![CDATA[";
  322. foreach (ln; lines) ret ~= indent_string ~ '\t' ~ ln;
  323. ret ~= indent_string ~ '\t' ~ "//]]>" ~ indent_string ~ "</script>";
  324. return ret;
  325. }
  326. static string filterMarkdown(I)(I text)
  327. {
  328. import vibe.textfilter.markdown : markdown = filterMarkdown;
  329. // TODO: indent
  330. return markdown(text);
  331. }
  332. static string filterHtmlescape(I)(I text)
  333. {
  334. import vibe.textfilter.html : htmlEscape;
  335. // TODO: indent
  336. return htmlEscape(text);
  337. }
  338. static this()
  339. {
  340. filters["css"] = (in input, scope output) { output(filterCss(input)); };
  341. filters["javascript"] = (in input, scope output) { output(filterJavascript(input)); };
  342. filters["markdown"] = (in input, scope output) { output(filterMarkdown(() @trusted { return cast(string)input; } ())); };
  343. filters["htmlescape"] = (in input, scope output) { output(filterHtmlescape(input)); };
  344. }
  345. static SafeFilterCallback[string] filters;
  346. }
  347. unittest {
  348. static string compile(string diet)() {
  349. import std.array : appender;
  350. import std.string : strip;
  351. import diet.html : compileHTMLDietString;
  352. auto dst = appender!string;
  353. dst.compileHTMLDietString!(diet, DefaultDietFilters);
  354. return strip(cast(string)(dst.data));
  355. }
  356. assert(compile!":css .test" == "<style><!--\n\t.test\n--></style>");
  357. assert(compile!":javascript test();" == "<script>\n\t//<![CDATA[\n\ttest();\n\t//]]>\n</script>");
  358. assert(compile!":markdown **test**" == "<p><strong>test</strong>\n</p>");
  359. assert(compile!":htmlescape <test>" == "&lt;test&gt;");
  360. assert(compile!":css !{\".test\"}" == "<style><!--\n\t.test\n--></style>");
  361. assert(compile!":javascript !{\"test();\"}" == "<script>\n\t//<![CDATA[\n\ttest();\n\t//]]>\n</script>");
  362. assert(compile!":markdown !{\"**test**\"}" == "<p><strong>test</strong>\n</p>");
  363. assert(compile!":htmlescape !{\"<test>\"}" == "&lt;test&gt;");
  364. assert(compile!":javascript\n\ttest();" == "<script>\n\t//<![CDATA[\n\ttest();\n\t//]]>\n</script>");
  365. }
  366. /**
  367. Creates a HTTPServerRequest suitable for writing unit tests.
  368. */
  369. HTTPServerRequest createTestHTTPServerRequest(URL url, HTTPMethod method = HTTPMethod.GET, InputStream data = null)
  370. @safe {
  371. InetHeaderMap headers;
  372. return createTestHTTPServerRequest(url, method, headers, data);
  373. }
  374. /// ditto
  375. HTTPServerRequest createTestHTTPServerRequest(URL url, HTTPMethod method, InetHeaderMap headers, InputStream data = null)
  376. @safe {
  377. auto tls = url.schema == "https";
  378. auto ret = new HTTPServerRequest(Clock.currTime(UTC()), url.port ? url.port : tls ? 443 : 80);
  379. ret.m_settings = new HTTPServerSettings;
  380. ret.requestPath = url.path;
  381. ret.queryString = url.queryString;
  382. ret.username = url.username;
  383. ret.password = url.password;
  384. ret.requestURI = url.localURI;
  385. ret.method = method;
  386. ret.tls = tls;
  387. ret.headers = headers;
  388. ret.bodyReader = data;
  389. return ret;
  390. }
  391. /**
  392. Creates a HTTPServerResponse suitable for writing unit tests.
  393. Params:
  394. data_sink = Optional output stream that captures the data that gets
  395. written to the response
  396. session_store = Optional session store to use when sessions are involved
  397. data_mode = If set to `TestHTTPResponseMode.bodyOnly`, only the body
  398. contents get written to `data_sink`. Otherwise the raw response
  399. including the HTTP header is written.
  400. */
  401. HTTPServerResponse createTestHTTPServerResponse(OutputStream data_sink = null,
  402. SessionStore session_store = null,
  403. TestHTTPResponseMode data_mode = TestHTTPResponseMode.plain)
  404. @safe {
  405. import vibe.stream.wrapper : createProxyStream;
  406. import vibe.http.internal.http1.server : HTTP1ServerExchange;
  407. HTTPServerSettings settings;
  408. if (session_store) {
  409. settings = new HTTPServerSettings;
  410. settings.sessionStore = session_store;
  411. }
  412. final class TestExchange : HTTP1ServerExchange {
  413. this(StreamProxy conn, OutputStream body_writer)
  414. {
  415. super(conn, ConnectionStreamProxy.init);
  416. m_bodyWriter = body_writer;
  417. }
  418. }
  419. StreamProxy outstr;
  420. if (data_sink && data_mode == TestHTTPResponseMode.plain)
  421. outstr = createProxyStream(Stream.init, data_sink);
  422. else outstr = createProxyStream(Stream.init, nullSink);
  423. auto exchange = new TestExchange(outstr, data_sink && data_mode == TestHTTPResponseMode.bodyOnly ? data_sink : null);
  424. auto ret = new HTTPServerResponse(exchange, settings, () @trusted { return vibeThreadAllocator(); } ());
  425. return ret;
  426. }
  427. /**************************************************************************************************/
  428. /* Public types */
  429. /**************************************************************************************************/
  430. /// Delegate based request handler
  431. alias HTTPServerRequestDelegate = void delegate(HTTPServerRequest req, HTTPServerResponse res) @safe;
  432. /// Static function based request handler
  433. alias HTTPServerRequestFunction = void function(HTTPServerRequest req, HTTPServerResponse res) @safe;
  434. /// Interface for class based request handlers
  435. interface HTTPServerRequestHandler {
  436. /// Handles incoming HTTP requests
  437. void handleRequest(HTTPServerRequest req, HTTPServerResponse res) @safe ;
  438. }
  439. /// Delegate based request handler with scoped parameters
  440. alias HTTPServerRequestDelegateS = void delegate(scope HTTPServerRequest req, scope HTTPServerResponse res) @safe;
  441. /// Static function based request handler with scoped parameters
  442. alias HTTPServerRequestFunctionS = void function(scope HTTPServerRequest req, scope HTTPServerResponse res) @safe;
  443. /// Interface for class based request handlers with scoped parameters
  444. interface HTTPServerRequestHandlerS {
  445. /// Handles incoming HTTP requests
  446. void handleRequest(scope HTTPServerRequest req, scope HTTPServerResponse res) @safe;
  447. }
  448. unittest {
  449. static assert(is(HTTPServerRequestDelegateS : HTTPServerRequestDelegate));
  450. static assert(is(HTTPServerRequestFunctionS : HTTPServerRequestFunction));
  451. }
  452. /// Aggregates all information about an HTTP error status.
  453. final class HTTPServerErrorInfo {
  454. /// The HTTP status code
  455. int code;
  456. /// The error message
  457. string message;
  458. /// Extended error message with debug information such as a stack trace
  459. string debugMessage;
  460. /// The error exception, if any
  461. Throwable exception;
  462. }
  463. /// Delegate type used for user defined error page generator callbacks.
  464. alias HTTPServerErrorPageHandler = void delegate(HTTPServerRequest req, HTTPServerResponse res, HTTPServerErrorInfo error) @safe;
  465. enum TestHTTPResponseMode {
  466. plain,
  467. bodyOnly
  468. }
  469. /**
  470. Specifies optional features of the HTTP server.
  471. Disabling unneeded features can speed up the server or reduce its memory usage.
  472. Note that the options `parseFormBody`, `parseJsonBody` and `parseMultiPartBody`
  473. will also drain the `HTTPServerRequest.bodyReader` stream whenever a request
  474. body with form or JSON data is encountered.
  475. */
  476. enum HTTPServerOption {
  477. none = 0,
  478. /** Enables stack traces (`HTTPServerErrorInfo.debugMessage`).
  479. Note that generating the stack traces are generally a costly
  480. operation that should usually be avoided in production
  481. environments. It can also reveal internal information about
  482. the application, such as function addresses, which can
  483. help an attacker to abuse possible security holes.
  484. */
  485. errorStackTraces = 1<<7,
  486. /// Enable port reuse in `listenTCP()`
  487. reusePort = 1<<8,
  488. /// Enable address reuse in `listenTCP()`
  489. reuseAddress = 1<<10,
  490. /// Enable *experimental* HTTP/2 support
  491. enableHTTP2 = 1<<11,
  492. /** The default set of options.
  493. Includes all parsing options, as well as the `errorStackTraces`
  494. option if the code is compiled in debug mode.
  495. */
  496. defaults = () { auto ret = reuseAddress; debug ret |= errorStackTraces; return ret; } (),
  497. }
  498. /**
  499. Contains all settings for configuring a basic HTTP server.
  500. The defaults are sufficient for most normal uses.
  501. */
  502. final class HTTPServerSettings {
  503. /** The port on which the HTTP server is listening.
  504. The default value is 80. If you are running a TLS enabled server you may want to set this
  505. to 443 instead.
  506. Using a value of `0` instructs the server to use any available port on
  507. the given `bindAddresses` the actual addresses and ports can then be
  508. queried with `TCPListener.bindAddresses`.
  509. */
  510. ushort port = 80;
  511. /** The interfaces on which the HTTP server is listening.
  512. By default, the server will listen on all IPv4 and IPv6 interfaces.
  513. */
  514. string[] bindAddresses = ["::", "0.0.0.0"];
  515. /** Determines the server host name.
  516. If multiple servers are listening on the same port, the host name will determine which one
  517. gets a request.
  518. */
  519. string hostName;
  520. /** Provides a way to reject incoming connections as early as possible.
  521. Allows to ban and unban network addresses and reduce the impact of DOS
  522. attacks.
  523. If the callback returns `true` for a specific `NetworkAddress`,
  524. then all incoming requests from that address will be rejected.
  525. */
  526. RejectConnectionPredicate rejectConnectionPredicate;
  527. /** Configures optional features of the HTTP server
  528. Disabling unneeded features can improve performance or reduce the server
  529. load in case of invalid or unwanted requests (DoS). By default,
  530. HTTPServerOption.defaults is used.
  531. */
  532. HTTPServerOption options = HTTPServerOption.defaults;
  533. /** Time of a request after which the connection is closed with an error; not supported yet
  534. The default limit of 0 means that the request time is not limited.
  535. */
  536. Duration maxRequestTime = 0.seconds;
  537. /** Maximum time between two request on a keep-alive connection
  538. The default value is 10 seconds.
  539. */
  540. Duration keepAliveTimeout = 10.seconds;
  541. /// Maximum number of transferred bytes per request after which the connection is closed with
  542. /// an error
  543. ulong maxRequestSize = 2097152;
  544. /// Maximum number of transferred bytes for the request header. This includes the request line
  545. /// the url and all headers.
  546. ulong maxRequestHeaderSize = 8192;
  547. /// Maximum number of bytes in a single line in the request header.
  548. size_t maxRequestHeaderLineSize = 4096;
  549. /// Sets a custom handler for displaying error pages for HTTP errors
  550. @property HTTPServerErrorPageHandler errorPageHandler() @safe { return errorPageHandler_; }
  551. /// ditto
  552. @property void errorPageHandler(HTTPServerErrorPageHandler del) @safe { errorPageHandler_ = del; }
  553. /// Scheduled for deprecation - use a `@safe` callback instead.
  554. @property void errorPageHandler(void delegate(HTTPServerRequest, HTTPServerResponse, HTTPServerErrorInfo) @system del)
  555. @system {
  556. this.errorPageHandler = (req, res, err) @trusted { del(req, res, err); };
  557. }
  558. package HTTPServerErrorPageHandler errorPageHandler_ = null;
  559. /// If set, a HTTPS server will be started instead of plain HTTP.
  560. TLSContext tlsContext;
  561. /// Session management is enabled if a session store instance is provided
  562. SessionStore sessionStore;
  563. string sessionIdCookie = "vibe.session_id";
  564. /// Session options to use when initializing a new session.
  565. SessionOption sessionOptions = SessionOption.httpOnly;
  566. ///
  567. import vibe.core.core : vibeVersionString;
  568. string serverString = "vibe.d/" ~ vibeVersionString;
  569. /** Specifies the format used for the access log.
  570. The log format is given using the Apache server syntax. By default NCSA combined is used.
  571. ---
  572. "%h - %u %t \"%r\" %s %b \"%{Referer}i\" \"%{User-Agent}i\""
  573. ---
  574. */
  575. string accessLogFormat = "%h - %u %t \"%r\" %s %b \"%{Referer}i\" \"%{User-Agent}i\"";
  576. /// Spefifies the name of a file to which access log messages are appended.
  577. string accessLogFile = "";
  578. /// If set, access log entries will be output to the console.
  579. bool accessLogToConsole = false;
  580. /** Specifies a custom access logger instance.
  581. */
  582. HTTPLogger accessLogger;
  583. /// Returns a duplicate of the settings object.
  584. @property HTTPServerSettings dup()
  585. @safe {
  586. auto ret = new HTTPServerSettings;
  587. foreach (mem; __traits(allMembers, HTTPServerSettings)) {
  588. static if (mem == "sslContext") {}
  589. else static if (mem == "bindAddresses") ret.bindAddresses = bindAddresses.dup;
  590. else static if (__traits(compiles, __traits(getMember, ret, mem) = __traits(getMember, this, mem)))
  591. __traits(getMember, ret, mem) = __traits(getMember, this, mem);
  592. }
  593. return ret;
  594. }
  595. /// Disable support for VibeDist and instead start listening immediately.
  596. bool disableDistHost = false;
  597. /** Responds to "Accept-Encoding" by using compression if possible.
  598. Compression can also be manually enabled by setting the
  599. "Content-Encoding" header of the HTTP response appropriately before
  600. sending the response body.
  601. This setting is disabled by default. Also note that there are still some
  602. known issues with the GZIP compression code.
  603. */
  604. bool useCompressionIfPossible = false;
  605. /** Interval between WebSocket ping frames.
  606. The default value is 60 seconds; set to Duration.zero to disable pings.
  607. */
  608. Duration webSocketPingInterval = 60.seconds;
  609. /** Constructs a new settings object with default values.
  610. */
  611. this() @safe {}
  612. /** Constructs a new settings object with a custom bind interface and/or port.
  613. The syntax of `bind_string` is `[<IP address>][:<port>]`, where either of
  614. the two parts can be left off. IPv6 addresses must be enclosed in square
  615. brackets, as they would within a URL.
  616. Throws:
  617. An exception is thrown if `bind_string` is malformed.
  618. */
  619. this(string bind_string)
  620. @safe {
  621. this();
  622. if (bind_string.startsWith('[')) {
  623. auto idx = bind_string.indexOf(']');
  624. enforce(idx > 0, "Missing closing bracket for IPv6 address.");
  625. bindAddresses = [bind_string[1 .. idx]];
  626. bind_string = bind_string[idx+1 .. $];
  627. enforce(bind_string.length == 0 || bind_string.startsWith(':'),
  628. "Only a colon may follow the IPv6 address.");
  629. }
  630. auto idx = bind_string.indexOf(':');
  631. if (idx < 0) {
  632. if (bind_string.length > 0) bindAddresses = [bind_string];
  633. } else {
  634. if (idx > 0) bindAddresses = [bind_string[0 .. idx]];
  635. port = bind_string[idx+1 .. $].to!ushort;
  636. }
  637. }
  638. ///
  639. unittest {
  640. auto s = new HTTPServerSettings(":8080");
  641. assert(s.bindAddresses == ["::", "0.0.0.0"]); // default bind addresses
  642. assert(s.port == 8080);
  643. s = new HTTPServerSettings("123.123.123.123");
  644. assert(s.bindAddresses == ["123.123.123.123"]);
  645. assert(s.port == 80);
  646. s = new HTTPServerSettings("[::1]:443");
  647. assert(s.bindAddresses == ["::1"]);
  648. assert(s.port == 443);
  649. }
  650. }
  651. /// Callback type used to determine whether to reject incoming connections
  652. alias RejectConnectionPredicate = bool delegate (in NetworkAddress) @safe nothrow;
  653. /**
  654. Options altering how sessions are created.
  655. Multiple values can be or'ed together.
  656. See_Also: HTTPServerResponse.startSession
  657. */
  658. enum SessionOption {
  659. /// No options.
  660. none = 0,
  661. /** Instructs the browser to disallow accessing the session ID from JavaScript.
  662. See_Also: Cookie.httpOnly
  663. */
  664. httpOnly = 1<<0,
  665. /** Instructs the browser to disallow sending the session ID over
  666. unencrypted connections.
  667. By default, the type of the connection on which the session is started
  668. will be used to determine if secure or noSecure is used.
  669. See_Also: noSecure, Cookie.secure
  670. */
  671. secure = 1<<1,
  672. /** Instructs the browser to allow sending the session ID over unencrypted
  673. connections.
  674. By default, the type of the connection on which the session is started
  675. will be used to determine if secure or noSecure is used.
  676. See_Also: secure, Cookie.secure
  677. */
  678. noSecure = 1<<2,
  679. /**
  680. Instructs the browser to allow sending this cookie along with cross-site requests.
  681. By default, the protection is `strict`. This flag allows to set it to `lax`.
  682. The strict value will prevent the cookie from being sent by the browser
  683. to the target site in all cross-site browsing context,
  684. even when following a regular link.
  685. */
  686. noSameSiteStrict = 1<<3,
  687. }
  688. /**
  689. Represents a HTTP request as received by the server side.
  690. */
  691. final class HTTPServerRequest : HTTPRequest {
  692. import std.variant : Variant;
  693. import vibe.container.dictionarylist : DictionaryList;
  694. package {
  695. SysTime m_timeCreated;
  696. HTTPServerSettings m_settings;
  697. ushort m_port;
  698. string m_peer;
  699. // lazily parsed request components
  700. Nullable!string m_path;
  701. Nullable!CookieValueMap m_cookies;
  702. Nullable!FormFields m_query;
  703. Nullable!Json m_json;
  704. Nullable!FormFields m_form;
  705. FilePartFormFields m_files;
  706. }
  707. /// The IP address of the client
  708. NetworkAddress clientAddress;
  709. /// Determines if the request should be logged to the access log file.
  710. bool noLog;
  711. /// Determines if the request was issued over an TLS encrypted channel.
  712. bool tls;
  713. /** Information about the TLS certificate provided by the client.
  714. Remarks: This field is only set if `tls` is true, and the peer
  715. presented a client certificate.
  716. */
  717. TLSCertificateInformation clientCertificate;
  718. /** The path part of the requested URI.
  719. */
  720. InetPath requestPath;
  721. /** The user name part of the URL, if present.
  722. */
  723. string username;
  724. /** The _password part of the URL, if present.
  725. */
  726. string password;
  727. /** The _query string part of the URL.
  728. */
  729. string queryString;
  730. /** A map of general parameters for the request.
  731. This map is supposed to be used by middleware functionality to store
  732. information for later stages. For example vibe.http.router.URLRouter uses this map
  733. to store the value of any named placeholders.
  734. */
  735. DictionaryList!(string, true, 8) params;
  736. /** A map of context items for the request.
  737. This is especially useful for passing application specific data down
  738. the chain of processors along with the request itself.
  739. For example, a generic route may be defined to check user login status,
  740. if the user is logged in, add a reference to user specific data to the
  741. context.
  742. This is implemented with `std.variant.Variant` to allow any type of data.
  743. */
  744. DictionaryList!(Variant, true, 2) context;
  745. /** Supplies the request body as a stream.
  746. Note that when certain server options are set (such as
  747. HTTPServerOption.parseJsonBody) and a matching request was sent,
  748. the returned stream will be empty. If needed, remove those
  749. options and do your own processing of the body when launching
  750. the server. HTTPServerOption has a list of all options that affect
  751. the request body.
  752. */
  753. InputStream bodyReader;
  754. /** The current Session object.
  755. This field is set if HTTPServerResponse.startSession() has been called
  756. on a previous response and if the client has sent back the matching
  757. cookie.
  758. Remarks: Requires the HTTPServerOption.parseCookies option.
  759. */
  760. Session session;
  761. this(SysTime time, ushort port)
  762. @safe scope {
  763. m_timeCreated = time.toUTC();
  764. m_port = port;
  765. }
  766. /// The IP address of the client in string form
  767. @property string peer()
  768. @safe nothrow scope {
  769. if (!m_peer) {
  770. version (Have_vibe_core) {} else scope (failure) assert(false);
  771. // store the IP address (IPv4 addresses forwarded over IPv6 are stored in IPv4 format)
  772. auto peer_address_string = this.clientAddress.toString();
  773. if (peer_address_string.startsWith("::ffff:") && peer_address_string[7 .. $].indexOf(':') < 0)
  774. m_peer = peer_address_string[7 .. $];
  775. else m_peer = peer_address_string;
  776. }
  777. return m_peer;
  778. }
  779. /** The _path part of the URL.
  780. Note that this function contains the decoded version of the
  781. requested path, which can yield incorrect results if the path
  782. contains URL encoded path separators. Use `requestPath` instead to
  783. get an encoding-aware representation.
  784. */
  785. deprecated("Use .requestPath instead")
  786. string path()
  787. @safe scope {
  788. if (m_path.isNull)
  789. m_path = urlDecode(requestPath.toString);
  790. return m_path.get;
  791. }
  792. /** Contains the list of cookies that are stored on the client.
  793. Note that the a single cookie name may occur multiple times if multiple
  794. cookies have that name but different paths or domains that all match
  795. the request URI. By default, the first cookie will be returned, which is
  796. the or one of the cookies with the closest path match.
  797. */
  798. @property ref CookieValueMap cookies()
  799. @safe return {
  800. if (m_cookies.isNull) {
  801. m_cookies = CookieValueMap.init;
  802. if (auto pv = "cookie" in headers)
  803. parseCookies(*pv, m_cookies.get);
  804. }
  805. return m_cookies.get;
  806. }
  807. /** Contains all _form fields supplied using the _query string.
  808. The fields are stored in the same order as they are received.
  809. */
  810. @property ref FormFields query()
  811. @safe return {
  812. if (m_query.isNull) {
  813. m_query = FormFields.init;
  814. parseURLEncodedForm(queryString, m_query.get);
  815. }
  816. return m_query.get;
  817. }
  818. /** Contains the parsed Json for a JSON request.
  819. A JSON request must have the Content-Type "application/json" or "application/vnd.api+json".
  820. */
  821. @property ref Json json()
  822. @safe return {
  823. if (m_json.isNull) {
  824. auto splitter = contentType.splitter(';');
  825. auto ctype = splitter.empty ? "" : splitter.front;
  826. if (icmp2(ctype, "application/json") == 0 || icmp2(ctype, "application/vnd.api+json") == 0) {
  827. auto bodyStr = bodyReader.readAllUTF8();
  828. if (!bodyStr.empty) m_json = parseJson(bodyStr);
  829. else m_json = Json.undefined;
  830. } else {
  831. m_json = Json.undefined;
  832. }
  833. }
  834. return m_json.get;
  835. }
  836. /// Get the json body when there is no content-type header
  837. unittest {
  838. assert(createTestHTTPServerRequest(URL("http://localhost/")).json.type == Json.Type.undefined);
  839. }
  840. /** Contains the parsed parameters of a HTML POST _form request.
  841. The fields are stored in the same order as they are received.
  842. Remarks:
  843. A form request must either have the Content-Type
  844. "application/x-www-form-urlencoded" or "multipart/form-data".
  845. */
  846. @property ref FormFields form()
  847. @safe return {
  848. if (m_form.isNull)
  849. parseFormAndFiles();
  850. return m_form.get;
  851. }
  852. /** Contains information about any uploaded file for a HTML _form request.
  853. */
  854. @property ref FilePartFormFields files()
  855. @safe return {
  856. // m_form and m_files are parsed in one step
  857. if (m_form.isNull) {
  858. parseFormAndFiles();
  859. assert(!m_form.isNull);
  860. }
  861. return m_files;
  862. }
  863. /** Time when this request started processing.
  864. */
  865. @property SysTime timeCreated() const @safe scope { return m_timeCreated; }
  866. /** The full URL that corresponds to this request.
  867. The host URL includes the protocol, host and optionally the user
  868. and password that was used for this request. This field is useful to
  869. construct self referencing URLs.
  870. Note that the port is currently not set, so that this only works if
  871. the standard port is used.
  872. */
  873. @property URL fullURL()
  874. const @safe scope {
  875. URL url;
  876. auto xfh = this.headers.get("X-Forwarded-Host");
  877. auto xfp = this.headers.get("X-Forwarded-Port");
  878. auto xfpr = this.headers.get("X-Forwarded-Proto");
  879. // Set URL host segment.
  880. if (xfh.length) {
  881. url.host = xfh;
  882. } else if (!this.host.empty) {
  883. url.host = this.host;
  884. } else if (!m_settings.hostName.empty) {
  885. url.host = m_settings.hostName;
  886. } else {
  887. url.host = m_settings.bindAddresses[0];
  888. }
  889. // Set URL schema segment.
  890. if (xfpr.length) {
  891. url.schema = xfpr;
  892. } else if (this.tls) {
  893. url.schema = "https";
  894. } else {
  895. url.schema = "http";
  896. }
  897. // Set URL port segment.
  898. if (xfp.length) {
  899. try {
  900. url.port = xfp.to!ushort;
  901. } catch (ConvException) {
  902. // TODO : Consider responding with a 400/etc. error from here.
  903. logWarn("X-Forwarded-Port header was not valid port (%s)", xfp);
  904. }
  905. } else if (!xfh) {
  906. if (url.schema == "https") {
  907. if (m_port != 443U) url.port = m_port;
  908. } else {
  909. if (m_port != 80U) url.port = m_port;
  910. }
  911. }
  912. if (url.host.startsWith('[')) { // handle IPv6 address
  913. auto idx = url.host.indexOf(']');
  914. if (idx >= 0 && idx+1 < url.host.length && url.host[idx+1] == ':')
  915. url.host = url.host[1 .. idx];
  916. } else { // handle normal host names or IPv4 address
  917. auto idx = url.host.indexOf(':');
  918. if (idx >= 0) url.host = url.host[0 .. idx];
  919. }
  920. url.username = this.username;
  921. url.password = this.password;
  922. url.localURI = this.requestURI;
  923. return url;
  924. }
  925. /** The relative path to the root folder.
  926. Using this function instead of absolute URLs for embedded links can be
  927. useful to avoid dead link when the site is piped through a
  928. reverse-proxy.
  929. The returned string always ends with a slash.
  930. */
  931. @property string rootDir()
  932. const @safe scope {
  933. import std.algorithm.searching : count;
  934. import std.range : empty;
  935. auto depth = requestPath.bySegment.count!(s => !s.name.empty);
  936. if (depth > 0 && !requestPath.endsWithSlash) depth--;
  937. return depth == 0 ? "./" : replicate("../", depth);
  938. }
  939. unittest {
  940. assert(createTestHTTPServerRequest(URL("http://localhost/")).rootDir == "./");
  941. assert(createTestHTTPServerRequest(URL("http://localhost/foo")).rootDir == "./");
  942. assert(createTestHTTPServerRequest(URL("http://localhost/foo/")).rootDir == "../");
  943. assert(createTestHTTPServerRequest(URL("http://localhost/foo/bar")).rootDir == "../");
  944. assert(createTestHTTPServerRequest(URL("http://localhost")).rootDir == "./");
  945. }
  946. /** The settings of the server serving this request.
  947. */
  948. package @property const(HTTPServerSettings) serverSettings()
  949. const @safe scope {
  950. return m_settings;
  951. }
  952. private void parseFormAndFiles()
  953. @safe scope {
  954. m_form = FormFields.init;
  955. parseFormData(m_form.get, m_files, headers.get("Content-Type", ""), bodyReader, m_settings.maxRequestHeaderLineSize);
  956. }
  957. }
  958. /**
  959. Represents a HTTP response as sent from the server side.
  960. */
  961. final class HTTPServerResponse : HTTPResponse {
  962. alias Allocator = typeof(vibeThreadAllocator());
  963. package {
  964. HTTPServerExchange m_exchange;
  965. Allocator m_requestAlloc;
  966. HTTPServerSettings m_settings;
  967. Session m_session;
  968. bool m_tls;
  969. SysTime m_timeFinalized;
  970. }
  971. static if (!is(Stream == StreamProxy)) {
  972. deprecated("Use the constructor taking a HTTPServerExchange argument instead")
  973. this(Stream conn, ConnectionStream raw_connection, HTTPServerSettings settings, Allocator req_alloc)
  974. @safe scope {
  975. this(StreamProxy(conn), InterfaceProxy!ConnectionStream(raw_connection), settings, req_alloc);
  976. }
  977. }
  978. deprecated("Use the constructor taking a HTTPServerExchange argument instead")
  979. this(StreamProxy conn, ConnectionStreamProxy raw_connection, HTTPServerSettings settings, Allocator req_alloc)
  980. @safe scope {
  981. import vibe.http.internal.http1.server : HTTP1ServerExchange;
  982. this(new HTTP1ServerExchange(conn, raw_connection), settings, req_alloc);
  983. }
  984. this(HTTPServerExchange exchange, HTTPServerSettings settings, Allocator req_alloc)
  985. @safe {
  986. m_exchange = exchange;
  987. m_settings = settings;
  988. m_requestAlloc = req_alloc;
  989. }
  990. /** Sends a redirect request to the client.
  991. Params:
  992. url = The URL to redirect to
  993. status = The HTTP redirect status (3xx) to send - by default this is $(D HTTPStatus.found)
  994. */
  995. void redirect(string url, int status = HTTPStatus.found)
  996. @safe scope {
  997. // Disallow any characters that may influence the header parsing
  998. enforce(!url.representation.canFind!(ch => ch < 0x20),
  999. "Control character in redirection URL.");
  1000. statusCode = status;
  1001. headers["Location"] = url;
  1002. writeBody("redirecting...");
  1003. }
  1004. /// ditto
  1005. void redirect(URL url, int status = HTTPStatus.found)
  1006. @safe scope {
  1007. redirect(url.toString(), status);
  1008. }
  1009. ///
  1010. @safe unittest {
  1011. import vibe.http.router;
  1012. void request_handler(HTTPServerRequest req, HTTPServerResponse res)
  1013. {
  1014. res.redirect("http://example.org/some_other_url");
  1015. }
  1016. void test()
  1017. {
  1018. auto router = new URLRouter;
  1019. router.get("/old_url", &request_handler);
  1020. listenHTTP(new HTTPServerSettings, router);
  1021. }
  1022. }
  1023. scope:
  1024. /** Returns the time at which the request was finalized.
  1025. Note that this field will only be set after `finalize` has been called.
  1026. */
  1027. @property SysTime timeFinalized() const @safe { return m_timeFinalized; }
  1028. /** Determines if the HTTP header has already been written.
  1029. */
  1030. @property bool headerWritten() const @safe { return m_exchange.headerWritten; }
  1031. /** Determines if the response does not need a body.
  1032. */
  1033. bool isHeadResponse() const @safe { return m_exchange.isHeadResponse; }
  1034. /** Determines if the response is sent over an encrypted connection.
  1035. */
  1036. bool tls() const @safe { return m_tls; }
  1037. /** Writes the entire response body at once.
  1038. Params:
  1039. data = The data to write as the body contents
  1040. status = Optional response status code to set
  1041. content_type = Optional content type to apply to the response.
  1042. If no content type is given and no "Content-Type" header is
  1043. set in the response, this will default to
  1044. `"application/octet-stream"`.
  1045. See_Also: `HTTPStatusCode`
  1046. */
  1047. void writeBody(in ubyte[] data, string content_type = null)
  1048. @safe {
  1049. if (content_type.length) headers["Content-Type"] = content_type;
  1050. else if ("Content-Type" !in headers) headers["Content-Type"] = "application/octet-stream";
  1051. headers["Content-Length"] = formatAlloc(m_requestAlloc, "%d", data.length);
  1052. bodyWriter.write(data);
  1053. }
  1054. /// ditto
  1055. void writeBody(in ubyte[] data, int status, string content_type = null)
  1056. @safe {
  1057. statusCode = status;
  1058. writeBody(data, content_type);
  1059. }
  1060. /// ditto
  1061. void writeBody(scope InputStream data, string content_type = null)
  1062. @safe {
  1063. if (content_type.length) headers["Content-Type"] = content_type;
  1064. else if ("Content-Type" !in headers) headers["Content-Type"] = "application/octet-stream";
  1065. data.pipe(bodyWriter);
  1066. }
  1067. /// ditto
  1068. void writeBody(scope InputStream data, int status, string content_type = null)
  1069. {
  1070. statusCode = status;
  1071. writeBody(data, content_type);
  1072. }
  1073. /** Writes the entire response body as a single string.
  1074. Params:
  1075. data = The string to write as the body contents
  1076. status = Optional response status code to set
  1077. content_type = Optional content type to apply to the response.
  1078. If no content type is given and no "Content-Type" header is
  1079. set in the response, this will default to
  1080. `"text/plain; charset=UTF-8"`.
  1081. See_Also: `HTTPStatusCode`
  1082. */
  1083. void writeBody(string data, string content_type = null)
  1084. @safe {
  1085. if (!content_type.length && "Content-Type" !in headers)
  1086. content_type = "text/plain; charset=UTF-8";
  1087. writeBody(cast(const(ubyte)[])data, content_type);
  1088. }
  1089. /// ditto
  1090. void writeBody(string data, int status, string content_type = null)
  1091. @safe {
  1092. statusCode = status;
  1093. writeBody(data, content_type);
  1094. }
  1095. /** Writes the whole response body at once, without doing any further encoding.
  1096. The caller has to make sure that the appropriate headers are set correctly
  1097. (i.e. Content-Type and Content-Encoding).
  1098. Note that the version taking a RandomAccessStream may perform additional
  1099. optimizations such as sending a file directly from the disk to the
  1100. network card using a DMA transfer.
  1101. */
  1102. void writeRawBody(RandomAccessStream)(RandomAccessStream stream) @safe
  1103. if (isRandomAccessStream!RandomAccessStream)
  1104. {
  1105. m_exchange.writeBody(this, RandomAccessStreamProxy(stream));
  1106. }
  1107. /// ditto
  1108. void writeRawBody(InputStream)(InputStream stream, size_t num_bytes = size_t.max) @safe
  1109. if (isInputStream!InputStream && !isRandomAccessStream!InputStream)
  1110. {
  1111. m_exchange.writeBody(this, InputStreamProxy(stream), num_bytes == 0 ? size_t.max : num_bytes);
  1112. }
  1113. /// ditto
  1114. void writeRawBody(RandomAccessStream)(RandomAccessStream stream, int status) @safe
  1115. if (isRandomAccessStream!RandomAccessStream)
  1116. {
  1117. statusCode = status;
  1118. writeRawBody(stream);
  1119. }
  1120. /// ditto
  1121. void writeRawBody(InputStream)(InputStream stream, int status, size_t num_bytes = 0) @safe
  1122. if (isInputStream!InputStream && !isRandomAccessStream!InputStream)
  1123. {
  1124. statusCode = status;
  1125. writeRawBody(stream, num_bytes);
  1126. }
  1127. /// Writes a JSON message with the specified status
  1128. void writeJsonBody(T)(T data, int status, bool allow_chunked = false)
  1129. {
  1130. statusCode = status;
  1131. writeJsonBody(data, allow_chunked);
  1132. }
  1133. /// ditto
  1134. void writeJsonBody(T)(T data, int status, string content_type, bool allow_chunked = false)
  1135. {
  1136. statusCode = status;
  1137. writeJsonBody(data, content_type, allow_chunked);
  1138. }
  1139. /// ditto
  1140. void writeJsonBody(T)(T data, string content_type, bool allow_chunked = false)
  1141. {
  1142. headers["Content-Type"] = content_type;
  1143. writeJsonBody(data, allow_chunked);
  1144. }
  1145. /// ditto
  1146. void writeJsonBody(T)(T data, bool allow_chunked = false)
  1147. {
  1148. doWriteJsonBody!(T, false)(data, allow_chunked);
  1149. }
  1150. /// ditto
  1151. void writePrettyJsonBody(T)(T data, bool allow_chunked = false)
  1152. {
  1153. doWriteJsonBody!(T, true)(data, allow_chunked);
  1154. }
  1155. private void doWriteJsonBody(T, bool PRETTY)(T data, bool allow_chunked = false)
  1156. {
  1157. import std.traits;
  1158. import vibe.stream.wrapper;
  1159. static if (!is(T == Json) && is(typeof(data.data())) && isArray!(typeof(data.data()))) {
  1160. static assert(!is(T == Appender!(typeof(data.data()))), "Passed an Appender!T to writeJsonBody - this is most probably not doing what's indended.");
  1161. }
  1162. if ("Content-Type" !in headers)
  1163. headers["Content-Type"] = "application/json; charset=UTF-8";
  1164. // set an explicit content-length field if chunked encoding is not allowed
  1165. if (!allow_chunked) {
  1166. import vibe.internal.rangeutil;
  1167. long length = 0;
  1168. auto counter = RangeCounter(() @trusted { return &length; } ());
  1169. static if (PRETTY) serializeToPrettyJson(counter, data);
  1170. else serializeToJson(counter, data);
  1171. headers["Content-Length"] = formatAlloc(m_requestAlloc, "%d", length);
  1172. }
  1173. auto rng = streamOutputRange!1024(bodyWriter);
  1174. static if (PRETTY) serializeToPrettyJson(() @trusted { return &rng; } (), data);
  1175. else serializeToJson(() @trusted { return &rng; } (), data);
  1176. }
  1177. /**
  1178. * Writes the response with no body.
  1179. *
  1180. * This method should be used in situations where no body is
  1181. * requested, such as a HEAD request. For an empty body, just use writeBody,
  1182. * as this method causes problems with some keep-alive connections.
  1183. */
  1184. void writeVoidBody()
  1185. @safe {
  1186. m_exchange.writeVoidBody(this);
  1187. }
  1188. /** A stream for writing the body of the HTTP response.
  1189. Note that after 'bodyWriter' has been accessed for the first time, it
  1190. is not allowed to change any header or the status code of the response.
  1191. */
  1192. @property InterfaceProxy!OutputStream bodyWriter()
  1193. @safe scope {
  1194. assert(!!m_exchange);
  1195. return m_exchange.bodyWriter(this);
  1196. }
  1197. /** Special method sending a SWITCHING_PROTOCOLS response to the client.
  1198. Notice: For the overload that returns a `ConnectionStream`, it must be
  1199. ensured that the returned instance doesn't outlive the request
  1200. handler callback.
  1201. Params:
  1202. protocol = The protocol set in the "Upgrade" header of the response.
  1203. Use an empty string to skip setting this field.
  1204. */
  1205. ConnectionStream switchProtocol(string protocol)
  1206. @safe {
  1207. return m_exchange.switchProtocol(this, protocol);
  1208. }
  1209. /// ditto
  1210. void switchProtocol(string protocol, scope void delegate(scope ConnectionStream) @safe del)
  1211. @safe {
  1212. m_exchange.switchProtocol(this, protocol, del);
  1213. }
  1214. /** Special method for handling CONNECT proxy tunnel
  1215. Notice: For the overload that returns a `ConnectionStream`, it must be
  1216. ensured that the returned instance doesn't outlive the request
  1217. handler callback.
  1218. */
  1219. ConnectionStream connectProxy()
  1220. @safe {
  1221. return m_exchange.connectProxy(this);
  1222. }
  1223. /// ditto
  1224. void connectProxy(scope void delegate(scope ConnectionStream) @safe del)
  1225. @safe {
  1226. m_exchange.connectProxy(this, del);
  1227. }
  1228. /** Sets the specified cookie value.
  1229. Params:
  1230. name = Name of the cookie
  1231. value = New cookie value - pass null to clear the cookie
  1232. path = Path (as seen by the client) of the directory tree in which the cookie is visible
  1233. encoding = Optional encoding (url, raw), default to URL encoding
  1234. */
  1235. Cookie setCookie(string name, string value, string path = "/", Cookie.Encoding encoding = Cookie.Encoding.url)
  1236. @safe {
  1237. auto cookie = new Cookie();
  1238. cookie.path = path;
  1239. cookie.setValue(value, encoding);
  1240. if (value is null) {
  1241. cookie.maxAge = 0;
  1242. cookie.expires = "Thu, 01 Jan 1970 00:00:00 GMT";
  1243. }
  1244. cookies[name] = cookie;
  1245. return cookie;
  1246. }
  1247. /**
  1248. Initiates a new session.
  1249. The session is stored in the SessionStore that was specified when
  1250. creating the server. Depending on this, the session can be persistent
  1251. or temporary and specific to this server instance.
  1252. */
  1253. Session startSession(string path = "/")
  1254. @safe {
  1255. return startSession(path, m_settings.sessionOptions);
  1256. }
  1257. /// ditto
  1258. Session startSession(string path, SessionOption options)
  1259. @safe {
  1260. assert(m_settings.sessionStore, "no session store set");
  1261. assert(!m_session, "Try to start a session, but already started one.");
  1262. bool secure;
  1263. if (options & SessionOption.secure) secure = true;
  1264. else if (options & SessionOption.noSecure) secure = false;
  1265. else secure = this.tls;
  1266. m_session = m_settings.sessionStore.create();
  1267. m_session.set("$sessionCookiePath", path);
  1268. m_session.set("$sessionCookieSecure", secure);
  1269. auto cookie = setCookie(m_settings.sessionIdCookie, m_session.id, path);
  1270. cookie.secure = secure;
  1271. cookie.httpOnly = (options & SessionOption.httpOnly) != 0;
  1272. cookie.sameSite = (options & SessionOption.noSameSiteStrict) ?
  1273. Cookie.SameSite.lax : Cookie.SameSite.strict;
  1274. return m_session;
  1275. }
  1276. /**
  1277. Terminates the current session (if any).
  1278. */
  1279. void terminateSession()
  1280. @safe {
  1281. if (!m_session) return;
  1282. auto cookie = setCookie(m_settings.sessionIdCookie, null, m_session.get!string("$sessionCookiePath"));
  1283. cookie.secure = m_session.get!bool("$sessionCookieSecure");
  1284. m_session.destroy();
  1285. m_session = Session.init;
  1286. }
  1287. @property ulong bytesWritten() @safe const { return m_exchange.bytesWritten; }
  1288. /**
  1289. Waits until either the connection closes, data arrives, or until the
  1290. given timeout is reached.
  1291. Returns:
  1292. $(D true) if the connection was closed and $(D false) if either the
  1293. timeout was reached, or if data has arrived for consumption.
  1294. See_Also: `connected`
  1295. */
  1296. bool waitForConnectionClose(Duration timeout = Duration.max)
  1297. @safe {
  1298. return m_exchange.waitForConnectionClose(timeout);
  1299. }
  1300. /**
  1301. Determines if the underlying connection is still alive.
  1302. Returns $(D true) if the remote peer is still connected and $(D false)
  1303. if the remote peer closed the connection.
  1304. See_Also: `waitForConnectionClose`
  1305. */
  1306. @property bool connected()
  1307. @safe const {
  1308. return m_exchange.connected;
  1309. }
  1310. /**
  1311. Finalizes the response. This is usually called automatically by the server.
  1312. This method can be called manually after writing the response to force
  1313. all network traffic associated with the current request to be finalized.
  1314. After the call returns, the `timeFinalized` property will be set.
  1315. */
  1316. void finalize()
  1317. @safe {
  1318. m_exchange.finalize(this);
  1319. }
  1320. }
  1321. interface HTTPServerExchange {
  1322. @safe:
  1323. @property bool isHeadResponse() const;
  1324. @property bool headerWritten() const;
  1325. @property ulong bytesWritten() const;
  1326. @property bool connected() const;
  1327. bool waitForConnectionClose(Duration timeout);
  1328. void writeBody(HTTPServerResponse res, RandomAccessStreamProxy streamx);
  1329. void writeBody(HTTPServerResponse res, InputStreamProxy stream, ulong num_bytes = ulong.max);
  1330. void writeVoidBody(HTTPServerResponse res);
  1331. OutputStreamProxy bodyWriter(HTTPServerResponse res);
  1332. ConnectionStream switchProtocol(HTTPServerResponse res, string protocol);
  1333. void switchProtocol(HTTPServerResponse res, string protocol, scope void delegate(scope ConnectionStream) @safe del);
  1334. ConnectionStream connectProxy(HTTPServerResponse res);
  1335. void connectProxy(HTTPServerResponse res, scope void delegate(scope ConnectionStream) @safe del);
  1336. void finalize(HTTPServerResponse res);
  1337. }
  1338. /**
  1339. Represents the request listener for a specific `listenHTTP` call.
  1340. This struct can be used to stop listening for HTTP requests at runtime.
  1341. */
  1342. struct HTTPListener {
  1343. private {
  1344. size_t[] m_virtualHostIDs;
  1345. }
  1346. private this(size_t[] ids) @safe { m_virtualHostIDs = ids; }
  1347. @property NetworkAddress[] bindAddresses()
  1348. @safe {
  1349. NetworkAddress[] ret;
  1350. foreach (l; s_listeners)
  1351. if (l.m_virtualHosts.canFind!(v => m_virtualHostIDs.canFind(v.id))) {
  1352. NetworkAddress a;
  1353. a = resolveHost(l.bindAddress);
  1354. a.port = l.bindPort;
  1355. ret ~= a;
  1356. }
  1357. return ret;
  1358. }
  1359. /** Stops handling HTTP requests and closes the TCP listening port if
  1360. possible.
  1361. */
  1362. void stopListening()
  1363. @safe {
  1364. import std.algorithm : countUntil;
  1365. foreach (vhid; m_virtualHostIDs) {
  1366. foreach (lidx, l; s_listeners) {
  1367. if (l.removeVirtualHost(vhid)) {
  1368. if (!l.hasVirtualHosts) {
  1369. l.m_listener.stopListening();
  1370. logInfo("Stopped to listen for HTTP%s requests on %s:%s", l.tlsContext ? "S": "", l.bindAddress, l.bindPort);
  1371. s_listeners = s_listeners[0 .. lidx] ~ s_listeners[lidx+1 .. $];
  1372. }
  1373. break;
  1374. }
  1375. }
  1376. }
  1377. }
  1378. }
  1379. /** Represents a single HTTP server port.
  1380. This class defines the incoming interface, port, and TLS configuration of
  1381. the public server port. The public server port may differ from the local
  1382. one if a reverse proxy of some kind is facing the public internet and
  1383. forwards to this HTTP server.
  1384. Multiple virtual hosts can be configured to be served from the same port.
  1385. Their TLS settings must be compatible and each virtual host must have a
  1386. unique name.
  1387. */
  1388. final class HTTPServerContext {
  1389. package struct VirtualHost {
  1390. HTTPServerRequestDelegate requestHandler;
  1391. HTTPServerSettings settings;
  1392. HTTPLogger[] loggers;
  1393. size_t id;
  1394. }
  1395. package {
  1396. TCPListener m_listener;
  1397. VirtualHost[] m_virtualHosts;
  1398. string m_bindAddress;
  1399. ushort m_bindPort;
  1400. TLSContext m_tlsContext;
  1401. static size_t s_vhostIDCounter = 1;
  1402. }
  1403. @safe:
  1404. this(string bind_address, ushort bind_port)
  1405. {
  1406. m_bindAddress = bind_address;
  1407. m_bindPort = bind_port;
  1408. }
  1409. /** Returns the TLS context associated with the listener.
  1410. For non-HTTPS listeners, `null` will be returned. Otherwise, if only a
  1411. single virtual host has been added, the TLS context of that host's
  1412. settings is returned. For multiple virtual hosts, an SNI context is
  1413. returned, which forwards to the individual contexts based on the
  1414. requested host name.
  1415. */
  1416. @property TLSContext tlsContext() { return m_tlsContext; }
  1417. /// The local network interface IP address associated with this listener
  1418. @property string bindAddress() const { return m_bindAddress; }
  1419. /// The local port associated with this listener
  1420. @property ushort bindPort() const { return m_bindPort; }
  1421. /// Determines if any virtual hosts have been addded
  1422. @property bool hasVirtualHosts() const { return m_virtualHosts.length > 0; }
  1423. /** Adds a single virtual host.
  1424. Note that the port and bind address defined in `settings` must match the
  1425. ones for this listener. The `settings.host` field must be unique for
  1426. all virtual hosts.
  1427. Returns: Returns a unique ID for the new virtual host
  1428. */
  1429. size_t addVirtualHost(HTTPServerSettings settings, HTTPServerRequestDelegate request_handler)
  1430. {
  1431. assert(settings.port == 0 || settings.port == m_bindPort, "Virtual host settings do not match bind port.");
  1432. assert(settings.bindAddresses.canFind(m_bindAddress), "Virtual host settings do not match bind address.");
  1433. VirtualHost vhost;
  1434. vhost.id = s_vhostIDCounter++;
  1435. vhost.settings = settings;
  1436. vhost.requestHandler = request_handler;
  1437. if (settings.accessLogger) vhost.loggers ~= settings.accessLogger;
  1438. if (settings.accessLogToConsole)
  1439. vhost.loggers ~= new HTTPConsoleLogger(settings, settings.accessLogFormat);
  1440. if (settings.accessLogFile.length)
  1441. vhost.loggers ~= new HTTPFileLogger(settings, settings.accessLogFormat, settings.accessLogFile);
  1442. if (!m_virtualHosts.length) m_tlsContext = settings.tlsContext;
  1443. enforce((m_tlsContext !is null) == (settings.tlsContext !is null),
  1444. "Cannot mix HTTP and HTTPS virtual hosts within the same listener.");
  1445. if (m_tlsContext) addSNIHost(settings);
  1446. m_virtualHosts ~= vhost;
  1447. if (settings.hostName.length) {
  1448. auto proto = settings.tlsContext ? "https" : "http";
  1449. auto port = settings.tlsContext && settings.port == 443 || !settings.tlsContext && settings.port == 80 ? "" : ":" ~ settings.port.to!string;
  1450. logInfo("Added virtual host %s://%s:%s/ (%s)", proto, settings.hostName, m_bindPort, m_bindAddress);
  1451. }
  1452. return vhost.id;
  1453. }
  1454. /// Removes a previously added virtual host using its ID.
  1455. bool removeVirtualHost(size_t id)
  1456. {
  1457. import std.algorithm.searching : countUntil;
  1458. auto idx = m_virtualHosts.countUntil!(c => c.id == id);
  1459. if (idx < 0) return false;
  1460. auto ctx = m_virtualHosts[idx];
  1461. m_virtualHosts = m_virtualHosts[0 .. idx] ~ m_virtualHosts[idx+1 .. $];
  1462. return true;
  1463. }
  1464. private void addSNIHost(HTTPServerSettings settings)
  1465. {
  1466. if (settings.tlsContext !is m_tlsContext && m_tlsContext.kind != TLSContextKind.serverSNI) {
  1467. logDebug("Create SNI TLS context for %s, port %s", bindAddress, bindPort);
  1468. m_tlsContext = createTLSContext(TLSContextKind.serverSNI);
  1469. m_tlsContext.sniCallback = &onSNI;
  1470. }
  1471. foreach (ctx; m_virtualHosts) {
  1472. /*enforce(ctx.settings.hostName != settings.hostName,
  1473. "A server with the host name '"~settings.hostName~"' is already "
  1474. "listening on "~addr~":"~to!string(settings.port)~".");*/
  1475. }
  1476. }
  1477. private TLSContext onSNI(string servername)
  1478. {
  1479. foreach (vhost; m_virtualHosts)
  1480. if (vhost.settings.hostName.icmp(servername) == 0) {
  1481. logDebug("Found context for SNI host '%s'.", servername);
  1482. return vhost.settings.tlsContext;
  1483. }
  1484. logDebug("No context found for SNI host '%s'.", servername);
  1485. return null;
  1486. }
  1487. }
  1488. /**************************************************************************************************/
  1489. /* Private types */
  1490. /**************************************************************************************************/
  1491. package final class LimitedHTTPInputStream : LimitedInputStream {
  1492. @safe:
  1493. this(InterfaceProxy!InputStream stream, ulong byte_limit, bool silent_limit = false) {
  1494. super(stream, byte_limit, silent_limit, true);
  1495. }
  1496. override void onSizeLimitReached() {
  1497. throw new HTTPStatusException(HTTPStatus.requestEntityTooLarge);
  1498. }
  1499. }
  1500. package final class TimeoutHTTPInputStream : InputStream {
  1501. @safe:
  1502. private {
  1503. long m_timeref;
  1504. long m_timeleft;
  1505. InterfaceProxy!InputStream m_in;
  1506. }
  1507. this(InterfaceProxy!InputStream stream, Duration timeleft, SysTime reftime)
  1508. {
  1509. enforce(timeleft > 0.seconds, "Timeout required");
  1510. m_in = stream;
  1511. m_timeleft = timeleft.total!"hnsecs"();
  1512. m_timeref = reftime.stdTime();
  1513. }
  1514. @property bool empty() { enforce(m_in, "InputStream missing"); return m_in.empty(); }
  1515. @property ulong leastSize() { enforce(m_in, "InputStream missing"); return m_in.leastSize(); }
  1516. @property bool dataAvailableForRead() { enforce(m_in, "InputStream missing"); return m_in.dataAvailableForRead; }
  1517. const(ubyte)[] peek() { return m_in.peek(); }
  1518. size_t read(scope ubyte[] dst, IOMode mode)
  1519. {
  1520. enforce(m_in, "InputStream missing");
  1521. size_t nread = 0;
  1522. checkTimeout();
  1523. // FIXME: this should use ConnectionStream.waitForData to enforce the timeout during the
  1524. // read operation
  1525. return m_in.read(dst, mode);
  1526. }
  1527. alias read = InputStream.read;
  1528. private void checkTimeout()
  1529. @safe {
  1530. auto curr = Clock.currStdTime();
  1531. auto diff = curr - m_timeref;
  1532. if (diff > m_timeleft) throw new HTTPStatusException(HTTPStatus.requestTimeout);
  1533. m_timeleft -= diff;
  1534. m_timeref = curr;
  1535. }
  1536. }
  1537. /**************************************************************************************************/
  1538. /* Private functions */
  1539. /**************************************************************************************************/
  1540. private {
  1541. import core.sync.mutex;
  1542. shared string s_distHost;
  1543. shared ushort s_distPort = 11000;
  1544. HTTPServerContext[] s_listeners;
  1545. }
  1546. /**
  1547. [private] Starts a HTTP server listening on the specified port.
  1548. This is the same as listenHTTP() except that it does not use a VibeDist host for
  1549. remote listening, even if specified on the command line.
  1550. */
  1551. private HTTPListener listenHTTPPlain(HTTPServerSettings settings, HTTPServerRequestDelegate request_handler)
  1552. @safe {
  1553. import vibe.core.core : runWorkerTaskDist;
  1554. import std.algorithm : canFind, find;
  1555. static TCPListener doListen(HTTPServerContext listen_info, bool reusePort, bool reuseAddress, bool is_tls)
  1556. @safe {
  1557. try {
  1558. TCPListenOptions options = TCPListenOptions.defaults;
  1559. if(reuseAddress) options |= TCPListenOptions.reuseAddress; else options &= ~TCPListenOptions.reuseAddress;
  1560. if(reusePort) options |= TCPListenOptions.reusePort; else options &= ~TCPListenOptions.reusePort;
  1561. auto ret = listenTCP(listen_info.bindPort, (TCPConnection conn) nothrow @safe {
  1562. try handleHTTPConnection(conn, listen_info);
  1563. catch (Exception e) {
  1564. logError("HTTP connection handler has thrown at the peer %s: %s", conn.peerAddress, e.msg);
  1565. debug logDebug("Full error: %s", () @trusted { return e.toString().sanitize(); } ());
  1566. try conn.close();
  1567. catch (Exception e) logError("Failed to close connection: %s", e.msg);
  1568. }
  1569. }, listen_info.bindAddress, options);
  1570. // support port 0 meaning any available port
  1571. if (listen_info.bindPort == 0)
  1572. listen_info.m_bindPort = ret.bindAddress.port;
  1573. auto proto = is_tls ? "https" : "http";
  1574. auto urladdr = listen_info.bindAddress;
  1575. if (urladdr.canFind(':')) urladdr = "["~urladdr~"]";
  1576. logInfo("Listening for requests on %s://%s:%s/", proto, urladdr, listen_info.bindPort);
  1577. return ret;
  1578. } catch( Exception e ) {
  1579. logWarn("Failed to listen on %s:%s", listen_info.bindAddress, listen_info.bindPort);
  1580. return TCPListener.init;
  1581. }
  1582. }
  1583. size_t[] vid;
  1584. // Check for every bind address/port, if a new listening socket needs to be created and
  1585. // check for conflicting servers
  1586. foreach (addr; settings.bindAddresses) {
  1587. HTTPServerContext linfo;
  1588. auto l = s_listeners.find!(l => l.bindAddress == addr && l.bindPort == settings.port);
  1589. if (!l.empty) linfo = l.front;
  1590. else {
  1591. auto li = new HTTPServerContext(addr, settings.port);
  1592. if (auto tcp_lst = doListen(li,
  1593. (settings.options & HTTPServerOption.reusePort) != 0,
  1594. (settings.options & HTTPServerOption.reuseAddress) != 0,
  1595. settings.tlsContext !is null)) // DMD BUG 2043
  1596. {
  1597. li.m_listener = tcp_lst;
  1598. s_listeners ~= li;
  1599. linfo = li;
  1600. }
  1601. }
  1602. if (linfo) vid ~= linfo.addVirtualHost(settings, request_handler);
  1603. }
  1604. enforce(vid.length > 0, "Failed to listen for incoming HTTP connections on any of the supplied interfaces.");
  1605. return HTTPListener(vid);
  1606. }
  1607. private void parseCookies(string str, ref CookieValueMap cookies)
  1608. @safe {
  1609. import std.encoding : sanitize;
  1610. import std.array : split;
  1611. import std.string : strip;
  1612. import std.algorithm.iteration : map, filter, each;
  1613. import vibe.http.common : Cookie;
  1614. () @trusted { return str.sanitize; } ()
  1615. .split(";")
  1616. .map!(kv => kv.strip.split("="))
  1617. .filter!(kv => kv.length == 2) //ignore illegal cookies
  1618. .each!(kv => cookies.add(kv[0], kv[1], Cookie.Encoding.raw) );
  1619. }
  1620. unittest
  1621. {
  1622. auto cvm = CookieValueMap();
  1623. parseCookies("foo=bar;; baz=zinga; öö=üü ; møøse=was=sacked; onlyval1; =onlyval2; onlykey=", cvm);
  1624. assert(cvm["foo"] == "bar");
  1625. assert(cvm["baz"] == "zinga");
  1626. assert(cvm["öö"] == "üü");
  1627. assert( "møøse" ! in cvm); //illegal cookie gets ignored
  1628. assert( "onlyval1" ! in cvm); //illegal cookie gets ignored
  1629. assert(cvm["onlykey"] == "");
  1630. assert(cvm[""] == "onlyval2");
  1631. assert(cvm.length() == 5);
  1632. cvm = CookieValueMap();
  1633. parseCookies("", cvm);
  1634. assert(cvm.length() == 0);
  1635. cvm = CookieValueMap();
  1636. parseCookies(";;=", cvm);
  1637. assert(cvm.length() == 1);
  1638. assert(cvm[""] == "");
  1639. }
  1640. shared static this()
  1641. {
  1642. version (VibeNoDefaultArgs) {}
  1643. else {
  1644. string disthost = s_distHost;
  1645. ushort distport = s_distPort;
  1646. import vibe.core.args : readOption;
  1647. readOption("disthost|d", () @trusted { return &disthost; } (), "Sets the name of a vibedist server to use for load balancing.");
  1648. readOption("distport", () @trusted { return &distport; } (), "Sets the port used for load balancing.");
  1649. setVibeDistHost(disthost, distport);
  1650. }
  1651. }
  1652. version (VibeDebugCatchAll) package alias UncaughtException = Throwable;
  1653. else package alias UncaughtException = Exception;