1 /*
2 * nghttp2 - HTTP/2 C Library
3 *
4 * Copyright (c) 2015 Tatsuhiro Tsujikawa
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25 #include "shrpx_mruby_module.h"
26
27 #include <array>
28
29 #include <mruby/variable.h>
30 #include <mruby/string.h>
31 #include <mruby/hash.h>
32 #include <mruby/array.h>
33
34 #include "shrpx_mruby.h"
35 #include "shrpx_mruby_module_env.h"
36 #include "shrpx_mruby_module_request.h"
37 #include "shrpx_mruby_module_response.h"
38
39 namespace shrpx {
40
41 namespace mruby {
42
43 namespace {
create_env(mrb_state * mrb)44 mrb_value create_env(mrb_state *mrb) {
45 auto module = mrb_module_get(mrb, "Nghttpx");
46
47 auto env_class = mrb_class_get_under(mrb, module, "Env");
48 auto request_class = mrb_class_get_under(mrb, module, "Request");
49 auto response_class = mrb_class_get_under(mrb, module, "Response");
50
51 auto env = mrb_obj_new(mrb, env_class, 0, nullptr);
52 auto req = mrb_obj_new(mrb, request_class, 0, nullptr);
53 auto resp = mrb_obj_new(mrb, response_class, 0, nullptr);
54
55 mrb_iv_set(mrb, env, mrb_intern_lit(mrb, "req"), req);
56 mrb_iv_set(mrb, env, mrb_intern_lit(mrb, "resp"), resp);
57
58 return env;
59 }
60 } // namespace
61
delete_downstream_from_module(mrb_state * mrb,Downstream * downstream)62 void delete_downstream_from_module(mrb_state *mrb, Downstream *downstream) {
63 auto module = mrb_module_get(mrb, "Nghttpx");
64 auto env = mrb_obj_iv_get(mrb, reinterpret_cast<RObject *>(module),
65 mrb_intern_lit(mrb, "env"));
66 if (mrb_nil_p(env)) {
67 return;
68 }
69
70 mrb_iv_remove(mrb, env, intern_ptr(mrb, downstream));
71 }
72
init_module(mrb_state * mrb)73 mrb_value init_module(mrb_state *mrb) {
74 auto module = mrb_define_module(mrb, "Nghttpx");
75
76 mrb_define_const(mrb, module, "REQUEST_PHASE",
77 mrb_fixnum_value(PHASE_REQUEST));
78 mrb_define_const(mrb, module, "RESPONSE_PHASE",
79 mrb_fixnum_value(PHASE_RESPONSE));
80
81 init_env_class(mrb, module);
82 init_request_class(mrb, module);
83 init_response_class(mrb, module);
84
85 return create_env(mrb);
86 }
87
create_headers_hash(mrb_state * mrb,const HeaderRefs & headers)88 mrb_value create_headers_hash(mrb_state *mrb, const HeaderRefs &headers) {
89 auto hash = mrb_hash_new(mrb);
90
91 for (auto &hd : headers) {
92 if (hd.name.empty() || hd.name[0] == ':') {
93 continue;
94 }
95 auto ai = mrb_gc_arena_save(mrb);
96
97 auto key = mrb_str_new(mrb, hd.name.c_str(), hd.name.size());
98 auto ary = mrb_hash_get(mrb, hash, key);
99 if (mrb_nil_p(ary)) {
100 ary = mrb_ary_new(mrb);
101 mrb_hash_set(mrb, hash, key, ary);
102 }
103 mrb_ary_push(mrb, ary, mrb_str_new(mrb, hd.value.c_str(), hd.value.size()));
104
105 mrb_gc_arena_restore(mrb, ai);
106 }
107
108 return hash;
109 }
110
111 } // namespace mruby
112
113 } // namespace shrpx
114