From e0c0ccee436913c4613ccce334443ef2ed1c8bd4 Mon Sep 17 00:00:00 2001 From: lukechampine Date: Fri, 12 May 2023 10:29:22 -0400 Subject: [PATCH 1/6] http: 404 or 500 on failed cache scry --- pkg/vere/io/http.c | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/pkg/vere/io/http.c b/pkg/vere/io/http.c index 98eabfcec0..5a792e09ef 100644 --- a/pkg/vere/io/http.c +++ b/pkg/vere/io/http.c @@ -651,14 +651,32 @@ _http_req_dispatch(u3_hreq* req_u, u3_noun req) */ static void _http_cache_respond(u3_hreq* req_u, u3_noun nun) { - // XX check auth - u3_noun auth, response_header, data; - u3x_qual(u3k(u3t(u3t(nun))), &auth, 0, &response_header, &data); - u3_noun status, headers; - u3x_cell(response_header, &status, &headers); + h2o_req_t* rec_u = req_u->rec_u; + u3_httd* htd_u = req_u->hon_u->htp_u->htd_u; - req_u->sat_e = u3_rsat_plan; - _http_start_respond(req_u, u3k(status), u3k(headers), u3k(data), c3y); + if ( u3_nul == nun ) { + h2o_send_error_404(rec_u, "Not Found", "not found", 0); + } + else if ( u3_none == u3r_at(7, nun) ) { + h2o_send_error_500(rec_u, "Internal Server Error", "scry failed", 0); + } + else { + u3_noun auth, response_header, data; + u3x_qual(u3k(u3t(u3t(nun))), &auth, 0, &response_header, &data); + u3_noun status, headers; + u3x_cell(response_header, &status, &headers); + + // check auth + if ( (c3y == auth) + && (c3n == _http_req_is_auth(&htd_u->fig_u, rec_u)) ) + { + h2o_send_error_403(rec_u, "Unauthorized", "unauthorized", 0); + } + else { + req_u->sat_e = u3_rsat_plan; + _http_start_respond(req_u, u3k(status), u3k(headers), u3k(data), c3y); + } + } u3z(nun); } From 377cbd40e01529491a53d89e4cbd50862b02cc8e Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Mon, 15 May 2023 15:22:06 -0400 Subject: [PATCH 2/6] http: fix refcounts in scry cache values --- pkg/vere/io/http.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/vere/io/http.c b/pkg/vere/io/http.c index 5a792e09ef..229aa90fae 100644 --- a/pkg/vere/io/http.c +++ b/pkg/vere/io/http.c @@ -692,7 +692,7 @@ _http_cache_scry_cb(void* vod_p, u3_noun nun) { cache_scry_cb_t* cbt = vod_p; u3_httd* htd_u = cbt->req_u->hon_u->htp_u->htd_u; - u3h_put(htd_u->nax_p, cbt->pax, nun); + u3h_put(htd_u->nax_p, cbt->pax, u3k(nun)); u3z(cbt->pax); _http_cache_respond(cbt->req_u, nun); c3_free(cbt); From 94305d4ffead4743ac009cdb72d60d6db34ec0b9 Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Mon, 15 May 2023 15:29:50 -0400 Subject: [PATCH 3/6] http: fix refcount of scry path --- pkg/vere/io/http.c | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/vere/io/http.c b/pkg/vere/io/http.c index 229aa90fae..a90078e4e0 100644 --- a/pkg/vere/io/http.c +++ b/pkg/vere/io/http.c @@ -723,6 +723,7 @@ _http_req_cache(u3_hreq* req_u) u3_nul, sac, cbt, _http_cache_scry_cb); return c3y; } + u3z(sac); _http_cache_respond(req_u, nac); return c3y; } From 725b2c1e1456a2120320d47e326bc71a1f7030a2 Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Fri, 12 May 2023 17:13:54 -0400 Subject: [PATCH 4/6] http: add state for scry-backed request lifecycle --- pkg/vere/io/http.c | 60 +++++++++++++++++++++++++++++++++------------- 1 file changed, 44 insertions(+), 16 deletions(-) diff --git a/pkg/vere/io/http.c b/pkg/vere/io/http.c index a90078e4e0..481b2e73b0 100644 --- a/pkg/vere/io/http.c +++ b/pkg/vere/io/http.c @@ -21,7 +21,8 @@ typedef struct _u3_h2o_serv { typedef enum { u3_rsat_init = 0, // initialized u3_rsat_plan = 1, // planned - u3_rsat_ripe = 2 // responded + u3_rsat_peek = 2, // peek planned + u3_rsat_ripe = 3 // responded } u3_rsat; /* u3_hreq: incoming http request. @@ -32,11 +33,20 @@ typedef struct _u3_h2o_serv { u3_rsat sat_e; // request state uv_timer_t* tim_u; // timeout void* gen_u; // response generator + struct _u3_preq* peq_u; // scry-backed (rsat_peek only) struct _u3_hcon* hon_u; // connection backlink struct _u3_hreq* nex_u; // next in connection's list struct _u3_hreq* pre_u; // prev in connection's list } u3_hreq; + /* u3_preq: scry-backed http request. + */ + typedef struct _u3_preq { + struct _u3_hreq* req_u; // originating request (nullable) + struct _u3_httd* htd_u; // device backpointer + u3_noun pax; // partial scry path + } u3_preq; + /* u3_hcon: incoming http connection. */ typedef struct _u3_hcon { @@ -466,6 +476,12 @@ _http_seq_unlink(u3_hreq* req_u) req_u->nex_u->pre_u = 0; } } + + // unlink from async scry request if present + // + if ( req_u->peq_u ) { + req_u->peq_u->req_u = 0; + } } /* _http_req_to_duct(): translate srv/con/req to duct @@ -564,6 +580,12 @@ _http_req_timer_cb(uv_timer_t* tim_u) h2o_send_error_generic(req_u->rec_u, 504, msg_c, msg_c, 0); } break; + case u3_rsat_peek: { + req_u->peq_u = 0; + c3_c* msg_c = "gateway timeout"; + h2o_send_error_generic(req_u->rec_u, 504, msg_c, msg_c, 0); + } break; + case u3_rsat_ripe: { u3_hgen* gen_u = req_u->gen_u; @@ -680,22 +702,24 @@ _http_cache_respond(u3_hreq* req_u, u3_noun nun) { u3z(nun); } -typedef struct _cache_scry_cb_t { - u3_hreq* req_u; - u3_noun pax; -} cache_scry_cb_t; - /* _http_cache_scry_cb(): insert scry result into noun cache */ static void _http_cache_scry_cb(void* vod_p, u3_noun nun) { - cache_scry_cb_t* cbt = vod_p; - u3_httd* htd_u = cbt->req_u->hon_u->htp_u->htd_u; - u3h_put(htd_u->nax_p, cbt->pax, u3k(nun)); - u3z(cbt->pax); - _http_cache_respond(cbt->req_u, nun); - c3_free(cbt); + u3_preq* peq_u = vod_p; + u3_httd* htd_u = peq_u->htd_u; + u3_hreq* req_u = peq_u->req_u; + + if ( req_u ) { + u3_assert(u3_rsat_peek == req_u->sat_e); + req_u->peq_u = 0; + _http_cache_respond(req_u, u3k(nun)); + } + + u3h_put(htd_u->nax_p, peq_u->pax, nun); + u3z(peq_u->pax); + c3_free(peq_u); } /* _http_req_cache(): attempt to serve http request from cache @@ -716,11 +740,14 @@ _http_req_cache(u3_hreq* req_u) u3_weak nac = u3h_get(htd_u->nax_p, sac); if ( u3_none == nac ) { // noun not in cache; scry it - cache_scry_cb_t* cbt = c3_malloc(sizeof(cache_scry_cb_t)); - cbt->req_u = req_u; - cbt->pax = sac; + req_u->peq_u = c3_malloc(sizeof(*req_u->peq_u)); + req_u->peq_u->req_u = req_u; + req_u->peq_u->htd_u = htd_u; + req_u->peq_u->pax = sac; + + req_u->sat_e = u3_rsat_peek; u3_pier_peek_last(htd_u->car_u.pir_u, u3_nul, c3__ex, - u3_nul, sac, cbt, _http_cache_scry_cb); + u3_nul, sac, req_u->peq_u, _http_cache_scry_cb); return c3y; } u3z(sac); @@ -953,6 +980,7 @@ _http_cancel_respond(u3_hreq* req_u) { switch ( req_u->sat_e ) { case u3_rsat_init: u3_assert(0); + case u3_rsat_peek: u3_assert(0); case u3_rsat_plan: { req_u->sat_e = u3_rsat_ripe; // XX confirm From 43126473317d18f004034b9add2024e744f47af0 Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Mon, 15 May 2023 17:05:18 -0400 Subject: [PATCH 5/6] http: zero-initialize request structs --- pkg/vere/io/http.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/pkg/vere/io/http.c b/pkg/vere/io/http.c index 481b2e73b0..06a7cff43c 100644 --- a/pkg/vere/io/http.c +++ b/pkg/vere/io/http.c @@ -611,11 +611,9 @@ _http_req_new(u3_hcon* hon_u, h2o_req_t* rec_u) { u3_hreq* req_u = h2o_mem_alloc_shared(&rec_u->pool, sizeof(*req_u), _http_req_done); + memset(req_u, 0, sizeof(*req_u)); req_u->rec_u = rec_u; req_u->sat_e = u3_rsat_init; - req_u->tim_u = 0; - req_u->gen_u = 0; - req_u->pre_u = 0; _http_req_link(hon_u, req_u); @@ -629,11 +627,9 @@ _http_seq_new(u3_hcon* hon_u, h2o_req_t* rec_u) { u3_hreq* req_u = h2o_mem_alloc_shared(&rec_u->pool, sizeof(*req_u), _http_seq_done); + memset(req_u, 0, sizeof(*req_u)); req_u->rec_u = rec_u; req_u->sat_e = u3_rsat_plan; - req_u->tim_u = 0; - req_u->gen_u = 0; - req_u->pre_u = 0; _http_seq_link(hon_u, req_u); From c1a93cb263777225cfcfad7158a0f75fb71079d4 Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Tue, 16 May 2023 20:44:03 -0400 Subject: [PATCH 6/6] vere: bumps version to 2.6 --- VERSION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/VERSION b/VERSION index 95e3ba8192..5154b3f68e 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -2.5 +2.6