diff --git a/src/HttpServer.cc b/src/HttpServer.cc index c908ab57..f6f63712 100644 --- a/src/HttpServer.cc +++ b/src/HttpServer.cc @@ -58,6 +58,7 @@ extern "C" { #include "app_helper.h" #include "http2.h" #include "util.h" +#include "libevent_util.h" #include "ssl.h" #ifndef O_BINARY diff --git a/src/Makefile.am b/src/Makefile.am index 0418917a..af8a76c5 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -56,8 +56,10 @@ if ENABLE_APP bin_PROGRAMS += nghttp nghttpd nghttpx -HELPER_OBJECTS = util.cc http2.cc timegm.c app_helper.cc nghttp2_gzip.c -HELPER_HFILES = util.h http2.h timegm.h app_helper.h nghttp2_config.h \ +HELPER_OBJECTS = util.cc libevent_util.cc \ + http2.cc timegm.c app_helper.cc nghttp2_gzip.c +HELPER_HFILES = util.h libevent_util.h \ + http2.h timegm.h app_helper.h nghttp2_config.h \ nghttp2_gzip.h HTML_PARSER_OBJECTS = @@ -78,7 +80,8 @@ if ENABLE_H2LOAD bin_PROGRAMS += h2load -h2load_SOURCES = util.cc util.h http2.cc http2.h h2load.cc h2load.h \ +h2load_SOURCES = util.cc util.h libevent_util.cc libevent_util.h \ + http2.cc http2.h h2load.cc h2load.h \ timegm.c timegm.h \ ssl.cc ssl.h \ h2load_session.h \ @@ -92,6 +95,7 @@ endif # ENABLE_H2LOAD NGHTTPX_SRCS = \ util.cc util.h http2.cc http2.h timegm.c timegm.h base64.h \ + libevent_util.cc libevent_util.h \ app_helper.cc app_helper.h \ ssl.cc ssl.h \ shrpx_config.cc shrpx_config.h \ diff --git a/src/h2load_http2_session.cc b/src/h2load_http2_session.cc index 690b11bb..136d73a1 100644 --- a/src/h2load_http2_session.cc +++ b/src/h2load_http2_session.cc @@ -28,6 +28,7 @@ #include "h2load.h" #include "util.h" +#include "libevent_util.h" using namespace nghttp2; diff --git a/src/h2load_spdy_session.h b/src/h2load_spdy_session.h index caf053f0..b393c30d 100644 --- a/src/h2load_spdy_session.h +++ b/src/h2load_spdy_session.h @@ -30,6 +30,7 @@ #include #include "util.h" +#include "libevent_util.h" namespace h2load { diff --git a/src/libevent_util.cc b/src/libevent_util.cc new file mode 100644 index 00000000..c3c44126 --- /dev/null +++ b/src/libevent_util.cc @@ -0,0 +1,117 @@ +/* + * nghttp2 - HTTP/2 C Library + * + * Copyright (c) 2014 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 "libevent_util.h" + +#include + +namespace nghttp2 { + +namespace util { + +EvbufferBuffer::EvbufferBuffer() + : evbuffer_(nullptr), + buf_(nullptr), + bufmax_(0), + buflen_(0) +{} + +EvbufferBuffer::EvbufferBuffer(evbuffer *evbuffer, uint8_t *buf, size_t bufmax) + : evbuffer_(evbuffer), + buf_(buf), + bufmax_(bufmax), + buflen_(0) +{} + +void EvbufferBuffer::reset(evbuffer *evbuffer, uint8_t *buf, size_t bufmax) +{ + evbuffer_ = evbuffer; + buf_ = buf; + bufmax_ = bufmax; + buflen_ = 0; +} + +int EvbufferBuffer::flush() +{ + int rv; + if(buflen_ > 0) { + rv = evbuffer_add(evbuffer_, buf_, buflen_); + if(rv == -1) { + return -1; + } + buflen_ = 0; + } + return 0; +} + +int EvbufferBuffer::add(const uint8_t *data, size_t datalen) +{ + int rv; + if(buflen_ + datalen > bufmax_) { + if(buflen_ > 0) { + rv = evbuffer_add(evbuffer_, buf_, buflen_); + if(rv == -1) { + return -1; + } + buflen_ = 0; + } + if(datalen > bufmax_) { + rv = evbuffer_add(evbuffer_, data, datalen); + if(rv == -1) { + return -1; + } + return 0; + } + } + memcpy(buf_ + buflen_, data, datalen); + buflen_ += datalen; + return 0; +} + +size_t EvbufferBuffer::get_buflen() const +{ + return buflen_; +} + +void bev_enable_unless(bufferevent *bev, int events) +{ + if((bufferevent_get_enabled(bev) & events) == events) { + return; + } + + bufferevent_enable(bev, events); +} + +void bev_disable_unless(bufferevent *bev, int events) +{ + if((bufferevent_get_enabled(bev) & events) == 0) { + return; + } + + bufferevent_disable(bev, events); +} + +} // namespace util + +} // namespace nghttp2 diff --git a/src/libevent_util.h b/src/libevent_util.h new file mode 100644 index 00000000..2a020d36 --- /dev/null +++ b/src/libevent_util.h @@ -0,0 +1,62 @@ +/* + * nghttp2 - HTTP/2 C Library + * + * Copyright (c) 2014 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 LIBEVENT_UTIL_H +#define LIBEVENT_UTIL_H + +#include "nghttp2_config.h" + +#include +#include + +namespace nghttp2 { + +namespace util { + +class EvbufferBuffer { +public: + EvbufferBuffer(); + EvbufferBuffer(evbuffer *evbuffer, uint8_t *buf, size_t bufmax); + void reset(evbuffer *evbuffer, uint8_t *buf, size_t bufmax); + int flush(); + int add(const uint8_t *data, size_t datalen); + size_t get_buflen() const; +private: + evbuffer *evbuffer_; + uint8_t *buf_; + size_t bufmax_; + size_t buflen_; +}; + +// These functions are provided to reduce epoll_ctl syscall. Avoid +// calling bufferevent_enable/disable() unless it is required by +// sniffing current enabled events. +void bev_enable_unless(bufferevent *bev, int events); +void bev_disable_unless(bufferevent *bev, int events); + +} // namespace util + +} // namespace nghttp2 + +#endif // LIBEVENT_UTIL_H diff --git a/src/nghttp.cc b/src/nghttp.cc index fe99ed33..b5026d78 100644 --- a/src/nghttp.cc +++ b/src/nghttp.cc @@ -67,6 +67,7 @@ #include "app_helper.h" #include "HtmlParser.h" #include "util.h" +#include "libevent_util.h" #include "base64.h" #include "http2.h" #include "nghttp2_gzip.h" diff --git a/src/shrpx_client_handler.cc b/src/shrpx_client_handler.cc index cff2ea3c..8723841b 100644 --- a/src/shrpx_client_handler.cc +++ b/src/shrpx_client_handler.cc @@ -40,6 +40,7 @@ #include "shrpx_spdy_upstream.h" #endif // HAVE_SPDYLAY #include "util.h" +#include "libevent_util.h" using namespace nghttp2; diff --git a/src/shrpx_http2_session.cc b/src/shrpx_http2_session.cc index 54df9569..c2e54656 100644 --- a/src/shrpx_http2_session.cc +++ b/src/shrpx_http2_session.cc @@ -44,6 +44,7 @@ #include "shrpx_worker_config.h" #include "http2.h" #include "util.h" +#include "libevent_util.h" #include "base64.h" using namespace nghttp2; diff --git a/src/shrpx_http2_upstream.cc b/src/shrpx_http2_upstream.cc index a6caf982..418241c0 100644 --- a/src/shrpx_http2_upstream.cc +++ b/src/shrpx_http2_upstream.cc @@ -38,6 +38,7 @@ #include "shrpx_worker_config.h" #include "http2.h" #include "util.h" +#include "libevent_util.h" #include "base64.h" #include "app_helper.h" diff --git a/src/shrpx_http_downstream_connection.cc b/src/shrpx_http_downstream_connection.cc index 849fb55e..a7834490 100644 --- a/src/shrpx_http_downstream_connection.cc +++ b/src/shrpx_http_downstream_connection.cc @@ -34,6 +34,7 @@ #include "shrpx_connect_blocker.h" #include "http2.h" #include "util.h" +#include "libevent_util.h" using namespace nghttp2; diff --git a/src/shrpx_io_control.cc b/src/shrpx_io_control.cc index a7e64d9f..36e6dfaa 100644 --- a/src/shrpx_io_control.cc +++ b/src/shrpx_io_control.cc @@ -27,6 +27,7 @@ #include #include "util.h" +#include "libevent_util.h" using namespace nghttp2; diff --git a/src/shrpx_listen_handler.cc b/src/shrpx_listen_handler.cc index 165080ea..c65adfd4 100644 --- a/src/shrpx_listen_handler.cc +++ b/src/shrpx_listen_handler.cc @@ -40,6 +40,7 @@ #include "shrpx_http2_session.h" #include "shrpx_connect_blocker.h" #include "util.h" +#include "libevent_util.h" using namespace nghttp2; diff --git a/src/shrpx_spdy_upstream.h b/src/shrpx_spdy_upstream.h index 113f2e4a..018e69e9 100644 --- a/src/shrpx_spdy_upstream.h +++ b/src/shrpx_spdy_upstream.h @@ -34,6 +34,7 @@ #include "shrpx_upstream.h" #include "shrpx_downstream_queue.h" #include "util.h" +#include "libevent_util.h" namespace shrpx { diff --git a/src/shrpx_worker.cc b/src/shrpx_worker.cc index 8c72de58..00ba23b7 100644 --- a/src/shrpx_worker.cc +++ b/src/shrpx_worker.cc @@ -39,6 +39,7 @@ #include "shrpx_worker_config.h" #include "shrpx_connect_blocker.h" #include "util.h" +#include "libevent_util.h" using namespace nghttp2; diff --git a/src/util.cc b/src/util.cc index 4724ea1a..a4b3e445 100644 --- a/src/util.cc +++ b/src/util.cc @@ -509,70 +509,6 @@ void write_uri_field(std::ostream& o, } } -EvbufferBuffer::EvbufferBuffer() - : evbuffer_(nullptr), - buf_(nullptr), - bufmax_(0), - buflen_(0) -{} - -EvbufferBuffer::EvbufferBuffer(evbuffer *evbuffer, uint8_t *buf, size_t bufmax) - : evbuffer_(evbuffer), - buf_(buf), - bufmax_(bufmax), - buflen_(0) -{} - -void EvbufferBuffer::reset(evbuffer *evbuffer, uint8_t *buf, size_t bufmax) -{ - evbuffer_ = evbuffer; - buf_ = buf; - bufmax_ = bufmax; - buflen_ = 0; -} - -int EvbufferBuffer::flush() -{ - int rv; - if(buflen_ > 0) { - rv = evbuffer_add(evbuffer_, buf_, buflen_); - if(rv == -1) { - return -1; - } - buflen_ = 0; - } - return 0; -} - -int EvbufferBuffer::add(const uint8_t *data, size_t datalen) -{ - int rv; - if(buflen_ + datalen > bufmax_) { - if(buflen_ > 0) { - rv = evbuffer_add(evbuffer_, buf_, buflen_); - if(rv == -1) { - return -1; - } - buflen_ = 0; - } - if(datalen > bufmax_) { - rv = evbuffer_add(evbuffer_, data, datalen); - if(rv == -1) { - return -1; - } - return 0; - } - } - memcpy(buf_ + buflen_, data, datalen); - buflen_ += datalen; - return 0; -} - -size_t EvbufferBuffer::get_buflen() const -{ - return buflen_; -} - bool numeric_host(const char *hostname) { struct addrinfo hints; @@ -657,24 +593,6 @@ char* get_exec_path(int argc, char **const argv, const char *cwd) return path; } -void bev_enable_unless(bufferevent *bev, int events) -{ - if((bufferevent_get_enabled(bev) & events) == events) { - return; - } - - bufferevent_enable(bev, events); -} - -void bev_disable_unless(bufferevent *bev, int events) -{ - if((bufferevent_get_enabled(bev) & events) == 0) { - return; - } - - bufferevent_disable(bev, events); -} - } // namespace util } // namespace nghttp2 diff --git a/src/util.h b/src/util.h index d46ecd32..493c2366 100644 --- a/src/util.h +++ b/src/util.h @@ -37,9 +37,6 @@ #include #include -#include -#include - #include "http-parser/http_parser.h" namespace nghttp2 { @@ -448,21 +445,6 @@ void write_uri_field(std::ostream& o, const char *uri, const http_parser_url &u, http_parser_url_fields field); -class EvbufferBuffer { -public: - EvbufferBuffer(); - EvbufferBuffer(evbuffer *evbuffer, uint8_t *buf, size_t bufmax); - void reset(evbuffer *evbuffer, uint8_t *buf, size_t bufmax); - int flush(); - int add(const uint8_t *data, size_t datalen); - size_t get_buflen() const; -private: - evbuffer *evbuffer_; - uint8_t *buf_; - size_t bufmax_; - size_t buflen_; -}; - bool numeric_host(const char *hostname); // Opens |path| with O_APPEND enabled. If file does not exist, it is @@ -482,12 +464,6 @@ std::string ascii_dump(const uint8_t *data, size_t len); // it. char* get_exec_path(int argc, char **const argv, const char *cwd); -// These functions are provided to reduce epoll_ctl syscall. Avoid -// calling bufferevent_enable/disable() unless it is required by -// sniffing current enabled events. -void bev_enable_unless(bufferevent *bev, int events); -void bev_disable_unless(bufferevent *bev, int events); - } // namespace util } // namespace nghttp2