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_response.h"
26
27 #include <mruby/variable.h>
28 #include <mruby/string.h>
29 #include <mruby/hash.h>
30 #include <mruby/array.h>
31
32 #include "shrpx_downstream.h"
33 #include "shrpx_upstream.h"
34 #include "shrpx_client_handler.h"
35 #include "shrpx_mruby.h"
36 #include "shrpx_mruby_module.h"
37 #include "shrpx_log.h"
38 #include "util.h"
39 #include "http2.h"
40
41 namespace shrpx {
42
43 namespace mruby {
44
45 namespace {
response_init(mrb_state * mrb,mrb_value self)46 mrb_value response_init(mrb_state *mrb, mrb_value self) { return self; }
47 } // namespace
48
49 namespace {
response_get_http_version_major(mrb_state * mrb,mrb_value self)50 mrb_value response_get_http_version_major(mrb_state *mrb, mrb_value self) {
51 auto data = static_cast<MRubyAssocData *>(mrb->ud);
52 auto downstream = data->downstream;
53 const auto &resp = downstream->response();
54 return mrb_fixnum_value(resp.http_major);
55 }
56 } // namespace
57
58 namespace {
response_get_http_version_minor(mrb_state * mrb,mrb_value self)59 mrb_value response_get_http_version_minor(mrb_state *mrb, mrb_value self) {
60 auto data = static_cast<MRubyAssocData *>(mrb->ud);
61 auto downstream = data->downstream;
62 const auto &resp = downstream->response();
63 return mrb_fixnum_value(resp.http_minor);
64 }
65 } // namespace
66
67 namespace {
response_get_status(mrb_state * mrb,mrb_value self)68 mrb_value response_get_status(mrb_state *mrb, mrb_value self) {
69 auto data = static_cast<MRubyAssocData *>(mrb->ud);
70 auto downstream = data->downstream;
71 const auto &resp = downstream->response();
72 return mrb_fixnum_value(resp.http_status);
73 }
74 } // namespace
75
76 namespace {
response_set_status(mrb_state * mrb,mrb_value self)77 mrb_value response_set_status(mrb_state *mrb, mrb_value self) {
78 auto data = static_cast<MRubyAssocData *>(mrb->ud);
79 auto downstream = data->downstream;
80 auto &resp = downstream->response();
81
82 mrb_int status;
83 mrb_get_args(mrb, "i", &status);
84 // We don't support 1xx status code for mruby scripting yet.
85 if (status < 200 || status > 999) {
86 mrb_raise(mrb, E_RUNTIME_ERROR,
87 "invalid status; it should be [200, 999], inclusive");
88 }
89
90 resp.http_status = status;
91
92 return self;
93 }
94 } // namespace
95
96 namespace {
response_get_headers(mrb_state * mrb,mrb_value self)97 mrb_value response_get_headers(mrb_state *mrb, mrb_value self) {
98 auto data = static_cast<MRubyAssocData *>(mrb->ud);
99 auto downstream = data->downstream;
100 const auto &resp = downstream->response();
101
102 return create_headers_hash(mrb, resp.fs.headers());
103 }
104 } // namespace
105
106 namespace {
response_mod_header(mrb_state * mrb,mrb_value self,bool repl)107 mrb_value response_mod_header(mrb_state *mrb, mrb_value self, bool repl) {
108 auto data = static_cast<MRubyAssocData *>(mrb->ud);
109 auto downstream = data->downstream;
110 auto &resp = downstream->response();
111 auto &balloc = downstream->get_block_allocator();
112
113 mrb_value key, values;
114 mrb_get_args(mrb, "So", &key, &values);
115
116 if (RSTRING_LEN(key) == 0) {
117 mrb_raise(mrb, E_RUNTIME_ERROR, "empty key is not allowed");
118 }
119
120 auto ai = mrb_gc_arena_save(mrb);
121
122 key = mrb_funcall(mrb, key, "downcase", 0);
123
124 auto keyref =
125 make_string_ref(balloc, StringRef{RSTRING_PTR(key),
126 static_cast<size_t>(RSTRING_LEN(key))});
127
128 mrb_gc_arena_restore(mrb, ai);
129
130 auto token = http2::lookup_token(keyref.byte(), keyref.size());
131
132 if (repl) {
133 size_t p = 0;
134 auto &headers = resp.fs.headers();
135 for (size_t i = 0; i < headers.size(); ++i) {
136 auto &kv = headers[i];
137 if (kv.name == keyref) {
138 continue;
139 }
140 if (i != p) {
141 headers[p] = std::move(kv);
142 }
143 ++p;
144 }
145 headers.resize(p);
146 }
147
148 if (mrb_array_p(values)) {
149 auto n = RARRAY_LEN(values);
150 for (int i = 0; i < n; ++i) {
151 auto value = mrb_ary_ref(mrb, values, i);
152 if (!mrb_string_p(value)) {
153 mrb_raise(mrb, E_RUNTIME_ERROR, "value must be string");
154 }
155
156 resp.fs.add_header_token(
157 keyref,
158 make_string_ref(balloc,
159 StringRef{RSTRING_PTR(value),
160 static_cast<size_t>(RSTRING_LEN(value))}),
161 false, token);
162 }
163 } else if (mrb_string_p(values)) {
164 resp.fs.add_header_token(
165 keyref,
166 make_string_ref(balloc,
167 StringRef{RSTRING_PTR(values),
168 static_cast<size_t>(RSTRING_LEN(values))}),
169 false, token);
170 } else {
171 mrb_raise(mrb, E_RUNTIME_ERROR, "value must be string");
172 }
173
174 return mrb_nil_value();
175 }
176 } // namespace
177
178 namespace {
response_set_header(mrb_state * mrb,mrb_value self)179 mrb_value response_set_header(mrb_state *mrb, mrb_value self) {
180 return response_mod_header(mrb, self, true);
181 }
182 } // namespace
183
184 namespace {
response_add_header(mrb_state * mrb,mrb_value self)185 mrb_value response_add_header(mrb_state *mrb, mrb_value self) {
186 return response_mod_header(mrb, self, false);
187 }
188 } // namespace
189
190 namespace {
response_clear_headers(mrb_state * mrb,mrb_value self)191 mrb_value response_clear_headers(mrb_state *mrb, mrb_value self) {
192 auto data = static_cast<MRubyAssocData *>(mrb->ud);
193 auto downstream = data->downstream;
194 auto &resp = downstream->response();
195
196 resp.fs.clear_headers();
197
198 return mrb_nil_value();
199 }
200 } // namespace
201
202 namespace {
response_return(mrb_state * mrb,mrb_value self)203 mrb_value response_return(mrb_state *mrb, mrb_value self) {
204 auto data = static_cast<MRubyAssocData *>(mrb->ud);
205 auto downstream = data->downstream;
206 auto &req = downstream->request();
207 auto &resp = downstream->response();
208 int rv;
209
210 auto &balloc = downstream->get_block_allocator();
211
212 if (downstream->get_response_state() == DownstreamState::MSG_COMPLETE) {
213 mrb_raise(mrb, E_RUNTIME_ERROR, "response has already been committed");
214 }
215
216 const char *val;
217 mrb_int vallen;
218 mrb_get_args(mrb, "|s", &val, &vallen);
219
220 const uint8_t *body = nullptr;
221 size_t bodylen = 0;
222
223 if (resp.http_status == 0) {
224 resp.http_status = 200;
225 }
226
227 if (downstream->expect_response_body() && vallen > 0) {
228 body = reinterpret_cast<const uint8_t *>(val);
229 bodylen = vallen;
230 }
231
232 auto cl = resp.fs.header(http2::HD_CONTENT_LENGTH);
233
234 if (resp.http_status == 204 ||
235 (resp.http_status == 200 && req.method == HTTP_CONNECT)) {
236 if (cl) {
237 // Delete content-length here
238 http2::erase_header(cl);
239 }
240
241 resp.fs.content_length = -1;
242 } else {
243 auto content_length = util::make_string_ref_uint(balloc, vallen);
244
245 if (cl) {
246 cl->value = content_length;
247 } else {
248 resp.fs.add_header_token(StringRef::from_lit("content-length"),
249 content_length, false, http2::HD_CONTENT_LENGTH);
250 }
251
252 resp.fs.content_length = vallen;
253 }
254
255 auto date = resp.fs.header(http2::HD_DATE);
256 if (!date) {
257 auto lgconf = log_config();
258 lgconf->update_tstamp(std::chrono::system_clock::now());
259 resp.fs.add_header_token(StringRef::from_lit("date"),
260 make_string_ref(balloc, lgconf->tstamp->time_http),
261 false, http2::HD_DATE);
262 }
263
264 auto upstream = downstream->get_upstream();
265
266 rv = upstream->send_reply(downstream, body, bodylen);
267 if (rv != 0) {
268 mrb_raise(mrb, E_RUNTIME_ERROR, "could not send response");
269 }
270
271 auto handler = upstream->get_client_handler();
272
273 handler->signal_write();
274
275 return self;
276 }
277 } // namespace
278
279 namespace {
response_send_info(mrb_state * mrb,mrb_value self)280 mrb_value response_send_info(mrb_state *mrb, mrb_value self) {
281 auto data = static_cast<MRubyAssocData *>(mrb->ud);
282 auto downstream = data->downstream;
283 auto &resp = downstream->response();
284 int rv;
285
286 if (downstream->get_response_state() == DownstreamState::MSG_COMPLETE) {
287 mrb_raise(mrb, E_RUNTIME_ERROR, "response has already been committed");
288 }
289
290 mrb_int http_status;
291 mrb_value hash;
292 mrb_get_args(mrb, "iH", &http_status, &hash);
293
294 if (http_status / 100 != 1) {
295 mrb_raise(mrb, E_RUNTIME_ERROR,
296 "status_code must be in range [100, 199], inclusive");
297 }
298
299 auto &balloc = downstream->get_block_allocator();
300
301 auto keys = mrb_hash_keys(mrb, hash);
302 auto keyslen = RARRAY_LEN(keys);
303
304 for (int i = 0; i < keyslen; ++i) {
305 auto key = mrb_ary_ref(mrb, keys, i);
306 if (!mrb_string_p(key)) {
307 mrb_raise(mrb, E_RUNTIME_ERROR, "key must be string");
308 }
309
310 auto values = mrb_hash_get(mrb, hash, key);
311
312 auto ai = mrb_gc_arena_save(mrb);
313
314 key = mrb_funcall(mrb, key, "downcase", 0);
315
316 auto keyref = make_string_ref(
317 balloc,
318 StringRef{RSTRING_PTR(key), static_cast<size_t>(RSTRING_LEN(key))});
319
320 mrb_gc_arena_restore(mrb, ai);
321
322 auto token = http2::lookup_token(keyref.byte(), keyref.size());
323
324 if (mrb_array_p(values)) {
325 auto n = RARRAY_LEN(values);
326 for (int i = 0; i < n; ++i) {
327 auto value = mrb_ary_ref(mrb, values, i);
328 if (!mrb_string_p(value)) {
329 mrb_raise(mrb, E_RUNTIME_ERROR, "value must be string");
330 }
331
332 resp.fs.add_header_token(
333 keyref,
334 make_string_ref(balloc,
335 StringRef{RSTRING_PTR(value),
336 static_cast<size_t>(RSTRING_LEN(value))}),
337 false, token);
338 }
339 } else if (mrb_string_p(values)) {
340 resp.fs.add_header_token(
341 keyref,
342 make_string_ref(balloc,
343 StringRef{RSTRING_PTR(values),
344 static_cast<size_t>(RSTRING_LEN(values))}),
345 false, token);
346 } else {
347 mrb_raise(mrb, E_RUNTIME_ERROR, "value must be string");
348 }
349 }
350
351 resp.http_status = http_status;
352
353 auto upstream = downstream->get_upstream();
354
355 rv = upstream->on_downstream_header_complete(downstream);
356 if (rv != 0) {
357 mrb_raise(mrb, E_RUNTIME_ERROR, "could not send non-final response");
358 }
359
360 auto handler = upstream->get_client_handler();
361
362 handler->signal_write();
363
364 return self;
365 }
366 } // namespace
367
init_response_class(mrb_state * mrb,RClass * module)368 void init_response_class(mrb_state *mrb, RClass *module) {
369 auto response_class =
370 mrb_define_class_under(mrb, module, "Response", mrb->object_class);
371
372 mrb_define_method(mrb, response_class, "initialize", response_init,
373 MRB_ARGS_NONE());
374 mrb_define_method(mrb, response_class, "http_version_major",
375 response_get_http_version_major, MRB_ARGS_NONE());
376 mrb_define_method(mrb, response_class, "http_version_minor",
377 response_get_http_version_minor, MRB_ARGS_NONE());
378 mrb_define_method(mrb, response_class, "status", response_get_status,
379 MRB_ARGS_NONE());
380 mrb_define_method(mrb, response_class, "status=", response_set_status,
381 MRB_ARGS_REQ(1));
382 mrb_define_method(mrb, response_class, "headers", response_get_headers,
383 MRB_ARGS_NONE());
384 mrb_define_method(mrb, response_class, "add_header", response_add_header,
385 MRB_ARGS_REQ(2));
386 mrb_define_method(mrb, response_class, "set_header", response_set_header,
387 MRB_ARGS_REQ(2));
388 mrb_define_method(mrb, response_class, "clear_headers",
389 response_clear_headers, MRB_ARGS_NONE());
390 mrb_define_method(mrb, response_class, "return", response_return,
391 MRB_ARGS_OPT(1));
392 mrb_define_method(mrb, response_class, "send_info", response_send_info,
393 MRB_ARGS_REQ(2));
394 }
395
396 } // namespace mruby
397
398 } // namespace shrpx
399