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_module.h"
|
|
|
|
|
2015-09-02 15:38:57 +02:00
|
|
|
#include <array>
|
|
|
|
|
2015-09-01 17:19:32 +02:00
|
|
|
#include <mruby/variable.h>
|
|
|
|
#include <mruby/string.h>
|
2015-09-02 15:25:04 +02:00
|
|
|
#include <mruby/hash.h>
|
|
|
|
#include <mruby/array.h>
|
2015-09-01 17:19:32 +02:00
|
|
|
|
2015-09-02 15:25:04 +02:00
|
|
|
#include "shrpx_mruby.h"
|
2015-09-04 16:56:07 +02:00
|
|
|
#include "shrpx_mruby_module_env.h"
|
2015-09-02 15:38:57 +02:00
|
|
|
#include "shrpx_mruby_module_request.h"
|
2015-09-02 17:40:14 +02:00
|
|
|
#include "shrpx_mruby_module_response.h"
|
2015-09-01 17:19:32 +02:00
|
|
|
|
|
|
|
namespace shrpx {
|
|
|
|
|
|
|
|
namespace mruby {
|
|
|
|
|
2015-09-02 15:25:04 +02:00
|
|
|
namespace {
|
2015-10-05 17:10:42 +02:00
|
|
|
mrb_value create_env(mrb_state *mrb) {
|
2015-09-02 15:25:04 +02:00
|
|
|
auto module = mrb_module_get(mrb, "Nghttpx");
|
2015-09-01 17:19:32 +02:00
|
|
|
|
2015-10-05 17:10:42 +02:00
|
|
|
auto env_class = mrb_class_get_under(mrb, module, "Env");
|
|
|
|
auto request_class = mrb_class_get_under(mrb, module, "Request");
|
|
|
|
auto response_class = mrb_class_get_under(mrb, module, "Response");
|
2015-09-04 16:56:07 +02:00
|
|
|
|
2015-10-05 17:10:42 +02:00
|
|
|
auto env = mrb_obj_new(mrb, env_class, 0, nullptr);
|
|
|
|
auto req = mrb_obj_new(mrb, request_class, 0, nullptr);
|
|
|
|
auto resp = mrb_obj_new(mrb, response_class, 0, nullptr);
|
2015-09-04 16:56:07 +02:00
|
|
|
|
2015-10-05 17:10:42 +02:00
|
|
|
mrb_iv_set(mrb, env, mrb_intern_lit(mrb, "req"), req);
|
|
|
|
mrb_iv_set(mrb, env, mrb_intern_lit(mrb, "resp"), resp);
|
2015-09-04 16:56:07 +02:00
|
|
|
|
2015-10-05 17:10:42 +02:00
|
|
|
return env;
|
2015-09-01 17:19:32 +02:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2015-09-04 16:56:07 +02:00
|
|
|
void delete_downstream_from_module(mrb_state *mrb, Downstream *downstream) {
|
|
|
|
auto module = mrb_module_get(mrb, "Nghttpx");
|
|
|
|
auto env = mrb_obj_iv_get(mrb, reinterpret_cast<RObject *>(module),
|
|
|
|
mrb_intern_lit(mrb, "env"));
|
|
|
|
if (mrb_nil_p(env)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mrb_iv_remove(mrb, env, intern_ptr(mrb, downstream));
|
|
|
|
}
|
|
|
|
|
2015-10-05 17:10:42 +02:00
|
|
|
mrb_value init_module(mrb_state *mrb) {
|
2015-09-01 17:19:32 +02:00
|
|
|
auto module = mrb_define_module(mrb, "Nghttpx");
|
|
|
|
|
2015-09-04 18:07:29 +02:00
|
|
|
mrb_define_const(mrb, module, "REQUEST_PHASE",
|
|
|
|
mrb_fixnum_value(PHASE_REQUEST));
|
|
|
|
mrb_define_const(mrb, module, "RESPONSE_PHASE",
|
|
|
|
mrb_fixnum_value(PHASE_RESPONSE));
|
2015-09-02 15:25:04 +02:00
|
|
|
|
2015-09-04 16:56:07 +02:00
|
|
|
init_env_class(mrb, module);
|
2015-09-01 17:19:32 +02:00
|
|
|
init_request_class(mrb, module);
|
2015-09-02 17:40:14 +02:00
|
|
|
init_response_class(mrb, module);
|
2015-10-05 17:10:42 +02:00
|
|
|
|
|
|
|
return create_env(mrb);
|
2015-09-01 17:19:32 +02:00
|
|
|
}
|
|
|
|
|
2016-03-09 13:15:32 +01:00
|
|
|
mrb_value create_headers_hash(mrb_state *mrb, const HeaderRefs &headers) {
|
2015-09-02 15:38:57 +02:00
|
|
|
auto hash = mrb_hash_new(mrb);
|
|
|
|
|
|
|
|
for (auto &hd : headers) {
|
|
|
|
if (hd.name.empty() || hd.name[0] == ':') {
|
|
|
|
continue;
|
|
|
|
}
|
2015-09-04 18:14:17 +02:00
|
|
|
auto ai = mrb_gc_arena_save(mrb);
|
|
|
|
|
2015-09-02 15:38:57 +02:00
|
|
|
auto key = mrb_str_new(mrb, hd.name.c_str(), hd.name.size());
|
|
|
|
auto ary = mrb_hash_get(mrb, hash, key);
|
|
|
|
if (mrb_nil_p(ary)) {
|
|
|
|
ary = mrb_ary_new(mrb);
|
|
|
|
mrb_hash_set(mrb, hash, key, ary);
|
|
|
|
}
|
|
|
|
mrb_ary_push(mrb, ary, mrb_str_new(mrb, hd.value.c_str(), hd.value.size()));
|
2015-09-04 18:14:17 +02:00
|
|
|
|
|
|
|
mrb_gc_arena_restore(mrb, ai);
|
2015-09-02 15:38:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
2015-09-01 17:19:32 +02:00
|
|
|
} // namespace mruby
|
|
|
|
|
|
|
|
} // namespace shrpx
|