c-hyper.c 36 KB

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