static int on_body(h2o_http1client_t *client, const char *errstr) { struct st_h2o_mruby_http_request_context_t *ctx = client->data; if (errstr != NULL) { h2o_buffer_t *tmp = ctx->resp.after_closed; ctx->resp.after_closed = client->sock->input; client->sock->input = tmp; ctx->client = NULL; ctx->resp.has_content = 1; } else if (client->sock->input->size != 0) { ctx->resp.has_content = 1; } if (ctx->resp.has_content) { if (ctx->shortcut_notify_cb != NULL) { ctx->shortcut_notify_cb(ctx->generator); } else if (!mrb_nil_p(ctx->receiver)) { int gc_arena = mrb_gc_arena_save(ctx->generator->ctx->mrb); mrb_value chunk = build_chunk(ctx); h2o_mruby_run_fiber(ctx->generator, detach_receiver(ctx), chunk, gc_arena, NULL); } } return 0; }
static void on_dispose(void *_ctx) { struct st_h2o_mruby_http_request_context_t *ctx = _ctx; /* clear the refs */ if (ctx->client != NULL) { h2o_http1client_cancel(ctx->client); ctx->client = NULL; } if (!mrb_nil_p(ctx->refs.request)) DATA_PTR(ctx->refs.request) = NULL; if (!mrb_nil_p(ctx->refs.input_stream)) DATA_PTR(ctx->refs.input_stream) = NULL; /* clear bufs */ h2o_buffer_dispose(&ctx->req.buf); h2o_buffer_dispose(&ctx->resp.after_closed); /* notify the app, if it is waiting to hear from us */ if (!mrb_nil_p(ctx->receiver)) { mrb_state *mrb = ctx->generator->ctx->mrb; int gc_arena = mrb_gc_arena_save(mrb); h2o_mruby_run_fiber(ctx->generator, detach_receiver(ctx), create_downstream_closed_exception(mrb), gc_arena, NULL); } }
static void post_response(struct st_h2o_mruby_http_request_context_t *ctx, int status, const h2o_http1client_header_t *headers_sorted, size_t num_headers) { mrb_state *mrb = ctx->generator->ctx->shared->mrb; int gc_arena = mrb_gc_arena_save(mrb); size_t i; mrb_value resp = mrb_ary_new_capa(mrb, 3); /* set status */ mrb_ary_set(mrb, resp, 0, mrb_fixnum_value(status)); /* set headers */ mrb_value headers_hash = mrb_hash_new_capa(mrb, (int)num_headers); for (i = 0; i < num_headers; ++i) { /* skip the headers, we determine the eos! */ if (h2o_memis(headers_sorted[i].name, headers_sorted[i].name_len, H2O_STRLIT("content-length")) || h2o_memis(headers_sorted[i].name, headers_sorted[i].name_len, H2O_STRLIT("transfer-encoding"))) continue; /* build and set the hash entry */ mrb_value k = mrb_str_new(mrb, headers_sorted[i].name, headers_sorted[i].name_len); mrb_value v = mrb_str_new(mrb, headers_sorted[i].value, headers_sorted[i].value_len); while (i + 1 < num_headers && h2o_memis(headers_sorted[i].name, headers_sorted[i].name_len, headers_sorted[i + 1].name, headers_sorted[i + 1].name_len)) { ++i; v = mrb_str_cat_lit(mrb, v, "\n"); v = mrb_str_cat(mrb, v, headers_sorted[i].value, headers_sorted[i].value_len); } mrb_hash_set(mrb, headers_hash, k, v); } mrb_ary_set(mrb, resp, 1, headers_hash); /* set input stream */ assert(mrb_nil_p(ctx->refs.input_stream)); ctx->refs.input_stream = h2o_mruby_create_data_instance( mrb, mrb_ary_entry(ctx->generator->ctx->shared->constants, H2O_MRUBY_HTTP_INPUT_STREAM_CLASS), ctx, &input_stream_type); mrb_ary_set(mrb, resp, 2, ctx->refs.input_stream); if (mrb_nil_p(ctx->receiver)) { /* is async */ mrb_funcall(mrb, ctx->refs.request, "_set_response", 1, resp); if (mrb->exc != NULL) { fprintf(stderr, "_set_response failed\n"); abort(); } } else { /* send response to the waiting receiver */ h2o_mruby_run_fiber(ctx->generator, detach_receiver(ctx), resp, NULL); } mrb_gc_arena_restore(mrb, gc_arena); }