c-hyper.c 35 KB

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