2
0

c-hyper.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228
  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(0 == k->bodywrites) {
  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: couldn't 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->bodywrites) {
  387. /* hyper doesn't 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: couldn't 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: couldn't 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: couldn't 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. don't 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: couldn't 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_setup(data, FIRSTSOCKET, -1, TRUE, FIRSTSOCKET);
  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_done,
  1072. sizeof(struct Curl_creader)
  1073. };
  1074. static CURLcode cr_hyper_add(struct Curl_easy *data)
  1075. {
  1076. struct Curl_creader *reader = NULL;
  1077. CURLcode result;
  1078. result = Curl_creader_create(&reader, data, &cr_hyper_protocol,
  1079. CURL_CR_PROTOCOL);
  1080. if(!result)
  1081. result = Curl_creader_add(data, reader);
  1082. if(result && reader)
  1083. Curl_creader_free(data, reader);
  1084. return result;
  1085. }
  1086. #endif /* !defined(CURL_DISABLE_HTTP) && defined(USE_HYPER) */