c-hyper.c 31 KB

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