1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
|
/*
* 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_request.h"
#include <mruby/variable.h>
#include <mruby/string.h>
#include <mruby/hash.h>
#include <mruby/array.h>
#include "shrpx_downstream.h"
#include "shrpx_upstream.h"
#include "shrpx_client_handler.h"
#include "shrpx_mruby.h"
#include "shrpx_mruby_module.h"
#include "shrpx_log.h"
#include "util.h"
#include "http2.h"
namespace shrpx {
namespace mruby {
namespace {
mrb_value request_init(mrb_state *mrb, mrb_value self) { return self; }
} // namespace
namespace {
mrb_value request_get_http_version_major(mrb_state *mrb, mrb_value self) {
auto data = static_cast<MRubyAssocData *>(mrb->ud);
auto downstream = data->downstream;
const auto &req = downstream->request();
return mrb_fixnum_value(req.http_major);
}
} // namespace
namespace {
mrb_value request_get_http_version_minor(mrb_state *mrb, mrb_value self) {
auto data = static_cast<MRubyAssocData *>(mrb->ud);
auto downstream = data->downstream;
const auto &req = downstream->request();
return mrb_fixnum_value(req.http_minor);
}
} // namespace
namespace {
mrb_value request_get_method(mrb_state *mrb, mrb_value self) {
auto data = static_cast<MRubyAssocData *>(mrb->ud);
auto downstream = data->downstream;
const auto &req = downstream->request();
auto method = http2::to_method_string(req.method);
return mrb_str_new(mrb, method.c_str(), method.size());
}
} // namespace
namespace {
mrb_value request_set_method(mrb_state *mrb, mrb_value self) {
auto data = static_cast<MRubyAssocData *>(mrb->ud);
auto downstream = data->downstream;
auto &req = downstream->request();
check_phase(mrb, data->phase, PHASE_REQUEST);
const char *method;
mrb_int n;
mrb_get_args(mrb, "s", &method, &n);
if (n == 0) {
mrb_raise(mrb, E_RUNTIME_ERROR, "method must not be empty string");
}
auto token =
http2::lookup_method_token(reinterpret_cast<const uint8_t *>(method), n);
if (token == -1) {
mrb_raise(mrb, E_RUNTIME_ERROR, "method not supported");
}
req.method = token;
return self;
}
} // namespace
namespace {
mrb_value request_get_authority(mrb_state *mrb, mrb_value self) {
auto data = static_cast<MRubyAssocData *>(mrb->ud);
auto downstream = data->downstream;
const auto &req = downstream->request();
return mrb_str_new(mrb, req.authority.c_str(), req.authority.size());
}
} // namespace
namespace {
mrb_value request_set_authority(mrb_state *mrb, mrb_value self) {
auto data = static_cast<MRubyAssocData *>(mrb->ud);
auto downstream = data->downstream;
auto &req = downstream->request();
auto &balloc = downstream->get_block_allocator();
check_phase(mrb, data->phase, PHASE_REQUEST);
const char *authority;
mrb_int n;
mrb_get_args(mrb, "s", &authority, &n);
if (n == 0) {
mrb_raise(mrb, E_RUNTIME_ERROR, "authority must not be empty string");
}
req.authority =
make_string_ref(balloc, StringRef{authority, static_cast<size_t>(n)});
return self;
}
} // namespace
namespace {
mrb_value request_get_scheme(mrb_state *mrb, mrb_value self) {
auto data = static_cast<MRubyAssocData *>(mrb->ud);
auto downstream = data->downstream;
const auto &req = downstream->request();
return mrb_str_new(mrb, req.scheme.c_str(), req.scheme.size());
}
} // namespace
namespace {
mrb_value request_set_scheme(mrb_state *mrb, mrb_value self) {
auto data = static_cast<MRubyAssocData *>(mrb->ud);
auto downstream = data->downstream;
auto &req = downstream->request();
auto &balloc = downstream->get_block_allocator();
check_phase(mrb, data->phase, PHASE_REQUEST);
const char *scheme;
mrb_int n;
mrb_get_args(mrb, "s", &scheme, &n);
if (n == 0) {
mrb_raise(mrb, E_RUNTIME_ERROR, "scheme must not be empty string");
}
req.scheme =
make_string_ref(balloc, StringRef{scheme, static_cast<size_t>(n)});
return self;
}
} // namespace
namespace {
mrb_value request_get_path(mrb_state *mrb, mrb_value self) {
auto data = static_cast<MRubyAssocData *>(mrb->ud);
auto downstream = data->downstream;
const auto &req = downstream->request();
return mrb_str_new(mrb, req.path.c_str(), req.path.size());
}
} // namespace
namespace {
mrb_value request_set_path(mrb_state *mrb, mrb_value self) {
auto data = static_cast<MRubyAssocData *>(mrb->ud);
auto downstream = data->downstream;
auto &req = downstream->request();
auto &balloc = downstream->get_block_allocator();
check_phase(mrb, data->phase, PHASE_REQUEST);
const char *path;
mrb_int pathlen;
mrb_get_args(mrb, "s", &path, &pathlen);
req.path =
make_string_ref(balloc, StringRef{path, static_cast<size_t>(pathlen)});
return self;
}
} // namespace
namespace {
mrb_value request_get_headers(mrb_state *mrb, mrb_value self) {
auto data = static_cast<MRubyAssocData *>(mrb->ud);
auto downstream = data->downstream;
const auto &req = downstream->request();
return create_headers_hash(mrb, req.fs.headers());
}
} // namespace
namespace {
mrb_value request_mod_header(mrb_state *mrb, mrb_value self, bool repl) {
auto data = static_cast<MRubyAssocData *>(mrb->ud);
auto downstream = data->downstream;
auto &req = downstream->request();
auto &balloc = downstream->get_block_allocator();
check_phase(mrb, data->phase, PHASE_REQUEST);
mrb_value key, values;
mrb_get_args(mrb, "So", &key, &values);
if (RSTRING_LEN(key) == 0) {
mrb_raise(mrb, E_RUNTIME_ERROR, "empty key is not allowed");
}
auto ai = mrb_gc_arena_save(mrb);
key = mrb_funcall(mrb, key, "downcase", 0);
auto keyref =
make_string_ref(balloc, StringRef{RSTRING_PTR(key),
static_cast<size_t>(RSTRING_LEN(key))});
mrb_gc_arena_restore(mrb, ai);
auto token = http2::lookup_token(keyref.byte(), keyref.size());
if (repl) {
size_t p = 0;
auto &headers = req.fs.headers();
for (size_t i = 0; i < headers.size(); ++i) {
auto &kv = headers[i];
if (kv.name == keyref) {
continue;
}
if (i != p) {
headers[p] = std::move(kv);
}
++p;
}
headers.resize(p);
}
if (mrb_array_p(values)) {
auto n = RARRAY_LEN(values);
for (int i = 0; i < n; ++i) {
auto value = mrb_ary_ref(mrb, values, i);
if (!mrb_string_p(value)) {
mrb_raise(mrb, E_RUNTIME_ERROR, "value must be string");
}
req.fs.add_header_token(
keyref,
make_string_ref(balloc,
StringRef{RSTRING_PTR(value),
static_cast<size_t>(RSTRING_LEN(value))}),
false, token);
}
} else if (mrb_string_p(values)) {
req.fs.add_header_token(
keyref,
make_string_ref(balloc,
StringRef{RSTRING_PTR(values),
static_cast<size_t>(RSTRING_LEN(values))}),
false, token);
} else {
mrb_raise(mrb, E_RUNTIME_ERROR, "value must be string");
}
return mrb_nil_value();
}
} // namespace
namespace {
mrb_value request_set_header(mrb_state *mrb, mrb_value self) {
return request_mod_header(mrb, self, true);
}
} // namespace
namespace {
mrb_value request_add_header(mrb_state *mrb, mrb_value self) {
return request_mod_header(mrb, self, false);
}
} // namespace
namespace {
mrb_value request_clear_headers(mrb_state *mrb, mrb_value self) {
auto data = static_cast<MRubyAssocData *>(mrb->ud);
auto downstream = data->downstream;
auto &req = downstream->request();
check_phase(mrb, data->phase, PHASE_REQUEST);
req.fs.clear_headers();
return mrb_nil_value();
}
} // namespace
namespace {
mrb_value request_push(mrb_state *mrb, mrb_value self) {
auto data = static_cast<MRubyAssocData *>(mrb->ud);
auto downstream = data->downstream;
auto upstream = downstream->get_upstream();
const char *uri;
mrb_int len;
mrb_get_args(mrb, "s", &uri, &len);
upstream->initiate_push(downstream, StringRef{uri, static_cast<size_t>(len)});
return mrb_nil_value();
}
} // namespace
void init_request_class(mrb_state *mrb, RClass *module) {
auto request_class =
mrb_define_class_under(mrb, module, "Request", mrb->object_class);
mrb_define_method(mrb, request_class, "initialize", request_init,
MRB_ARGS_NONE());
mrb_define_method(mrb, request_class, "http_version_major",
request_get_http_version_major, MRB_ARGS_NONE());
mrb_define_method(mrb, request_class, "http_version_minor",
request_get_http_version_minor, MRB_ARGS_NONE());
mrb_define_method(mrb, request_class, "method", request_get_method,
MRB_ARGS_NONE());
mrb_define_method(mrb, request_class, "method=", request_set_method,
MRB_ARGS_REQ(1));
mrb_define_method(mrb, request_class, "authority", request_get_authority,
MRB_ARGS_NONE());
mrb_define_method(mrb, request_class, "authority=", request_set_authority,
MRB_ARGS_REQ(1));
mrb_define_method(mrb, request_class, "scheme", request_get_scheme,
MRB_ARGS_NONE());
mrb_define_method(mrb, request_class, "scheme=", request_set_scheme,
MRB_ARGS_REQ(1));
mrb_define_method(mrb, request_class, "path", request_get_path,
MRB_ARGS_NONE());
mrb_define_method(mrb, request_class, "path=", request_set_path,
MRB_ARGS_REQ(1));
mrb_define_method(mrb, request_class, "headers", request_get_headers,
MRB_ARGS_NONE());
mrb_define_method(mrb, request_class, "add_header", request_add_header,
MRB_ARGS_REQ(2));
mrb_define_method(mrb, request_class, "set_header", request_set_header,
MRB_ARGS_REQ(2));
mrb_define_method(mrb, request_class, "clear_headers", request_clear_headers,
MRB_ARGS_NONE());
mrb_define_method(mrb, request_class, "push", request_push, MRB_ARGS_REQ(1));
}
} // namespace mruby
} // namespace shrpx
|