c-hyper.c 35 KB

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