client.d 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300
  1. /**
  2. A simple HTTP/1.1 client implementation.
  3. Copyright: © 2012-2014 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
  6. */
  7. module vibe.http.client;
  8. public import vibe.core.net;
  9. public import vibe.http.common;
  10. public import vibe.inet.url;
  11. import vibe.container.dictionarylist;
  12. import vibe.container.internal.utilallocator;
  13. import vibe.container.ringbuffer : RingBuffer;
  14. import vibe.core.connectionpool;
  15. import vibe.core.core;
  16. import vibe.core.log;
  17. import vibe.data.json;
  18. import vibe.inet.message;
  19. import vibe.inet.url;
  20. import vibe.stream.counting;
  21. import vibe.stream.tls;
  22. import vibe.stream.operations;
  23. import vibe.stream.wrapper : createConnectionProxyStream;
  24. import vibe.stream.zlib;
  25. import vibe.internal.freelistref;
  26. import vibe.internal.interfaceproxy : InterfaceProxy, interfaceProxy;
  27. import core.exception : AssertError;
  28. import std.algorithm : splitter;
  29. import std.array;
  30. import std.conv;
  31. import std.encoding : sanitize;
  32. import std.exception;
  33. import std.format;
  34. import std.string;
  35. import std.typecons;
  36. import std.datetime;
  37. import std.socket : AddressFamily;
  38. version(Posix)
  39. {
  40. version = UnixSocket;
  41. }
  42. /**************************************************************************************************/
  43. /* Public functions */
  44. /**************************************************************************************************/
  45. @safe:
  46. /**
  47. Performs a synchronous HTTP request on the specified URL.
  48. The requester parameter allows to customize the request and to specify the request body for
  49. non-GET requests before it is sent. A response object is then returned or passed to the
  50. responder callback synchronously.
  51. This function is a low-level HTTP client facility. It will not perform automatic redirect,
  52. caching or similar tasks. For a high-level download facility (similar to cURL), see the
  53. `vibe.inet.urltransfer` module.
  54. Note that it is highly recommended to use one of the overloads that take a responder callback,
  55. as they can avoid some memory allocations and are safe against accidentally leaving stale
  56. response objects (objects whose response body wasn't fully read). For the returning overloads
  57. of the function it is recommended to put a `scope(exit)` right after the call in which
  58. `HTTPClientResponse.dropBody` is called to avoid this.
  59. See_also: `vibe.inet.urltransfer.download`
  60. */
  61. HTTPClientResponse requestHTTP(string url, scope void delegate(scope HTTPClientRequest req) requester = null, const(HTTPClientSettings) settings = defaultSettings)
  62. {
  63. return requestHTTP(URL.parse(url), requester, settings);
  64. }
  65. /// ditto
  66. HTTPClientResponse requestHTTP(URL url, scope void delegate(scope HTTPClientRequest req) requester = null, const(HTTPClientSettings) settings = defaultSettings)
  67. {
  68. auto cli = connectHTTP(url, settings);
  69. auto res = cli.request(
  70. (scope req){ httpRequesterDg(req, url, settings, requester); },
  71. );
  72. // make sure the connection stays locked if the body still needs to be read
  73. if( res.m_client ) res.lockedConnection = cli;
  74. logTrace("Returning HTTPClientResponse for conn %s", () @trusted { return cast(void*)res.lockedConnection.__conn; } ());
  75. return res;
  76. }
  77. /// ditto
  78. void requestHTTP(string url, scope void delegate(scope HTTPClientRequest req) requester, scope void delegate(scope HTTPClientResponse req) responder, const(HTTPClientSettings) settings = defaultSettings)
  79. {
  80. requestHTTP(URL(url), requester, responder, settings);
  81. }
  82. /// ditto
  83. void requestHTTP(URL url, scope void delegate(scope HTTPClientRequest req) requester, scope void delegate(scope HTTPClientResponse req) responder, const(HTTPClientSettings) settings = defaultSettings)
  84. {
  85. auto cli = connectHTTP(url, settings);
  86. cli.request(
  87. (scope req){ httpRequesterDg(req, url, settings, requester); },
  88. responder
  89. );
  90. assert(!cli.m_requesting, "HTTP client still requesting after return!?");
  91. assert(!cli.m_responding, "HTTP client still responding after return!?");
  92. }
  93. private bool isTLSRequired(in URL url, in HTTPClientSettings settings)
  94. {
  95. version(UnixSocket) {
  96. enforce(url.schema == "http" || url.schema == "https" || url.schema == "http+unix" || url.schema == "https+unix", "URL schema must be http(s) or http(s)+unix.");
  97. } else {
  98. enforce(url.schema == "http" || url.schema == "https", "URL schema must be http(s).");
  99. }
  100. enforce(url.host.length > 0, "URL must contain a host name.");
  101. bool use_tls;
  102. if (settings.proxyURL.schema !is null)
  103. use_tls = settings.proxyURL.schema == "https";
  104. else
  105. {
  106. version(UnixSocket)
  107. use_tls = url.schema == "https" || url.schema == "https+unix";
  108. else
  109. use_tls = url.schema == "https";
  110. }
  111. return use_tls;
  112. }
  113. private void httpRequesterDg(scope HTTPClientRequest req, in URL url, in HTTPClientSettings settings, scope void delegate(scope HTTPClientRequest req) requester)
  114. {
  115. import std.algorithm.searching : canFind;
  116. import vibe.http.internal.basic_auth_client: addBasicAuth;
  117. if (url.localURI.length) {
  118. assert(url.path.absolute, "Request URL path must be absolute.");
  119. req.requestURL = url.localURI;
  120. }
  121. if (settings.proxyURL.schema !is null)
  122. req.requestURL = url.toString(); // proxy exception to the URL representation
  123. // IPv6 addresses need to be put into brackets
  124. auto hoststr = url.host.canFind(':') ? "["~url.host~"]" : url.host;
  125. // Provide port number when it is not the default one (RFC2616 section 14.23)
  126. if (url.port && url.port != url.defaultPort)
  127. req.headers["Host"] = format("%s:%d", hoststr, url.port);
  128. else
  129. req.headers["Host"] = hoststr;
  130. if ("authorization" !in req.headers && url.username != "")
  131. req.addBasicAuth(url.username, url.password);
  132. if (requester) () @trusted { requester(req); } ();
  133. }
  134. /** Posts a simple JSON request. Note that the server www.example.org does not
  135. exists, so there will be no meaningful result.
  136. */
  137. unittest {
  138. import vibe.core.log;
  139. import vibe.http.client;
  140. import vibe.stream.operations;
  141. void test()
  142. {
  143. requestHTTP("http://www.example.org/",
  144. (scope req) {
  145. req.method = HTTPMethod.POST;
  146. //req.writeJsonBody(["name": "My Name"]);
  147. },
  148. (scope res) {
  149. logInfo("Response: %s", res.bodyReader.readAllUTF8());
  150. }
  151. );
  152. }
  153. }
  154. /**
  155. Returns a HTTPClient proxy object that is connected to the specified host.
  156. Internally, a connection pool is used to reuse already existing connections. Note that
  157. usually requestHTTP should be used for making requests instead of manually using a
  158. HTTPClient to do so.
  159. */
  160. auto connectHTTP(string host, ushort port = 0, bool use_tls = false, const(HTTPClientSettings) settings = null)
  161. {
  162. auto sttngs = settings ? settings : defaultSettings;
  163. if (port == 0) port = use_tls ? 443 : 80;
  164. auto ckey = ConnInfo(host, sttngs.tlsPeerName, port, use_tls, sttngs.proxyURL.host, sttngs.proxyURL.port, sttngs.networkInterface);
  165. ConnectionPool!HTTPClient pool;
  166. s_connections.opApply((ref c) @safe {
  167. if (c[0] == ckey)
  168. pool = c[1];
  169. return 0;
  170. });
  171. if (!pool) {
  172. logDebug("Create HTTP client pool %s(%s):%s %s proxy %s:%d", host, sttngs.tlsPeerName, port, use_tls, sttngs.proxyURL.host, sttngs.proxyURL.port);
  173. pool = new ConnectionPool!HTTPClient({
  174. auto ret = new HTTPClient;
  175. ret.connect(host, port, use_tls, sttngs);
  176. return ret;
  177. });
  178. if (s_connections.full) s_connections.removeFront();
  179. s_connections.put(tuple(ckey, pool));
  180. }
  181. return pool.lockConnection();
  182. }
  183. /// Ditto
  184. auto connectHTTP(URL url, const(HTTPClientSettings) settings = null)
  185. {
  186. const use_tls = isTLSRequired(url, settings);
  187. return connectHTTP(url.getFilteredHost, url.port, use_tls, settings);
  188. }
  189. static ~this()
  190. {
  191. foreach (ci; s_connections) {
  192. ci[1].removeUnused((conn) {
  193. conn.disconnect();
  194. });
  195. }
  196. }
  197. private struct ConnInfo { string host; string tlsPeerName; ushort port; bool useTLS; string proxyIP; ushort proxyPort; NetworkAddress bind_addr; }
  198. private static RingBuffer!(Tuple!(ConnInfo, ConnectionPool!HTTPClient), 16) s_connections;
  199. /**************************************************************************************************/
  200. /* Public types */
  201. /**************************************************************************************************/
  202. /**
  203. Defines an HTTP/HTTPS proxy request or a connection timeout for an HTTPClient.
  204. */
  205. class HTTPClientSettings {
  206. URL proxyURL;
  207. Duration defaultKeepAliveTimeout = 10.seconds;
  208. /// Timeout for establishing a connection to the server
  209. Duration connectTimeout = Duration.max;
  210. /// Timeout during read operations on the underyling transport
  211. Duration readTimeout = Duration.max;
  212. /// Forces a specific network interface to use for outgoing connections.
  213. NetworkAddress networkInterface = anyAddress;
  214. /// Can be used to force looking up IPv4/IPv6 addresses for host names.
  215. AddressFamily dnsAddressFamily = AddressFamily.UNSPEC;
  216. /** Allows to customize the TLS context before connecting to a server.
  217. Note that this overrides a callback set with `HTTPClient.setTLSContextSetup`.
  218. */
  219. void delegate(TLSContext ctx) @safe nothrow tlsContextSetup;
  220. /**
  221. TLS Peer name override.
  222. Allows to customize the tls peer name sent to server during the TLS connection setup (SNI)
  223. */
  224. string tlsPeerName;
  225. @property HTTPClientSettings dup()
  226. const @safe {
  227. auto ret = new HTTPClientSettings;
  228. ret.proxyURL = this.proxyURL;
  229. ret.connectTimeout = this.connectTimeout;
  230. ret.readTimeout = this.readTimeout;
  231. ret.networkInterface = this.networkInterface;
  232. ret.dnsAddressFamily = this.dnsAddressFamily;
  233. ret.tlsContextSetup = this.tlsContextSetup;
  234. ret.tlsPeerName = this.tlsPeerName;
  235. return ret;
  236. }
  237. }
  238. ///
  239. unittest {
  240. void test() {
  241. HTTPClientSettings settings = new HTTPClientSettings;
  242. settings.proxyURL = URL.parse("http://proxyuser:proxypass@192.168.2.50:3128");
  243. settings.defaultKeepAliveTimeout = 0.seconds; // closes connection immediately after receiving the data.
  244. requestHTTP("http://www.example.org",
  245. (scope req){
  246. req.method = HTTPMethod.GET;
  247. },
  248. (scope res){
  249. logInfo("Headers:");
  250. foreach (key, ref value; res.headers.byKeyValue) {
  251. logInfo("%s: %s", key, value);
  252. }
  253. logInfo("Response: %s", res.bodyReader.readAllUTF8());
  254. }, settings);
  255. }
  256. }
  257. version (Have_vibe_core)
  258. unittest { // test connect timeout
  259. import std.conv : to;
  260. import vibe.core.stream : pipe, nullSink;
  261. HTTPClientSettings settings = new HTTPClientSettings;
  262. settings.connectTimeout = 50.msecs;
  263. // Use an IP address that is guaranteed to be unassigned globally to force
  264. // a timeout (see RFC 3330)
  265. auto cli = connectHTTP("192.0.2.0", 80, false, settings);
  266. auto timer = setTimer(500.msecs, { assert(false, "Connect timeout occurred too late"); });
  267. scope (exit) timer.stop();
  268. try {
  269. cli.request(
  270. (scope req) { assert(false, "Expected no connection"); },
  271. (scope res) { assert(false, "Expected no response"); }
  272. );
  273. assert(false, "Response read expected to fail due to timeout");
  274. } catch(Exception e) {}
  275. }
  276. unittest { // test read timeout
  277. import std.conv : to;
  278. import vibe.core.stream : pipe, nullSink;
  279. version (VibeLibasyncDriver) {
  280. logInfo("Skipping HTTP client read timeout test due to buggy libasync driver.");
  281. } else {
  282. HTTPClientSettings settings = new HTTPClientSettings;
  283. settings.readTimeout = 50.msecs;
  284. auto l = listenTCP(0, (conn) {
  285. try conn.pipe(nullSink);
  286. catch (Exception e) assert(false, e.msg);
  287. conn.close();
  288. }, "127.0.0.1");
  289. auto cli = connectHTTP("127.0.0.1", l.bindAddress.port, false, settings);
  290. auto timer = setTimer(500.msecs, { assert(false, "Read timeout occurred too late"); });
  291. scope (exit) {
  292. timer.stop();
  293. l.stopListening();
  294. cli.disconnect();
  295. sleep(10.msecs); // allow the read connection end to fully close
  296. }
  297. try {
  298. cli.request(
  299. (scope req) { req.method = HTTPMethod.GET; },
  300. (scope res) { assert(false, "Expected no response"); }
  301. );
  302. assert(false, "Response read expected to fail due to timeout");
  303. } catch(Exception e) {}
  304. }
  305. }
  306. /**
  307. Implementation of a HTTP 1.0/1.1 client with keep-alive support.
  308. Note that it is usually recommended to use requestHTTP for making requests as that will use a
  309. pool of HTTPClient instances to keep the number of connection establishments low while not
  310. blocking requests from different tasks.
  311. */
  312. final class HTTPClient {
  313. @safe:
  314. enum maxHeaderLineLength = 4096;
  315. private {
  316. Rebindable!(const(HTTPClientSettings)) m_settings;
  317. string m_server;
  318. string m_tlsPeerName;
  319. ushort m_port;
  320. bool m_useTLS;
  321. TCPConnection m_conn;
  322. InterfaceProxy!Stream m_stream;
  323. TLSStream m_tlsStream;
  324. TLSContext m_tls;
  325. static __gshared m_userAgent = "vibe.d/"~vibeVersionString~" (HTTPClient, +http://vibed.org/)";
  326. static __gshared void function(TLSContext) ms_tlsSetup;
  327. bool m_requesting = false, m_responding = false;
  328. SysTime m_keepAliveLimit;
  329. Duration m_keepAliveTimeout;
  330. }
  331. /** Get the current settings for the HTTP client. **/
  332. @property const(HTTPClientSettings) settings() const {
  333. return m_settings;
  334. }
  335. /**
  336. Sets the default user agent string for new HTTP requests.
  337. */
  338. static void setUserAgentString(string str) @trusted { m_userAgent = str; }
  339. /**
  340. Sets a callback that will be called for every TLS context that is created.
  341. Setting such a callback is useful for adjusting the validation parameters
  342. of the TLS context.
  343. */
  344. static void setTLSSetupCallback(void function(TLSContext) @safe func) @trusted { ms_tlsSetup = func; }
  345. /**
  346. Sets up this HTTPClient to connect to a specific server.
  347. This method may only be called if any previous connection has been closed.
  348. The actual connection is deferred until a request is initiated (using `HTTPClient.request`).
  349. */
  350. void connect(string server, ushort port = 80, bool use_tls = false, const(HTTPClientSettings) settings = defaultSettings)
  351. {
  352. assert(!m_conn);
  353. assert(port != 0);
  354. disconnect();
  355. m_conn = TCPConnection.init;
  356. m_settings = settings;
  357. m_keepAliveTimeout = settings.defaultKeepAliveTimeout;
  358. m_keepAliveLimit = Clock.currTime(UTC()) + m_keepAliveTimeout;
  359. m_server = server;
  360. m_tlsPeerName = settings.tlsPeerName.length ? settings.tlsPeerName : server;
  361. m_port = port;
  362. m_useTLS = use_tls;
  363. if (use_tls) {
  364. m_tls = createTLSContext(TLSContextKind.client);
  365. // this will be changed to trustedCert once a proper root CA store is available by default
  366. m_tls.peerValidationMode = TLSPeerValidationMode.none;
  367. if (settings.tlsContextSetup) settings.tlsContextSetup(m_tls);
  368. else () @trusted { if (ms_tlsSetup) ms_tlsSetup(m_tls); } ();
  369. }
  370. }
  371. /**
  372. Forcefully closes the TCP connection.
  373. Before calling this method, be sure that no request is currently being processed.
  374. */
  375. void disconnect()
  376. nothrow {
  377. if (m_conn) {
  378. version (Have_vibe_core) {}
  379. else scope(failure) assert(false);
  380. if (m_conn.connected) {
  381. try m_stream.finalize();
  382. catch (Exception e) logDebug("Failed to finalize connection stream when closing HTTP client connection: %s", e.msg);
  383. m_conn.close();
  384. }
  385. if (m_useTLS) () @trusted { return destroy(m_stream); } ();
  386. m_stream = InterfaceProxy!Stream.init;
  387. () @trusted { return destroy(m_conn); } ();
  388. m_conn = TCPConnection.init;
  389. }
  390. }
  391. private void doProxyRequest(T, U)(ref T res, U requester, ref bool close_conn, ref bool has_body)
  392. @trusted { // scope new
  393. import std.conv : to;
  394. scope request_allocator = createRequestAllocator();
  395. scope (exit) freeRequestAllocator(request_allocator);
  396. res.dropBody();
  397. scope(failure)
  398. res.disconnect();
  399. if (res.statusCode != 407) {
  400. throw new HTTPStatusException(HTTPStatus.internalServerError, "Proxy returned Proxy-Authenticate without a 407 status code.");
  401. }
  402. // send the request again with the proxy authentication information if available
  403. if (m_settings.proxyURL.username is null) {
  404. throw new HTTPStatusException(HTTPStatus.proxyAuthenticationRequired, "Proxy Authentication Required.");
  405. }
  406. m_responding = false;
  407. close_conn = false;
  408. bool found_proxy_auth;
  409. foreach (string proxyAuth; res.headers.getAll("Proxy-Authenticate"))
  410. {
  411. if (proxyAuth.length >= "Basic".length && proxyAuth[0.."Basic".length] == "Basic")
  412. {
  413. found_proxy_auth = true;
  414. break;
  415. }
  416. }
  417. if (!found_proxy_auth)
  418. {
  419. throw new HTTPStatusException(HTTPStatus.notAcceptable, "The Proxy Server didn't allow Basic Authentication");
  420. }
  421. SysTime connected_time;
  422. has_body = doRequestWithRetry(requester, true, close_conn, connected_time);
  423. m_responding = true;
  424. static if (is(T == HTTPClientResponse))
  425. res = new HTTPClientResponse(this, close_conn);
  426. else
  427. res = scoped!HTTPClientResponse(this, close_conn);
  428. res.initialize(has_body, request_allocator, connected_time);
  429. if (res.headers.get("Proxy-Authenticate", null) !is null){
  430. res.dropBody();
  431. throw new HTTPStatusException(HTTPStatus.proxyAuthenticationRequired, "Proxy Authentication Failed.");
  432. }
  433. }
  434. /**
  435. Performs a HTTP request.
  436. `requester` is called first to populate the request with headers and the desired
  437. HTTP method and version. After a response has been received it is then passed
  438. to the caller which can in turn read the reponse body. Any part of the body
  439. that has not been processed will automatically be consumed and dropped.
  440. Note that the `requester` callback might be invoked multiple times in the event
  441. that a request has to be resent due to a connection failure.
  442. Also note that the second form of this method (returning a `HTTPClientResponse`) is
  443. not recommended to use as it may accidentially block a HTTP connection when
  444. only part of the response body was read and also requires a heap allocation
  445. for the response object. The callback based version on the other hand uses
  446. a stack allocation and guarantees that the request has been fully processed
  447. once it has returned.
  448. */
  449. void request(scope void delegate(scope HTTPClientRequest req) requester, scope void delegate(scope HTTPClientResponse) responder)
  450. @trusted { // scope new
  451. scope request_allocator = createRequestAllocator();
  452. scope (exit) freeRequestAllocator(request_allocator);
  453. scope (failure) {
  454. m_responding = false;
  455. disconnect();
  456. }
  457. bool close_conn;
  458. SysTime connected_time;
  459. bool has_body = doRequestWithRetry(requester, false, close_conn, connected_time);
  460. m_responding = true;
  461. auto res = scoped!HTTPClientResponse(this, close_conn);
  462. res.initialize(has_body, request_allocator, connected_time);
  463. // proxy implementation
  464. if (res.headers.get("Proxy-Authenticate", null) !is null) {
  465. doProxyRequest(res, requester, close_conn, has_body);
  466. }
  467. Exception user_exception;
  468. while (true)
  469. {
  470. try responder(res);
  471. catch (Exception e) {
  472. logDebug("Error while handling response: %s", e.toString().sanitize());
  473. user_exception = e;
  474. }
  475. if (res.statusCode < 200) {
  476. // just an informational status -> read and handle next response
  477. if (m_responding) res.dropBody();
  478. if (m_conn) {
  479. res = scoped!HTTPClientResponse(this, close_conn);
  480. res.initialize(has_body, request_allocator, connected_time);
  481. continue;
  482. }
  483. }
  484. if (m_responding) {
  485. logDebug("Failed to handle the complete response of the server - disconnecting.");
  486. res.disconnect();
  487. }
  488. assert(!m_responding, "Still in responding state after finalizing the response!?");
  489. if (user_exception || res.headers.get("Connection") == "close")
  490. disconnect();
  491. break;
  492. }
  493. if (user_exception) throw user_exception;
  494. }
  495. /// ditto
  496. HTTPClientResponse request(scope void delegate(HTTPClientRequest) requester)
  497. {
  498. bool close_conn;
  499. SysTime connected_time;
  500. scope (failure) {
  501. m_responding = false;
  502. disconnect();
  503. }
  504. bool has_body = doRequestWithRetry(requester, false, close_conn, connected_time);
  505. m_responding = true;
  506. auto res = new HTTPClientResponse(this, close_conn);
  507. res.initialize(has_body, () @trusted { return vibeThreadAllocator(); } (), connected_time);
  508. // proxy implementation
  509. if (res.headers.get("Proxy-Authenticate", null) !is null) {
  510. doProxyRequest(res, requester, close_conn, has_body);
  511. }
  512. return res;
  513. }
  514. private bool doRequestWithRetry(scope void delegate(HTTPClientRequest req) requester, bool confirmed_proxy_auth /* basic only */, out bool close_conn, out SysTime connected_time)
  515. {
  516. if (m_conn && m_conn.connected && Clock.currTime(UTC()) > m_keepAliveLimit){
  517. logDebug("Disconnected to avoid timeout");
  518. disconnect();
  519. }
  520. // check if this isn't the first request on a connection
  521. bool is_persistent_request = m_conn && m_conn.connected;
  522. // retry the request if the connection gets closed prematurely and this is a persistent request
  523. bool has_body;
  524. foreach (i; 0 .. is_persistent_request ? 2 : 1) {
  525. connected_time = Clock.currTime(UTC());
  526. close_conn = false;
  527. has_body = doRequest(requester, close_conn, false, connected_time);
  528. logTrace("HTTP client waiting for response");
  529. if (!m_stream.empty) break;
  530. }
  531. return has_body;
  532. }
  533. private bool doRequest(scope void delegate(HTTPClientRequest req) requester, ref bool close_conn, bool confirmed_proxy_auth = false /* basic only */, SysTime connected_time = Clock.currTime(UTC()))
  534. {
  535. assert(!m_requesting, "Interleaved HTTP client requests detected!");
  536. assert(!m_responding, "Interleaved HTTP client request/response detected!");
  537. m_requesting = true;
  538. scope(exit) m_requesting = false;
  539. if (!m_conn || !m_conn.connected || m_conn.waitForDataEx(0.seconds) == WaitForDataStatus.noMoreData) {
  540. if (m_conn)
  541. disconnect(); // make sure all resources are freed
  542. if (m_settings.proxyURL.host !is null){
  543. enum AddressType {
  544. IPv4,
  545. IPv6,
  546. Host
  547. }
  548. static AddressType getAddressType(string host){
  549. import std.regex : regex, Captures, Regex, matchFirst;
  550. static IPv4Regex = regex(`^\s*((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))\s*$`, ``);
  551. static IPv6Regex = regex(`^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$`, ``);
  552. if (!matchFirst(host, IPv4Regex).empty)
  553. {
  554. return AddressType.IPv4;
  555. }
  556. else if (!matchFirst(host, IPv6Regex).empty)
  557. {
  558. return AddressType.IPv6;
  559. }
  560. else
  561. {
  562. return AddressType.Host;
  563. }
  564. }
  565. import std.functional : memoize;
  566. alias findAddressType = memoize!getAddressType;
  567. bool use_dns;
  568. if (() @trusted { return findAddressType(m_settings.proxyURL.host); } () == AddressType.Host)
  569. {
  570. use_dns = true;
  571. }
  572. NetworkAddress proxyAddr = resolveHost(m_settings.proxyURL.host, m_settings.dnsAddressFamily, use_dns,
  573. m_settings.connectTimeout);
  574. proxyAddr.port = m_settings.proxyURL.port;
  575. m_conn = connectTCPWithTimeout(proxyAddr, m_settings.networkInterface, m_settings.connectTimeout);
  576. }
  577. else {
  578. version(UnixSocket)
  579. {
  580. import core.sys.posix.sys.un;
  581. import core.sys.posix.sys.socket;
  582. import std.regex : regex, Captures, Regex, matchFirst, ctRegex;
  583. import core.stdc.string : strcpy;
  584. NetworkAddress addr;
  585. if (m_server[0] == '/')
  586. {
  587. addr.family = AF_UNIX;
  588. sockaddr_un* s = addr.sockAddrUnix();
  589. enforce(s.sun_path.length > m_server.length, "Unix sockets cannot have that long a name.");
  590. s.sun_family = AF_UNIX;
  591. () @trusted { strcpy(cast(char*)s.sun_path.ptr,m_server.toStringz()); } ();
  592. } else
  593. {
  594. addr = resolveHost(m_server, m_settings.dnsAddressFamily, true, m_settings.connectTimeout);
  595. addr.port = m_port;
  596. }
  597. m_conn = connectTCPWithTimeout(addr, m_settings.networkInterface, m_settings.connectTimeout);
  598. } else
  599. {
  600. auto addr = resolveHost(m_server, m_settings.dnsAddressFamily, true, m_settings.connectTimeout);
  601. addr.port = m_port;
  602. m_conn = connectTCPWithTimeout(addr, m_settings.networkInterface, m_settings.connectTimeout);
  603. }
  604. }
  605. if (m_settings.readTimeout != Duration.max)
  606. m_conn.readTimeout = m_settings.readTimeout;
  607. m_stream = m_conn;
  608. if (m_useTLS) {
  609. try m_tlsStream = createTLSStream(m_conn, m_tls, TLSStreamState.connecting, m_tlsPeerName, m_conn.remoteAddress);
  610. catch (Exception e) {
  611. m_conn.close();
  612. m_conn = TCPConnection.init;
  613. throw e;
  614. }
  615. m_stream = m_tlsStream;
  616. }
  617. }
  618. return () @trusted { // scoped
  619. auto req = scoped!HTTPClientRequest(m_stream, m_conn);
  620. if (m_useTLS)
  621. req.m_peerCertificate = m_tlsStream.peerCertificate;
  622. req.headers["User-Agent"] = m_userAgent;
  623. if (m_settings.proxyURL.host !is null){
  624. req.headers["Proxy-Connection"] = "keep-alive";
  625. if (confirmed_proxy_auth)
  626. {
  627. import std.base64;
  628. ubyte[] user_pass = cast(ubyte[])(m_settings.proxyURL.username ~ ":" ~ m_settings.proxyURL.password);
  629. req.headers["Proxy-Authorization"] = "Basic " ~ cast(string) Base64.encode(user_pass);
  630. }
  631. }
  632. else {
  633. req.headers["Connection"] = "keep-alive";
  634. }
  635. req.headers["Accept-Encoding"] = "gzip, deflate";
  636. req.headers["Host"] = m_server;
  637. requester(req);
  638. if (req.httpVersion == HTTPVersion.HTTP_1_0)
  639. close_conn = true;
  640. else if (m_settings.proxyURL.host !is null)
  641. close_conn = req.headers.get("Proxy-Connection", "keep-alive") != "keep-alive";
  642. else
  643. close_conn = req.headers.get("Connection", "keep-alive") != "keep-alive";
  644. req.finalize();
  645. return req.method != HTTPMethod.HEAD;
  646. } ();
  647. }
  648. }
  649. private auto connectTCPWithTimeout(NetworkAddress addr, NetworkAddress bind_address, Duration timeout)
  650. {
  651. auto ret = connectTCP(addr, bind_address, timeout);
  652. // Avoid additional latency in the request-response cycle
  653. ret.tcpNoDelay = true;
  654. return ret;
  655. }
  656. /**
  657. Represents a HTTP client request (as sent to the server).
  658. */
  659. final class HTTPClientRequest : HTTPRequest {
  660. import vibe.internal.array : FixedAppender;
  661. private {
  662. InterfaceProxy!OutputStream m_bodyWriter;
  663. FreeListRef!ChunkedOutputStream m_chunkedStream;
  664. bool m_headerWritten = false;
  665. FixedAppender!(string, 22) m_contentLengthBuffer;
  666. TCPConnection m_rawConn;
  667. TLSCertificateInformation m_peerCertificate;
  668. }
  669. /// private
  670. this(InterfaceProxy!Stream conn, TCPConnection raw_conn)
  671. {
  672. super(conn);
  673. m_rawConn = raw_conn;
  674. }
  675. @property NetworkAddress localAddress() const { return m_rawConn.localAddress; }
  676. @property NetworkAddress remoteAddress() const { return m_rawConn.remoteAddress; }
  677. @property ref inout(TLSCertificateInformation) peerCertificate() inout { return m_peerCertificate; }
  678. /**
  679. Accesses the Content-Length header of the request.
  680. Negative values correspond to an unset Content-Length header.
  681. */
  682. @property long contentLength() const { return headers.get("Content-Length", "-1").to!long(); }
  683. /// ditto
  684. @property void contentLength(long value)
  685. {
  686. if (value >= 0) headers["Content-Length"] = clengthString(value);
  687. else if ("Content-Length" in headers) headers.remove("Content-Length");
  688. }
  689. /**
  690. Writes the whole request body at once using raw bytes.
  691. */
  692. void writeBody(RandomAccessStream data)
  693. {
  694. writeBody(data, data.size - data.tell());
  695. }
  696. /// ditto
  697. void writeBody(InputStream data)
  698. {
  699. data.pipe(bodyWriter);
  700. finalize();
  701. }
  702. /// ditto
  703. void writeBody(InputStream data, ulong length)
  704. {
  705. headers["Content-Length"] = clengthString(length);
  706. data.pipe(bodyWriter, length);
  707. finalize();
  708. }
  709. /// ditto
  710. void writeBody(in ubyte[] data, string content_type = null)
  711. {
  712. if( content_type != "" ) headers["Content-Type"] = content_type;
  713. headers["Content-Length"] = clengthString(data.length);
  714. bodyWriter.write(data);
  715. finalize();
  716. }
  717. /**
  718. Writes the request body as JSON data.
  719. */
  720. void writeJsonBody(T)(T data, bool allow_chunked = false)
  721. {
  722. import vibe.stream.wrapper : streamOutputRange;
  723. headers["Content-Type"] = "application/json; charset=UTF-8";
  724. // set an explicit content-length field if chunked encoding is not allowed
  725. if (!allow_chunked) {
  726. import vibe.internal.rangeutil;
  727. long length = 0;
  728. auto counter = () @trusted { return RangeCounter(&length); } ();
  729. () @trusted { serializeToJson(counter, data); } ();
  730. headers["Content-Length"] = clengthString(length);
  731. }
  732. auto rng = streamOutputRange!1024(bodyWriter);
  733. () @trusted { serializeToJson(&rng, data); } ();
  734. rng.flush();
  735. finalize();
  736. }
  737. /** Writes the request body as form data.
  738. */
  739. void writeFormBody(T)(T key_value_map)
  740. {
  741. import vibe.inet.webform : formEncode;
  742. import vibe.stream.wrapper : streamOutputRange;
  743. import vibe.internal.rangeutil;
  744. long length = 0;
  745. auto counter = () @trusted { return RangeCounter(&length); } ();
  746. counter.formEncode(key_value_map);
  747. headers["Content-Length"] = clengthString(length);
  748. headers["Content-Type"] = "application/x-www-form-urlencoded";
  749. auto dst = streamOutputRange!1024(bodyWriter);
  750. () @trusted { return &dst; } ().formEncode(key_value_map);
  751. }
  752. ///
  753. unittest {
  754. void test(HTTPClientRequest req) {
  755. req.writeFormBody(["foo": "bar"]);
  756. }
  757. }
  758. void writePart(MultiPart part)
  759. {
  760. assert(false, "TODO");
  761. }
  762. /**
  763. An output stream suitable for writing the request body.
  764. The first retrieval will cause the request header to be written, make sure
  765. that all headers are set up in advance.s
  766. */
  767. @property InterfaceProxy!OutputStream bodyWriter()
  768. {
  769. if (m_bodyWriter) return m_bodyWriter;
  770. assert(!m_headerWritten, "Trying to write request body after body was already written.");
  771. if (httpVersion != HTTPVersion.HTTP_1_0
  772. && "Content-Length" !in headers && "Transfer-Encoding" !in headers
  773. && headers.get("Connection", "") != "close")
  774. {
  775. headers["Transfer-Encoding"] = "chunked";
  776. }
  777. writeHeader();
  778. m_bodyWriter = m_conn;
  779. if (headers.get("Transfer-Encoding", null) == "chunked") {
  780. m_chunkedStream = createChunkedOutputStreamFL(m_bodyWriter);
  781. m_bodyWriter = m_chunkedStream;
  782. }
  783. return m_bodyWriter;
  784. }
  785. private void writeHeader()
  786. {
  787. import vibe.stream.wrapper;
  788. assert(!m_headerWritten, "HTTPClient tried to write headers twice.");
  789. m_headerWritten = true;
  790. auto output = streamOutputRange!1024(m_conn);
  791. formattedWrite(() @trusted { return &output; } (), "%s %s %s\r\n", httpMethodString(method), requestURL, getHTTPVersionString(httpVersion));
  792. logTrace("--------------------");
  793. logTrace("HTTP client request:");
  794. logTrace("--------------------");
  795. logTrace("%s", this);
  796. foreach (k, v; headers.byKeyValue) {
  797. () @trusted { formattedWrite(&output, "%s: %s\r\n", k, v); } ();
  798. logTrace("%s: %s", k, v);
  799. }
  800. output.put("\r\n");
  801. logTrace("--------------------");
  802. }
  803. private void finalize()
  804. {
  805. // test if already finalized
  806. if (m_headerWritten && !m_bodyWriter)
  807. return;
  808. // force the request to be sent
  809. if (!m_headerWritten) writeHeader();
  810. else {
  811. bodyWriter.flush();
  812. if (m_chunkedStream) {
  813. m_bodyWriter.finalize();
  814. m_conn.flush();
  815. }
  816. m_bodyWriter = typeof(m_bodyWriter).init;
  817. m_conn = typeof(m_conn).init;
  818. }
  819. }
  820. private string clengthString(ulong len)
  821. {
  822. m_contentLengthBuffer.clear();
  823. () @trusted { formattedWrite(&m_contentLengthBuffer, "%s", len); } ();
  824. return () @trusted { return m_contentLengthBuffer.data; } ();
  825. }
  826. }
  827. /**
  828. Represents a HTTP client response (as received from the server).
  829. */
  830. final class HTTPClientResponse : HTTPResponse {
  831. @safe:
  832. private {
  833. HTTPClient m_client;
  834. LockedConnection!HTTPClient lockedConnection;
  835. FreeListRef!LimitedInputStream m_limitedInputStream;
  836. FreeListRef!ChunkedInputStream m_chunkedInputStream;
  837. FreeListRef!ZlibInputStream m_zlibInputStream;
  838. FreeListRef!EndCallbackInputStream m_endCallback;
  839. InterfaceProxy!InputStream m_bodyReader;
  840. bool m_closeConn;
  841. int m_maxRequests;
  842. }
  843. /// Contains the keep-alive 'max' parameter, indicates how many requests a client can
  844. /// make before the server closes the connection.
  845. @property int maxRequests() const {
  846. return m_maxRequests;
  847. }
  848. /// All cookies that shall be set on the client for this request
  849. override @property ref DictionaryList!Cookie cookies() {
  850. if ("Set-Cookie" in this.headers && m_cookies.length == 0) {
  851. foreach (cookieString; this.headers.getAll("Set-Cookie")) {
  852. auto cookie = parseHTTPCookie(cookieString);
  853. if (cookie[0].length)
  854. m_cookies[cookie[0]] = cookie[1];
  855. }
  856. }
  857. return m_cookies;
  858. }
  859. /// private
  860. this(HTTPClient client, bool close_conn)
  861. nothrow {
  862. m_client = client;
  863. m_closeConn = close_conn;
  864. }
  865. private void initialize(Allocator)(bool has_body, Allocator alloc, SysTime connected_time = Clock.currTime(UTC()))
  866. {
  867. scope(failure) finalize(true);
  868. // read and parse status line ("HTTP/#.# #[ $]\r\n")
  869. logTrace("HTTP client reading status line");
  870. string stln = () @trusted { return cast(string)m_client.m_stream.readLine(HTTPClient.maxHeaderLineLength, "\r\n", alloc); } ();
  871. logTrace("stln: %s", stln);
  872. this.httpVersion = parseHTTPVersion(stln);
  873. enforce(stln.startsWith(" "));
  874. stln = stln[1 .. $];
  875. this.statusCode = parse!int(stln);
  876. if( stln.length > 0 ){
  877. enforce(stln.startsWith(" "));
  878. stln = stln[1 .. $];
  879. this.statusPhrase = stln;
  880. }
  881. // read headers until an empty line is hit
  882. parseRFC5322Header(m_client.m_stream, this.headers, HTTPClient.maxHeaderLineLength, alloc, false);
  883. logTrace("---------------------");
  884. logTrace("HTTP client response:");
  885. logTrace("---------------------");
  886. logTrace("%s", this);
  887. foreach (k, v; this.headers.byKeyValue)
  888. logTrace("%s: %s", k, v);
  889. logTrace("---------------------");
  890. Duration server_timeout;
  891. bool has_server_timeout;
  892. if (auto pka = "Keep-Alive" in this.headers) {
  893. foreach(s; splitter(*pka, ',')){
  894. auto pair = s.splitter('=');
  895. auto name = pair.front.strip();
  896. pair.popFront();
  897. if (icmp(name, "timeout") == 0) {
  898. has_server_timeout = true;
  899. server_timeout = pair.front.to!int().seconds;
  900. } else if (icmp(name, "max") == 0) {
  901. m_maxRequests = pair.front.to!int();
  902. }
  903. }
  904. }
  905. Duration elapsed = Clock.currTime(UTC()) - connected_time;
  906. if (this.headers.get("Connection") == "close") {
  907. // this header will trigger m_client.disconnect() in m_client.doRequest() when it goes out of scope
  908. } else if (has_server_timeout && m_client.m_keepAliveTimeout > server_timeout) {
  909. m_client.m_keepAliveLimit = Clock.currTime(UTC()) + server_timeout - elapsed;
  910. } else if (this.httpVersion == HTTPVersion.HTTP_1_1) {
  911. m_client.m_keepAliveLimit = Clock.currTime(UTC()) + m_client.m_keepAliveTimeout;
  912. }
  913. if (!has_body) finalize();
  914. }
  915. ~this()
  916. {
  917. debug if (m_client) {
  918. import core.stdc.stdio;
  919. printf("WARNING: HTTPClientResponse not fully processed before being finalized\n");
  920. }
  921. }
  922. /**
  923. An input stream suitable for reading the response body.
  924. */
  925. @property InterfaceProxy!InputStream bodyReader()
  926. {
  927. if( m_bodyReader ) return m_bodyReader;
  928. assert (m_client, "Response was already read or no response body, may not use bodyReader.");
  929. // prepare body the reader
  930. if (auto pte = "Transfer-Encoding" in this.headers) {
  931. enforce(*pte == "chunked");
  932. m_chunkedInputStream = createChunkedInputStreamFL(m_client.m_stream);
  933. m_bodyReader = this.m_chunkedInputStream;
  934. } else if (auto pcl = "Content-Length" in this.headers) {
  935. m_limitedInputStream = createLimitedInputStreamFL(m_client.m_stream, to!ulong(*pcl));
  936. m_bodyReader = m_limitedInputStream;
  937. } else if (isKeepAliveResponse) {
  938. m_limitedInputStream = createLimitedInputStreamFL(m_client.m_stream, 0);
  939. m_bodyReader = m_limitedInputStream;
  940. } else {
  941. m_bodyReader = m_client.m_stream;
  942. }
  943. if( auto pce = "Content-Encoding" in this.headers ){
  944. if( *pce == "deflate" ){
  945. m_zlibInputStream = createDeflateInputStreamFL(m_bodyReader);
  946. m_bodyReader = m_zlibInputStream;
  947. } else if( *pce == "gzip" || *pce == "x-gzip"){
  948. m_zlibInputStream = createGzipInputStreamFL(m_bodyReader);
  949. m_bodyReader = m_zlibInputStream;
  950. }
  951. else enforce(*pce == "identity" || *pce == "", "Unsuported content encoding: "~*pce);
  952. }
  953. // be sure to free resouces as soon as the response has been read
  954. m_endCallback = createEndCallbackInputStreamFL(m_bodyReader, &this.finalize);
  955. m_bodyReader = m_endCallback;
  956. return m_bodyReader;
  957. }
  958. /**
  959. Provides unsafe means to read raw data from the connection.
  960. No transfer decoding and no content decoding is done on the data.
  961. Not that the provided delegate must read the whole stream,
  962. as the state of the response is unknown after raw bytes have been
  963. taken. Failure to read the right amount of data will lead to
  964. protocol corruption in later requests.
  965. */
  966. void readRawBody(scope void delegate(scope InterfaceProxy!InputStream stream) @safe del)
  967. {
  968. assert(!m_bodyReader, "May not mix use of readRawBody and bodyReader.");
  969. del(interfaceProxy!InputStream(m_client.m_stream));
  970. finalize();
  971. }
  972. /// ditto
  973. static if (!is(InputStream == InterfaceProxy!InputStream))
  974. void readRawBody(scope void delegate(scope InputStream stream) @safe del)
  975. {
  976. import vibe.internal.interfaceproxy : asInterface;
  977. assert(!m_bodyReader, "May not mix use of readRawBody and bodyReader.");
  978. del(m_client.m_stream.asInterface!(.InputStream));
  979. finalize();
  980. }
  981. /**
  982. Reads the whole response body and tries to parse it as JSON.
  983. */
  984. Json readJson(){
  985. auto bdy = bodyReader.readAllUTF8();
  986. return () @trusted { return parseJson(bdy); } ();
  987. }
  988. /**
  989. Reads and discards the response body.
  990. */
  991. void dropBody()
  992. {
  993. if (m_client) {
  994. if( bodyReader.empty ){
  995. finalize();
  996. } else {
  997. bodyReader.pipe(nullSink);
  998. assert(!lockedConnection.__conn);
  999. }
  1000. }
  1001. }
  1002. /**
  1003. Forcefully terminates the connection regardless of the current state.
  1004. Note that this will only actually disconnect if the request has not yet
  1005. been fully processed. If the whole body was already read, the
  1006. connection is not owned by the current request operation anymore and
  1007. cannot be accessed. Use a "Connection: close" header instead in this
  1008. case to let the server close the connection.
  1009. */
  1010. void disconnect()
  1011. {
  1012. finalize(true);
  1013. }
  1014. /**
  1015. Switches the connection to a new protocol and returns the resulting ConnectionStream.
  1016. The caller caller gets ownership of the ConnectionStream and is responsible
  1017. for closing it.
  1018. Notice:
  1019. When using the overload that returns a `ConnectionStream`, the caller
  1020. must make sure that the stream is not used after the
  1021. `HTTPClientRequest` has been destroyed.
  1022. Params:
  1023. new_protocol = The protocol to which the connection is expected to
  1024. upgrade. Should match the Upgrade header of the request. If an
  1025. empty string is passed, the "Upgrade" header will be ignored and
  1026. should be checked by other means.
  1027. */
  1028. ConnectionStream switchProtocol(string new_protocol)
  1029. {
  1030. enforce(statusCode == HTTPStatus.switchingProtocols, "Server did not send a 101 - Switching Protocols response");
  1031. string *resNewProto = "Upgrade" in headers;
  1032. enforce(resNewProto, "Server did not send an Upgrade header");
  1033. enforce(!new_protocol.length || !icmp(*resNewProto, new_protocol),
  1034. "Expected Upgrade: " ~ new_protocol ~", received Upgrade: " ~ *resNewProto);
  1035. auto stream = createConnectionProxyStream!(typeof(m_client.m_stream), typeof(m_client.m_conn))(m_client.m_stream, m_client.m_conn);
  1036. m_closeConn = true; // cannot reuse connection for further requests!
  1037. return stream;
  1038. }
  1039. /// ditto
  1040. void switchProtocol(string new_protocol, scope void delegate(ConnectionStream str) @safe del)
  1041. {
  1042. enforce(statusCode == HTTPStatus.switchingProtocols, "Server did not send a 101 - Switching Protocols response");
  1043. string *resNewProto = "Upgrade" in headers;
  1044. enforce(resNewProto, "Server did not send an Upgrade header");
  1045. enforce(!new_protocol.length || !icmp(*resNewProto, new_protocol),
  1046. "Expected Upgrade: " ~ new_protocol ~", received Upgrade: " ~ *resNewProto);
  1047. auto stream = createConnectionProxyStream(m_client.m_stream, m_client.m_conn);
  1048. scope (exit) () @trusted { destroy(stream); } ();
  1049. m_closeConn = true;
  1050. del(stream);
  1051. }
  1052. private @property isKeepAliveResponse()
  1053. const {
  1054. string conn;
  1055. if (this.httpVersion == HTTPVersion.HTTP_1_0) {
  1056. // Workaround for non-standard-conformant servers - for example see #1780
  1057. auto pcl = "Content-Length" in this.headers;
  1058. if (pcl) conn = this.headers.get("Connection", "close");
  1059. else return false; // can't use keepalive when no content length is set
  1060. }
  1061. else conn = this.headers.get("Connection", "keep-alive");
  1062. return icmp(conn, "close") != 0;
  1063. }
  1064. private void finalize()
  1065. {
  1066. finalize(m_closeConn);
  1067. }
  1068. private void finalize(bool disconnect)
  1069. {
  1070. // ignore duplicate and too early calls to finalize
  1071. // (too early happesn for empty response bodies)
  1072. if (!m_client) return;
  1073. auto cli = m_client;
  1074. m_client = null;
  1075. cli.m_responding = false;
  1076. destroy(m_bodyReader);
  1077. destroy(m_endCallback);
  1078. destroy(m_zlibInputStream);
  1079. destroy(m_chunkedInputStream);
  1080. destroy(m_limitedInputStream);
  1081. if (disconnect) cli.disconnect();
  1082. destroy(lockedConnection);
  1083. }
  1084. }
  1085. /** Returns clean host string. In case of unix socket it performs urlDecode on host. */
  1086. package auto getFilteredHost(URL url)
  1087. {
  1088. version(UnixSocket)
  1089. {
  1090. import vibe.textfilter.urlencode : urlDecode;
  1091. if (url.schema == "https+unix" || url.schema == "http+unix")
  1092. return urlDecode(url.host);
  1093. else
  1094. return url.host;
  1095. } else
  1096. return url.host;
  1097. }
  1098. // This object is a placeholder and should to never be modified.
  1099. package @property const(HTTPClientSettings) defaultSettings()
  1100. @trusted nothrow {
  1101. __gshared HTTPClientSettings ret = new HTTPClientSettings;
  1102. return ret;
  1103. }