From 4bc200f9dccbed86d0bd97a4a897385bc174ee60 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Wed, 18 Jul 2012 01:08:05 +0900 Subject: [PATCH] Added simple accesslog --- examples/Makefile.am | 1 + examples/shrpx.cc | 5 +++ examples/shrpx_accesslog.cc | 67 ++++++++++++++++++++++++++++++++ examples/shrpx_accesslog.h | 39 +++++++++++++++++++ examples/shrpx_client_handler.cc | 4 ++ examples/shrpx_config.cc | 3 +- examples/shrpx_config.h | 1 + examples/shrpx_spdy_upstream.cc | 5 +++ 8 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 examples/shrpx_accesslog.cc create mode 100644 examples/shrpx_accesslog.h diff --git a/examples/Makefile.am b/examples/Makefile.am index e5b1f443..2e0a4fa9 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -88,6 +88,7 @@ shrpx_SOURCES = ${HELPER_OBJECTS} ${HELPER_HFILES} \ shrpx_ssl.cc shrpx_ssl.h \ shrpx_thread_event_receiver.cc shrpx_thread_event_receiver.h \ shrpx_worker.cc shrpx_worker.h \ + shrpx_accesslog.cc shrpx_accesslog.h\ http-parser/http_parser.c http-parser/http_parser.h endif # HAVE_LIBEVENT_OPENSSL diff --git a/examples/shrpx.cc b/examples/shrpx.cc index 21084df0..0c72b807 100644 --- a/examples/shrpx.cc +++ b/examples/shrpx.cc @@ -373,6 +373,7 @@ void print_help(std::ostream& out) << " Specify write timeout for backend\n" << " connection. Default: " << get_config()->downstream_write_timeout.tv_sec << "\n" + << " --accesslog Print simple accesslog to stderr.\n" << " -h, --help Print this help.\n" << std::endl; } @@ -405,6 +406,7 @@ int main(int argc, char **argv) {"frontend-write-timeout", required_argument, &flag, 4 }, {"backend-read-timeout", required_argument, &flag, 5 }, {"backend-write-timeout", required_argument, &flag, 6 }, + {"accesslog", no_argument, &flag, 7 }, {"help", no_argument, 0, 'h' }, {0, 0, 0, 0 } }; @@ -492,6 +494,9 @@ int main(int argc, char **argv) mod_config()->downstream_write_timeout = tv; break; } + case 7: + mod_config()->accesslog = true; + break; default: break; } diff --git a/examples/shrpx_accesslog.cc b/examples/shrpx_accesslog.cc new file mode 100644 index 00000000..b0186c39 --- /dev/null +++ b/examples/shrpx_accesslog.cc @@ -0,0 +1,67 @@ +/* + * Spdylay - SPDY Library + * + * Copyright (c) 2012 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_accesslog.h" + +#include +#include +#include + +namespace shrpx { + +namespace { +void get_datestr(char *buf) +{ + time_t now = time(0); + if(ctime_r(&now, buf) == 0) { + buf[0] = '\0'; + } else { + size_t len = strlen(buf); + if(len == 0) { + buf[0] = '\0'; + } else { + buf[strlen(buf)-1] = '\0'; + } + } +} +} // namespace + +void upstream_connect(const std::string& client_ip) +{ + char datestr[64]; + get_datestr(datestr); + fprintf(stderr, "[%s] %s\n", datestr, client_ip.c_str()); + fflush(stderr); +} + +void upstream_spdy_stream(const std::string& client_ip, int32_t stream_id) +{ + char datestr[64]; + get_datestr(datestr); + fprintf(stderr, "[%s] %s stream_id=%d\n", datestr, client_ip.c_str(), + stream_id); + fflush(stderr); +} + +} // namespace shrpx diff --git a/examples/shrpx_accesslog.h b/examples/shrpx_accesslog.h new file mode 100644 index 00000000..5f150c3f --- /dev/null +++ b/examples/shrpx_accesslog.h @@ -0,0 +1,39 @@ +/* + * Spdylay - SPDY Library + * + * Copyright (c) 2012 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_ACCESSLOG_H +#define SHRPX_ACCESSLOG_H + +#include "shrpx.h" + +#include + +namespace shrpx { + +void upstream_connect(const std::string& client_ip); +void upstream_spdy_stream(const std::string& client_ip, int32_t stream_id); + +} // namespace shrpx + +#endif // SHRPX_LOG_H diff --git a/examples/shrpx_client_handler.cc b/examples/shrpx_client_handler.cc index 2205f35f..6fe2eeae 100644 --- a/examples/shrpx_client_handler.cc +++ b/examples/shrpx_client_handler.cc @@ -31,6 +31,7 @@ #include "shrpx_https_upstream.h" #include "shrpx_config.h" #include "shrpx_downstream_connection.h" +#include "shrpx_accesslog.h" namespace shrpx { @@ -95,6 +96,9 @@ void upstream_eventcb(bufferevent *bev, short events, void *arg) if(ENABLE_LOG) { LOG(INFO) << "Upstream connected. handler " << handler; } + if(get_config()->accesslog) { + upstream_connect(handler->get_ipaddr()); + } handler->set_bev_cb(upstream_readcb, upstream_writecb, upstream_eventcb); handler->validate_next_proto(); if(ENABLE_LOG) { diff --git a/examples/shrpx_config.cc b/examples/shrpx_config.cc index e4e09534..8f6c6dc0 100644 --- a/examples/shrpx_config.cc +++ b/examples/shrpx_config.cc @@ -42,7 +42,8 @@ Config::Config() num_worker(0), spdy_max_concurrent_streams(0), spdy_proxy(false), - add_x_forwarded_for(false) + add_x_forwarded_for(false), + accesslog(false) {} namespace { diff --git a/examples/shrpx_config.h b/examples/shrpx_config.h index 9042ab59..b44ea0cd 100644 --- a/examples/shrpx_config.h +++ b/examples/shrpx_config.h @@ -68,6 +68,7 @@ struct Config { size_t spdy_max_concurrent_streams; bool spdy_proxy; bool add_x_forwarded_for; + bool accesslog; Config(); }; diff --git a/examples/shrpx_spdy_upstream.cc b/examples/shrpx_spdy_upstream.cc index 5ab9be81..7fe45246 100644 --- a/examples/shrpx_spdy_upstream.cc +++ b/examples/shrpx_spdy_upstream.cc @@ -33,6 +33,7 @@ #include "shrpx_downstream_connection.h" #include "shrpx_config.h" #include "shrpx_http.h" +#include "shrpx_accesslog.h" #include "util.h" using namespace spdylay; @@ -211,6 +212,10 @@ void on_ctrl_recv_callback upstream->rst_stream(downstream, SPDYLAY_INTERNAL_ERROR); return; } + if(get_config()->accesslog) { + upstream_spdy_stream(upstream->get_client_handler()->get_ipaddr(), + frame->syn_stream.stream_id); + } downstream->set_request_state(Downstream::HEADER_COMPLETE); if(frame->syn_stream.hd.flags & SPDYLAY_CTRL_FLAG_FIN) { if(ENABLE_LOG) {