c-hyper.c 33 KB

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