コード例 #1
0
ファイル: http_request.c プロジェクト: simudream/h2o
void h2o_mruby_http_request_init_context(h2o_mruby_context_t *ctx)
{
    mrb_state *mrb = ctx->mrb;
    struct RClass *module, *klass;

    mrb_define_method(mrb, mrb->kernel_module, "http_request", http_request_method, MRB_ARGS_ARG(1, 2));

    module = mrb_define_module(mrb, "H2O");
    klass = mrb_define_class_under(mrb, module, "HttpRequest", mrb->object_class);
    mrb_ary_set(mrb, ctx->constants, H2O_MRUBY_HTTP_REQUEST_CLASS, mrb_obj_value(klass));

    klass = mrb_define_class_under(mrb, module, "HttpInputStream", mrb->object_class);
    mrb_ary_set(mrb, ctx->constants, H2O_MRUBY_HTTP_INPUT_STREAM_CLASS, mrb_obj_value(klass));

    h2o_mruby_define_callback(mrb, "_h2o__http_join_response", H2O_MRUBY_CALLBACK_ID_HTTP_JOIN_RESPONSE);
    h2o_mruby_define_callback(mrb, "_h2o__http_fetch_chunk", H2O_MRUBY_CALLBACK_ID_HTTP_FETCH_CHUNK);

    h2o_mruby_eval_expr(mrb, "module H2O\n"
                             "  class HttpRequest\n"
                             "    def join\n"
                             "      if !@resp\n"
                             "        @resp = _h2o__http_join_response(self)\n"
                             "      end\n"
                             "      @resp\n"
                             "    end\n"
                             "    def _set_response(resp)\n"
                             "      @resp = resp\n"
                             "    end\n"
                             "  end\n"
                             "  class HttpInputStream\n"
                             "    def each\n"
                             "      while c = _h2o__http_fetch_chunk(self)\n"
                             "        yield c\n"
                             "      end\n"
                             "    end\n"
                             "    def join\n"
                             "      s = \"\"\n"
                             "      each do |c|\n"
                             "        s << c\n"
                             "      end\n"
                             "      s\n"
                             "    end\n"
                             "  end\n"
                             "end");
    h2o_mruby_assert(mrb);
}
コード例 #2
0
ファイル: chunked.c プロジェクト: LakhanRathi92/h2o
void h2o_mruby_send_chunked_init_context(h2o_mruby_shared_context_t *shared_ctx)
{
    mrb_state *mrb = shared_ctx->mrb;

    mrb_define_method(mrb, mrb->kernel_module, "_h2o_send_chunk", send_chunked_method, MRB_ARGS_ARG(1, 0));
    h2o_mruby_define_callback(mrb, "_h2o_send_chunk_eos", H2O_MRUBY_CALLBACK_ID_SEND_CHUNKED_EOS);
    mrb_ary_set(mrb, shared_ctx->constants, H2O_MRUBY_CHUNKED_PROC_EACH_TO_FIBER,
                h2o_mruby_eval_expr(mrb, "Proc.new do |src|\n"
                                         "  fiber = Fiber.new do\n"
                                         "    src.each do |chunk|\n"
                                         "      _h2o_send_chunk(chunk)\n"
                                         "    end\n"
                                         "    _h2o_send_chunk_eos()\n"
                                         "  end\n"
                                         "  fiber.resume\n"
                                         "end"));
    h2o_mruby_assert(mrb);
}