2
0

cf-h2-proxy.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661
  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.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(USE_NGHTTP2) && !defined(CURL_DISABLE_PROXY)
  26. #include <nghttp2/nghttp2.h>
  27. #include "urldata.h"
  28. #include "cfilters.h"
  29. #include "connect.h"
  30. #include "curl_trc.h"
  31. #include "bufq.h"
  32. #include "dynbuf.h"
  33. #include "dynhds.h"
  34. #include "http1.h"
  35. #include "http2.h"
  36. #include "http_proxy.h"
  37. #include "multiif.h"
  38. #include "sendf.h"
  39. #include "cf-h2-proxy.h"
  40. /* The last 3 #include files should be in this order */
  41. #include "curl_printf.h"
  42. #include "curl_memory.h"
  43. #include "memdebug.h"
  44. #define PROXY_H2_CHUNK_SIZE (16*1024)
  45. #define PROXY_HTTP2_HUGE_WINDOW_SIZE (100 * 1024 * 1024)
  46. #define H2_TUNNEL_WINDOW_SIZE (10 * 1024 * 1024)
  47. #define PROXY_H2_NW_RECV_CHUNKS (H2_TUNNEL_WINDOW_SIZE / PROXY_H2_CHUNK_SIZE)
  48. #define PROXY_H2_NW_SEND_CHUNKS 1
  49. #define H2_TUNNEL_RECV_CHUNKS (H2_TUNNEL_WINDOW_SIZE / PROXY_H2_CHUNK_SIZE)
  50. #define H2_TUNNEL_SEND_CHUNKS ((128 * 1024) / PROXY_H2_CHUNK_SIZE)
  51. typedef enum {
  52. H2_TUNNEL_INIT, /* init/default/no tunnel state */
  53. H2_TUNNEL_CONNECT, /* CONNECT request is being send */
  54. H2_TUNNEL_RESPONSE, /* CONNECT response received completely */
  55. H2_TUNNEL_ESTABLISHED,
  56. H2_TUNNEL_FAILED
  57. } h2_tunnel_state;
  58. struct tunnel_stream {
  59. struct http_resp *resp;
  60. struct bufq recvbuf;
  61. struct bufq sendbuf;
  62. char *authority;
  63. int32_t stream_id;
  64. uint32_t error;
  65. h2_tunnel_state state;
  66. BIT(has_final_response);
  67. BIT(closed);
  68. BIT(reset);
  69. };
  70. static CURLcode tunnel_stream_init(struct Curl_cfilter *cf,
  71. struct tunnel_stream *ts)
  72. {
  73. const char *hostname;
  74. int port;
  75. bool ipv6_ip;
  76. CURLcode result;
  77. ts->state = H2_TUNNEL_INIT;
  78. ts->stream_id = -1;
  79. Curl_bufq_init2(&ts->recvbuf, PROXY_H2_CHUNK_SIZE, H2_TUNNEL_RECV_CHUNKS,
  80. BUFQ_OPT_SOFT_LIMIT);
  81. Curl_bufq_init(&ts->sendbuf, PROXY_H2_CHUNK_SIZE, H2_TUNNEL_SEND_CHUNKS);
  82. result = Curl_http_proxy_get_destination(cf, &hostname, &port, &ipv6_ip);
  83. if(result)
  84. return result;
  85. ts->authority = /* host:port with IPv6 support */
  86. aprintf("%s%s%s:%d", ipv6_ip ? "[":"", hostname,
  87. ipv6_ip ? "]" : "", port);
  88. if(!ts->authority)
  89. return CURLE_OUT_OF_MEMORY;
  90. return CURLE_OK;
  91. }
  92. static void tunnel_stream_clear(struct tunnel_stream *ts)
  93. {
  94. Curl_http_resp_free(ts->resp);
  95. Curl_bufq_free(&ts->recvbuf);
  96. Curl_bufq_free(&ts->sendbuf);
  97. Curl_safefree(ts->authority);
  98. memset(ts, 0, sizeof(*ts));
  99. ts->state = H2_TUNNEL_INIT;
  100. }
  101. static void h2_tunnel_go_state(struct Curl_cfilter *cf,
  102. struct tunnel_stream *ts,
  103. h2_tunnel_state new_state,
  104. struct Curl_easy *data)
  105. {
  106. (void)cf;
  107. if(ts->state == new_state)
  108. return;
  109. /* leaving this one */
  110. switch(ts->state) {
  111. case H2_TUNNEL_CONNECT:
  112. data->req.ignorebody = FALSE;
  113. break;
  114. default:
  115. break;
  116. }
  117. /* entering this one */
  118. switch(new_state) {
  119. case H2_TUNNEL_INIT:
  120. CURL_TRC_CF(data, cf, "[%d] new tunnel state 'init'", ts->stream_id);
  121. tunnel_stream_clear(ts);
  122. break;
  123. case H2_TUNNEL_CONNECT:
  124. CURL_TRC_CF(data, cf, "[%d] new tunnel state 'connect'", ts->stream_id);
  125. ts->state = H2_TUNNEL_CONNECT;
  126. break;
  127. case H2_TUNNEL_RESPONSE:
  128. CURL_TRC_CF(data, cf, "[%d] new tunnel state 'response'", ts->stream_id);
  129. ts->state = H2_TUNNEL_RESPONSE;
  130. break;
  131. case H2_TUNNEL_ESTABLISHED:
  132. CURL_TRC_CF(data, cf, "[%d] new tunnel state 'established'",
  133. ts->stream_id);
  134. infof(data, "CONNECT phase completed");
  135. data->state.authproxy.done = TRUE;
  136. data->state.authproxy.multipass = FALSE;
  137. FALLTHROUGH();
  138. case H2_TUNNEL_FAILED:
  139. if(new_state == H2_TUNNEL_FAILED)
  140. CURL_TRC_CF(data, cf, "[%d] new tunnel state 'failed'", ts->stream_id);
  141. ts->state = new_state;
  142. /* If a proxy-authorization header was used for the proxy, then we should
  143. make sure that it is not accidentally used for the document request
  144. after we have connected. So let's free and clear it here. */
  145. Curl_safefree(data->state.aptr.proxyuserpwd);
  146. break;
  147. }
  148. }
  149. struct cf_h2_proxy_ctx {
  150. nghttp2_session *h2;
  151. /* The easy handle used in the current filter call, cleared at return */
  152. struct cf_call_data call_data;
  153. struct bufq inbufq; /* network receive buffer */
  154. struct bufq outbufq; /* network send buffer */
  155. struct tunnel_stream tunnel; /* our tunnel CONNECT stream */
  156. int32_t goaway_error;
  157. int32_t last_stream_id;
  158. BIT(conn_closed);
  159. BIT(rcvd_goaway);
  160. BIT(sent_goaway);
  161. BIT(nw_out_blocked);
  162. };
  163. /* How to access `call_data` from a cf_h2 filter */
  164. #undef CF_CTX_CALL_DATA
  165. #define CF_CTX_CALL_DATA(cf) \
  166. ((struct cf_h2_proxy_ctx *)(cf)->ctx)->call_data
  167. static void cf_h2_proxy_ctx_clear(struct cf_h2_proxy_ctx *ctx)
  168. {
  169. struct cf_call_data save = ctx->call_data;
  170. if(ctx->h2) {
  171. nghttp2_session_del(ctx->h2);
  172. }
  173. Curl_bufq_free(&ctx->inbufq);
  174. Curl_bufq_free(&ctx->outbufq);
  175. tunnel_stream_clear(&ctx->tunnel);
  176. memset(ctx, 0, sizeof(*ctx));
  177. ctx->call_data = save;
  178. }
  179. static void cf_h2_proxy_ctx_free(struct cf_h2_proxy_ctx *ctx)
  180. {
  181. if(ctx) {
  182. cf_h2_proxy_ctx_clear(ctx);
  183. free(ctx);
  184. }
  185. }
  186. static void drain_tunnel(struct Curl_cfilter *cf,
  187. struct Curl_easy *data,
  188. struct tunnel_stream *tunnel)
  189. {
  190. struct cf_h2_proxy_ctx *ctx = cf->ctx;
  191. unsigned char bits;
  192. (void)cf;
  193. bits = CURL_CSELECT_IN;
  194. if(!tunnel->closed && !tunnel->reset &&
  195. !Curl_bufq_is_empty(&ctx->tunnel.sendbuf))
  196. bits |= CURL_CSELECT_OUT;
  197. if(data->state.select_bits != bits) {
  198. CURL_TRC_CF(data, cf, "[%d] DRAIN select_bits=%x",
  199. tunnel->stream_id, bits);
  200. data->state.select_bits = bits;
  201. Curl_expire(data, 0, EXPIRE_RUN_NOW);
  202. }
  203. }
  204. static ssize_t proxy_nw_in_reader(void *reader_ctx,
  205. unsigned char *buf, size_t buflen,
  206. CURLcode *err)
  207. {
  208. struct Curl_cfilter *cf = reader_ctx;
  209. ssize_t nread;
  210. if(cf) {
  211. struct Curl_easy *data = CF_DATA_CURRENT(cf);
  212. nread = Curl_conn_cf_recv(cf->next, data, (char *)buf, buflen, err);
  213. CURL_TRC_CF(data, cf, "[0] nw_in_reader(len=%zu) -> %zd, %d",
  214. buflen, nread, *err);
  215. }
  216. else {
  217. nread = 0;
  218. }
  219. return nread;
  220. }
  221. static ssize_t proxy_h2_nw_out_writer(void *writer_ctx,
  222. const unsigned char *buf, size_t buflen,
  223. CURLcode *err)
  224. {
  225. struct Curl_cfilter *cf = writer_ctx;
  226. ssize_t nwritten;
  227. if(cf) {
  228. struct Curl_easy *data = CF_DATA_CURRENT(cf);
  229. nwritten = Curl_conn_cf_send(cf->next, data, (const char *)buf, buflen,
  230. FALSE, err);
  231. CURL_TRC_CF(data, cf, "[0] nw_out_writer(len=%zu) -> %zd, %d",
  232. buflen, nwritten, *err);
  233. }
  234. else {
  235. nwritten = 0;
  236. }
  237. return nwritten;
  238. }
  239. static int proxy_h2_client_new(struct Curl_cfilter *cf,
  240. nghttp2_session_callbacks *cbs)
  241. {
  242. struct cf_h2_proxy_ctx *ctx = cf->ctx;
  243. nghttp2_option *o;
  244. nghttp2_mem mem = {NULL, Curl_nghttp2_malloc, Curl_nghttp2_free,
  245. Curl_nghttp2_calloc, Curl_nghttp2_realloc};
  246. int rc = nghttp2_option_new(&o);
  247. if(rc)
  248. return rc;
  249. /* We handle window updates ourself to enforce buffer limits */
  250. nghttp2_option_set_no_auto_window_update(o, 1);
  251. #if NGHTTP2_VERSION_NUM >= 0x013200
  252. /* with 1.50.0 */
  253. /* turn off RFC 9113 leading and trailing white spaces validation against
  254. HTTP field value. */
  255. nghttp2_option_set_no_rfc9113_leading_and_trailing_ws_validation(o, 1);
  256. #endif
  257. rc = nghttp2_session_client_new3(&ctx->h2, cbs, cf, o, &mem);
  258. nghttp2_option_del(o);
  259. return rc;
  260. }
  261. static ssize_t on_session_send(nghttp2_session *h2,
  262. const uint8_t *buf, size_t blen,
  263. int flags, void *userp);
  264. static int proxy_h2_on_frame_recv(nghttp2_session *session,
  265. const nghttp2_frame *frame,
  266. void *userp);
  267. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  268. static int proxy_h2_on_frame_send(nghttp2_session *session,
  269. const nghttp2_frame *frame,
  270. void *userp);
  271. #endif
  272. static int proxy_h2_on_stream_close(nghttp2_session *session,
  273. int32_t stream_id,
  274. uint32_t error_code, void *userp);
  275. static int proxy_h2_on_header(nghttp2_session *session,
  276. const nghttp2_frame *frame,
  277. const uint8_t *name, size_t namelen,
  278. const uint8_t *value, size_t valuelen,
  279. uint8_t flags,
  280. void *userp);
  281. static int tunnel_recv_callback(nghttp2_session *session, uint8_t flags,
  282. int32_t stream_id,
  283. const uint8_t *mem, size_t len, void *userp);
  284. /*
  285. * Initialize the cfilter context
  286. */
  287. static CURLcode cf_h2_proxy_ctx_init(struct Curl_cfilter *cf,
  288. struct Curl_easy *data)
  289. {
  290. struct cf_h2_proxy_ctx *ctx = cf->ctx;
  291. CURLcode result = CURLE_OUT_OF_MEMORY;
  292. nghttp2_session_callbacks *cbs = NULL;
  293. int rc;
  294. DEBUGASSERT(!ctx->h2);
  295. memset(&ctx->tunnel, 0, sizeof(ctx->tunnel));
  296. Curl_bufq_init(&ctx->inbufq, PROXY_H2_CHUNK_SIZE, PROXY_H2_NW_RECV_CHUNKS);
  297. Curl_bufq_init(&ctx->outbufq, PROXY_H2_CHUNK_SIZE, PROXY_H2_NW_SEND_CHUNKS);
  298. if(tunnel_stream_init(cf, &ctx->tunnel))
  299. goto out;
  300. rc = nghttp2_session_callbacks_new(&cbs);
  301. if(rc) {
  302. failf(data, "Couldn't initialize nghttp2 callbacks");
  303. goto out;
  304. }
  305. nghttp2_session_callbacks_set_send_callback(cbs, on_session_send);
  306. nghttp2_session_callbacks_set_on_frame_recv_callback(
  307. cbs, proxy_h2_on_frame_recv);
  308. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  309. nghttp2_session_callbacks_set_on_frame_send_callback(cbs,
  310. proxy_h2_on_frame_send);
  311. #endif
  312. nghttp2_session_callbacks_set_on_data_chunk_recv_callback(
  313. cbs, tunnel_recv_callback);
  314. nghttp2_session_callbacks_set_on_stream_close_callback(
  315. cbs, proxy_h2_on_stream_close);
  316. nghttp2_session_callbacks_set_on_header_callback(cbs, proxy_h2_on_header);
  317. /* The nghttp2 session is not yet setup, do it */
  318. rc = proxy_h2_client_new(cf, cbs);
  319. if(rc) {
  320. failf(data, "Couldn't initialize nghttp2");
  321. goto out;
  322. }
  323. {
  324. nghttp2_settings_entry iv[3];
  325. iv[0].settings_id = NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS;
  326. iv[0].value = Curl_multi_max_concurrent_streams(data->multi);
  327. iv[1].settings_id = NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE;
  328. iv[1].value = H2_TUNNEL_WINDOW_SIZE;
  329. iv[2].settings_id = NGHTTP2_SETTINGS_ENABLE_PUSH;
  330. iv[2].value = 0;
  331. rc = nghttp2_submit_settings(ctx->h2, NGHTTP2_FLAG_NONE, iv, 3);
  332. if(rc) {
  333. failf(data, "nghttp2_submit_settings() failed: %s(%d)",
  334. nghttp2_strerror(rc), rc);
  335. result = CURLE_HTTP2;
  336. goto out;
  337. }
  338. }
  339. rc = nghttp2_session_set_local_window_size(ctx->h2, NGHTTP2_FLAG_NONE, 0,
  340. PROXY_HTTP2_HUGE_WINDOW_SIZE);
  341. if(rc) {
  342. failf(data, "nghttp2_session_set_local_window_size() failed: %s(%d)",
  343. nghttp2_strerror(rc), rc);
  344. result = CURLE_HTTP2;
  345. goto out;
  346. }
  347. /* all set, traffic will be send on connect */
  348. result = CURLE_OK;
  349. out:
  350. if(cbs)
  351. nghttp2_session_callbacks_del(cbs);
  352. CURL_TRC_CF(data, cf, "[0] init proxy ctx -> %d", result);
  353. return result;
  354. }
  355. static int proxy_h2_should_close_session(struct cf_h2_proxy_ctx *ctx)
  356. {
  357. return !nghttp2_session_want_read(ctx->h2) &&
  358. !nghttp2_session_want_write(ctx->h2);
  359. }
  360. static CURLcode proxy_h2_nw_out_flush(struct Curl_cfilter *cf,
  361. struct Curl_easy *data)
  362. {
  363. struct cf_h2_proxy_ctx *ctx = cf->ctx;
  364. ssize_t nwritten;
  365. CURLcode result;
  366. (void)data;
  367. if(Curl_bufq_is_empty(&ctx->outbufq))
  368. return CURLE_OK;
  369. nwritten = Curl_bufq_pass(&ctx->outbufq, proxy_h2_nw_out_writer, cf,
  370. &result);
  371. if(nwritten < 0) {
  372. if(result == CURLE_AGAIN) {
  373. CURL_TRC_CF(data, cf, "[0] flush nw send buffer(%zu) -> EAGAIN",
  374. Curl_bufq_len(&ctx->outbufq));
  375. ctx->nw_out_blocked = 1;
  376. }
  377. return result;
  378. }
  379. CURL_TRC_CF(data, cf, "[0] nw send buffer flushed");
  380. return Curl_bufq_is_empty(&ctx->outbufq) ? CURLE_OK : CURLE_AGAIN;
  381. }
  382. /*
  383. * Processes pending input left in network input buffer.
  384. * This function returns 0 if it succeeds, or -1 and error code will
  385. * be assigned to *err.
  386. */
  387. static int proxy_h2_process_pending_input(struct Curl_cfilter *cf,
  388. struct Curl_easy *data,
  389. CURLcode *err)
  390. {
  391. struct cf_h2_proxy_ctx *ctx = cf->ctx;
  392. const unsigned char *buf;
  393. size_t blen;
  394. ssize_t rv;
  395. while(Curl_bufq_peek(&ctx->inbufq, &buf, &blen)) {
  396. rv = nghttp2_session_mem_recv(ctx->h2, (const uint8_t *)buf, blen);
  397. CURL_TRC_CF(data, cf, "[0] %zu bytes to nghttp2 -> %zd", blen, rv);
  398. if(rv < 0) {
  399. failf(data,
  400. "process_pending_input: nghttp2_session_mem_recv() returned "
  401. "%zd:%s", rv, nghttp2_strerror((int)rv));
  402. *err = CURLE_RECV_ERROR;
  403. return -1;
  404. }
  405. Curl_bufq_skip(&ctx->inbufq, (size_t)rv);
  406. if(Curl_bufq_is_empty(&ctx->inbufq)) {
  407. CURL_TRC_CF(data, cf, "[0] all data in connection buffer processed");
  408. break;
  409. }
  410. else {
  411. CURL_TRC_CF(data, cf, "[0] process_pending_input: %zu bytes left "
  412. "in connection buffer", Curl_bufq_len(&ctx->inbufq));
  413. }
  414. }
  415. return 0;
  416. }
  417. static CURLcode proxy_h2_progress_ingress(struct Curl_cfilter *cf,
  418. struct Curl_easy *data)
  419. {
  420. struct cf_h2_proxy_ctx *ctx = cf->ctx;
  421. CURLcode result = CURLE_OK;
  422. ssize_t nread;
  423. /* Process network input buffer fist */
  424. if(!Curl_bufq_is_empty(&ctx->inbufq)) {
  425. CURL_TRC_CF(data, cf, "[0] process %zu bytes in connection buffer",
  426. Curl_bufq_len(&ctx->inbufq));
  427. if(proxy_h2_process_pending_input(cf, data, &result) < 0)
  428. return result;
  429. }
  430. /* Receive data from the "lower" filters, e.g. network until
  431. * it is time to stop or we have enough data for this stream */
  432. while(!ctx->conn_closed && /* not closed the connection */
  433. !ctx->tunnel.closed && /* nor the tunnel */
  434. Curl_bufq_is_empty(&ctx->inbufq) && /* and we consumed our input */
  435. !Curl_bufq_is_full(&ctx->tunnel.recvbuf)) {
  436. nread = Curl_bufq_slurp(&ctx->inbufq, proxy_nw_in_reader, cf, &result);
  437. CURL_TRC_CF(data, cf, "[0] read %zu bytes nw data -> %zd, %d",
  438. Curl_bufq_len(&ctx->inbufq), nread, result);
  439. if(nread < 0) {
  440. if(result != CURLE_AGAIN) {
  441. failf(data, "Failed receiving HTTP2 data");
  442. return result;
  443. }
  444. break;
  445. }
  446. else if(nread == 0) {
  447. ctx->conn_closed = TRUE;
  448. break;
  449. }
  450. if(proxy_h2_process_pending_input(cf, data, &result))
  451. return result;
  452. }
  453. if(ctx->conn_closed && Curl_bufq_is_empty(&ctx->inbufq)) {
  454. connclose(cf->conn, "GOAWAY received");
  455. }
  456. return CURLE_OK;
  457. }
  458. static CURLcode proxy_h2_progress_egress(struct Curl_cfilter *cf,
  459. struct Curl_easy *data)
  460. {
  461. struct cf_h2_proxy_ctx *ctx = cf->ctx;
  462. int rv = 0;
  463. ctx->nw_out_blocked = 0;
  464. while(!rv && !ctx->nw_out_blocked && nghttp2_session_want_write(ctx->h2))
  465. rv = nghttp2_session_send(ctx->h2);
  466. if(nghttp2_is_fatal(rv)) {
  467. CURL_TRC_CF(data, cf, "[0] nghttp2_session_send error (%s)%d",
  468. nghttp2_strerror(rv), rv);
  469. return CURLE_SEND_ERROR;
  470. }
  471. return proxy_h2_nw_out_flush(cf, data);
  472. }
  473. static ssize_t on_session_send(nghttp2_session *h2,
  474. const uint8_t *buf, size_t blen, int flags,
  475. void *userp)
  476. {
  477. struct Curl_cfilter *cf = userp;
  478. struct cf_h2_proxy_ctx *ctx = cf->ctx;
  479. struct Curl_easy *data = CF_DATA_CURRENT(cf);
  480. ssize_t nwritten;
  481. CURLcode result = CURLE_OK;
  482. (void)h2;
  483. (void)flags;
  484. DEBUGASSERT(data);
  485. nwritten = Curl_bufq_write_pass(&ctx->outbufq, buf, blen,
  486. proxy_h2_nw_out_writer, cf, &result);
  487. if(nwritten < 0) {
  488. if(result == CURLE_AGAIN) {
  489. return NGHTTP2_ERR_WOULDBLOCK;
  490. }
  491. failf(data, "Failed sending HTTP2 data");
  492. return NGHTTP2_ERR_CALLBACK_FAILURE;
  493. }
  494. if(!nwritten)
  495. return NGHTTP2_ERR_WOULDBLOCK;
  496. return nwritten;
  497. }
  498. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  499. static int proxy_h2_fr_print(const nghttp2_frame *frame,
  500. char *buffer, size_t blen)
  501. {
  502. switch(frame->hd.type) {
  503. case NGHTTP2_DATA: {
  504. return msnprintf(buffer, blen,
  505. "FRAME[DATA, len=%d, eos=%d, padlen=%d]",
  506. (int)frame->hd.length,
  507. !!(frame->hd.flags & NGHTTP2_FLAG_END_STREAM),
  508. (int)frame->data.padlen);
  509. }
  510. case NGHTTP2_HEADERS: {
  511. return msnprintf(buffer, blen,
  512. "FRAME[HEADERS, len=%d, hend=%d, eos=%d]",
  513. (int)frame->hd.length,
  514. !!(frame->hd.flags & NGHTTP2_FLAG_END_HEADERS),
  515. !!(frame->hd.flags & NGHTTP2_FLAG_END_STREAM));
  516. }
  517. case NGHTTP2_PRIORITY: {
  518. return msnprintf(buffer, blen,
  519. "FRAME[PRIORITY, len=%d, flags=%d]",
  520. (int)frame->hd.length, frame->hd.flags);
  521. }
  522. case NGHTTP2_RST_STREAM: {
  523. return msnprintf(buffer, blen,
  524. "FRAME[RST_STREAM, len=%d, flags=%d, error=%u]",
  525. (int)frame->hd.length, frame->hd.flags,
  526. frame->rst_stream.error_code);
  527. }
  528. case NGHTTP2_SETTINGS: {
  529. if(frame->hd.flags & NGHTTP2_FLAG_ACK) {
  530. return msnprintf(buffer, blen, "FRAME[SETTINGS, ack=1]");
  531. }
  532. return msnprintf(buffer, blen,
  533. "FRAME[SETTINGS, len=%d]", (int)frame->hd.length);
  534. }
  535. case NGHTTP2_PUSH_PROMISE:
  536. return msnprintf(buffer, blen,
  537. "FRAME[PUSH_PROMISE, len=%d, hend=%d]",
  538. (int)frame->hd.length,
  539. !!(frame->hd.flags & NGHTTP2_FLAG_END_HEADERS));
  540. case NGHTTP2_PING:
  541. return msnprintf(buffer, blen,
  542. "FRAME[PING, len=%d, ack=%d]",
  543. (int)frame->hd.length,
  544. frame->hd.flags & NGHTTP2_FLAG_ACK);
  545. case NGHTTP2_GOAWAY: {
  546. char scratch[128];
  547. size_t s_len = sizeof(scratch)/sizeof(scratch[0]);
  548. size_t len = (frame->goaway.opaque_data_len < s_len) ?
  549. frame->goaway.opaque_data_len : s_len-1;
  550. if(len)
  551. memcpy(scratch, frame->goaway.opaque_data, len);
  552. scratch[len] = '\0';
  553. return msnprintf(buffer, blen, "FRAME[GOAWAY, error=%d, reason='%s', "
  554. "last_stream=%d]", frame->goaway.error_code,
  555. scratch, frame->goaway.last_stream_id);
  556. }
  557. case NGHTTP2_WINDOW_UPDATE: {
  558. return msnprintf(buffer, blen,
  559. "FRAME[WINDOW_UPDATE, incr=%d]",
  560. frame->window_update.window_size_increment);
  561. }
  562. default:
  563. return msnprintf(buffer, blen, "FRAME[%d, len=%d, flags=%d]",
  564. frame->hd.type, (int)frame->hd.length,
  565. frame->hd.flags);
  566. }
  567. }
  568. static int proxy_h2_on_frame_send(nghttp2_session *session,
  569. const nghttp2_frame *frame,
  570. void *userp)
  571. {
  572. struct Curl_cfilter *cf = userp;
  573. struct Curl_easy *data = CF_DATA_CURRENT(cf);
  574. (void)session;
  575. DEBUGASSERT(data);
  576. if(data && Curl_trc_cf_is_verbose(cf, data)) {
  577. char buffer[256];
  578. int len;
  579. len = proxy_h2_fr_print(frame, buffer, sizeof(buffer)-1);
  580. buffer[len] = 0;
  581. CURL_TRC_CF(data, cf, "[%d] -> %s", frame->hd.stream_id, buffer);
  582. }
  583. return 0;
  584. }
  585. #endif /* !CURL_DISABLE_VERBOSE_STRINGS */
  586. static int proxy_h2_on_frame_recv(nghttp2_session *session,
  587. const nghttp2_frame *frame,
  588. void *userp)
  589. {
  590. struct Curl_cfilter *cf = userp;
  591. struct cf_h2_proxy_ctx *ctx = cf->ctx;
  592. struct Curl_easy *data = CF_DATA_CURRENT(cf);
  593. int32_t stream_id = frame->hd.stream_id;
  594. (void)session;
  595. DEBUGASSERT(data);
  596. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  597. if(Curl_trc_cf_is_verbose(cf, data)) {
  598. char buffer[256];
  599. int len;
  600. len = proxy_h2_fr_print(frame, buffer, sizeof(buffer)-1);
  601. buffer[len] = 0;
  602. CURL_TRC_CF(data, cf, "[%d] <- %s",frame->hd.stream_id, buffer);
  603. }
  604. #endif /* !CURL_DISABLE_VERBOSE_STRINGS */
  605. if(!stream_id) {
  606. /* stream ID zero is for connection-oriented stuff */
  607. DEBUGASSERT(data);
  608. switch(frame->hd.type) {
  609. case NGHTTP2_SETTINGS:
  610. /* Since the initial stream window is 64K, a request might be on HOLD,
  611. * due to exhaustion. The (initial) SETTINGS may announce a much larger
  612. * window and *assume* that we treat this like a WINDOW_UPDATE. Some
  613. * servers send an explicit WINDOW_UPDATE, but not all seem to do that.
  614. * To be safe, we UNHOLD a stream in order not to stall. */
  615. if(CURL_WANT_SEND(data)) {
  616. drain_tunnel(cf, data, &ctx->tunnel);
  617. }
  618. break;
  619. case NGHTTP2_GOAWAY:
  620. ctx->rcvd_goaway = TRUE;
  621. break;
  622. default:
  623. break;
  624. }
  625. return 0;
  626. }
  627. if(stream_id != ctx->tunnel.stream_id) {
  628. CURL_TRC_CF(data, cf, "[%d] rcvd FRAME not for tunnel", stream_id);
  629. return NGHTTP2_ERR_CALLBACK_FAILURE;
  630. }
  631. switch(frame->hd.type) {
  632. case NGHTTP2_HEADERS:
  633. /* nghttp2 guarantees that :status is received, and we store it to
  634. stream->status_code. Fuzzing has proven this can still be reached
  635. without status code having been set. */
  636. if(!ctx->tunnel.resp)
  637. return NGHTTP2_ERR_CALLBACK_FAILURE;
  638. /* Only final status code signals the end of header */
  639. CURL_TRC_CF(data, cf, "[%d] got http status: %d",
  640. stream_id, ctx->tunnel.resp->status);
  641. if(!ctx->tunnel.has_final_response) {
  642. if(ctx->tunnel.resp->status / 100 != 1) {
  643. ctx->tunnel.has_final_response = TRUE;
  644. }
  645. }
  646. break;
  647. case NGHTTP2_WINDOW_UPDATE:
  648. if(CURL_WANT_SEND(data)) {
  649. drain_tunnel(cf, data, &ctx->tunnel);
  650. }
  651. break;
  652. default:
  653. break;
  654. }
  655. return 0;
  656. }
  657. static int proxy_h2_on_header(nghttp2_session *session,
  658. const nghttp2_frame *frame,
  659. const uint8_t *name, size_t namelen,
  660. const uint8_t *value, size_t valuelen,
  661. uint8_t flags,
  662. void *userp)
  663. {
  664. struct Curl_cfilter *cf = userp;
  665. struct cf_h2_proxy_ctx *ctx = cf->ctx;
  666. struct Curl_easy *data = CF_DATA_CURRENT(cf);
  667. int32_t stream_id = frame->hd.stream_id;
  668. CURLcode result;
  669. (void)flags;
  670. (void)data;
  671. (void)session;
  672. DEBUGASSERT(stream_id); /* should never be a zero stream ID here */
  673. if(stream_id != ctx->tunnel.stream_id) {
  674. CURL_TRC_CF(data, cf, "[%d] header for non-tunnel stream: "
  675. "%.*s: %.*s", stream_id,
  676. (int)namelen, name, (int)valuelen, value);
  677. return NGHTTP2_ERR_CALLBACK_FAILURE;
  678. }
  679. if(frame->hd.type == NGHTTP2_PUSH_PROMISE)
  680. return NGHTTP2_ERR_CALLBACK_FAILURE;
  681. if(ctx->tunnel.has_final_response) {
  682. /* we do not do anything with trailers for tunnel streams */
  683. return 0;
  684. }
  685. if(namelen == sizeof(HTTP_PSEUDO_STATUS) - 1 &&
  686. memcmp(HTTP_PSEUDO_STATUS, name, namelen) == 0) {
  687. int http_status;
  688. struct http_resp *resp;
  689. /* status: always comes first, we might get more than one response,
  690. * link the previous ones for keepers */
  691. result = Curl_http_decode_status(&http_status,
  692. (const char *)value, valuelen);
  693. if(result)
  694. return NGHTTP2_ERR_CALLBACK_FAILURE;
  695. result = Curl_http_resp_make(&resp, http_status, NULL);
  696. if(result)
  697. return NGHTTP2_ERR_CALLBACK_FAILURE;
  698. resp->prev = ctx->tunnel.resp;
  699. ctx->tunnel.resp = resp;
  700. CURL_TRC_CF(data, cf, "[%d] status: HTTP/2 %03d",
  701. stream_id, ctx->tunnel.resp->status);
  702. return 0;
  703. }
  704. if(!ctx->tunnel.resp)
  705. return NGHTTP2_ERR_CALLBACK_FAILURE;
  706. result = Curl_dynhds_add(&ctx->tunnel.resp->headers,
  707. (const char *)name, namelen,
  708. (const char *)value, valuelen);
  709. if(result)
  710. return NGHTTP2_ERR_CALLBACK_FAILURE;
  711. CURL_TRC_CF(data, cf, "[%d] header: %.*s: %.*s",
  712. stream_id, (int)namelen, name, (int)valuelen, value);
  713. return 0; /* 0 is successful */
  714. }
  715. static ssize_t tunnel_send_callback(nghttp2_session *session,
  716. int32_t stream_id,
  717. uint8_t *buf, size_t length,
  718. uint32_t *data_flags,
  719. nghttp2_data_source *source,
  720. void *userp)
  721. {
  722. struct Curl_cfilter *cf = userp;
  723. struct cf_h2_proxy_ctx *ctx = cf->ctx;
  724. struct Curl_easy *data = CF_DATA_CURRENT(cf);
  725. struct tunnel_stream *ts;
  726. CURLcode result;
  727. ssize_t nread;
  728. (void)source;
  729. (void)data;
  730. (void)ctx;
  731. if(!stream_id)
  732. return NGHTTP2_ERR_INVALID_ARGUMENT;
  733. ts = nghttp2_session_get_stream_user_data(session, stream_id);
  734. if(!ts)
  735. return NGHTTP2_ERR_CALLBACK_FAILURE;
  736. DEBUGASSERT(ts == &ctx->tunnel);
  737. nread = Curl_bufq_read(&ts->sendbuf, buf, length, &result);
  738. if(nread < 0) {
  739. if(result != CURLE_AGAIN)
  740. return NGHTTP2_ERR_CALLBACK_FAILURE;
  741. return NGHTTP2_ERR_DEFERRED;
  742. }
  743. if(ts->closed && Curl_bufq_is_empty(&ts->sendbuf))
  744. *data_flags = NGHTTP2_DATA_FLAG_EOF;
  745. CURL_TRC_CF(data, cf, "[%d] tunnel_send_callback -> %zd",
  746. ts->stream_id, nread);
  747. return nread;
  748. }
  749. static int tunnel_recv_callback(nghttp2_session *session, uint8_t flags,
  750. int32_t stream_id,
  751. const uint8_t *mem, size_t len, void *userp)
  752. {
  753. struct Curl_cfilter *cf = userp;
  754. struct cf_h2_proxy_ctx *ctx = cf->ctx;
  755. ssize_t nwritten;
  756. CURLcode result;
  757. (void)flags;
  758. (void)session;
  759. DEBUGASSERT(stream_id); /* should never be a zero stream ID here */
  760. if(stream_id != ctx->tunnel.stream_id)
  761. return NGHTTP2_ERR_CALLBACK_FAILURE;
  762. nwritten = Curl_bufq_write(&ctx->tunnel.recvbuf, mem, len, &result);
  763. if(nwritten < 0) {
  764. if(result != CURLE_AGAIN)
  765. return NGHTTP2_ERR_CALLBACK_FAILURE;
  766. nwritten = 0;
  767. }
  768. DEBUGASSERT((size_t)nwritten == len);
  769. return 0;
  770. }
  771. static int proxy_h2_on_stream_close(nghttp2_session *session,
  772. int32_t stream_id,
  773. uint32_t error_code, void *userp)
  774. {
  775. struct Curl_cfilter *cf = userp;
  776. struct cf_h2_proxy_ctx *ctx = cf->ctx;
  777. struct Curl_easy *data = CF_DATA_CURRENT(cf);
  778. (void)session;
  779. (void)data;
  780. if(stream_id != ctx->tunnel.stream_id)
  781. return 0;
  782. CURL_TRC_CF(data, cf, "[%d] proxy_h2_on_stream_close, %s (err %d)",
  783. stream_id, nghttp2_http2_strerror(error_code), error_code);
  784. ctx->tunnel.closed = TRUE;
  785. ctx->tunnel.error = error_code;
  786. return 0;
  787. }
  788. static CURLcode proxy_h2_submit(int32_t *pstream_id,
  789. struct Curl_cfilter *cf,
  790. struct Curl_easy *data,
  791. nghttp2_session *h2,
  792. struct httpreq *req,
  793. const nghttp2_priority_spec *pri_spec,
  794. void *stream_user_data,
  795. nghttp2_data_source_read_callback read_callback,
  796. void *read_ctx)
  797. {
  798. struct dynhds h2_headers;
  799. nghttp2_nv *nva = NULL;
  800. int32_t stream_id = -1;
  801. size_t nheader;
  802. CURLcode result;
  803. (void)cf;
  804. Curl_dynhds_init(&h2_headers, 0, DYN_HTTP_REQUEST);
  805. result = Curl_http_req_to_h2(&h2_headers, req, data);
  806. if(result)
  807. goto out;
  808. nva = Curl_dynhds_to_nva(&h2_headers, &nheader);
  809. if(!nva) {
  810. result = CURLE_OUT_OF_MEMORY;
  811. goto out;
  812. }
  813. if(read_callback) {
  814. nghttp2_data_provider data_prd;
  815. data_prd.read_callback = read_callback;
  816. data_prd.source.ptr = read_ctx;
  817. stream_id = nghttp2_submit_request(h2, pri_spec, nva, nheader,
  818. &data_prd, stream_user_data);
  819. }
  820. else {
  821. stream_id = nghttp2_submit_request(h2, pri_spec, nva, nheader,
  822. NULL, stream_user_data);
  823. }
  824. if(stream_id < 0) {
  825. failf(data, "nghttp2_session_upgrade2() failed: %s(%d)",
  826. nghttp2_strerror(stream_id), stream_id);
  827. result = CURLE_SEND_ERROR;
  828. goto out;
  829. }
  830. result = CURLE_OK;
  831. out:
  832. free(nva);
  833. Curl_dynhds_free(&h2_headers);
  834. *pstream_id = stream_id;
  835. return result;
  836. }
  837. static CURLcode submit_CONNECT(struct Curl_cfilter *cf,
  838. struct Curl_easy *data,
  839. struct tunnel_stream *ts)
  840. {
  841. struct cf_h2_proxy_ctx *ctx = cf->ctx;
  842. CURLcode result;
  843. struct httpreq *req = NULL;
  844. result = Curl_http_proxy_create_CONNECT(&req, cf, data, 2);
  845. if(result)
  846. goto out;
  847. result = Curl_creader_set_null(data);
  848. if(result)
  849. goto out;
  850. infof(data, "Establish HTTP/2 proxy tunnel to %s", req->authority);
  851. result = proxy_h2_submit(&ts->stream_id, cf, data, ctx->h2, req,
  852. NULL, ts, tunnel_send_callback, cf);
  853. if(result) {
  854. CURL_TRC_CF(data, cf, "[%d] send, nghttp2_submit_request error: %s",
  855. ts->stream_id, nghttp2_strerror(ts->stream_id));
  856. }
  857. out:
  858. if(req)
  859. Curl_http_req_free(req);
  860. if(result)
  861. failf(data, "Failed sending CONNECT to proxy");
  862. return result;
  863. }
  864. static CURLcode inspect_response(struct Curl_cfilter *cf,
  865. struct Curl_easy *data,
  866. struct tunnel_stream *ts)
  867. {
  868. CURLcode result = CURLE_OK;
  869. struct dynhds_entry *auth_reply = NULL;
  870. (void)cf;
  871. DEBUGASSERT(ts->resp);
  872. if(ts->resp->status/100 == 2) {
  873. infof(data, "CONNECT tunnel established, response %d", ts->resp->status);
  874. h2_tunnel_go_state(cf, ts, H2_TUNNEL_ESTABLISHED, data);
  875. return CURLE_OK;
  876. }
  877. if(ts->resp->status == 401) {
  878. auth_reply = Curl_dynhds_cget(&ts->resp->headers, "WWW-Authenticate");
  879. }
  880. else if(ts->resp->status == 407) {
  881. auth_reply = Curl_dynhds_cget(&ts->resp->headers, "Proxy-Authenticate");
  882. }
  883. if(auth_reply) {
  884. CURL_TRC_CF(data, cf, "[0] CONNECT: fwd auth header '%s'",
  885. auth_reply->value);
  886. result = Curl_http_input_auth(data, ts->resp->status == 407,
  887. auth_reply->value);
  888. if(result)
  889. return result;
  890. if(data->req.newurl) {
  891. /* Indicator that we should try again */
  892. Curl_safefree(data->req.newurl);
  893. h2_tunnel_go_state(cf, ts, H2_TUNNEL_INIT, data);
  894. return CURLE_OK;
  895. }
  896. }
  897. /* Seems to have failed */
  898. return CURLE_RECV_ERROR;
  899. }
  900. static CURLcode H2_CONNECT(struct Curl_cfilter *cf,
  901. struct Curl_easy *data,
  902. struct tunnel_stream *ts)
  903. {
  904. struct cf_h2_proxy_ctx *ctx = cf->ctx;
  905. CURLcode result = CURLE_OK;
  906. DEBUGASSERT(ts);
  907. DEBUGASSERT(ts->authority);
  908. do {
  909. switch(ts->state) {
  910. case H2_TUNNEL_INIT:
  911. /* Prepare the CONNECT request and make a first attempt to send. */
  912. CURL_TRC_CF(data, cf, "[0] CONNECT start for %s", ts->authority);
  913. result = submit_CONNECT(cf, data, ts);
  914. if(result)
  915. goto out;
  916. h2_tunnel_go_state(cf, ts, H2_TUNNEL_CONNECT, data);
  917. FALLTHROUGH();
  918. case H2_TUNNEL_CONNECT:
  919. /* see that the request is completely sent */
  920. result = proxy_h2_progress_ingress(cf, data);
  921. if(!result)
  922. result = proxy_h2_progress_egress(cf, data);
  923. if(result && result != CURLE_AGAIN) {
  924. h2_tunnel_go_state(cf, ts, H2_TUNNEL_FAILED, data);
  925. break;
  926. }
  927. if(ts->has_final_response) {
  928. h2_tunnel_go_state(cf, ts, H2_TUNNEL_RESPONSE, data);
  929. }
  930. else {
  931. result = CURLE_OK;
  932. goto out;
  933. }
  934. FALLTHROUGH();
  935. case H2_TUNNEL_RESPONSE:
  936. DEBUGASSERT(ts->has_final_response);
  937. result = inspect_response(cf, data, ts);
  938. if(result)
  939. goto out;
  940. break;
  941. case H2_TUNNEL_ESTABLISHED:
  942. return CURLE_OK;
  943. case H2_TUNNEL_FAILED:
  944. return CURLE_RECV_ERROR;
  945. default:
  946. break;
  947. }
  948. } while(ts->state == H2_TUNNEL_INIT);
  949. out:
  950. if((result && (result != CURLE_AGAIN)) || ctx->tunnel.closed)
  951. h2_tunnel_go_state(cf, ts, H2_TUNNEL_FAILED, data);
  952. return result;
  953. }
  954. static CURLcode cf_h2_proxy_connect(struct Curl_cfilter *cf,
  955. struct Curl_easy *data,
  956. bool blocking, bool *done)
  957. {
  958. struct cf_h2_proxy_ctx *ctx = cf->ctx;
  959. CURLcode result = CURLE_OK;
  960. struct cf_call_data save;
  961. timediff_t check;
  962. struct tunnel_stream *ts = &ctx->tunnel;
  963. if(cf->connected) {
  964. *done = TRUE;
  965. return CURLE_OK;
  966. }
  967. /* Connect the lower filters first */
  968. if(!cf->next->connected) {
  969. result = Curl_conn_cf_connect(cf->next, data, blocking, done);
  970. if(result || !*done)
  971. return result;
  972. }
  973. *done = FALSE;
  974. CF_DATA_SAVE(save, cf, data);
  975. if(!ctx->h2) {
  976. result = cf_h2_proxy_ctx_init(cf, data);
  977. if(result)
  978. goto out;
  979. }
  980. DEBUGASSERT(ts->authority);
  981. check = Curl_timeleft(data, NULL, TRUE);
  982. if(check <= 0) {
  983. failf(data, "Proxy CONNECT aborted due to timeout");
  984. result = CURLE_OPERATION_TIMEDOUT;
  985. goto out;
  986. }
  987. /* for the secondary socket (FTP), use the "connect to host"
  988. * but ignore the "connect to port" (use the secondary port)
  989. */
  990. result = H2_CONNECT(cf, data, ts);
  991. out:
  992. *done = (result == CURLE_OK) && (ts->state == H2_TUNNEL_ESTABLISHED);
  993. if(*done) {
  994. cf->connected = TRUE;
  995. /* The real request will follow the CONNECT, reset request partially */
  996. Curl_req_soft_reset(&data->req, data);
  997. Curl_client_reset(data);
  998. }
  999. CF_DATA_RESTORE(cf, save);
  1000. return result;
  1001. }
  1002. static void cf_h2_proxy_close(struct Curl_cfilter *cf, struct Curl_easy *data)
  1003. {
  1004. struct cf_h2_proxy_ctx *ctx = cf->ctx;
  1005. if(ctx) {
  1006. struct cf_call_data save;
  1007. CF_DATA_SAVE(save, cf, data);
  1008. cf_h2_proxy_ctx_clear(ctx);
  1009. CF_DATA_RESTORE(cf, save);
  1010. }
  1011. if(cf->next)
  1012. cf->next->cft->do_close(cf->next, data);
  1013. }
  1014. static void cf_h2_proxy_destroy(struct Curl_cfilter *cf,
  1015. struct Curl_easy *data)
  1016. {
  1017. struct cf_h2_proxy_ctx *ctx = cf->ctx;
  1018. (void)data;
  1019. if(ctx) {
  1020. cf_h2_proxy_ctx_free(ctx);
  1021. cf->ctx = NULL;
  1022. }
  1023. }
  1024. static CURLcode cf_h2_proxy_shutdown(struct Curl_cfilter *cf,
  1025. struct Curl_easy *data, bool *done)
  1026. {
  1027. struct cf_h2_proxy_ctx *ctx = cf->ctx;
  1028. struct cf_call_data save;
  1029. CURLcode result;
  1030. int rv;
  1031. if(!cf->connected || !ctx->h2 || cf->shutdown || ctx->conn_closed) {
  1032. *done = TRUE;
  1033. return CURLE_OK;
  1034. }
  1035. CF_DATA_SAVE(save, cf, data);
  1036. if(!ctx->sent_goaway) {
  1037. rv = nghttp2_submit_goaway(ctx->h2, NGHTTP2_FLAG_NONE,
  1038. 0, 0,
  1039. (const uint8_t *)"shutdown",
  1040. sizeof("shutdown"));
  1041. if(rv) {
  1042. failf(data, "nghttp2_submit_goaway() failed: %s(%d)",
  1043. nghttp2_strerror(rv), rv);
  1044. result = CURLE_SEND_ERROR;
  1045. goto out;
  1046. }
  1047. ctx->sent_goaway = TRUE;
  1048. }
  1049. /* GOAWAY submitted, process egress and ingress until nghttp2 is done. */
  1050. result = CURLE_OK;
  1051. if(nghttp2_session_want_write(ctx->h2))
  1052. result = proxy_h2_progress_egress(cf, data);
  1053. if(!result && nghttp2_session_want_read(ctx->h2))
  1054. result = proxy_h2_progress_ingress(cf, data);
  1055. *done = (ctx->conn_closed ||
  1056. (!result && !nghttp2_session_want_write(ctx->h2) &&
  1057. !nghttp2_session_want_read(ctx->h2)));
  1058. out:
  1059. CF_DATA_RESTORE(cf, save);
  1060. cf->shutdown = (result || *done);
  1061. return result;
  1062. }
  1063. static bool cf_h2_proxy_data_pending(struct Curl_cfilter *cf,
  1064. const struct Curl_easy *data)
  1065. {
  1066. struct cf_h2_proxy_ctx *ctx = cf->ctx;
  1067. if((ctx && !Curl_bufq_is_empty(&ctx->inbufq)) ||
  1068. (ctx && ctx->tunnel.state == H2_TUNNEL_ESTABLISHED &&
  1069. !Curl_bufq_is_empty(&ctx->tunnel.recvbuf)))
  1070. return TRUE;
  1071. return cf->next ? cf->next->cft->has_data_pending(cf->next, data) : FALSE;
  1072. }
  1073. static void cf_h2_proxy_adjust_pollset(struct Curl_cfilter *cf,
  1074. struct Curl_easy *data,
  1075. struct easy_pollset *ps)
  1076. {
  1077. struct cf_h2_proxy_ctx *ctx = cf->ctx;
  1078. struct cf_call_data save;
  1079. curl_socket_t sock = Curl_conn_cf_get_socket(cf, data);
  1080. bool want_recv, want_send;
  1081. if(!cf->connected && ctx->h2) {
  1082. want_send = nghttp2_session_want_write(ctx->h2) ||
  1083. !Curl_bufq_is_empty(&ctx->outbufq) ||
  1084. !Curl_bufq_is_empty(&ctx->tunnel.sendbuf);
  1085. want_recv = nghttp2_session_want_read(ctx->h2);
  1086. }
  1087. else
  1088. Curl_pollset_check(data, ps, sock, &want_recv, &want_send);
  1089. if(ctx->h2 && (want_recv || want_send)) {
  1090. bool c_exhaust, s_exhaust;
  1091. CF_DATA_SAVE(save, cf, data);
  1092. c_exhaust = !nghttp2_session_get_remote_window_size(ctx->h2);
  1093. s_exhaust = ctx->tunnel.stream_id >= 0 &&
  1094. !nghttp2_session_get_stream_remote_window_size(
  1095. ctx->h2, ctx->tunnel.stream_id);
  1096. want_recv = (want_recv || c_exhaust || s_exhaust);
  1097. want_send = (!s_exhaust && want_send) ||
  1098. (!c_exhaust && nghttp2_session_want_write(ctx->h2)) ||
  1099. !Curl_bufq_is_empty(&ctx->outbufq) ||
  1100. !Curl_bufq_is_empty(&ctx->tunnel.sendbuf);
  1101. Curl_pollset_set(data, ps, sock, want_recv, want_send);
  1102. CURL_TRC_CF(data, cf, "adjust_pollset, want_recv=%d want_send=%d",
  1103. want_recv, want_send);
  1104. CF_DATA_RESTORE(cf, save);
  1105. }
  1106. else if(ctx->sent_goaway && !cf->shutdown) {
  1107. /* shutdown in progress */
  1108. CF_DATA_SAVE(save, cf, data);
  1109. want_send = nghttp2_session_want_write(ctx->h2) ||
  1110. !Curl_bufq_is_empty(&ctx->outbufq) ||
  1111. !Curl_bufq_is_empty(&ctx->tunnel.sendbuf);
  1112. want_recv = nghttp2_session_want_read(ctx->h2);
  1113. Curl_pollset_set(data, ps, sock, want_recv, want_send);
  1114. CURL_TRC_CF(data, cf, "adjust_pollset, want_recv=%d want_send=%d",
  1115. want_recv, want_send);
  1116. CF_DATA_RESTORE(cf, save);
  1117. }
  1118. }
  1119. static ssize_t h2_handle_tunnel_close(struct Curl_cfilter *cf,
  1120. struct Curl_easy *data,
  1121. CURLcode *err)
  1122. {
  1123. struct cf_h2_proxy_ctx *ctx = cf->ctx;
  1124. ssize_t rv = 0;
  1125. if(ctx->tunnel.error == NGHTTP2_REFUSED_STREAM) {
  1126. CURL_TRC_CF(data, cf, "[%d] REFUSED_STREAM, try again on a new "
  1127. "connection", ctx->tunnel.stream_id);
  1128. connclose(cf->conn, "REFUSED_STREAM"); /* do not use this anymore */
  1129. *err = CURLE_RECV_ERROR; /* trigger Curl_retry_request() later */
  1130. return -1;
  1131. }
  1132. else if(ctx->tunnel.error != NGHTTP2_NO_ERROR) {
  1133. failf(data, "HTTP/2 stream %u was not closed cleanly: %s (err %u)",
  1134. ctx->tunnel.stream_id, nghttp2_http2_strerror(ctx->tunnel.error),
  1135. ctx->tunnel.error);
  1136. *err = CURLE_HTTP2_STREAM;
  1137. return -1;
  1138. }
  1139. else if(ctx->tunnel.reset) {
  1140. failf(data, "HTTP/2 stream %u was reset", ctx->tunnel.stream_id);
  1141. *err = CURLE_RECV_ERROR;
  1142. return -1;
  1143. }
  1144. *err = CURLE_OK;
  1145. rv = 0;
  1146. CURL_TRC_CF(data, cf, "[%d] handle_tunnel_close -> %zd, %d",
  1147. ctx->tunnel.stream_id, rv, *err);
  1148. return rv;
  1149. }
  1150. static ssize_t tunnel_recv(struct Curl_cfilter *cf, struct Curl_easy *data,
  1151. char *buf, size_t len, CURLcode *err)
  1152. {
  1153. struct cf_h2_proxy_ctx *ctx = cf->ctx;
  1154. ssize_t nread = -1;
  1155. *err = CURLE_AGAIN;
  1156. if(!Curl_bufq_is_empty(&ctx->tunnel.recvbuf)) {
  1157. nread = Curl_bufq_read(&ctx->tunnel.recvbuf,
  1158. (unsigned char *)buf, len, err);
  1159. if(nread < 0)
  1160. goto out;
  1161. DEBUGASSERT(nread > 0);
  1162. }
  1163. if(nread < 0) {
  1164. if(ctx->tunnel.closed) {
  1165. nread = h2_handle_tunnel_close(cf, data, err);
  1166. }
  1167. else if(ctx->tunnel.reset ||
  1168. (ctx->conn_closed && Curl_bufq_is_empty(&ctx->inbufq)) ||
  1169. (ctx->rcvd_goaway &&
  1170. ctx->last_stream_id < ctx->tunnel.stream_id)) {
  1171. *err = CURLE_RECV_ERROR;
  1172. nread = -1;
  1173. }
  1174. }
  1175. else if(nread == 0) {
  1176. *err = CURLE_AGAIN;
  1177. nread = -1;
  1178. }
  1179. out:
  1180. CURL_TRC_CF(data, cf, "[%d] tunnel_recv(len=%zu) -> %zd, %d",
  1181. ctx->tunnel.stream_id, len, nread, *err);
  1182. return nread;
  1183. }
  1184. static ssize_t cf_h2_proxy_recv(struct Curl_cfilter *cf,
  1185. struct Curl_easy *data,
  1186. char *buf, size_t len, CURLcode *err)
  1187. {
  1188. struct cf_h2_proxy_ctx *ctx = cf->ctx;
  1189. ssize_t nread = -1;
  1190. struct cf_call_data save;
  1191. CURLcode result;
  1192. if(ctx->tunnel.state != H2_TUNNEL_ESTABLISHED) {
  1193. *err = CURLE_RECV_ERROR;
  1194. return -1;
  1195. }
  1196. CF_DATA_SAVE(save, cf, data);
  1197. if(Curl_bufq_is_empty(&ctx->tunnel.recvbuf)) {
  1198. *err = proxy_h2_progress_ingress(cf, data);
  1199. if(*err)
  1200. goto out;
  1201. }
  1202. nread = tunnel_recv(cf, data, buf, len, err);
  1203. if(nread > 0) {
  1204. CURL_TRC_CF(data, cf, "[%d] increase window by %zd",
  1205. ctx->tunnel.stream_id, nread);
  1206. nghttp2_session_consume(ctx->h2, ctx->tunnel.stream_id, (size_t)nread);
  1207. }
  1208. result = proxy_h2_progress_egress(cf, data);
  1209. if(result && (result != CURLE_AGAIN)) {
  1210. *err = result;
  1211. nread = -1;
  1212. }
  1213. out:
  1214. if(!Curl_bufq_is_empty(&ctx->tunnel.recvbuf) &&
  1215. (nread >= 0 || *err == CURLE_AGAIN)) {
  1216. /* data pending and no fatal error to report. Need to trigger
  1217. * draining to avoid stalling when no socket events happen. */
  1218. drain_tunnel(cf, data, &ctx->tunnel);
  1219. }
  1220. CURL_TRC_CF(data, cf, "[%d] cf_recv(len=%zu) -> %zd %d",
  1221. ctx->tunnel.stream_id, len, nread, *err);
  1222. CF_DATA_RESTORE(cf, save);
  1223. return nread;
  1224. }
  1225. static ssize_t cf_h2_proxy_send(struct Curl_cfilter *cf,
  1226. struct Curl_easy *data,
  1227. const void *buf, size_t len, bool eos,
  1228. CURLcode *err)
  1229. {
  1230. struct cf_h2_proxy_ctx *ctx = cf->ctx;
  1231. struct cf_call_data save;
  1232. int rv;
  1233. ssize_t nwritten;
  1234. CURLcode result;
  1235. (void)eos; /* TODO, maybe useful for blocks? */
  1236. if(ctx->tunnel.state != H2_TUNNEL_ESTABLISHED) {
  1237. *err = CURLE_SEND_ERROR;
  1238. return -1;
  1239. }
  1240. CF_DATA_SAVE(save, cf, data);
  1241. if(ctx->tunnel.closed) {
  1242. nwritten = -1;
  1243. *err = CURLE_SEND_ERROR;
  1244. goto out;
  1245. }
  1246. else {
  1247. nwritten = Curl_bufq_write(&ctx->tunnel.sendbuf, buf, len, err);
  1248. if(nwritten < 0 && (*err != CURLE_AGAIN))
  1249. goto out;
  1250. }
  1251. if(!Curl_bufq_is_empty(&ctx->tunnel.sendbuf)) {
  1252. /* req body data is buffered, resume the potentially suspended stream */
  1253. rv = nghttp2_session_resume_data(ctx->h2, ctx->tunnel.stream_id);
  1254. if(nghttp2_is_fatal(rv)) {
  1255. *err = CURLE_SEND_ERROR;
  1256. nwritten = -1;
  1257. goto out;
  1258. }
  1259. }
  1260. result = proxy_h2_progress_ingress(cf, data);
  1261. if(result) {
  1262. *err = result;
  1263. nwritten = -1;
  1264. goto out;
  1265. }
  1266. /* Call the nghttp2 send loop and flush to write ALL buffered data,
  1267. * headers and/or request body completely out to the network */
  1268. result = proxy_h2_progress_egress(cf, data);
  1269. if(result && (result != CURLE_AGAIN)) {
  1270. *err = result;
  1271. nwritten = -1;
  1272. goto out;
  1273. }
  1274. if(proxy_h2_should_close_session(ctx)) {
  1275. /* nghttp2 thinks this session is done. If the stream has not been
  1276. * closed, this is an error state for out transfer */
  1277. if(ctx->tunnel.closed) {
  1278. *err = CURLE_SEND_ERROR;
  1279. nwritten = -1;
  1280. }
  1281. else {
  1282. CURL_TRC_CF(data, cf, "[0] send: nothing to do in this session");
  1283. *err = CURLE_HTTP2;
  1284. nwritten = -1;
  1285. }
  1286. }
  1287. out:
  1288. if(!Curl_bufq_is_empty(&ctx->tunnel.recvbuf) &&
  1289. (nwritten >= 0 || *err == CURLE_AGAIN)) {
  1290. /* data pending and no fatal error to report. Need to trigger
  1291. * draining to avoid stalling when no socket events happen. */
  1292. drain_tunnel(cf, data, &ctx->tunnel);
  1293. }
  1294. CURL_TRC_CF(data, cf, "[%d] cf_send(len=%zu) -> %zd, %d, "
  1295. "h2 windows %d-%d (stream-conn), buffers %zu-%zu (stream-conn)",
  1296. ctx->tunnel.stream_id, len, nwritten, *err,
  1297. nghttp2_session_get_stream_remote_window_size(
  1298. ctx->h2, ctx->tunnel.stream_id),
  1299. nghttp2_session_get_remote_window_size(ctx->h2),
  1300. Curl_bufq_len(&ctx->tunnel.sendbuf),
  1301. Curl_bufq_len(&ctx->outbufq));
  1302. CF_DATA_RESTORE(cf, save);
  1303. return nwritten;
  1304. }
  1305. static CURLcode cf_h2_proxy_flush(struct Curl_cfilter *cf,
  1306. struct Curl_easy *data)
  1307. {
  1308. struct cf_h2_proxy_ctx *ctx = cf->ctx;
  1309. struct cf_call_data save;
  1310. CURLcode result = CURLE_OK;
  1311. CF_DATA_SAVE(save, cf, data);
  1312. if(!Curl_bufq_is_empty(&ctx->tunnel.sendbuf)) {
  1313. /* resume the potentially suspended tunnel */
  1314. int rv = nghttp2_session_resume_data(ctx->h2, ctx->tunnel.stream_id);
  1315. if(nghttp2_is_fatal(rv)) {
  1316. result = CURLE_SEND_ERROR;
  1317. goto out;
  1318. }
  1319. }
  1320. result = proxy_h2_progress_egress(cf, data);
  1321. out:
  1322. CURL_TRC_CF(data, cf, "[%d] flush -> %d, "
  1323. "h2 windows %d-%d (stream-conn), buffers %zu-%zu (stream-conn)",
  1324. ctx->tunnel.stream_id, result,
  1325. nghttp2_session_get_stream_remote_window_size(
  1326. ctx->h2, ctx->tunnel.stream_id),
  1327. nghttp2_session_get_remote_window_size(ctx->h2),
  1328. Curl_bufq_len(&ctx->tunnel.sendbuf),
  1329. Curl_bufq_len(&ctx->outbufq));
  1330. CF_DATA_RESTORE(cf, save);
  1331. return result;
  1332. }
  1333. static bool proxy_h2_connisalive(struct Curl_cfilter *cf,
  1334. struct Curl_easy *data,
  1335. bool *input_pending)
  1336. {
  1337. struct cf_h2_proxy_ctx *ctx = cf->ctx;
  1338. bool alive = TRUE;
  1339. *input_pending = FALSE;
  1340. if(!cf->next || !cf->next->cft->is_alive(cf->next, data, input_pending))
  1341. return FALSE;
  1342. if(*input_pending) {
  1343. /* This happens before we have sent off a request and the connection is
  1344. not in use by any other transfer, there should not be any data here,
  1345. only "protocol frames" */
  1346. CURLcode result;
  1347. ssize_t nread = -1;
  1348. *input_pending = FALSE;
  1349. nread = Curl_bufq_slurp(&ctx->inbufq, proxy_nw_in_reader, cf, &result);
  1350. if(nread != -1) {
  1351. if(proxy_h2_process_pending_input(cf, data, &result) < 0)
  1352. /* immediate error, considered dead */
  1353. alive = FALSE;
  1354. else {
  1355. alive = !proxy_h2_should_close_session(ctx);
  1356. }
  1357. }
  1358. else if(result != CURLE_AGAIN) {
  1359. /* the read failed so let's say this is dead anyway */
  1360. alive = FALSE;
  1361. }
  1362. }
  1363. return alive;
  1364. }
  1365. static bool cf_h2_proxy_is_alive(struct Curl_cfilter *cf,
  1366. struct Curl_easy *data,
  1367. bool *input_pending)
  1368. {
  1369. struct cf_h2_proxy_ctx *ctx = cf->ctx;
  1370. CURLcode result;
  1371. struct cf_call_data save;
  1372. CF_DATA_SAVE(save, cf, data);
  1373. result = (ctx && ctx->h2 && proxy_h2_connisalive(cf, data, input_pending));
  1374. CURL_TRC_CF(data, cf, "[0] conn alive -> %d, input_pending=%d",
  1375. result, *input_pending);
  1376. CF_DATA_RESTORE(cf, save);
  1377. return result;
  1378. }
  1379. static CURLcode cf_h2_proxy_query(struct Curl_cfilter *cf,
  1380. struct Curl_easy *data,
  1381. int query, int *pres1, void *pres2)
  1382. {
  1383. struct cf_h2_proxy_ctx *ctx = cf->ctx;
  1384. switch(query) {
  1385. case CF_QUERY_NEED_FLUSH: {
  1386. if(!Curl_bufq_is_empty(&ctx->outbufq) ||
  1387. !Curl_bufq_is_empty(&ctx->tunnel.sendbuf)) {
  1388. CURL_TRC_CF(data, cf, "needs flush");
  1389. *pres1 = TRUE;
  1390. return CURLE_OK;
  1391. }
  1392. break;
  1393. }
  1394. default:
  1395. break;
  1396. }
  1397. return cf->next ?
  1398. cf->next->cft->query(cf->next, data, query, pres1, pres2) :
  1399. CURLE_UNKNOWN_OPTION;
  1400. }
  1401. static CURLcode cf_h2_proxy_cntrl(struct Curl_cfilter *cf,
  1402. struct Curl_easy *data,
  1403. int event, int arg1, void *arg2)
  1404. {
  1405. CURLcode result = CURLE_OK;
  1406. struct cf_call_data save;
  1407. (void)arg1;
  1408. (void)arg2;
  1409. switch(event) {
  1410. case CF_CTRL_FLUSH:
  1411. CF_DATA_SAVE(save, cf, data);
  1412. result = cf_h2_proxy_flush(cf, data);
  1413. CF_DATA_RESTORE(cf, save);
  1414. break;
  1415. default:
  1416. break;
  1417. }
  1418. return result;
  1419. }
  1420. struct Curl_cftype Curl_cft_h2_proxy = {
  1421. "H2-PROXY",
  1422. CF_TYPE_IP_CONNECT|CF_TYPE_PROXY,
  1423. CURL_LOG_LVL_NONE,
  1424. cf_h2_proxy_destroy,
  1425. cf_h2_proxy_connect,
  1426. cf_h2_proxy_close,
  1427. cf_h2_proxy_shutdown,
  1428. Curl_cf_http_proxy_get_host,
  1429. cf_h2_proxy_adjust_pollset,
  1430. cf_h2_proxy_data_pending,
  1431. cf_h2_proxy_send,
  1432. cf_h2_proxy_recv,
  1433. cf_h2_proxy_cntrl,
  1434. cf_h2_proxy_is_alive,
  1435. Curl_cf_def_conn_keep_alive,
  1436. cf_h2_proxy_query,
  1437. };
  1438. CURLcode Curl_cf_h2_proxy_insert_after(struct Curl_cfilter *cf,
  1439. struct Curl_easy *data)
  1440. {
  1441. struct Curl_cfilter *cf_h2_proxy = NULL;
  1442. struct cf_h2_proxy_ctx *ctx;
  1443. CURLcode result = CURLE_OUT_OF_MEMORY;
  1444. (void)data;
  1445. ctx = calloc(1, sizeof(*ctx));
  1446. if(!ctx)
  1447. goto out;
  1448. result = Curl_cf_create(&cf_h2_proxy, &Curl_cft_h2_proxy, ctx);
  1449. if(result)
  1450. goto out;
  1451. Curl_conn_cf_insert_after(cf, cf_h2_proxy);
  1452. result = CURLE_OK;
  1453. out:
  1454. if(result)
  1455. cf_h2_proxy_ctx_free(ctx);
  1456. return result;
  1457. }
  1458. #endif /* defined(USE_NGHTTP2) && !defined(CURL_DISABLE_PROXY) */