cf-h2-proxy.c 48 KB

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