nghttpx: Detect mruby presence and guard mruby related code with ifdef

This commit is contained in:
Tatsuhiro Tsujikawa 2015-09-04 00:54:41 +09:00
parent 200217d8ea
commit d044c58558
8 changed files with 75 additions and 7 deletions

View File

@ -119,6 +119,11 @@ AC_ARG_WITH([spdylay],
[Use spdylay [default=check]])],
[request_spdylay=$withval], [request_spdylay=check])
AC_ARG_WITH([mruby],
[AS_HELP_STRING([--with-mruby],
[Use mruby [default=check]])],
[request_mruby=$withval], [request_mruby=check])
AC_ARG_WITH([cython],
[AS_HELP_STRING([--with-cython=PATH],
[Use cython in given PATH])],
@ -370,6 +375,30 @@ fi
AM_CONDITIONAL([HAVE_SPDYLAY], [ test "x${have_spdylay}" = "xyes" ])
# mruby (for src/nghttpx)
LIBS_OLD=$LIBS
have_mruby=no
if test "x${request_mruby}" != "xno"; then
AC_CHECK_LIB([mruby], [mrb_open], [have_mruby=yes], [have_mruby=no])
if test "x${have_mruby}" = "xyes"; then
AC_CHECK_HEADER([mruby.h], [have_mruby=yes], [have_mruby=no])
if test "x${have_mruby}" = "xyes"; then
LIBMRUBY_LIBS=-lmruby
LIBMRUBY_CFLAGS=
AC_SUBST([LIBMRUBY_LIBS])
AC_SUBST([LIBMRUBY_CFLAGS])
fi
fi
fi
LIBS=$LIBS_OLD
if test "x${request_mruby}" = "xyes" &&
test "x${have_mruby}" != "xyes"; then
AC_MSG_ERROR([mruby was requested (--with-mruby) but not found])
fi
AM_CONDITIONAL([HAVE_MRUBY], [test "x${have_mruby}" = "xyes"])
# Check Boost Asio library
have_asio_lib=no
@ -717,6 +746,7 @@ AC_MSG_NOTICE([summary of build options:
Libev: ${have_libev}
Libevent(SSL): ${have_libevent_openssl}
Spdylay: ${have_spdylay}
MRuby: ${have_mruby}
Jansson: ${have_jansson}
Jemalloc: ${have_jemalloc}
Zlib: ${have_zlib}

View File

@ -124,21 +124,26 @@ NGHTTPX_SRCS = \
shrpx_memcached_connection.cc shrpx_memcached_connection.h \
shrpx_memcached_request.h \
shrpx_memcached_result.h \
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
NGHTTPX_SRCS += shrpx_spdy_upstream.cc shrpx_spdy_upstream.h
endif # HAVE_SPDYLAY
if HAVE_MRUBY
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
endif # HAVE_MRUBY
noinst_LIBRARIES = libnghttpx.a
libnghttpx_a_SOURCES = ${NGHTTPX_SRCS}
nghttpx_SOURCES = shrpx.cc shrpx.h
nghttpx_LDADD = libnghttpx.a ${LDADD} -lmruby
nghttpx_CPPFLAGS = ${AM_CPPFLAGS} @LIBMRUBY_CFLAGS@
nghttpx_LDADD = libnghttpx.a ${LDADD} @LIBMRUBY_LIBS@
if HAVE_CUNIT
check_PROGRAMS += nghttpx-unittest
@ -152,9 +157,10 @@ nghttpx_unittest_SOURCES = shrpx-unittest.cc \
nghttp2_gzip.c nghttp2_gzip.h \
buffer_test.cc buffer_test.h \
memchunk_test.cc memchunk_test.h
nghttpx_unittest_CPPFLAGS = ${AM_CPPFLAGS}\
nghttpx_unittest_CPPFLAGS = ${AM_CPPFLAGS} @LIBMRUBY_CFLAGS@ \
-DNGHTTP2_TESTS_DIR=\"$(top_srcdir)/tests\"
nghttpx_unittest_LDADD = libnghttpx.a ${LDADD} -lmruby @CUNIT_LIBS@ @TESTLDADD@
nghttpx_unittest_LDADD = libnghttpx.a ${LDADD} \
@LIBMRUBY_LIBS@ @CUNIT_LIBS@ @TESTLDADD@
TESTS += nghttpx-unittest
endif # HAVE_CUNIT

View File

@ -160,9 +160,11 @@ int ConnectionHandler::create_single_worker() {
single_worker_ = make_unique<Worker>(loop_, sv_ssl_ctx, cl_ssl_ctx, cert_tree,
ticket_keys_);
#ifdef HAVE_MRUBY
if (single_worker_->create_mruby_context() != 0) {
return -1;
}
#endif // HAVE_MRUBY
return 0;
}
@ -184,9 +186,11 @@ int ConnectionHandler::create_worker_thread(size_t num) {
auto worker = make_unique<Worker>(loop, sv_ssl_ctx, cl_ssl_ctx, cert_tree,
ticket_keys_);
#ifdef HAVE_MRUBY
if (worker->create_mruby_context() != 0) {
return -1;
}
#endif // HAVE_MRUBY
workers_.push_back(std::move(worker));

View File

@ -37,7 +37,9 @@
#include "shrpx_http.h"
#include "shrpx_worker.h"
#include "shrpx_http2_session.h"
#ifdef HAVE_MRUBY
#include "shrpx_mruby.h"
#endif // HAVE_MRUBY
#include "http2.h"
#include "util.h"
#include "base64.h"
@ -318,6 +320,7 @@ int Http2Upstream::on_request_headers(Downstream *downstream,
downstream->set_request_state(Downstream::HEADER_COMPLETE);
#ifdef HAVE_MRUBY
auto upstream = downstream->get_upstream();
auto handler = upstream->get_client_handler();
auto worker = handler->get_worker();
@ -329,6 +332,7 @@ int Http2Upstream::on_request_headers(Downstream *downstream,
}
return 0;
}
#endif // HAVE_MRUBY
if (frame->hd.flags & NGHTTP2_FLAG_END_STREAM) {
downstream->disable_upstream_rtimer();
@ -1271,6 +1275,7 @@ int Http2Upstream::on_downstream_header_complete(Downstream *downstream) {
downstream->get_request_http2_scheme());
}
#ifdef HAVE_MRUBY
if (!downstream->get_non_final_response()) {
auto worker = handler_->get_worker();
auto mruby_ctx = worker->get_mruby_context();
@ -1286,6 +1291,7 @@ int Http2Upstream::on_downstream_header_complete(Downstream *downstream) {
return -1;
}
}
#endif // HAVE_MRUBY
size_t nheader = downstream->get_response_headers().size();
auto nva = std::vector<nghttp2_nv>();

View File

@ -37,7 +37,9 @@
#include "shrpx_log_config.h"
#include "shrpx_worker.h"
#include "shrpx_http2_session.h"
#ifdef HAVE_MRUBY
#include "shrpx_mruby.h"
#endif // HAVE_MRUBY
#include "http2.h"
#include "util.h"
#include "template.h"
@ -328,6 +330,7 @@ int htp_hdrs_completecb(http_parser *htp) {
downstream->set_request_state(Downstream::HEADER_COMPLETE);
#ifdef HAVE_MRUBY
auto handler = upstream->get_client_handler();
auto worker = handler->get_worker();
auto mruby_ctx = worker->get_mruby_context();
@ -336,6 +339,7 @@ int htp_hdrs_completecb(http_parser *htp) {
downstream->set_response_http_status(500);
return -1;
}
#endif // HAVE_MRUBY
// mruby hook may change method value
@ -888,6 +892,7 @@ int HttpsUpstream::on_downstream_header_complete(Downstream *downstream) {
}
}
#ifdef HAVE_MRUBY
if (!downstream->get_non_final_response()) {
auto worker = handler_->get_worker();
auto mruby_ctx = worker->get_mruby_context();
@ -901,6 +906,7 @@ int HttpsUpstream::on_downstream_header_complete(Downstream *downstream) {
return -1;
}
}
#endif // HAVE_MRUBY
auto connect_method = downstream->get_request_method() == HTTP_CONNECT;

View File

@ -36,7 +36,9 @@
#include "shrpx_downstream_connection.h"
#include "shrpx_config.h"
#include "shrpx_http.h"
#ifdef HAVE_MRUBY
#include "shrpx_mruby.h"
#endif // HAVE_MRUBY
#include "shrpx_worker.h"
#include "shrpx_http2_session.h"
#include "http2.h"
@ -243,6 +245,7 @@ void on_ctrl_recv_callback(spdylay_session *session, spdylay_frame_type type,
downstream->set_request_state(Downstream::HEADER_COMPLETE);
#ifdef HAVE_MRUBY
auto handler = upstream->get_client_handler();
auto worker = handler->get_worker();
auto mruby_ctx = worker->get_mruby_context();
@ -254,6 +257,7 @@ void on_ctrl_recv_callback(spdylay_session *session, spdylay_frame_type type,
}
return;
}
#endif // HAVE_MRUBY
if (frame->syn_stream.hd.flags & SPDYLAY_CTRL_FLAG_FIN) {
if (!downstream->validate_request_bodylen()) {
@ -930,6 +934,7 @@ int SpdyUpstream::on_downstream_header_complete(Downstream *downstream) {
return 0;
}
#ifdef HAVE_MRUBY
auto worker = handler_->get_worker();
auto mruby_ctx = worker->get_mruby_context();
@ -943,6 +948,7 @@ int SpdyUpstream::on_downstream_header_complete(Downstream *downstream) {
if (downstream->get_response_state() == Downstream::MSG_COMPLETE) {
return -1;
}
#endif // HAVE_MRUBY
if (LOG_ENABLED(INFO)) {
DLOG(INFO, downstream) << "HTTP response header completed";

View File

@ -37,7 +37,9 @@
#include "shrpx_log_config.h"
#include "shrpx_connect_blocker.h"
#include "shrpx_memcached_dispatcher.h"
#ifdef HAVE_MRUBY
#include "shrpx_mruby.h"
#endif // HAVE_MRUBY
#include "util.h"
#include "template.h"
@ -266,6 +268,7 @@ MemcachedDispatcher *Worker::get_session_cache_memcached_dispatcher() {
return session_cache_memcached_dispatcher_.get();
}
#ifdef HAVE_MRUBY
int Worker::create_mruby_context() {
mruby_ctx_ = mruby::create_mruby_context();
if (!mruby_ctx_) {
@ -278,5 +281,6 @@ int Worker::create_mruby_context() {
mruby::MRubyContext *Worker::get_mruby_context() const {
return mruby_ctx_.get();
}
#endif // HAVE_MRUBY
} // namespace shrpx

View File

@ -51,11 +51,13 @@ class Http2Session;
class ConnectBlocker;
class MemcachedDispatcher;
#ifdef HAVE_MRUBY
namespace mruby {
class MRubyContext;
} // namespace mruby
#endif // HAVE_MRUBY
namespace ssl {
class CertLookupTree;
@ -130,9 +132,11 @@ public:
MemcachedDispatcher *get_session_cache_memcached_dispatcher();
#ifdef HAVE_MRUBY
int create_mruby_context();
mruby::MRubyContext *get_mruby_context() const;
#endif // HAVE_MRUBY
private:
#ifndef NOTHREADS
@ -147,7 +151,9 @@ private:
WorkerStat worker_stat_;
std::vector<DownstreamGroup> dgrps_;
std::unique_ptr<MemcachedDispatcher> session_cache_memcached_dispatcher_;
#ifdef HAVE_MRUBY
std::unique_ptr<mruby::MRubyContext> mruby_ctx_;
#endif // HAVE_MRUBY
struct ev_loop *loop_;
// Following fields are shared across threads if