c-hyper.c 35 KB

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