c-hyper.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. /* Curl's integration with Hyper. This replaces certain functions in http.c,
  25. * based on configuration #defines. This implementation supports HTTP/1.1 but
  26. * not HTTP/2.
  27. */
  28. #include "curl_setup.h"
  29. #if !defined(CURL_DISABLE_HTTP) && defined(USE_HYPER)
  30. #ifdef HAVE_NETINET_IN_H
  31. #include <netinet/in.h>
  32. #endif
  33. #ifdef HAVE_NETDB_H
  34. #include <netdb.h>
  35. #endif
  36. #ifdef HAVE_ARPA_INET_H
  37. #include <arpa/inet.h>
  38. #endif
  39. #ifdef HAVE_NET_IF_H
  40. #include <net/if.h>
  41. #endif
  42. #ifdef HAVE_SYS_IOCTL_H
  43. #include <sys/ioctl.h>
  44. #endif
  45. #ifdef HAVE_SYS_PARAM_H
  46. #include <sys/param.h>
  47. #endif
  48. #include <hyper.h>
  49. #include "urldata.h"
  50. #include "cfilters.h"
  51. #include "sendf.h"
  52. #include "headers.h"
  53. #include "transfer.h"
  54. #include "multiif.h"
  55. #include "progress.h"
  56. #include "content_encoding.h"
  57. #include "ws.h"
  58. /* The last 3 #include files should be in this order */
  59. #include "curl_printf.h"
  60. #include "curl_memory.h"
  61. #include "memdebug.h"
  62. static CURLcode cr_hyper_add(struct Curl_easy *data);
  63. typedef enum {
  64. USERDATA_NOT_SET = 0, /* for tasks with no userdata set; must be zero */
  65. USERDATA_RESP_BODY
  66. } userdata_t;
  67. size_t Curl_hyper_recv(void *userp, hyper_context *ctx,
  68. uint8_t *buf, size_t buflen)
  69. {
  70. struct hyp_io_ctx *io_ctx = userp;
  71. struct Curl_easy *data = io_ctx->data;
  72. struct connectdata *conn = data->conn;
  73. CURLcode result;
  74. ssize_t nread;
  75. DEBUGASSERT(conn);
  76. (void)ctx;
  77. DEBUGF(infof(data, "Curl_hyper_recv(%zu)", buflen));
  78. result = Curl_conn_recv(data, io_ctx->sockindex,
  79. (char *)buf, buflen, &nread);
  80. if(result == CURLE_AGAIN) {
  81. /* would block, register interest */
  82. DEBUGF(infof(data, "Curl_hyper_recv(%zu) -> EAGAIN", buflen));
  83. if(data->hyp.read_waker)
  84. hyper_waker_free(data->hyp.read_waker);
  85. data->hyp.read_waker = hyper_context_waker(ctx);
  86. if(!data->hyp.read_waker) {
  87. failf(data, "Couldn't make the read hyper_context_waker");
  88. return HYPER_IO_ERROR;
  89. }
  90. return HYPER_IO_PENDING;
  91. }
  92. else if(result) {
  93. failf(data, "Curl_read failed");
  94. return HYPER_IO_ERROR;
  95. }
  96. DEBUGF(infof(data, "Curl_hyper_recv(%zu) -> %zd", buflen, nread));
  97. return (size_t)nread;
  98. }
  99. size_t Curl_hyper_send(void *userp, hyper_context *ctx,
  100. const uint8_t *buf, size_t buflen)
  101. {
  102. struct hyp_io_ctx *io_ctx = userp;
  103. struct Curl_easy *data = io_ctx->data;
  104. CURLcode result;
  105. size_t nwrote;
  106. DEBUGF(infof(data, "Curl_hyper_send(%zu)", buflen));
  107. result = Curl_conn_send(data, io_ctx->sockindex,
  108. (void *)buf, buflen, &nwrote);
  109. if(result == CURLE_AGAIN) {
  110. DEBUGF(infof(data, "Curl_hyper_send(%zu) -> EAGAIN", buflen));
  111. /* would block, register interest */
  112. if(data->hyp.write_waker)
  113. hyper_waker_free(data->hyp.write_waker);
  114. data->hyp.write_waker = hyper_context_waker(ctx);
  115. if(!data->hyp.write_waker) {
  116. failf(data, "Couldn't make the write hyper_context_waker");
  117. return HYPER_IO_ERROR;
  118. }
  119. return HYPER_IO_PENDING;
  120. }
  121. else if(result) {
  122. failf(data, "Curl_write failed");
  123. return HYPER_IO_ERROR;
  124. }
  125. DEBUGF(infof(data, "Curl_hyper_send(%zu) -> %zd", buflen, nwrote));
  126. return (size_t)nwrote;
  127. }
  128. static int hyper_each_header(void *userdata,
  129. const uint8_t *name,
  130. size_t name_len,
  131. const uint8_t *value,
  132. size_t value_len)
  133. {
  134. struct Curl_easy *data = (struct Curl_easy *)userdata;
  135. size_t len;
  136. char *headp;
  137. CURLcode result;
  138. int writetype;
  139. if(name_len + value_len + 2 > CURL_MAX_HTTP_HEADER) {
  140. failf(data, "Too long response header");
  141. data->state.hresult = CURLE_TOO_LARGE;
  142. return HYPER_ITER_BREAK;
  143. }
  144. Curl_dyn_reset(&data->state.headerb);
  145. if(name_len) {
  146. if(Curl_dyn_addf(&data->state.headerb, "%.*s: %.*s\r\n",
  147. (int) name_len, name, (int) value_len, value))
  148. return HYPER_ITER_BREAK;
  149. }
  150. else {
  151. if(Curl_dyn_addn(&data->state.headerb, STRCONST("\r\n")))
  152. return HYPER_ITER_BREAK;
  153. }
  154. len = Curl_dyn_len(&data->state.headerb);
  155. headp = Curl_dyn_ptr(&data->state.headerb);
  156. result = Curl_http_header(data, headp, len);
  157. if(result) {
  158. data->state.hresult = result;
  159. return HYPER_ITER_BREAK;
  160. }
  161. Curl_debug(data, CURLINFO_HEADER_IN, headp, len);
  162. writetype = CLIENTWRITE_HEADER;
  163. if(data->state.hconnect)
  164. writetype |= CLIENTWRITE_CONNECT;
  165. if(data->req.httpcode/100 == 1)
  166. writetype |= CLIENTWRITE_1XX;
  167. result = Curl_client_write(data, writetype, headp, len);
  168. if(result) {
  169. data->state.hresult = CURLE_ABORTED_BY_CALLBACK;
  170. return HYPER_ITER_BREAK;
  171. }
  172. result = Curl_bump_headersize(data, len, FALSE);
  173. if(result) {
  174. data->state.hresult = result;
  175. return HYPER_ITER_BREAK;
  176. }
  177. return HYPER_ITER_CONTINUE;
  178. }
  179. static int hyper_body_chunk(void *userdata, const hyper_buf *chunk)
  180. {
  181. char *buf = (char *)hyper_buf_bytes(chunk);
  182. size_t len = hyper_buf_len(chunk);
  183. struct Curl_easy *data = (struct Curl_easy *)userdata;
  184. struct SingleRequest *k = &data->req;
  185. CURLcode result = CURLE_OK;
  186. if(!k->bodywritten) {
  187. #if defined(USE_NTLM)
  188. struct connectdata *conn = data->conn;
  189. if(conn->bits.close &&
  190. (((data->req.httpcode == 401) &&
  191. (conn->http_ntlm_state == NTLMSTATE_TYPE2)) ||
  192. ((data->req.httpcode == 407) &&
  193. (conn->proxy_ntlm_state == NTLMSTATE_TYPE2)))) {
  194. infof(data, "Connection closed while negotiating NTLM");
  195. data->state.authproblem = TRUE;
  196. Curl_safefree(data->req.newurl);
  197. }
  198. #endif
  199. if(Curl_http_exp100_is_selected(data)) {
  200. if(data->req.httpcode < 400) {
  201. Curl_http_exp100_got100(data);
  202. if(data->hyp.send_body_waker) {
  203. hyper_waker_wake(data->hyp.send_body_waker);
  204. data->hyp.send_body_waker = NULL;
  205. }
  206. }
  207. else { /* >= 4xx */
  208. Curl_req_abort_sending(data);
  209. }
  210. }
  211. if(data->state.hconnect && (data->req.httpcode/100 != 2) &&
  212. data->state.authproxy.done) {
  213. data->req.done = TRUE;
  214. result = CURLE_OK;
  215. }
  216. else
  217. result = Curl_http_firstwrite(data);
  218. if(result || data->req.done) {
  219. infof(data, "Return early from hyper_body_chunk");
  220. data->state.hresult = result;
  221. return HYPER_ITER_BREAK;
  222. }
  223. }
  224. result = Curl_client_write(data, CLIENTWRITE_BODY, buf, len);
  225. if(result) {
  226. data->state.hresult = result;
  227. return HYPER_ITER_BREAK;
  228. }
  229. return HYPER_ITER_CONTINUE;
  230. }
  231. /*
  232. * Hyper does not consider the status line, the first line in an HTTP/1
  233. * response, to be a header. The libcurl API does. This function sends the
  234. * status line in the header callback. */
  235. static CURLcode status_line(struct Curl_easy *data,
  236. struct connectdata *conn,
  237. uint16_t http_status,
  238. int http_version,
  239. const uint8_t *reason, size_t rlen)
  240. {
  241. CURLcode result;
  242. size_t len;
  243. const char *vstr;
  244. int writetype;
  245. vstr = http_version == HYPER_HTTP_VERSION_1_1 ? "1.1" :
  246. (http_version == HYPER_HTTP_VERSION_2 ? "2" : "1.0");
  247. /* We need to set 'httpcodeq' for functions that check the response code in
  248. a single place. */
  249. data->req.httpcode = http_status;
  250. data->req.httpversion = http_version == HYPER_HTTP_VERSION_1_1? 11 :
  251. (http_version == HYPER_HTTP_VERSION_2 ? 20 : 10);
  252. if(data->state.hconnect)
  253. /* CONNECT */
  254. data->info.httpproxycode = http_status;
  255. else {
  256. conn->httpversion = (unsigned char)data->req.httpversion;
  257. if(http_version == HYPER_HTTP_VERSION_1_0)
  258. data->state.httpwant = CURL_HTTP_VERSION_1_0;
  259. result = Curl_http_statusline(data, conn);
  260. if(result)
  261. return result;
  262. }
  263. Curl_dyn_reset(&data->state.headerb);
  264. result = Curl_dyn_addf(&data->state.headerb, "HTTP/%s %03d %.*s\r\n",
  265. vstr,
  266. (int)http_status,
  267. (int)rlen, reason);
  268. if(result)
  269. return result;
  270. len = Curl_dyn_len(&data->state.headerb);
  271. Curl_debug(data, CURLINFO_HEADER_IN, Curl_dyn_ptr(&data->state.headerb),
  272. len);
  273. writetype = CLIENTWRITE_HEADER|CLIENTWRITE_STATUS;
  274. if(data->state.hconnect)
  275. writetype |= CLIENTWRITE_CONNECT;
  276. result = Curl_client_write(data, writetype,
  277. Curl_dyn_ptr(&data->state.headerb), len);
  278. if(result)
  279. return result;
  280. result = Curl_bump_headersize(data, len, FALSE);
  281. return result;
  282. }
  283. /*
  284. * Hyper does not pass on the last empty response header. The libcurl API
  285. * does. This function sends an empty header in the header callback.
  286. */
  287. static CURLcode empty_header(struct Curl_easy *data)
  288. {
  289. CURLcode result = Curl_http_size(data);
  290. if(!result) {
  291. result = hyper_each_header(data, NULL, 0, NULL, 0) ?
  292. CURLE_WRITE_ERROR : CURLE_OK;
  293. if(result)
  294. failf(data, "hyperstream: could not pass blank header");
  295. /* Hyper does chunked decoding itself. If it was added during
  296. * response header processing, remove it again. */
  297. Curl_cwriter_remove_by_name(data, "chunked");
  298. }
  299. return result;
  300. }
  301. CURLcode Curl_hyper_stream(struct Curl_easy *data,
  302. struct connectdata *conn,
  303. int *didwhat,
  304. int select_res)
  305. {
  306. hyper_response *resp = NULL;
  307. uint16_t http_status;
  308. int http_version;
  309. hyper_headers *headers = NULL;
  310. hyper_body *resp_body = NULL;
  311. struct hyptransfer *h = &data->hyp;
  312. hyper_task *task;
  313. hyper_task *foreach;
  314. const uint8_t *reasonp;
  315. size_t reason_len;
  316. CURLcode result = CURLE_OK;
  317. struct SingleRequest *k = &data->req;
  318. (void)conn;
  319. if(data->hyp.send_body_waker) {
  320. hyper_waker_wake(data->hyp.send_body_waker);
  321. data->hyp.send_body_waker = NULL;
  322. }
  323. if(select_res & CURL_CSELECT_IN) {
  324. if(h->read_waker)
  325. hyper_waker_wake(h->read_waker);
  326. h->read_waker = NULL;
  327. }
  328. if(select_res & CURL_CSELECT_OUT) {
  329. if(h->write_waker)
  330. hyper_waker_wake(h->write_waker);
  331. h->write_waker = NULL;
  332. }
  333. do {
  334. hyper_task_return_type t;
  335. task = hyper_executor_poll(h->exec);
  336. if(!task) {
  337. *didwhat = KEEP_RECV;
  338. break;
  339. }
  340. t = hyper_task_type(task);
  341. if(t == HYPER_TASK_ERROR) {
  342. hyper_error *hypererr = hyper_task_value(task);
  343. hyper_task_free(task);
  344. if(data->state.hresult) {
  345. /* override Hyper's view, might not even be an error */
  346. result = data->state.hresult;
  347. infof(data, "hyperstream is done (by early callback)");
  348. }
  349. else {
  350. uint8_t errbuf[256];
  351. size_t errlen = hyper_error_print(hypererr, errbuf, sizeof(errbuf));
  352. hyper_code code = hyper_error_code(hypererr);
  353. failf(data, "Hyper: [%d] %.*s", (int)code, (int)errlen, errbuf);
  354. switch(code) {
  355. case HYPERE_ABORTED_BY_CALLBACK:
  356. result = CURLE_OK;
  357. break;
  358. case HYPERE_UNEXPECTED_EOF:
  359. if(!data->req.bytecount)
  360. result = CURLE_GOT_NOTHING;
  361. else
  362. result = CURLE_RECV_ERROR;
  363. break;
  364. case HYPERE_INVALID_PEER_MESSAGE:
  365. /* bump headerbytecount to avoid the count remaining at zero and
  366. appearing to not having read anything from the peer at all */
  367. data->req.headerbytecount++;
  368. result = CURLE_UNSUPPORTED_PROTOCOL; /* maybe */
  369. break;
  370. default:
  371. result = CURLE_RECV_ERROR;
  372. break;
  373. }
  374. }
  375. data->req.done = TRUE;
  376. hyper_error_free(hypererr);
  377. break;
  378. }
  379. else if(t == HYPER_TASK_EMPTY) {
  380. void *userdata = hyper_task_userdata(task);
  381. hyper_task_free(task);
  382. if((userdata_t)userdata == USERDATA_RESP_BODY) {
  383. /* end of transfer */
  384. data->req.done = TRUE;
  385. infof(data, "hyperstream is done");
  386. if(!k->bodywritten) {
  387. /* hyper does not always call the body write callback */
  388. result = Curl_http_firstwrite(data);
  389. }
  390. break;
  391. }
  392. else {
  393. /* A background task for hyper; ignore */
  394. continue;
  395. }
  396. }
  397. DEBUGASSERT(HYPER_TASK_RESPONSE);
  398. resp = hyper_task_value(task);
  399. hyper_task_free(task);
  400. *didwhat = KEEP_RECV;
  401. if(!resp) {
  402. failf(data, "hyperstream: could not get response");
  403. return CURLE_RECV_ERROR;
  404. }
  405. http_status = hyper_response_status(resp);
  406. http_version = hyper_response_version(resp);
  407. reasonp = hyper_response_reason_phrase(resp);
  408. reason_len = hyper_response_reason_phrase_len(resp);
  409. if(http_status == 417 && Curl_http_exp100_is_selected(data)) {
  410. infof(data, "Got 417 while waiting for a 100");
  411. data->state.disableexpect = TRUE;
  412. data->req.newurl = strdup(data->state.url);
  413. Curl_req_abort_sending(data);
  414. }
  415. result = status_line(data, conn,
  416. http_status, http_version, reasonp, reason_len);
  417. if(result)
  418. break;
  419. headers = hyper_response_headers(resp);
  420. if(!headers) {
  421. failf(data, "hyperstream: could not get response headers");
  422. result = CURLE_RECV_ERROR;
  423. break;
  424. }
  425. /* the headers are already received */
  426. hyper_headers_foreach(headers, hyper_each_header, data);
  427. if(data->state.hresult) {
  428. result = data->state.hresult;
  429. break;
  430. }
  431. result = empty_header(data);
  432. if(result)
  433. break;
  434. k->deductheadercount =
  435. (100 <= http_status && 199 >= http_status)?k->headerbytecount:0;
  436. #ifdef USE_WEBSOCKETS
  437. if(k->upgr101 == UPGR101_WS) {
  438. if(http_status == 101) {
  439. /* verify the response */
  440. result = Curl_ws_accept(data, NULL, 0);
  441. if(result)
  442. return result;
  443. }
  444. else {
  445. failf(data, "Expected 101, got %u", k->httpcode);
  446. result = CURLE_HTTP_RETURNED_ERROR;
  447. break;
  448. }
  449. }
  450. #endif
  451. /* Curl_http_auth_act() checks what authentication methods that are
  452. * available and decides which one (if any) to use. It will set 'newurl'
  453. * if an auth method was picked. */
  454. result = Curl_http_auth_act(data);
  455. if(result)
  456. break;
  457. resp_body = hyper_response_body(resp);
  458. if(!resp_body) {
  459. failf(data, "hyperstream: could not get response body");
  460. result = CURLE_RECV_ERROR;
  461. break;
  462. }
  463. foreach = hyper_body_foreach(resp_body, hyper_body_chunk, data);
  464. if(!foreach) {
  465. failf(data, "hyperstream: body foreach failed");
  466. result = CURLE_OUT_OF_MEMORY;
  467. break;
  468. }
  469. hyper_task_set_userdata(foreach, (void *)USERDATA_RESP_BODY);
  470. if(HYPERE_OK != hyper_executor_push(h->exec, foreach)) {
  471. failf(data, "Couldn't hyper_executor_push the body-foreach");
  472. result = CURLE_OUT_OF_MEMORY;
  473. break;
  474. }
  475. hyper_response_free(resp);
  476. resp = NULL;
  477. } while(1);
  478. if(resp)
  479. hyper_response_free(resp);
  480. return result;
  481. }
  482. static CURLcode debug_request(struct Curl_easy *data,
  483. const char *method,
  484. const char *path)
  485. {
  486. char *req = aprintf("%s %s HTTP/1.1\r\n", method, path);
  487. if(!req)
  488. return CURLE_OUT_OF_MEMORY;
  489. Curl_debug(data, CURLINFO_HEADER_OUT, req, strlen(req));
  490. free(req);
  491. return CURLE_OK;
  492. }
  493. /*
  494. * Given a full header line "name: value" (optional CRLF in the input, should
  495. * be in the output), add to Hyper and send to the debug callback.
  496. *
  497. * Supports multiple headers.
  498. */
  499. CURLcode Curl_hyper_header(struct Curl_easy *data, hyper_headers *headers,
  500. const char *line)
  501. {
  502. const char *p;
  503. const char *n;
  504. size_t nlen;
  505. const char *v;
  506. size_t vlen;
  507. bool newline = TRUE;
  508. int numh = 0;
  509. if(!line)
  510. return CURLE_OK;
  511. n = line;
  512. do {
  513. size_t linelen = 0;
  514. p = strchr(n, ':');
  515. if(!p)
  516. /* this is fine if we already added at least one header */
  517. return numh ? CURLE_OK : CURLE_BAD_FUNCTION_ARGUMENT;
  518. nlen = p - n;
  519. p++; /* move past the colon */
  520. while(*p == ' ')
  521. p++;
  522. v = p;
  523. p = strchr(v, '\r');
  524. if(!p) {
  525. p = strchr(v, '\n');
  526. if(p)
  527. linelen = 1; /* LF only */
  528. else {
  529. p = strchr(v, '\0');
  530. newline = FALSE; /* no newline */
  531. }
  532. }
  533. else
  534. linelen = 2; /* CRLF ending */
  535. linelen += (p - n);
  536. vlen = p - v;
  537. if(HYPERE_OK != hyper_headers_add(headers, (uint8_t *)n, nlen,
  538. (uint8_t *)v, vlen)) {
  539. failf(data, "hyper refused to add header '%s'", line);
  540. return CURLE_OUT_OF_MEMORY;
  541. }
  542. if(data->set.verbose) {
  543. char *ptr = NULL;
  544. if(!newline) {
  545. ptr = aprintf("%.*s\r\n", (int)linelen, line);
  546. if(!ptr)
  547. return CURLE_OUT_OF_MEMORY;
  548. Curl_debug(data, CURLINFO_HEADER_OUT, ptr, linelen + 2);
  549. free(ptr);
  550. }
  551. else
  552. Curl_debug(data, CURLINFO_HEADER_OUT, (char *)n, linelen);
  553. }
  554. numh++;
  555. n += linelen;
  556. } while(newline);
  557. return CURLE_OK;
  558. }
  559. static CURLcode request_target(struct Curl_easy *data,
  560. struct connectdata *conn,
  561. const char *method,
  562. hyper_request *req)
  563. {
  564. CURLcode result;
  565. struct dynbuf r;
  566. Curl_dyn_init(&r, DYN_HTTP_REQUEST);
  567. result = Curl_http_target(data, conn, &r);
  568. if(result)
  569. return result;
  570. if(hyper_request_set_uri(req, (uint8_t *)Curl_dyn_uptr(&r),
  571. Curl_dyn_len(&r))) {
  572. failf(data, "error setting uri to hyper");
  573. result = CURLE_OUT_OF_MEMORY;
  574. }
  575. else
  576. result = debug_request(data, method, Curl_dyn_ptr(&r));
  577. Curl_dyn_free(&r);
  578. return result;
  579. }
  580. static int uploadstreamed(void *userdata, hyper_context *ctx,
  581. hyper_buf **chunk)
  582. {
  583. size_t fillcount;
  584. struct Curl_easy *data = (struct Curl_easy *)userdata;
  585. CURLcode result;
  586. char *xfer_ulbuf;
  587. size_t xfer_ulblen;
  588. bool eos;
  589. int rc = HYPER_POLL_ERROR;
  590. (void)ctx;
  591. result = Curl_multi_xfer_ulbuf_borrow(data, &xfer_ulbuf, &xfer_ulblen);
  592. if(result)
  593. goto out;
  594. result = Curl_client_read(data, xfer_ulbuf, xfer_ulblen, &fillcount, &eos);
  595. if(result)
  596. goto out;
  597. if(fillcount) {
  598. hyper_buf *copy = hyper_buf_copy((uint8_t *)xfer_ulbuf, fillcount);
  599. if(copy)
  600. *chunk = copy;
  601. else {
  602. result = CURLE_OUT_OF_MEMORY;
  603. goto out;
  604. }
  605. /* increasing the writebytecount here is a little premature but we
  606. do not know exactly when the body is sent */
  607. data->req.writebytecount += fillcount;
  608. Curl_pgrsSetUploadCounter(data, data->req.writebytecount);
  609. rc = HYPER_POLL_READY;
  610. }
  611. else if(eos) {
  612. *chunk = NULL;
  613. rc = HYPER_POLL_READY;
  614. }
  615. else {
  616. /* paused, save a waker */
  617. if(data->hyp.send_body_waker)
  618. hyper_waker_free(data->hyp.send_body_waker);
  619. data->hyp.send_body_waker = hyper_context_waker(ctx);
  620. rc = HYPER_POLL_PENDING;
  621. }
  622. out:
  623. Curl_multi_xfer_ulbuf_release(data, xfer_ulbuf);
  624. data->state.hresult = result;
  625. return rc;
  626. }
  627. /*
  628. * finalize_request() sets up last headers and optional body settings
  629. */
  630. static CURLcode finalize_request(struct Curl_easy *data,
  631. hyper_headers *headers,
  632. hyper_request *hyperreq,
  633. Curl_HttpReq httpreq)
  634. {
  635. CURLcode result = CURLE_OK;
  636. struct dynbuf req;
  637. if((httpreq == HTTPREQ_GET) || (httpreq == HTTPREQ_HEAD))
  638. Curl_pgrsSetUploadSize(data, 0); /* no request body */
  639. else {
  640. hyper_body *body;
  641. Curl_dyn_init(&req, DYN_HTTP_REQUEST);
  642. result = Curl_http_req_complete(data, &req, httpreq);
  643. if(result)
  644. return result;
  645. /* if the "complete" above did produce more than the closing line,
  646. parse the added headers */
  647. if(Curl_dyn_len(&req) != 2 || strcmp(Curl_dyn_ptr(&req), "\r\n")) {
  648. result = Curl_hyper_header(data, headers, Curl_dyn_ptr(&req));
  649. if(result)
  650. return result;
  651. }
  652. Curl_dyn_free(&req);
  653. body = hyper_body_new();
  654. hyper_body_set_userdata(body, data);
  655. hyper_body_set_data_func(body, uploadstreamed);
  656. if(HYPERE_OK != hyper_request_set_body(hyperreq, body)) {
  657. /* fail */
  658. result = CURLE_OUT_OF_MEMORY;
  659. }
  660. }
  661. return cr_hyper_add(data);
  662. }
  663. static CURLcode cookies(struct Curl_easy *data,
  664. struct connectdata *conn,
  665. hyper_headers *headers)
  666. {
  667. struct dynbuf req;
  668. CURLcode result;
  669. Curl_dyn_init(&req, DYN_HTTP_REQUEST);
  670. result = Curl_http_cookies(data, conn, &req);
  671. if(!result)
  672. result = Curl_hyper_header(data, headers, Curl_dyn_ptr(&req));
  673. Curl_dyn_free(&req);
  674. return result;
  675. }
  676. /* called on 1xx responses */
  677. static void http1xx_cb(void *arg, struct hyper_response *resp)
  678. {
  679. struct Curl_easy *data = (struct Curl_easy *)arg;
  680. hyper_headers *headers = NULL;
  681. CURLcode result = CURLE_OK;
  682. uint16_t http_status;
  683. int http_version;
  684. const uint8_t *reasonp;
  685. size_t reason_len;
  686. infof(data, "Got HTTP 1xx informational");
  687. http_status = hyper_response_status(resp);
  688. http_version = hyper_response_version(resp);
  689. reasonp = hyper_response_reason_phrase(resp);
  690. reason_len = hyper_response_reason_phrase_len(resp);
  691. result = status_line(data, data->conn,
  692. http_status, http_version, reasonp, reason_len);
  693. if(!result) {
  694. headers = hyper_response_headers(resp);
  695. if(!headers) {
  696. failf(data, "hyperstream: could not get 1xx response headers");
  697. result = CURLE_RECV_ERROR;
  698. }
  699. }
  700. data->state.hresult = result;
  701. if(!result) {
  702. /* the headers are already received */
  703. hyper_headers_foreach(headers, hyper_each_header, data);
  704. /* this callback also sets data->state.hresult on error */
  705. if(empty_header(data))
  706. result = CURLE_OUT_OF_MEMORY;
  707. }
  708. if(data->state.hresult)
  709. infof(data, "ERROR in 1xx, bail out");
  710. }
  711. /*
  712. * Curl_http() gets called from the generic multi_do() function when an HTTP
  713. * request is to be performed. This creates and sends a properly constructed
  714. * HTTP request.
  715. */
  716. CURLcode Curl_http(struct Curl_easy *data, bool *done)
  717. {
  718. struct connectdata *conn = data->conn;
  719. struct hyptransfer *h = &data->hyp;
  720. hyper_io *io = NULL;
  721. hyper_clientconn_options *options = NULL;
  722. hyper_task *task = NULL; /* for the handshake */
  723. hyper_task *sendtask = NULL; /* for the send */
  724. hyper_clientconn *client = NULL;
  725. hyper_request *req = NULL;
  726. hyper_headers *headers = NULL;
  727. hyper_task *handshake = NULL;
  728. CURLcode result;
  729. const char *p_accept; /* Accept: string */
  730. const char *method;
  731. Curl_HttpReq httpreq;
  732. const char *te = NULL; /* transfer-encoding */
  733. hyper_code rc;
  734. /* Always consider the DO phase done after this function call, even if there
  735. may be parts of the request that is not yet sent, since we can deal with
  736. the rest of the request in the PERFORM phase. */
  737. *done = TRUE;
  738. result = Curl_client_start(data);
  739. if(result)
  740. return result;
  741. /* Add collecting of headers written to client. For a new connection,
  742. * we might have done that already, but reuse
  743. * or multiplex needs it here as well. */
  744. result = Curl_headers_init(data);
  745. if(result)
  746. return result;
  747. infof(data, "Time for the Hyper dance");
  748. memset(h, 0, sizeof(struct hyptransfer));
  749. result = Curl_http_host(data, conn);
  750. if(result)
  751. return result;
  752. Curl_http_method(data, conn, &method, &httpreq);
  753. DEBUGASSERT(data->req.bytecount == 0);
  754. /* setup the authentication headers */
  755. {
  756. char *pq = NULL;
  757. if(data->state.up.query) {
  758. pq = aprintf("%s?%s", data->state.up.path, data->state.up.query);
  759. if(!pq)
  760. return CURLE_OUT_OF_MEMORY;
  761. }
  762. result = Curl_http_output_auth(data, conn, method, httpreq,
  763. (pq ? pq : data->state.up.path), FALSE);
  764. free(pq);
  765. if(result)
  766. return result;
  767. }
  768. result = Curl_http_req_set_reader(data, httpreq, &te);
  769. if(result)
  770. goto error;
  771. result = Curl_http_range(data, httpreq);
  772. if(result)
  773. return result;
  774. result = Curl_http_useragent(data);
  775. if(result)
  776. return result;
  777. io = hyper_io_new();
  778. if(!io) {
  779. failf(data, "Couldn't create hyper IO");
  780. result = CURLE_OUT_OF_MEMORY;
  781. goto error;
  782. }
  783. /* tell Hyper how to read/write network data */
  784. h->io_ctx.data = data;
  785. h->io_ctx.sockindex = FIRSTSOCKET;
  786. hyper_io_set_userdata(io, &h->io_ctx);
  787. hyper_io_set_read(io, Curl_hyper_recv);
  788. hyper_io_set_write(io, Curl_hyper_send);
  789. /* create an executor to poll futures */
  790. if(!h->exec) {
  791. h->exec = hyper_executor_new();
  792. if(!h->exec) {
  793. failf(data, "Couldn't create hyper executor");
  794. result = CURLE_OUT_OF_MEMORY;
  795. goto error;
  796. }
  797. }
  798. options = hyper_clientconn_options_new();
  799. if(!options) {
  800. failf(data, "Couldn't create hyper client options");
  801. result = CURLE_OUT_OF_MEMORY;
  802. goto error;
  803. }
  804. if(conn->alpn == CURL_HTTP_VERSION_2) {
  805. failf(data, "ALPN protocol h2 not supported with Hyper");
  806. result = CURLE_UNSUPPORTED_PROTOCOL;
  807. goto error;
  808. }
  809. hyper_clientconn_options_set_preserve_header_case(options, 1);
  810. hyper_clientconn_options_set_preserve_header_order(options, 1);
  811. hyper_clientconn_options_http1_allow_multiline_headers(options, 1);
  812. hyper_clientconn_options_exec(options, h->exec);
  813. /* "Both the `io` and the `options` are consumed in this function call" */
  814. handshake = hyper_clientconn_handshake(io, options);
  815. if(!handshake) {
  816. failf(data, "Couldn't create hyper client handshake");
  817. result = CURLE_OUT_OF_MEMORY;
  818. goto error;
  819. }
  820. io = NULL;
  821. options = NULL;
  822. if(HYPERE_OK != hyper_executor_push(h->exec, handshake)) {
  823. failf(data, "Couldn't hyper_executor_push the handshake");
  824. result = CURLE_OUT_OF_MEMORY;
  825. goto error;
  826. }
  827. handshake = NULL; /* ownership passed on */
  828. task = hyper_executor_poll(h->exec);
  829. if(!task) {
  830. failf(data, "Couldn't hyper_executor_poll the handshake");
  831. result = CURLE_OUT_OF_MEMORY;
  832. goto error;
  833. }
  834. client = hyper_task_value(task);
  835. hyper_task_free(task);
  836. req = hyper_request_new();
  837. if(!req) {
  838. failf(data, "Couldn't hyper_request_new");
  839. result = CURLE_OUT_OF_MEMORY;
  840. goto error;
  841. }
  842. if(!Curl_use_http_1_1plus(data, conn)) {
  843. if(HYPERE_OK != hyper_request_set_version(req,
  844. HYPER_HTTP_VERSION_1_0)) {
  845. failf(data, "error setting HTTP version");
  846. result = CURLE_OUT_OF_MEMORY;
  847. goto error;
  848. }
  849. }
  850. if(hyper_request_set_method(req, (uint8_t *)method, strlen(method))) {
  851. failf(data, "error setting method");
  852. result = CURLE_OUT_OF_MEMORY;
  853. goto error;
  854. }
  855. result = request_target(data, conn, method, req);
  856. if(result)
  857. goto error;
  858. headers = hyper_request_headers(req);
  859. if(!headers) {
  860. failf(data, "hyper_request_headers");
  861. result = CURLE_OUT_OF_MEMORY;
  862. goto error;
  863. }
  864. rc = hyper_request_on_informational(req, http1xx_cb, data);
  865. if(rc) {
  866. result = CURLE_OUT_OF_MEMORY;
  867. goto error;
  868. }
  869. if(data->state.aptr.host) {
  870. result = Curl_hyper_header(data, headers, data->state.aptr.host);
  871. if(result)
  872. goto error;
  873. }
  874. #ifndef CURL_DISABLE_PROXY
  875. if(data->state.aptr.proxyuserpwd) {
  876. result = Curl_hyper_header(data, headers, data->state.aptr.proxyuserpwd);
  877. if(result)
  878. goto error;
  879. }
  880. #endif
  881. if(data->state.aptr.userpwd) {
  882. result = Curl_hyper_header(data, headers, data->state.aptr.userpwd);
  883. if(result)
  884. goto error;
  885. }
  886. if((data->state.use_range && data->state.aptr.rangeline)) {
  887. result = Curl_hyper_header(data, headers, data->state.aptr.rangeline);
  888. if(result)
  889. goto error;
  890. }
  891. if(data->set.str[STRING_USERAGENT] &&
  892. *data->set.str[STRING_USERAGENT] &&
  893. data->state.aptr.uagent) {
  894. result = Curl_hyper_header(data, headers, data->state.aptr.uagent);
  895. if(result)
  896. goto error;
  897. }
  898. p_accept = Curl_checkheaders(data,
  899. STRCONST("Accept"))?NULL:"Accept: */*\r\n";
  900. if(p_accept) {
  901. result = Curl_hyper_header(data, headers, p_accept);
  902. if(result)
  903. goto error;
  904. }
  905. if(te) {
  906. result = Curl_hyper_header(data, headers, te);
  907. if(result)
  908. goto error;
  909. }
  910. #ifndef CURL_DISABLE_ALTSVC
  911. if(conn->bits.altused && !Curl_checkheaders(data, STRCONST("Alt-Used"))) {
  912. char *altused = aprintf("Alt-Used: %s:%d\r\n",
  913. conn->conn_to_host.name, conn->conn_to_port);
  914. if(!altused) {
  915. result = CURLE_OUT_OF_MEMORY;
  916. goto error;
  917. }
  918. result = Curl_hyper_header(data, headers, altused);
  919. if(result)
  920. goto error;
  921. free(altused);
  922. }
  923. #endif
  924. #ifndef CURL_DISABLE_PROXY
  925. if(conn->bits.httpproxy && !conn->bits.tunnel_proxy &&
  926. !Curl_checkheaders(data, STRCONST("Proxy-Connection")) &&
  927. !Curl_checkProxyheaders(data, conn, STRCONST("Proxy-Connection"))) {
  928. result = Curl_hyper_header(data, headers, "Proxy-Connection: Keep-Alive");
  929. if(result)
  930. goto error;
  931. }
  932. #endif
  933. Curl_safefree(data->state.aptr.ref);
  934. if(data->state.referer && !Curl_checkheaders(data, STRCONST("Referer"))) {
  935. data->state.aptr.ref = aprintf("Referer: %s\r\n", data->state.referer);
  936. if(!data->state.aptr.ref)
  937. result = CURLE_OUT_OF_MEMORY;
  938. else
  939. result = Curl_hyper_header(data, headers, data->state.aptr.ref);
  940. if(result)
  941. goto error;
  942. }
  943. #ifdef HAVE_LIBZ
  944. /* we only consider transfer-encoding magic if libz support is built-in */
  945. result = Curl_transferencode(data);
  946. if(result)
  947. goto error;
  948. result = Curl_hyper_header(data, headers, data->state.aptr.te);
  949. if(result)
  950. goto error;
  951. #endif
  952. if(!Curl_checkheaders(data, STRCONST("Accept-Encoding")) &&
  953. data->set.str[STRING_ENCODING]) {
  954. Curl_safefree(data->state.aptr.accept_encoding);
  955. data->state.aptr.accept_encoding =
  956. aprintf("Accept-Encoding: %s\r\n", data->set.str[STRING_ENCODING]);
  957. if(!data->state.aptr.accept_encoding)
  958. result = CURLE_OUT_OF_MEMORY;
  959. else
  960. result = Curl_hyper_header(data, headers,
  961. data->state.aptr.accept_encoding);
  962. if(result)
  963. goto error;
  964. }
  965. else
  966. Curl_safefree(data->state.aptr.accept_encoding);
  967. result = cookies(data, conn, headers);
  968. if(result)
  969. goto error;
  970. if(!result && conn->handler->protocol&(CURLPROTO_WS|CURLPROTO_WSS))
  971. result = Curl_ws_request(data, headers);
  972. result = Curl_add_timecondition(data, headers);
  973. if(result)
  974. goto error;
  975. result = Curl_add_custom_headers(data, FALSE, headers);
  976. if(result)
  977. goto error;
  978. result = finalize_request(data, headers, req, httpreq);
  979. if(result)
  980. goto error;
  981. Curl_debug(data, CURLINFO_HEADER_OUT, (char *)"\r\n", 2);
  982. if(data->req.upload_chunky && data->req.authneg) {
  983. data->req.upload_chunky = TRUE;
  984. }
  985. else {
  986. data->req.upload_chunky = FALSE;
  987. }
  988. sendtask = hyper_clientconn_send(client, req);
  989. if(!sendtask) {
  990. failf(data, "hyper_clientconn_send");
  991. result = CURLE_OUT_OF_MEMORY;
  992. goto error;
  993. }
  994. req = NULL;
  995. if(HYPERE_OK != hyper_executor_push(h->exec, sendtask)) {
  996. failf(data, "Couldn't hyper_executor_push the send");
  997. result = CURLE_OUT_OF_MEMORY;
  998. goto error;
  999. }
  1000. sendtask = NULL; /* ownership passed on */
  1001. hyper_clientconn_free(client);
  1002. client = NULL;
  1003. if((httpreq == HTTPREQ_GET) || (httpreq == HTTPREQ_HEAD)) {
  1004. /* HTTP GET/HEAD download */
  1005. Curl_pgrsSetUploadSize(data, 0); /* nothing */
  1006. }
  1007. Curl_xfer_setup1(data, CURL_XFER_SENDRECV, -1, TRUE);
  1008. conn->datastream = Curl_hyper_stream;
  1009. /* clear userpwd and proxyuserpwd to avoid reusing old credentials
  1010. * from reused connections */
  1011. Curl_safefree(data->state.aptr.userpwd);
  1012. #ifndef CURL_DISABLE_PROXY
  1013. Curl_safefree(data->state.aptr.proxyuserpwd);
  1014. #endif
  1015. return CURLE_OK;
  1016. error:
  1017. DEBUGASSERT(result);
  1018. if(io)
  1019. hyper_io_free(io);
  1020. if(options)
  1021. hyper_clientconn_options_free(options);
  1022. if(handshake)
  1023. hyper_task_free(handshake);
  1024. if(client)
  1025. hyper_clientconn_free(client);
  1026. if(req)
  1027. hyper_request_free(req);
  1028. return result;
  1029. }
  1030. void Curl_hyper_done(struct Curl_easy *data)
  1031. {
  1032. struct hyptransfer *h = &data->hyp;
  1033. if(h->exec) {
  1034. hyper_executor_free(h->exec);
  1035. h->exec = NULL;
  1036. }
  1037. if(h->read_waker) {
  1038. hyper_waker_free(h->read_waker);
  1039. h->read_waker = NULL;
  1040. }
  1041. if(h->write_waker) {
  1042. hyper_waker_free(h->write_waker);
  1043. h->write_waker = NULL;
  1044. }
  1045. if(h->send_body_waker) {
  1046. hyper_waker_free(h->send_body_waker);
  1047. h->send_body_waker = NULL;
  1048. }
  1049. }
  1050. static CURLcode cr_hyper_unpause(struct Curl_easy *data,
  1051. struct Curl_creader *reader)
  1052. {
  1053. (void)reader;
  1054. if(data->hyp.send_body_waker) {
  1055. hyper_waker_wake(data->hyp.send_body_waker);
  1056. data->hyp.send_body_waker = NULL;
  1057. }
  1058. return CURLE_OK;
  1059. }
  1060. /* Hyper client reader, handling unpausing */
  1061. static const struct Curl_crtype cr_hyper_protocol = {
  1062. "cr-hyper",
  1063. Curl_creader_def_init,
  1064. Curl_creader_def_read,
  1065. Curl_creader_def_close,
  1066. Curl_creader_def_needs_rewind,
  1067. Curl_creader_def_total_length,
  1068. Curl_creader_def_resume_from,
  1069. Curl_creader_def_rewind,
  1070. cr_hyper_unpause,
  1071. Curl_creader_def_is_paused,
  1072. Curl_creader_def_done,
  1073. sizeof(struct Curl_creader)
  1074. };
  1075. static CURLcode cr_hyper_add(struct Curl_easy *data)
  1076. {
  1077. struct Curl_creader *reader = NULL;
  1078. CURLcode result;
  1079. result = Curl_creader_create(&reader, data, &cr_hyper_protocol,
  1080. CURL_CR_PROTOCOL);
  1081. if(!result)
  1082. result = Curl_creader_add(data, reader);
  1083. if(result && reader)
  1084. Curl_creader_free(data, reader);
  1085. return result;
  1086. }
  1087. #endif /* !defined(CURL_DISABLE_HTTP) && defined(USE_HYPER) */