2015-09-01 17:19:32 +02:00
|
|
|
/*
|
|
|
|
* nghttp2 - HTTP/2 C Library
|
|
|
|
*
|
|
|
|
* Copyright (c) 2015 Tatsuhiro Tsujikawa
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
#include "shrpx_mruby.h"
|
|
|
|
|
|
|
|
#include <mruby/compile.h>
|
|
|
|
#include <mruby/string.h>
|
|
|
|
|
|
|
|
#include "shrpx_downstream.h"
|
|
|
|
#include "shrpx_config.h"
|
|
|
|
#include "shrpx_mruby_module.h"
|
2015-09-02 16:21:45 +02:00
|
|
|
#include "shrpx_downstream_connection.h"
|
2017-02-16 14:46:22 +01:00
|
|
|
#include "shrpx_log.h"
|
2015-09-01 17:19:32 +02:00
|
|
|
|
|
|
|
namespace shrpx {
|
|
|
|
|
|
|
|
namespace mruby {
|
|
|
|
|
2015-10-05 17:10:42 +02:00
|
|
|
MRubyContext::MRubyContext(mrb_state *mrb, mrb_value app, mrb_value env)
|
|
|
|
: mrb_(mrb), app_(std::move(app)), env_(std::move(env)) {}
|
2015-09-01 17:19:32 +02:00
|
|
|
|
2015-09-11 19:40:55 +02:00
|
|
|
MRubyContext::~MRubyContext() {
|
|
|
|
if (mrb_) {
|
|
|
|
mrb_close(mrb_);
|
|
|
|
}
|
|
|
|
}
|
2015-09-01 17:19:32 +02:00
|
|
|
|
2015-10-05 17:10:42 +02:00
|
|
|
int MRubyContext::run_app(Downstream *downstream, int phase) {
|
|
|
|
if (!mrb_) {
|
2015-09-01 17:19:32 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-09-04 18:07:29 +02:00
|
|
|
MRubyAssocData data{downstream, phase};
|
2015-09-02 15:25:04 +02:00
|
|
|
|
2015-09-02 18:32:15 +02:00
|
|
|
mrb_->ud = &data;
|
2015-09-01 17:19:32 +02:00
|
|
|
|
|
|
|
int rv = 0;
|
2015-09-02 18:32:15 +02:00
|
|
|
auto ai = mrb_gc_arena_save(mrb_);
|
2015-10-05 17:10:42 +02:00
|
|
|
auto ai_d = defer([ai, this]() { mrb_gc_arena_restore(mrb_, ai); });
|
|
|
|
|
|
|
|
const char *method;
|
|
|
|
switch (phase) {
|
|
|
|
case PHASE_REQUEST:
|
|
|
|
if (!mrb_respond_to(mrb_, app_, mrb_intern_lit(mrb_, "on_req"))) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
method = "on_req";
|
|
|
|
break;
|
|
|
|
case PHASE_RESPONSE:
|
|
|
|
if (!mrb_respond_to(mrb_, app_, mrb_intern_lit(mrb_, "on_resp"))) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
method = "on_resp";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
2015-09-01 17:19:32 +02:00
|
|
|
|
2015-10-05 17:10:42 +02:00
|
|
|
auto res = mrb_funcall(mrb_, app_, method, 1, env_);
|
2015-09-01 17:19:32 +02:00
|
|
|
(void)res;
|
|
|
|
|
2015-09-02 18:32:15 +02:00
|
|
|
if (mrb_->exc) {
|
2015-09-03 15:29:16 +02:00
|
|
|
// If response has been committed, ignore error
|
2018-10-17 03:03:32 +02:00
|
|
|
if (downstream->get_response_state() != DownstreamState::MSG_COMPLETE) {
|
2015-09-03 15:29:16 +02:00
|
|
|
rv = -1;
|
|
|
|
}
|
2015-10-05 17:10:42 +02:00
|
|
|
|
|
|
|
auto exc = mrb_obj_value(mrb_->exc);
|
|
|
|
auto inspect = mrb_inspect(mrb_, exc);
|
2015-09-01 17:19:32 +02:00
|
|
|
|
|
|
|
LOG(ERROR) << "Exception caught while executing mruby code: "
|
2015-10-05 17:10:42 +02:00
|
|
|
<< mrb_str_to_cstr(mrb_, inspect);
|
2015-09-01 17:19:32 +02:00
|
|
|
}
|
|
|
|
|
2015-09-02 18:32:15 +02:00
|
|
|
mrb_->ud = nullptr;
|
2015-09-01 17:19:32 +02:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
2015-09-02 15:25:04 +02:00
|
|
|
|
|
|
|
int MRubyContext::run_on_request_proc(Downstream *downstream) {
|
2015-10-05 17:10:42 +02:00
|
|
|
return run_app(downstream, PHASE_REQUEST);
|
2015-09-02 15:25:04 +02:00
|
|
|
}
|
2015-09-01 17:19:32 +02:00
|
|
|
|
2015-09-02 15:25:04 +02:00
|
|
|
int MRubyContext::run_on_response_proc(Downstream *downstream) {
|
2015-10-05 17:10:42 +02:00
|
|
|
return run_app(downstream, PHASE_RESPONSE);
|
2015-09-01 17:19:32 +02:00
|
|
|
}
|
|
|
|
|
2015-09-04 16:56:07 +02:00
|
|
|
void MRubyContext::delete_downstream(Downstream *downstream) {
|
2015-09-04 18:20:32 +02:00
|
|
|
if (!mrb_) {
|
|
|
|
return;
|
|
|
|
}
|
2015-09-04 16:56:07 +02:00
|
|
|
delete_downstream_from_module(mrb_, downstream);
|
|
|
|
}
|
|
|
|
|
2015-10-05 17:10:42 +02:00
|
|
|
namespace {
|
|
|
|
mrb_value instantiate_app(mrb_state *mrb, RProc *proc) {
|
|
|
|
mrb->ud = nullptr;
|
|
|
|
|
|
|
|
auto res = mrb_run(mrb, proc, mrb_top_self(mrb));
|
|
|
|
|
|
|
|
if (mrb->exc) {
|
|
|
|
auto exc = mrb_obj_value(mrb->exc);
|
|
|
|
auto inspect = mrb_inspect(mrb, exc);
|
|
|
|
|
|
|
|
LOG(ERROR) << "Exception caught while executing mruby code: "
|
|
|
|
<< mrb_str_to_cstr(mrb, inspect);
|
|
|
|
|
|
|
|
return mrb_nil_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2015-09-01 17:19:32 +02:00
|
|
|
// Based on
|
|
|
|
// https://github.com/h2o/h2o/blob/master/lib/handler/mruby.c. It is
|
|
|
|
// very hard to write these kind of code because mruby has almost no
|
2018-06-03 06:10:32 +02:00
|
|
|
// documentation about compiling or generating code, at least at the
|
2015-09-01 17:19:32 +02:00
|
|
|
// time of this writing.
|
2016-02-14 14:27:59 +01:00
|
|
|
RProc *compile(mrb_state *mrb, const StringRef &filename) {
|
|
|
|
if (filename.empty()) {
|
2015-09-01 17:19:32 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-02-14 14:27:59 +01:00
|
|
|
auto infile = fopen(filename.c_str(), "rb");
|
2015-09-01 17:19:32 +02:00
|
|
|
if (infile == nullptr) {
|
2018-09-30 05:23:01 +02:00
|
|
|
LOG(ERROR) << "Could not open mruby file " << filename;
|
2015-09-01 17:19:32 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
auto infile_d = defer(fclose, infile);
|
|
|
|
|
|
|
|
auto mrbc = mrbc_context_new(mrb);
|
|
|
|
if (mrbc == nullptr) {
|
|
|
|
LOG(ERROR) << "mrb_context_new failed";
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
auto mrbc_d = defer(mrbc_context_free, mrb, mrbc);
|
|
|
|
|
|
|
|
auto parser = mrb_parse_file(mrb, infile, nullptr);
|
|
|
|
if (parser == nullptr) {
|
|
|
|
LOG(ERROR) << "mrb_parse_nstring failed";
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
auto parser_d = defer(mrb_parser_free, parser);
|
|
|
|
|
|
|
|
if (parser->nerr != 0) {
|
|
|
|
LOG(ERROR) << "mruby parser detected parse error";
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto proc = mrb_generate_code(mrb, parser);
|
|
|
|
if (proc == nullptr) {
|
|
|
|
LOG(ERROR) << "mrb_generate_code failed";
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return proc;
|
|
|
|
}
|
|
|
|
|
2016-02-14 14:27:59 +01:00
|
|
|
std::unique_ptr<MRubyContext> create_mruby_context(const StringRef &filename) {
|
|
|
|
if (filename.empty()) {
|
2018-10-15 16:02:44 +02:00
|
|
|
return std::make_unique<MRubyContext>(nullptr, mrb_nil_value(),
|
|
|
|
mrb_nil_value());
|
2015-09-04 16:56:07 +02:00
|
|
|
}
|
|
|
|
|
2015-09-01 17:19:32 +02:00
|
|
|
auto mrb = mrb_open();
|
|
|
|
if (mrb == nullptr) {
|
|
|
|
LOG(ERROR) << "mrb_open failed";
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-10-05 17:10:42 +02:00
|
|
|
auto ai = mrb_gc_arena_save(mrb);
|
2015-09-01 17:19:32 +02:00
|
|
|
|
2015-10-05 17:10:42 +02:00
|
|
|
auto req_proc = compile(mrb, filename);
|
2015-09-01 17:19:32 +02:00
|
|
|
|
2015-10-05 17:10:42 +02:00
|
|
|
if (!req_proc) {
|
|
|
|
mrb_gc_arena_restore(mrb, ai);
|
|
|
|
LOG(ERROR) << "Could not compile mruby code " << filename;
|
2015-09-01 17:19:32 +02:00
|
|
|
mrb_close(mrb);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-10-05 17:10:42 +02:00
|
|
|
auto env = init_module(mrb);
|
2015-09-01 17:19:32 +02:00
|
|
|
|
2015-10-05 17:10:42 +02:00
|
|
|
auto app = instantiate_app(mrb, req_proc);
|
|
|
|
if (mrb_nil_p(app)) {
|
|
|
|
mrb_gc_arena_restore(mrb, ai);
|
|
|
|
LOG(ERROR) << "Could not instantiate mruby app from " << filename;
|
2015-09-01 17:19:32 +02:00
|
|
|
mrb_close(mrb);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-10-05 17:10:42 +02:00
|
|
|
mrb_gc_arena_restore(mrb, ai);
|
|
|
|
|
|
|
|
// TODO These are not necessary, because we retain app and env?
|
|
|
|
mrb_gc_protect(mrb, env);
|
|
|
|
mrb_gc_protect(mrb, app);
|
|
|
|
|
2018-10-15 16:02:44 +02:00
|
|
|
return std::make_unique<MRubyContext>(mrb, std::move(app), std::move(env));
|
2015-09-01 17:19:32 +02:00
|
|
|
}
|
|
|
|
|
2015-09-04 16:56:07 +02:00
|
|
|
mrb_sym intern_ptr(mrb_state *mrb, void *ptr) {
|
|
|
|
auto p = reinterpret_cast<uintptr_t>(ptr);
|
|
|
|
|
|
|
|
return mrb_intern(mrb, reinterpret_cast<const char *>(&p), sizeof(p));
|
|
|
|
}
|
|
|
|
|
2015-09-04 18:07:29 +02:00
|
|
|
void check_phase(mrb_state *mrb, int phase, int phase_mask) {
|
|
|
|
if ((phase & phase_mask) == 0) {
|
|
|
|
mrb_raise(mrb, E_RUNTIME_ERROR, "operation was not allowed in this phase");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-01 17:19:32 +02:00
|
|
|
} // namespace mruby
|
|
|
|
|
|
|
|
} // namespace shrpx
|