Skip to content

Commit

Permalink
Release vere-v2.6 to soon (#398)
Browse files Browse the repository at this point in the history
  • Loading branch information
pkova authored May 17, 2023
2 parents dcd7e0a + 75df849 commit 44eaca1
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 30 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.5
2.6
101 changes: 72 additions & 29 deletions pkg/vere/io/http.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;

Expand All @@ -589,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);

Expand All @@ -607,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);

Expand Down Expand Up @@ -651,33 +669,53 @@ _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);
}

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, 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
Expand All @@ -698,13 +736,17 @@ _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);
_http_cache_respond(req_u, nac);
return c3y;
}
Expand Down Expand Up @@ -934,6 +976,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
Expand Down

0 comments on commit 44eaca1

Please sign in to comment.