2015-09-02 15:38:57 +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_request.h"
|
|
|
|
|
|
|
|
#include <mruby/variable.h>
|
|
|
|
#include <mruby/string.h>
|
|
|
|
#include <mruby/hash.h>
|
|
|
|
#include <mruby/array.h>
|
|
|
|
|
|
|
|
#include "shrpx_downstream.h"
|
2015-09-02 19:02:28 +02:00
|
|
|
#include "shrpx_upstream.h"
|
|
|
|
#include "shrpx_client_handler.h"
|
2015-09-02 15:38:57 +02:00
|
|
|
#include "shrpx_mruby.h"
|
|
|
|
#include "shrpx_mruby_module.h"
|
|
|
|
#include "util.h"
|
2015-09-02 16:22:33 +02:00
|
|
|
#include "http2.h"
|
2015-09-02 15:38:57 +02:00
|
|
|
|
|
|
|
namespace shrpx {
|
|
|
|
|
|
|
|
namespace mruby {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
mrb_value request_init(mrb_state *mrb, mrb_value self) { return self; }
|
|
|
|
} // namespace
|
|
|
|
|
2015-09-02 16:22:33 +02:00
|
|
|
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;
|
2016-01-13 14:45:52 +01:00
|
|
|
const auto &req = downstream->request();
|
|
|
|
return mrb_fixnum_value(req.http_major);
|
2015-09-02 16:22:33 +02:00
|
|
|
}
|
|
|
|
} // 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;
|
2016-01-13 14:45:52 +01:00
|
|
|
const auto &req = downstream->request();
|
|
|
|
return mrb_fixnum_value(req.http_minor);
|
2015-09-02 16:22:33 +02:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
mrb_value request_get_method(mrb_state *mrb, mrb_value self) {
|
|
|
|
auto data = static_cast<MRubyAssocData *>(mrb->ud);
|
|
|
|
auto downstream = data->downstream;
|
2016-01-13 14:45:52 +01:00
|
|
|
const auto &req = downstream->request();
|
|
|
|
auto method = http2::to_method_string(req.method);
|
2015-09-02 16:22:33 +02:00
|
|
|
|
2016-02-28 13:35:26 +01:00
|
|
|
return mrb_str_new(mrb, method.c_str(), method.size());
|
2015-09-02 16:22:33 +02:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
mrb_value request_set_method(mrb_state *mrb, mrb_value self) {
|
|
|
|
auto data = static_cast<MRubyAssocData *>(mrb->ud);
|
|
|
|
auto downstream = data->downstream;
|
2016-01-13 14:45:52 +01:00
|
|
|
auto &req = downstream->request();
|
2015-09-02 16:22:33 +02:00
|
|
|
|
2015-09-04 18:07:29 +02:00
|
|
|
check_phase(mrb, data->phase, PHASE_REQUEST);
|
|
|
|
|
2015-09-02 16:22:33 +02:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2016-01-13 14:45:52 +01:00
|
|
|
req.method = token;
|
2015-09-02 16:22:33 +02:00
|
|
|
|
|
|
|
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;
|
2016-01-13 14:45:52 +01:00
|
|
|
const auto &req = downstream->request();
|
2015-09-02 16:22:33 +02:00
|
|
|
|
2016-01-13 14:45:52 +01:00
|
|
|
return mrb_str_new(mrb, req.authority.c_str(), req.authority.size());
|
2015-09-02 16:22:33 +02:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
mrb_value request_set_authority(mrb_state *mrb, mrb_value self) {
|
|
|
|
auto data = static_cast<MRubyAssocData *>(mrb->ud);
|
|
|
|
auto downstream = data->downstream;
|
2016-01-13 14:45:52 +01:00
|
|
|
auto &req = downstream->request();
|
2015-09-02 16:22:33 +02:00
|
|
|
|
2016-03-10 14:42:07 +01:00
|
|
|
auto &balloc = downstream->get_block_allocator();
|
|
|
|
|
2015-09-04 18:07:29 +02:00
|
|
|
check_phase(mrb, data->phase, PHASE_REQUEST);
|
|
|
|
|
2015-09-02 16:22:33 +02:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2016-03-10 14:42:07 +01:00
|
|
|
req.authority =
|
|
|
|
make_string_ref(balloc, StringRef{authority, static_cast<size_t>(n)});
|
2015-09-02 16:22:33 +02:00
|
|
|
|
|
|
|
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;
|
2016-01-13 14:45:52 +01:00
|
|
|
const auto &req = downstream->request();
|
2015-09-02 16:22:33 +02:00
|
|
|
|
2016-01-13 14:45:52 +01:00
|
|
|
return mrb_str_new(mrb, req.scheme.c_str(), req.scheme.size());
|
2015-09-02 16:22:33 +02:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
mrb_value request_set_scheme(mrb_state *mrb, mrb_value self) {
|
|
|
|
auto data = static_cast<MRubyAssocData *>(mrb->ud);
|
|
|
|
auto downstream = data->downstream;
|
2016-01-13 14:45:52 +01:00
|
|
|
auto &req = downstream->request();
|
2015-09-02 16:22:33 +02:00
|
|
|
|
2016-03-10 14:42:07 +01:00
|
|
|
auto &balloc = downstream->get_block_allocator();
|
|
|
|
|
2015-09-04 18:07:29 +02:00
|
|
|
check_phase(mrb, data->phase, PHASE_REQUEST);
|
|
|
|
|
2015-09-02 16:22:33 +02:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2016-03-10 14:42:07 +01:00
|
|
|
req.scheme =
|
|
|
|
make_string_ref(balloc, StringRef{scheme, static_cast<size_t>(n)});
|
2015-09-02 16:22:33 +02:00
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2015-09-02 15:38:57 +02:00
|
|
|
namespace {
|
|
|
|
mrb_value request_get_path(mrb_state *mrb, mrb_value self) {
|
|
|
|
auto data = static_cast<MRubyAssocData *>(mrb->ud);
|
|
|
|
auto downstream = data->downstream;
|
2016-01-13 14:45:52 +01:00
|
|
|
const auto &req = downstream->request();
|
2015-09-02 15:38:57 +02:00
|
|
|
|
2016-01-13 14:45:52 +01:00
|
|
|
return mrb_str_new(mrb, req.path.c_str(), req.path.size());
|
2015-09-02 15:38:57 +02:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
mrb_value request_set_path(mrb_state *mrb, mrb_value self) {
|
|
|
|
auto data = static_cast<MRubyAssocData *>(mrb->ud);
|
|
|
|
auto downstream = data->downstream;
|
2016-01-13 14:45:52 +01:00
|
|
|
auto &req = downstream->request();
|
2015-09-02 15:38:57 +02:00
|
|
|
|
2016-03-10 14:42:07 +01:00
|
|
|
auto &balloc = downstream->get_block_allocator();
|
|
|
|
|
2015-09-04 18:07:29 +02:00
|
|
|
check_phase(mrb, data->phase, PHASE_REQUEST);
|
|
|
|
|
2015-09-02 15:38:57 +02:00
|
|
|
const char *path;
|
|
|
|
mrb_int pathlen;
|
|
|
|
mrb_get_args(mrb, "s", &path, &pathlen);
|
|
|
|
|
2016-03-10 14:42:07 +01:00
|
|
|
req.path =
|
|
|
|
make_string_ref(balloc, StringRef{path, static_cast<size_t>(pathlen)});
|
2015-09-02 15:38:57 +02:00
|
|
|
|
|
|
|
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;
|
2016-01-13 14:45:52 +01:00
|
|
|
const auto &req = downstream->request();
|
|
|
|
return create_headers_hash(mrb, req.fs.headers());
|
2015-09-02 15:38:57 +02:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
2015-09-03 19:53:02 +02:00
|
|
|
mrb_value request_mod_header(mrb_state *mrb, mrb_value self, bool repl) {
|
2015-09-02 15:38:57 +02:00
|
|
|
auto data = static_cast<MRubyAssocData *>(mrb->ud);
|
|
|
|
auto downstream = data->downstream;
|
2016-01-13 14:45:52 +01:00
|
|
|
auto &req = downstream->request();
|
2016-03-10 16:50:27 +01:00
|
|
|
auto &balloc = downstream->get_block_allocator();
|
2015-09-02 15:38:57 +02:00
|
|
|
|
2015-09-04 18:07:29 +02:00
|
|
|
check_phase(mrb, data->phase, PHASE_REQUEST);
|
|
|
|
|
2015-09-02 15:38:57 +02:00
|
|
|
mrb_value key, values;
|
2016-11-03 14:58:45 +01:00
|
|
|
mrb_get_args(mrb, "So", &key, &values);
|
2015-09-02 15:38:57 +02:00
|
|
|
|
|
|
|
if (RSTRING_LEN(key) == 0) {
|
|
|
|
mrb_raise(mrb, E_RUNTIME_ERROR, "empty key is not allowed");
|
|
|
|
}
|
|
|
|
|
2016-11-03 14:47:52 +01:00
|
|
|
auto ai = mrb_gc_arena_save(mrb);
|
|
|
|
|
2015-09-02 15:38:57 +02:00
|
|
|
key = mrb_funcall(mrb, key, "downcase", 0);
|
|
|
|
|
2016-02-20 13:41:23 +01:00
|
|
|
auto keyref =
|
2016-03-10 16:50:27 +01:00
|
|
|
make_string_ref(balloc, StringRef{RSTRING_PTR(key),
|
|
|
|
static_cast<size_t>(RSTRING_LEN(key))});
|
2016-11-03 14:47:52 +01:00
|
|
|
|
|
|
|
mrb_gc_arena_restore(mrb, ai);
|
|
|
|
|
2016-02-20 13:41:23 +01:00
|
|
|
auto token = http2::lookup_token(keyref.byte(), keyref.size());
|
|
|
|
|
2015-09-03 19:53:02 +02:00
|
|
|
if (repl) {
|
2015-09-04 18:18:18 +02:00
|
|
|
size_t p = 0;
|
2016-01-13 14:45:52 +01:00
|
|
|
auto &headers = req.fs.headers();
|
2015-09-04 18:18:18 +02:00
|
|
|
for (size_t i = 0; i < headers.size(); ++i) {
|
2016-02-20 13:41:23 +01:00
|
|
|
auto &kv = headers[i];
|
|
|
|
if (kv.name == keyref) {
|
2015-09-04 18:18:18 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (i != p) {
|
2016-02-20 13:41:23 +01:00
|
|
|
headers[p++] = std::move(kv);
|
2015-09-03 19:53:02 +02:00
|
|
|
}
|
2015-09-02 15:38:57 +02:00
|
|
|
}
|
2015-09-04 18:18:18 +02:00
|
|
|
headers.resize(p);
|
2015-09-02 15:38:57 +02:00
|
|
|
}
|
|
|
|
|
2016-11-03 14:58:45 +01:00
|
|
|
if (mrb_array_p(values)) {
|
2015-09-02 15:38:57 +02:00
|
|
|
auto n = mrb_ary_len(mrb, values);
|
|
|
|
for (int i = 0; i < n; ++i) {
|
2016-11-03 14:44:42 +01:00
|
|
|
auto value = mrb_ary_ref(mrb, values, i);
|
2016-11-03 14:58:45 +01:00
|
|
|
if (!mrb_string_p(value)) {
|
|
|
|
mrb_raise(mrb, E_RUNTIME_ERROR, "value must be string");
|
|
|
|
}
|
|
|
|
|
2016-02-20 13:41:23 +01:00
|
|
|
req.fs.add_header_token(
|
2016-03-10 16:50:27 +01:00
|
|
|
keyref,
|
|
|
|
make_string_ref(balloc,
|
|
|
|
StringRef{RSTRING_PTR(value),
|
|
|
|
static_cast<size_t>(RSTRING_LEN(value))}),
|
2016-02-20 13:41:23 +01:00
|
|
|
false, token);
|
2015-09-02 15:38:57 +02:00
|
|
|
}
|
2016-11-03 14:58:45 +01:00
|
|
|
} else if (mrb_string_p(values)) {
|
2016-03-10 16:50:27 +01:00
|
|
|
req.fs.add_header_token(
|
|
|
|
keyref,
|
|
|
|
make_string_ref(balloc,
|
|
|
|
StringRef{RSTRING_PTR(values),
|
|
|
|
static_cast<size_t>(RSTRING_LEN(values))}),
|
|
|
|
false, token);
|
2016-11-03 14:58:45 +01:00
|
|
|
} else {
|
|
|
|
mrb_raise(mrb, E_RUNTIME_ERROR, "value must be string");
|
2015-09-02 15:38:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return mrb_nil_value();
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2015-09-03 19:53:02 +02:00
|
|
|
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;
|
2016-01-13 14:45:52 +01:00
|
|
|
auto &req = downstream->request();
|
2015-09-03 19:53:02 +02:00
|
|
|
|
2015-09-04 18:07:29 +02:00
|
|
|
check_phase(mrb, data->phase, PHASE_REQUEST);
|
|
|
|
|
2016-01-13 14:45:52 +01:00
|
|
|
req.fs.clear_headers();
|
2015-09-03 19:53:02 +02:00
|
|
|
|
|
|
|
return mrb_nil_value();
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2015-09-05 15:47:07 +02:00
|
|
|
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);
|
|
|
|
|
2016-03-03 16:38:43 +01:00
|
|
|
upstream->initiate_push(downstream, StringRef{uri, static_cast<size_t>(len)});
|
2015-09-05 15:47:07 +02:00
|
|
|
|
|
|
|
return mrb_nil_value();
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2015-09-02 15:38:57 +02:00
|
|
|
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());
|
2015-09-02 16:22:33 +02:00
|
|
|
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));
|
2015-09-02 15:38:57 +02:00
|
|
|
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());
|
2015-09-03 19:53:02 +02:00
|
|
|
mrb_define_method(mrb, request_class, "add_header", request_add_header,
|
|
|
|
MRB_ARGS_REQ(2));
|
2015-09-02 15:38:57 +02:00
|
|
|
mrb_define_method(mrb, request_class, "set_header", request_set_header,
|
|
|
|
MRB_ARGS_REQ(2));
|
2015-09-03 19:53:02 +02:00
|
|
|
mrb_define_method(mrb, request_class, "clear_headers", request_clear_headers,
|
|
|
|
MRB_ARGS_NONE());
|
2015-09-05 15:47:07 +02:00
|
|
|
mrb_define_method(mrb, request_class, "push", request_push, MRB_ARGS_REQ(1));
|
2015-09-02 15:38:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mruby
|
|
|
|
|
|
|
|
} // namespace shrpx
|