Commit e06af025 authored by Tatsuhiro Tsujikawa's avatar Tatsuhiro Tsujikawa

nghttpx: Add Response mruby object

parent 5cd34920
......@@ -127,6 +127,7 @@ NGHTTPX_SRCS = \
shrpx_mruby.cc shrpx_mruby.h \
shrpx_mruby_module.cc shrpx_mruby_module.h \
shrpx_mruby_module_request.cc shrpx_mruby_module_request.h \
shrpx_mruby_module_response.cc shrpx_mruby_module_response.h \
buffer.h memchunk.h template.h
if HAVE_SPDYLAY
......
......@@ -664,6 +664,15 @@ const Headers::value_type *get_header(const HeaderIndex &hdidx, int16_t token,
return &nva[i];
}
Headers::value_type *get_header(const HeaderIndex &hdidx, int16_t token,
Headers &nva) {
auto i = hdidx[token];
if (i == -1) {
return nullptr;
}
return &nva[i];
}
namespace {
template <typename InputIt> InputIt skip_lws(InputIt first, InputIt last) {
for (; first != last; ++first) {
......
......@@ -262,6 +262,9 @@ bool http2_mandatory_request_headers_presence(const HeaderIndex &hdidx);
const Headers::value_type *get_header(const HeaderIndex &hdidx, int16_t token,
const Headers &nva);
Headers::value_type *get_header(const HeaderIndex &hdidx, int16_t token,
Headers &nva);
struct LinkHeader {
// The region of URI is [uri.first, uri.second).
std::pair<const char *, const char *> uri;
......
......@@ -609,6 +609,8 @@ const Headers &Downstream::get_response_headers() const {
return response_headers_;
}
Headers &Downstream::get_response_headers() { return response_headers_; }
int Downstream::index_response_headers() {
return index_headers(response_hdidx_, response_headers_,
response_content_length_);
......@@ -619,6 +621,10 @@ Downstream::get_response_header(int16_t token) const {
return http2::get_header(response_hdidx_, token, response_headers_);
}
Headers::value_type *Downstream::get_response_header(int16_t token) {
return http2::get_header(response_hdidx_, token, response_headers_);
}
void Downstream::rewrite_location_response_header(
const std::string &upstream_scheme) {
auto hd =
......
......@@ -208,6 +208,7 @@ public:
bool request_submission_ready() const;
// downstream response API
const Headers &get_response_headers() const;
Headers &get_response_headers();
// Lower the response header field names and indexes response
// headers. If there are invalid headers (e.g., multiple
// Content-Length with different values), returns -1.
......@@ -217,6 +218,7 @@ public:
// the beginning. If no such header is found, returns nullptr.
// This function must be called after response headers are indexed.
const Headers::value_type *get_response_header(int16_t token) const;
Headers::value_type *get_response_header(int16_t token);
// Rewrites the location response header field.
void rewrite_location_response_header(const std::string &upstream_scheme);
void add_response_header(std::string name, std::string value);
......
......@@ -309,14 +309,20 @@ int Http2Upstream::on_request_headers(Downstream *downstream,
downstream->inspect_http2_request();
downstream->set_request_state(Downstream::HEADER_COMPLETE);
auto upstream = downstream->get_upstream();
auto handler = upstream->get_client_handler();
auto worker = handler->get_worker();
auto mruby_ctx = worker->get_mruby_context();
mruby_ctx->run_on_request_proc(downstream);
if (mruby_ctx->run_on_request_proc(downstream) != 0) {
if (error_reply(downstream, 500) != 0) {
return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
}
return 0;
}
downstream->set_request_state(Downstream::HEADER_COMPLETE);
if (frame->hd.flags & NGHTTP2_FLAG_END_STREAM) {
downstream->disable_upstream_rtimer();
......
......@@ -273,10 +273,6 @@ int htp_hdrs_completecb(http_parser *htp) {
return -1;
}
auto handler = upstream->get_client_handler();
auto worker = handler->get_worker();
auto mruby_ctx = worker->get_mruby_context();
downstream->inspect_http1_request();
if (downstream->get_request_method() != HTTP_CONNECT) {
......@@ -310,10 +306,17 @@ int htp_hdrs_completecb(http_parser *htp) {
}
}
mruby_ctx->run_on_request_proc(downstream);
downstream->set_request_state(Downstream::HEADER_COMPLETE);
auto handler = upstream->get_client_handler();
auto worker = handler->get_worker();
auto mruby_ctx = worker->get_mruby_context();
if (mruby_ctx->run_on_request_proc(downstream) != 0) {
downstream->set_response_http_status(500);
return -1;
}
if (downstream->get_response_state() == Downstream::MSG_COMPLETE) {
return 0;
}
......@@ -483,13 +486,16 @@ int HttpsUpstream::on_read() {
if (htperr == HPE_INVALID_METHOD) {
status_code = 501;
} else if (downstream) {
if (downstream->get_request_state() == Downstream::CONNECT_FAIL) {
status_code = 503;
} else if (downstream->get_request_state() ==
Downstream::HTTP1_REQUEST_HEADER_TOO_LARGE) {
status_code = 431;
} else {
status_code = 400;
status_code = downstream->get_response_http_status();
if (status_code == 0) {
if (downstream->get_request_state() == Downstream::CONNECT_FAIL) {
status_code = 503;
} else if (downstream->get_request_state() ==
Downstream::HTTP1_REQUEST_HEADER_TOO_LARGE) {
status_code = 431;
} else {
status_code = 400;
}
}
} else {
status_code = 400;
......
......@@ -78,6 +78,10 @@ int run_request_proc(mrb_state *mrb, Downstream *downstream, RProc *proc) {
downstream->index_request_headers();
}
if (data.response_headers_dirty) {
downstream->index_response_headers();
}
if (downstream->get_response_state() == Downstream::MSG_COMPLETE) {
downstream->pop_downstream_connection();
}
......
......@@ -55,6 +55,7 @@ private:
struct MRubyAssocData {
Downstream *downstream;
bool request_headers_dirty;
bool response_headers_dirty;
};
RProc *compile(mrb_state *mrb, const char *filename);
......
......@@ -33,6 +33,7 @@
#include "shrpx_mruby.h"
#include "shrpx_mruby_module_request.h"
#include "shrpx_mruby_module_response.h"
namespace shrpx {
......@@ -45,9 +46,10 @@ mrb_value run(mrb_state *mrb, mrb_value self) {
auto module = mrb_module_get(mrb, "Nghttpx");
auto request_class = mrb_class_get_under(mrb, module, "Request");
auto request = mrb_obj_new(mrb, request_class, 0, nullptr);
auto response_class = mrb_class_get_under(mrb, module, "Response");
std::array<mrb_value, 1> args{{request}};
std::array<mrb_value, 2> args{{mrb_obj_new(mrb, response_class, 0, nullptr),
mrb_obj_new(mrb, request_class, 0, nullptr)}};
return mrb_yield_argv(mrb, b, args.size(), args.data());
}
} // namespace
......@@ -58,6 +60,7 @@ void init_module(mrb_state *mrb) {
mrb_define_class_method(mrb, module, "run", run, MRB_ARGS_BLOCK());
init_request_class(mrb, module);
init_response_class(mrb, module);
}
mrb_value create_headers_hash(mrb_state *mrb, const Headers &headers) {
......
/*
* 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_response.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_mruby.h"
#include "shrpx_mruby_module.h"
#include "util.h"
#include "http2.h"
namespace shrpx {
namespace mruby {
namespace {
mrb_value response_init(mrb_state *mrb, mrb_value self) { return self; }
} // namespace
namespace {
mrb_value response_get_http_version_major(mrb_state *mrb, mrb_value self) {
auto data = static_cast<MRubyAssocData *>(mrb->ud);
auto downstream = data->downstream;
return mrb_fixnum_value(downstream->get_response_major());
}
} // namespace
namespace {
mrb_value response_get_http_version_minor(mrb_state *mrb, mrb_value self) {
auto data = static_cast<MRubyAssocData *>(mrb->ud);
auto downstream = data->downstream;
return mrb_fixnum_value(downstream->get_response_minor());
}
} // namespace
namespace {
mrb_value response_get_status(mrb_state *mrb, mrb_value self) {
auto data = static_cast<MRubyAssocData *>(mrb->ud);
auto downstream = data->downstream;
return mrb_fixnum_value(downstream->get_response_http_status());
}
} // namespace
namespace {
mrb_value response_set_status(mrb_state *mrb, mrb_value self) {
auto data = static_cast<MRubyAssocData *>(mrb->ud);
auto downstream = data->downstream;
mrb_int status;
mrb_get_args(mrb, "i", &status);
// We don't support 1xx status code for mruby scripting yet.
if (status < 200 || status > 999) {
mrb_raise(mrb, E_RUNTIME_ERROR,
"invalid status; it should be [200, 999], inclusive");
}
downstream->set_response_http_status(status);
return self;
}
} // namespace
namespace {
mrb_value response_get_headers(mrb_state *mrb, mrb_value self) {
auto data = static_cast<MRubyAssocData *>(mrb->ud);
auto downstream = data->downstream;
return create_headers_hash(mrb, downstream->get_response_headers());
}
} // namespace
namespace {
mrb_value response_set_header(mrb_state *mrb, mrb_value self) {
auto data = static_cast<MRubyAssocData *>(mrb->ud);
auto downstream = data->downstream;
mrb_value key, values;
mrb_get_args(mrb, "oo", &key, &values);
if (RSTRING_LEN(key) == 0) {
mrb_raise(mrb, E_RUNTIME_ERROR, "empty key is not allowed");
}
key = mrb_funcall(mrb, key, "downcase", 0);
// making name empty will effectively delete header fields
for (auto &hd : downstream->get_response_headers()) {
if (util::streq(std::begin(hd.name), hd.name.size(), RSTRING_PTR(key),
RSTRING_LEN(key))) {
hd.name = "";
}
}
if (mrb_obj_is_instance_of(mrb, values, mrb->array_class)) {
auto n = mrb_ary_len(mrb, values);
for (int i = 0; i < n; ++i) {
auto value = mrb_ary_entry(values, i);
downstream->add_response_header(
std::string(RSTRING_PTR(key), RSTRING_LEN(key)),
std::string(RSTRING_PTR(value), RSTRING_LEN(value)));
}
} else if (!mrb_nil_p(values)) {
downstream->add_response_header(
std::string(RSTRING_PTR(key), RSTRING_LEN(key)),
std::string(RSTRING_PTR(values), RSTRING_LEN(values)));
}
data->response_headers_dirty = true;
return mrb_nil_value();
}
} // namespace
namespace {
mrb_value response_end(mrb_state *mrb, mrb_value self) {
auto data = static_cast<MRubyAssocData *>(mrb->ud);
auto downstream = data->downstream;
int rv;
if (downstream->get_response_state() == Downstream::MSG_COMPLETE) {
mrb_raise(mrb, E_RUNTIME_ERROR, "response has already been committed");
}
mrb_value val;
mrb_get_args(mrb, "|o", &val);
const char *body = nullptr;
size_t bodylen = 0;
if (!mrb_nil_p(val)) {
body = RSTRING_PTR(val);
bodylen = RSTRING_LEN(val);
}
if (downstream->get_response_http_status() == 0) {
downstream->set_response_http_status(200);
}
if (data->response_headers_dirty) {
downstream->index_response_headers();
data->response_headers_dirty = false;
}
auto cl = downstream->get_response_header(http2::HD_CONTENT_LENGTH);
if (cl) {
cl->value = util::utos(bodylen);
} else {
downstream->add_response_header("content-length", util::utos(bodylen),
http2::HD_CONTENT_LENGTH);
}
downstream->set_response_content_length(bodylen);
auto upstream = downstream->get_upstream();
rv = upstream->on_downstream_header_complete(downstream);
if (rv != 0) {
mrb_raise(mrb, E_RUNTIME_ERROR, "could not send response");
}
if (downstream->expect_response_body()) {
auto output = downstream->get_response_buf();
output->append(body, bodylen);
}
downstream->set_response_state(Downstream::MSG_COMPLETE);
return self;
}
} // namespace
void init_response_class(mrb_state *mrb, RClass *module) {
auto response_class =
mrb_define_class_under(mrb, module, "Response", mrb->object_class);
mrb_define_method(mrb, response_class, "initialize", response_init,
MRB_ARGS_NONE());
mrb_define_method(mrb, response_class, "http_version_major",
response_get_http_version_major, MRB_ARGS_NONE());
mrb_define_method(mrb, response_class, "http_version_minor",
response_get_http_version_minor, MRB_ARGS_NONE());
mrb_define_method(mrb, response_class, "status", response_get_status,
MRB_ARGS_NONE());
mrb_define_method(mrb, response_class, "status=", response_set_status,
MRB_ARGS_REQ(1));
mrb_define_method(mrb, response_class, "headers", response_get_headers,
MRB_ARGS_NONE());
mrb_define_method(mrb, response_class, "set_header", response_set_header,
MRB_ARGS_REQ(2));
mrb_define_method(mrb, response_class, "end", response_end, MRB_ARGS_OPT(1));
}
} // namespace mruby
} // namespace shrpx
/*
* 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.
*/
#ifndef SHRPX_MRUBY_MODULE_RESPONSE_H
#define SHRPX_MRUBY_MODULE_RESPONSE_H
#include "shrpx.h"
#include <mruby.h>
using namespace nghttp2;
namespace shrpx {
namespace mruby {
void init_response_class(mrb_state *mrb, RClass *module);
} // namespace mruby
} // namespace shrpx
#endif // SHRPX_MRUBY_MODULE_RESPONSE_H
......@@ -36,6 +36,9 @@
#include "shrpx_downstream_connection.h"
#include "shrpx_config.h"
#include "shrpx_http.h"
#include "shrpx_mruby.h"
#include "shrpx_worker.h"
#include "shrpx_http2_session.h"
#include "http2.h"
#include "util.h"
#include "template.h"
......@@ -237,6 +240,19 @@ void on_ctrl_recv_callback(spdylay_session *session, spdylay_frame_type type,
downstream->inspect_http2_request();
downstream->set_request_state(Downstream::HEADER_COMPLETE);
auto handler = upstream->get_client_handler();
auto worker = handler->get_worker();
auto mruby_ctx = worker->get_mruby_context();
if (mruby_ctx->run_on_request_proc(downstream) != 0) {
if (upstream->error_reply(downstream, 500) != 0) {
ULOG(FATAL, upstream) << "error_reply failed";
return;
}
return;
}
if (frame->syn_stream.hd.flags & SPDYLAY_CTRL_FLAG_FIN) {
if (!downstream->validate_request_bodylen()) {
upstream->rst_stream(downstream, SPDYLAY_PROTOCOL_ERROR);
......@@ -247,6 +263,10 @@ void on_ctrl_recv_callback(spdylay_session *session, spdylay_frame_type type,
downstream->set_request_state(Downstream::MSG_COMPLETE);
}
if (downstream->get_response_state() == Downstream::MSG_COMPLETE) {
return;
}
upstream->start_downstream(downstream);
break;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment