Remove spdyd and spdycat
This commit is contained in:
parent
9e9a7fb160
commit
551ae72f3a
|
@ -979,6 +979,7 @@ int HttpServer::run()
|
|||
SSL_CTX_set_options(ssl_ctx, SSL_OP_ALL|SSL_OP_NO_SSLv2);
|
||||
SSL_CTX_set_mode(ssl_ctx, SSL_MODE_AUTO_RETRY);
|
||||
SSL_CTX_set_mode(ssl_ctx, SSL_MODE_RELEASE_BUFFERS);
|
||||
SSL_CTX_set_mode(ssl_ctx, SSL_MODE_ENABLE_PARTIAL_WRITE);
|
||||
if(SSL_CTX_use_PrivateKey_file(ssl_ctx,
|
||||
config_->private_key_file.c_str(),
|
||||
SSL_FILETYPE_PEM) != 1) {
|
||||
|
|
|
@ -35,11 +35,7 @@ AM_CXXFLAGS = -std=c++11
|
|||
|
||||
LDADD = $(top_builddir)/lib/libnghttp2.la
|
||||
|
||||
bin_PROGRAMS += spdycat nghttp nghttpd
|
||||
|
||||
if ENABLE_SPDYD
|
||||
bin_PROGRAMS += spdyd
|
||||
endif # ENABLE_SPDYD
|
||||
bin_PROGRAMS += nghttp nghttpd
|
||||
|
||||
if HAVE_LIBEVENT_OPENSSL
|
||||
# bin_PROGRAMS += shrpx
|
||||
|
@ -68,10 +64,6 @@ if HAVE_LIBXML2
|
|||
HTML_PARSER_OBJECTS += HtmlParser.cc
|
||||
endif # HAVE_LIBXML2
|
||||
|
||||
spdycat_SOURCES = ${HELPER_OBJECTS} ${HELPER_HFILES} spdycat.cc \
|
||||
${HTML_PARSER_OBJECTS} ${HTML_PARSER_HFILES} \
|
||||
http-parser/http_parser.c http-parser/http_parser.h
|
||||
|
||||
nghttp_SOURCES = ${HELPER_OBJECTS} ${HELPER_HFILES} nghttp.cc \
|
||||
${HTML_PARSER_OBJECTS} ${HTML_PARSER_HFILES} \
|
||||
http-parser/http_parser.c http-parser/http_parser.h
|
||||
|
@ -79,16 +71,6 @@ nghttp_SOURCES = ${HELPER_OBJECTS} ${HELPER_HFILES} nghttp.cc \
|
|||
nghttpd_SOURCES = ${HELPER_OBJECTS} ${HELPER_HFILES} nghttpd.cc \
|
||||
HttpServer.cc HttpServer.h
|
||||
|
||||
if ENABLE_SPDYD
|
||||
SPDY_SERVER_OBJECTS = SpdyServer.cc
|
||||
SPDY_SERVER_HFILES = SpdyServer.h
|
||||
|
||||
spdyd_SOURCES = ${HELPER_OBJECTS} ${HELPER_HFILES} \
|
||||
${EVENT_OBJECTS} ${EVENT_HFILES} \
|
||||
${SPDY_SERVER_OBJECTS} ${SPDY_SERVER_HFILES} \
|
||||
spdyd.cc
|
||||
endif # ENABLE_SPDYD
|
||||
|
||||
if HAVE_LIBEVENT_OPENSSL
|
||||
# SHRPX_SRCS = \
|
||||
# util.cc util.h timegm.c timegm.h base64.h \
|
||||
|
|
1035
src/SpdyServer.cc
1035
src/SpdyServer.cc
File diff suppressed because it is too large
Load Diff
166
src/SpdyServer.h
166
src/SpdyServer.h
|
@ -1,166 +0,0 @@
|
|||
/*
|
||||
* nghttp2 - HTTP/2.0 C 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 SPDY_SERVER_H
|
||||
#define SPDY_SERVER_H
|
||||
|
||||
#include "nghttp2_config.h"
|
||||
|
||||
#include <stdint.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include <cstdlib>
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
|
||||
#include <openssl/ssl.h>
|
||||
|
||||
#include <nghttp2/nghttp2.h>
|
||||
|
||||
namespace nghttp2 {
|
||||
|
||||
struct Config {
|
||||
std::string htdocs;
|
||||
bool verbose;
|
||||
bool daemon;
|
||||
std::string host;
|
||||
uint16_t port;
|
||||
std::string private_key_file;
|
||||
std::string cert_file;
|
||||
nghttp2_on_request_recv_callback on_request_recv_callback;
|
||||
void *data_ptr;
|
||||
bool verify_client;
|
||||
bool no_tls;
|
||||
Config();
|
||||
};
|
||||
|
||||
class Sessions;
|
||||
|
||||
class EventHandler {
|
||||
public:
|
||||
EventHandler(const Config *config);
|
||||
virtual ~EventHandler() {}
|
||||
virtual int execute(Sessions *sessions) = 0;
|
||||
virtual bool want_read() = 0;
|
||||
virtual bool want_write() = 0;
|
||||
virtual int fd() const = 0;
|
||||
virtual bool finish() = 0;
|
||||
const Config* config() const
|
||||
{
|
||||
return config_;
|
||||
}
|
||||
bool mark_del()
|
||||
{
|
||||
return mark_del_;
|
||||
}
|
||||
void mark_del(bool d)
|
||||
{
|
||||
mark_del_ = d;
|
||||
}
|
||||
private:
|
||||
const Config *config_;
|
||||
bool mark_del_;
|
||||
};
|
||||
|
||||
struct Request {
|
||||
int32_t stream_id;
|
||||
std::vector<std::pair<std::string, std::string>> headers;
|
||||
int file;
|
||||
std::pair<std::string, size_t> response_body;
|
||||
Request(int32_t stream_id);
|
||||
~Request();
|
||||
};
|
||||
|
||||
class SpdyEventHandler : public EventHandler {
|
||||
public:
|
||||
SpdyEventHandler(const Config* config,
|
||||
int fd, SSL *ssl,
|
||||
const nghttp2_session_callbacks *callbacks,
|
||||
int64_t session_id);
|
||||
virtual ~SpdyEventHandler();
|
||||
virtual int execute(Sessions *sessions);
|
||||
virtual bool want_read();
|
||||
virtual bool want_write();
|
||||
virtual int fd() const;
|
||||
virtual bool finish();
|
||||
|
||||
ssize_t send_data(const uint8_t *data, size_t len, int flags);
|
||||
|
||||
ssize_t recv_data(uint8_t *data, size_t len, int flags);
|
||||
|
||||
bool would_block() const;
|
||||
|
||||
int submit_file_response(const std::string& status,
|
||||
int32_t stream_id,
|
||||
time_t last_modified,
|
||||
off_t file_length,
|
||||
nghttp2_data_provider *data_prd);
|
||||
|
||||
int submit_response(const std::string& status,
|
||||
int32_t stream_id,
|
||||
nghttp2_data_provider *data_prd);
|
||||
|
||||
int submit_response
|
||||
(const std::string& status,
|
||||
int32_t stream_id,
|
||||
const std::vector<std::pair<std::string, std::string>>& headers,
|
||||
nghttp2_data_provider *data_prd);
|
||||
|
||||
void add_stream(int32_t stream_id, Request *req);
|
||||
void remove_stream(int32_t stream_id);
|
||||
Request* get_stream(int32_t stream_id);
|
||||
int64_t session_id() const;
|
||||
private:
|
||||
nghttp2_session *session_;
|
||||
int fd_;
|
||||
SSL* ssl_;
|
||||
int64_t session_id_;
|
||||
uint8_t io_flags_;
|
||||
std::map<int32_t, Request*> id2req_;
|
||||
};
|
||||
|
||||
class SpdyServer {
|
||||
public:
|
||||
SpdyServer(const Config* config);
|
||||
~SpdyServer();
|
||||
int listen();
|
||||
int run();
|
||||
private:
|
||||
const Config *config_;
|
||||
int sfd_[2];
|
||||
};
|
||||
|
||||
void htdocs_on_request_recv_callback
|
||||
(nghttp2_session *session, int32_t stream_id, void *user_data);
|
||||
|
||||
ssize_t file_read_callback
|
||||
(nghttp2_session *session, int32_t stream_id,
|
||||
uint8_t *buf, size_t length, int *eof,
|
||||
nghttp2_data_source *source, void *user_data);
|
||||
|
||||
} // namespace nghttp2
|
||||
|
||||
#endif // SPDY_SERVER_H
|
|
@ -50,407 +50,33 @@ namespace nghttp2 {
|
|||
|
||||
bool ssl_debug = false;
|
||||
|
||||
Spdylay::Spdylay(int fd, SSL *ssl,
|
||||
const nghttp2_session_callbacks *callbacks,
|
||||
void *user_data)
|
||||
: fd_(fd), ssl_(ssl), user_data_(user_data), io_flags_(0)
|
||||
int select_next_proto_cb(SSL* ssl,
|
||||
unsigned char **out, unsigned char *outlen,
|
||||
const unsigned char *in, unsigned int inlen,
|
||||
void *arg)
|
||||
{
|
||||
int r = nghttp2_session_client_new(&session_, callbacks, this);
|
||||
assert(r == 0);
|
||||
}
|
||||
|
||||
Spdylay::~Spdylay()
|
||||
{
|
||||
nghttp2_session_del(session_);
|
||||
}
|
||||
|
||||
int Spdylay::recv()
|
||||
{
|
||||
return nghttp2_session_recv(session_);
|
||||
}
|
||||
|
||||
int Spdylay::send()
|
||||
{
|
||||
return nghttp2_session_send(session_);
|
||||
}
|
||||
|
||||
ssize_t Spdylay::send_data(const uint8_t *data, size_t len, int flags)
|
||||
{
|
||||
ssize_t r;
|
||||
io_flags_ = 0;
|
||||
if(ssl_) {
|
||||
ERR_clear_error();
|
||||
r = SSL_write(ssl_, data, len);
|
||||
if(r < 0) {
|
||||
io_flags_ = get_ssl_io_demand(ssl_, r);
|
||||
}
|
||||
} else {
|
||||
while((r = ::send(fd_, data, len, 0)) == -1 && errno == EINTR);
|
||||
if(r == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
|
||||
io_flags_ |= WANT_WRITE;
|
||||
if(ssl_debug) {
|
||||
print_timer();
|
||||
std::cout << " NPN select next protocol: the remote server offers:"
|
||||
<< std::endl;
|
||||
}
|
||||
for(unsigned int i = 0; i < inlen; i += in[i]+1) {
|
||||
if(ssl_debug) {
|
||||
std::cout << " * ";
|
||||
std::cout.write(reinterpret_cast<const char*>(&in[i+1]), in[i]);
|
||||
std::cout << std::endl;
|
||||
}
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
ssize_t Spdylay::recv_data(uint8_t *data, size_t len, int flags)
|
||||
{
|
||||
ssize_t r;
|
||||
io_flags_ = 0;
|
||||
if(ssl_) {
|
||||
ERR_clear_error();
|
||||
r = SSL_read(ssl_, data, len);
|
||||
if(r < 0) {
|
||||
io_flags_ = get_ssl_io_demand(ssl_, r);
|
||||
}
|
||||
} else {
|
||||
while((r = ::recv(fd_, data, len, 0)) == -1 && errno == EINTR);
|
||||
if(r == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
|
||||
io_flags_ |= WANT_READ;
|
||||
}
|
||||
if(nghttp2_select_next_protocol(out, outlen, in, inlen) <= 0) {
|
||||
std::cerr << "Server did not advertise HTTP/2.0 protocol."
|
||||
<< std::endl;
|
||||
abort();
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
bool Spdylay::want_read()
|
||||
{
|
||||
return nghttp2_session_want_read(session_) || (io_flags_ & WANT_READ);
|
||||
}
|
||||
|
||||
bool Spdylay::want_write()
|
||||
{
|
||||
return nghttp2_session_want_write(session_) || (io_flags_ & WANT_WRITE);
|
||||
}
|
||||
|
||||
bool Spdylay::finish()
|
||||
{
|
||||
return !nghttp2_session_want_read(session_) &&
|
||||
!nghttp2_session_want_write(session_);
|
||||
}
|
||||
|
||||
int Spdylay::fd() const
|
||||
{
|
||||
return fd_;
|
||||
}
|
||||
|
||||
void* Spdylay::user_data()
|
||||
{
|
||||
return user_data_;
|
||||
}
|
||||
|
||||
int Spdylay::submit_request(const std::string& scheme,
|
||||
const std::string& hostport,
|
||||
const std::string& path,
|
||||
const std::map<std::string,std::string> &headers,
|
||||
int32_t pri,
|
||||
const nghttp2_data_provider *data_prd,
|
||||
int64_t data_length,
|
||||
void *stream_user_data)
|
||||
{
|
||||
enum eStaticHeaderPosition
|
||||
{
|
||||
POS_METHOD = 0,
|
||||
POS_PATH,
|
||||
POS_SCHEME,
|
||||
POS_HOST,
|
||||
POS_ACCEPT,
|
||||
POS_ACCEPT_ENCODING,
|
||||
POS_USERAGENT
|
||||
};
|
||||
|
||||
const char *static_nv[] = {
|
||||
":method", data_prd ? "POST" : "GET",
|
||||
":path", path.c_str(),
|
||||
":scheme", scheme.c_str(),
|
||||
":host", hostport.c_str(),
|
||||
"accept", "*/*",
|
||||
"accept-encoding", "gzip, deflate",
|
||||
"user-agent", "nghttp2/" NGHTTP2_VERSION
|
||||
};
|
||||
|
||||
int hardcoded_entry_count = sizeof(static_nv) / sizeof(*static_nv);
|
||||
int header_count = headers.size();
|
||||
int total_entry_count = hardcoded_entry_count + header_count * 2;
|
||||
if(data_prd) {
|
||||
++total_entry_count;
|
||||
if(ssl_debug) {
|
||||
std::cout << " NPN selected the protocol: "
|
||||
<< std::string((const char*)*out, (size_t)*outlen) << std::endl;
|
||||
}
|
||||
|
||||
const char **nv = new const char*[total_entry_count + 1];
|
||||
|
||||
memcpy(nv, static_nv, hardcoded_entry_count * sizeof(*static_nv));
|
||||
|
||||
auto i = std::begin(headers);
|
||||
auto end = std::end(headers);
|
||||
|
||||
int pos = hardcoded_entry_count;
|
||||
|
||||
std::string content_length_str;
|
||||
if(data_prd) {
|
||||
std::stringstream ss;
|
||||
ss << data_length;
|
||||
content_length_str = ss.str();
|
||||
nv[pos++] = "content-length";
|
||||
nv[pos++] = content_length_str.c_str();
|
||||
}
|
||||
while( i != end ) {
|
||||
const char *key = (*i).first.c_str();
|
||||
const char *value = (*i).second.c_str();
|
||||
if ( util::strieq( key, "accept" ) ) {
|
||||
nv[POS_ACCEPT*2+1] = value;
|
||||
}
|
||||
else if ( util::strieq( key, "user-agent" ) ) {
|
||||
nv[POS_USERAGENT*2+1] = value;
|
||||
}
|
||||
else if ( util::strieq( key, "host" ) ) {
|
||||
nv[POS_HOST*2+1] = value;
|
||||
}
|
||||
else {
|
||||
nv[pos] = key;
|
||||
nv[pos+1] = value;
|
||||
pos += 2;
|
||||
}
|
||||
++i;
|
||||
}
|
||||
nv[pos] = nullptr;
|
||||
|
||||
int r = nghttp2_submit_request(session_, pri, nv, data_prd,
|
||||
stream_user_data);
|
||||
|
||||
delete [] nv;
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
int Spdylay::submit_settings(nghttp2_settings_entry *iv, size_t niv)
|
||||
{
|
||||
return nghttp2_submit_settings(session_, iv, niv);
|
||||
}
|
||||
|
||||
bool Spdylay::would_block()
|
||||
{
|
||||
return io_flags_;
|
||||
}
|
||||
|
||||
int connect_to(const std::string& host, uint16_t port)
|
||||
{
|
||||
struct addrinfo hints;
|
||||
int fd = -1;
|
||||
int r;
|
||||
char service[10];
|
||||
snprintf(service, sizeof(service), "%u", port);
|
||||
memset(&hints, 0, sizeof(struct addrinfo));
|
||||
hints.ai_family = AF_UNSPEC;
|
||||
hints.ai_socktype = SOCK_STREAM;
|
||||
struct addrinfo *res;
|
||||
r = getaddrinfo(host.c_str(), service, &hints, &res);
|
||||
if(r != 0) {
|
||||
std::cerr << "getaddrinfo: " << gai_strerror(r) << std::endl;
|
||||
return -1;
|
||||
}
|
||||
for(struct addrinfo *rp = res; rp; rp = rp->ai_next) {
|
||||
fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
|
||||
if(fd == -1) {
|
||||
continue;
|
||||
}
|
||||
while((r = connect(fd, rp->ai_addr, rp->ai_addrlen)) == -1 &&
|
||||
errno == EINTR);
|
||||
if(r == 0) {
|
||||
break;
|
||||
}
|
||||
close(fd);
|
||||
fd = -1;
|
||||
}
|
||||
freeaddrinfo(res);
|
||||
return fd;
|
||||
}
|
||||
|
||||
int nonblock_connect_to(const std::string& host, uint16_t port, int timeout)
|
||||
{
|
||||
struct addrinfo hints;
|
||||
int fd = -1;
|
||||
int r;
|
||||
char service[10];
|
||||
snprintf(service, sizeof(service), "%u", port);
|
||||
memset(&hints, 0, sizeof(struct addrinfo));
|
||||
hints.ai_family = AF_UNSPEC;
|
||||
hints.ai_socktype = SOCK_STREAM;
|
||||
struct addrinfo *res;
|
||||
r = getaddrinfo(host.c_str(), service, &hints, &res);
|
||||
if(r != 0) {
|
||||
std::cerr << "getaddrinfo: " << gai_strerror(r) << std::endl;
|
||||
return -1;
|
||||
}
|
||||
for(struct addrinfo *rp = res; rp; rp = rp->ai_next) {
|
||||
fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
|
||||
if(fd == -1) {
|
||||
continue;
|
||||
}
|
||||
if(make_non_block(fd) == -1) {
|
||||
close(fd);
|
||||
fd = -1;
|
||||
continue;
|
||||
}
|
||||
while((r = connect(fd, rp->ai_addr, rp->ai_addrlen)) == -1 &&
|
||||
errno == EINTR);
|
||||
if(r == 0) {
|
||||
break;
|
||||
} else if(errno == EINPROGRESS) {
|
||||
struct timeval tv1, tv2;
|
||||
struct pollfd pfd = {fd, POLLOUT, 0};
|
||||
if(timeout != -1) {
|
||||
get_time(&tv1);
|
||||
}
|
||||
r = poll(&pfd, 1, timeout);
|
||||
if(r == 0) {
|
||||
return -2;
|
||||
} else if(r == -1) {
|
||||
return -1;
|
||||
} else {
|
||||
if(timeout != -1) {
|
||||
get_time(&tv2);
|
||||
timeout -= time_delta(tv2, tv1);
|
||||
if(timeout <= 0) {
|
||||
return -2;
|
||||
}
|
||||
}
|
||||
int socket_error;
|
||||
socklen_t optlen = sizeof(socket_error);
|
||||
r = getsockopt(fd, SOL_SOCKET, SO_ERROR, &socket_error, &optlen);
|
||||
if(r == 0 && socket_error == 0) {
|
||||
break;
|
||||
} else {
|
||||
close(fd);
|
||||
fd = -1;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
close(fd);
|
||||
fd = -1;
|
||||
}
|
||||
}
|
||||
freeaddrinfo(res);
|
||||
return fd;
|
||||
}
|
||||
|
||||
int make_listen_socket(const std::string& host, uint16_t port, int family)
|
||||
{
|
||||
addrinfo hints;
|
||||
int fd = -1;
|
||||
int r;
|
||||
char service[10];
|
||||
snprintf(service, sizeof(service), "%u", port);
|
||||
memset(&hints, 0, sizeof(addrinfo));
|
||||
hints.ai_family = family;
|
||||
hints.ai_socktype = SOCK_STREAM;
|
||||
hints.ai_flags = AI_PASSIVE;
|
||||
#ifdef AI_ADDRCONFIG
|
||||
hints.ai_flags |= AI_ADDRCONFIG;
|
||||
#endif // AI_ADDRCONFIG
|
||||
addrinfo *res, *rp;
|
||||
const char* host_ptr;
|
||||
if(host.empty()) {
|
||||
host_ptr = 0;
|
||||
} else {
|
||||
host_ptr = host.c_str();
|
||||
}
|
||||
r = getaddrinfo(host_ptr, service, &hints, &res);
|
||||
if(r != 0) {
|
||||
std::cerr << "getaddrinfo: " << gai_strerror(r) << std::endl;
|
||||
return -1;
|
||||
}
|
||||
for(rp = res; rp; rp = rp->ai_next) {
|
||||
fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
|
||||
if(fd == -1) {
|
||||
continue;
|
||||
}
|
||||
int val = 1;
|
||||
if(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val,
|
||||
static_cast<socklen_t>(sizeof(val))) == -1) {
|
||||
close(fd);
|
||||
continue;
|
||||
}
|
||||
#ifdef IPV6_V6ONLY
|
||||
if(family == AF_INET6) {
|
||||
if(setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &val,
|
||||
static_cast<socklen_t>(sizeof(val))) == -1) {
|
||||
close(fd);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
#endif // IPV6_V6ONLY
|
||||
|
||||
if(bind(fd, rp->ai_addr, rp->ai_addrlen) == 0) {
|
||||
break;
|
||||
}
|
||||
close(fd);
|
||||
}
|
||||
freeaddrinfo(res);
|
||||
if(rp == 0) {
|
||||
return -1;
|
||||
} else {
|
||||
if(listen(fd, 16) == -1) {
|
||||
close(fd);
|
||||
return -1;
|
||||
} else {
|
||||
return fd;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int make_non_block(int fd)
|
||||
{
|
||||
int flags, r;
|
||||
while((flags = fcntl(fd, F_GETFL, 0)) == -1 && errno == EINTR);
|
||||
if(flags == -1) {
|
||||
return -1;
|
||||
}
|
||||
while((r = fcntl(fd, F_SETFL, flags | O_NONBLOCK)) == -1 && errno == EINTR);
|
||||
if(r == -1) {
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int set_tcp_nodelay(int fd)
|
||||
{
|
||||
int val = 1;
|
||||
return setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &val, (socklen_t)sizeof(val));
|
||||
}
|
||||
|
||||
ssize_t send_callback(nghttp2_session *session,
|
||||
const uint8_t *data, size_t len, int flags,
|
||||
void *user_data)
|
||||
{
|
||||
Spdylay *sc = (Spdylay*)user_data;
|
||||
ssize_t r = sc->send_data(data, len, flags);
|
||||
if(r < 0) {
|
||||
if(sc->would_block()) {
|
||||
r = NGHTTP2_ERR_WOULDBLOCK;
|
||||
} else {
|
||||
r = NGHTTP2_ERR_CALLBACK_FAILURE;
|
||||
}
|
||||
} else if(r == 0) {
|
||||
// In OpenSSL, r == 0 means EOF because SSL_write may do read.
|
||||
r = NGHTTP2_ERR_CALLBACK_FAILURE;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
ssize_t recv_callback(nghttp2_session *session,
|
||||
uint8_t *data, size_t len, int flags, void *user_data)
|
||||
{
|
||||
Spdylay *sc = (Spdylay*)user_data;
|
||||
ssize_t r = sc->recv_data(data, len, flags);
|
||||
if(r < 0) {
|
||||
if(sc->would_block()) {
|
||||
r = NGHTTP2_ERR_WOULDBLOCK;
|
||||
} else {
|
||||
r = NGHTTP2_ERR_CALLBACK_FAILURE;
|
||||
}
|
||||
} else if(r == 0) {
|
||||
r = NGHTTP2_ERR_EOF;
|
||||
}
|
||||
return r;
|
||||
return SSL_TLSEXT_ERR_OK;
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
@ -791,124 +417,6 @@ void on_data_send_callback
|
|||
fflush(stdout);
|
||||
}
|
||||
|
||||
void ctl_poll(pollfd *pollfd, Spdylay *sc)
|
||||
{
|
||||
pollfd->events = 0;
|
||||
if(sc->want_read()) {
|
||||
pollfd->events |= POLLIN;
|
||||
}
|
||||
if(sc->want_write()) {
|
||||
pollfd->events |= POLLOUT;
|
||||
}
|
||||
}
|
||||
|
||||
int select_next_proto_cb(SSL* ssl,
|
||||
unsigned char **out, unsigned char *outlen,
|
||||
const unsigned char *in, unsigned int inlen,
|
||||
void *arg)
|
||||
{
|
||||
if(ssl_debug) {
|
||||
print_timer();
|
||||
std::cout << " NPN select next protocol: the remote server offers:"
|
||||
<< std::endl;
|
||||
}
|
||||
for(unsigned int i = 0; i < inlen; i += in[i]+1) {
|
||||
if(ssl_debug) {
|
||||
std::cout << " * ";
|
||||
std::cout.write(reinterpret_cast<const char*>(&in[i+1]), in[i]);
|
||||
std::cout << std::endl;
|
||||
}
|
||||
}
|
||||
if(nghttp2_select_next_protocol(out, outlen, in, inlen) <= 0) {
|
||||
std::cerr << "Server did not advertise HTTP/2.0 protocol."
|
||||
<< std::endl;
|
||||
abort();
|
||||
}
|
||||
if(ssl_debug) {
|
||||
std::cout << " NPN selected the protocol: "
|
||||
<< std::string((const char*)*out, (size_t)*outlen) << std::endl;
|
||||
}
|
||||
return SSL_TLSEXT_ERR_OK;
|
||||
}
|
||||
|
||||
void setup_ssl_ctx(SSL_CTX *ssl_ctx, void *next_proto_select_cb_arg)
|
||||
{
|
||||
/* Disable SSLv2 and enable all workarounds for buggy servers */
|
||||
SSL_CTX_set_options(ssl_ctx, SSL_OP_ALL|SSL_OP_NO_SSLv2);
|
||||
SSL_CTX_set_mode(ssl_ctx, SSL_MODE_AUTO_RETRY);
|
||||
SSL_CTX_set_mode(ssl_ctx, SSL_MODE_RELEASE_BUFFERS);
|
||||
SSL_CTX_set_mode(ssl_ctx, SSL_MODE_ENABLE_PARTIAL_WRITE);
|
||||
SSL_CTX_set_next_proto_select_cb(ssl_ctx, select_next_proto_cb,
|
||||
next_proto_select_cb_arg);
|
||||
}
|
||||
|
||||
int ssl_handshake(SSL *ssl, int fd)
|
||||
{
|
||||
if(SSL_set_fd(ssl, fd) == 0) {
|
||||
std::cerr << ERR_error_string(ERR_get_error(), 0) << std::endl;
|
||||
return -1;
|
||||
}
|
||||
ERR_clear_error();
|
||||
int r = SSL_connect(ssl);
|
||||
if(r <= 0) {
|
||||
std::cerr << ERR_error_string(ERR_get_error(), 0) << std::endl;
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ssl_nonblock_handshake(SSL *ssl, int fd, int& timeout)
|
||||
{
|
||||
if(SSL_set_fd(ssl, fd) == 0) {
|
||||
std::cerr << ERR_error_string(ERR_get_error(), 0) << std::endl;
|
||||
return -1;
|
||||
}
|
||||
ERR_clear_error();
|
||||
pollfd pfd;
|
||||
pfd.fd = fd;
|
||||
pfd.events = POLLOUT;
|
||||
timeval tv1, tv2;
|
||||
while(1) {
|
||||
if(timeout != -1) {
|
||||
get_time(&tv1);
|
||||
}
|
||||
int rv = poll(&pfd, 1, timeout);
|
||||
if(rv == 0) {
|
||||
return -2;
|
||||
} else if(rv == -1) {
|
||||
return -1;
|
||||
}
|
||||
ERR_clear_error();
|
||||
rv = SSL_connect(ssl);
|
||||
if(rv == 0) {
|
||||
std::cerr << ERR_error_string(ERR_get_error(), 0) << std::endl;
|
||||
return -1;
|
||||
} else if(rv < 0) {
|
||||
if(timeout != -1) {
|
||||
get_time(&tv2);
|
||||
timeout -= time_delta(tv2, tv1);
|
||||
if(timeout <= 0) {
|
||||
return -2;
|
||||
}
|
||||
}
|
||||
switch(SSL_get_error(ssl, rv)) {
|
||||
case SSL_ERROR_WANT_READ:
|
||||
pfd.events = POLLIN;
|
||||
break;
|
||||
case SSL_ERROR_WANT_WRITE:
|
||||
pfd.events = POLLOUT;
|
||||
break;
|
||||
default:
|
||||
std::cerr << ERR_error_string(ERR_get_error(), 0) << std::endl;
|
||||
return -1;
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int64_t time_delta(const timeval& a, const timeval& b)
|
||||
{
|
||||
int64_t res = (a.tv_sec - b.tv_sec) * 1000;
|
||||
|
@ -916,18 +424,6 @@ int64_t time_delta(const timeval& a, const timeval& b)
|
|||
return res;
|
||||
}
|
||||
|
||||
uint8_t get_ssl_io_demand(SSL *ssl, ssize_t r)
|
||||
{
|
||||
switch(SSL_get_error(ssl, r)) {
|
||||
case SSL_ERROR_WANT_WRITE:
|
||||
return WANT_WRITE;
|
||||
case SSL_ERROR_WANT_READ:
|
||||
return WANT_READ;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
namespace {
|
||||
timeval base_tv;
|
||||
} // namespace
|
||||
|
|
|
@ -41,55 +41,10 @@ namespace nghttp2 {
|
|||
|
||||
extern bool ssl_debug;
|
||||
|
||||
class Spdylay {
|
||||
public:
|
||||
Spdylay(int fd, SSL *ssl,
|
||||
const nghttp2_session_callbacks *callbacks,
|
||||
void *user_data);
|
||||
~Spdylay();
|
||||
int recv();
|
||||
int send();
|
||||
ssize_t send_data(const uint8_t *data, size_t len, int flags);
|
||||
ssize_t recv_data(uint8_t *data, size_t len, int flags);
|
||||
bool want_read();
|
||||
bool want_write();
|
||||
bool finish();
|
||||
int fd() const;
|
||||
int submit_request(const std::string& scheme,
|
||||
const std::string& hostport, const std::string& path,
|
||||
const std::map<std::string,std::string>& headers,
|
||||
int32_t pri,
|
||||
const nghttp2_data_provider *data_prd,
|
||||
int64_t data_length,
|
||||
void *stream_user_data);
|
||||
int submit_settings(nghttp2_settings_entry *iv, size_t niv);
|
||||
bool would_block();
|
||||
void* user_data();
|
||||
private:
|
||||
int fd_;
|
||||
SSL *ssl_;
|
||||
nghttp2_session *session_;
|
||||
void *user_data_;
|
||||
uint8_t io_flags_;
|
||||
bool debug_;
|
||||
};
|
||||
|
||||
int connect_to(const std::string& host, uint16_t port);
|
||||
|
||||
int nonblock_connect_to(const std::string& host, uint16_t port, int timeout);
|
||||
|
||||
int make_listen_socket(const std::string& host, uint16_t port, int family);
|
||||
|
||||
int make_non_block(int fd);
|
||||
|
||||
int set_tcp_nodelay(int fd);
|
||||
|
||||
ssize_t send_callback(nghttp2_session *session,
|
||||
const uint8_t *data, size_t len, int flags,
|
||||
void *user_data);
|
||||
|
||||
ssize_t recv_callback(nghttp2_session *session,
|
||||
uint8_t *data, size_t len, int flags, void *user_data);
|
||||
int select_next_proto_cb(SSL* ssl,
|
||||
unsigned char **out, unsigned char *outlen,
|
||||
const unsigned char *in, unsigned int inlen,
|
||||
void *arg);
|
||||
|
||||
void print_nv(char **nv);
|
||||
|
||||
|
@ -126,19 +81,6 @@ void on_data_send_callback
|
|||
(nghttp2_session *session, uint16_t length, uint8_t flags, int32_t stream_id,
|
||||
void *user_data);
|
||||
|
||||
void ctl_poll(pollfd *pollfd, Spdylay *sc);
|
||||
|
||||
int select_next_proto_cb(SSL* ssl,
|
||||
unsigned char **out, unsigned char *outlen,
|
||||
const unsigned char *in, unsigned int inlen,
|
||||
void *arg);
|
||||
|
||||
void setup_ssl_ctx(SSL_CTX *ssl_ctx, void *next_proto_select_cb_arg);
|
||||
|
||||
int ssl_handshake(SSL *ssl, int fd);
|
||||
|
||||
int ssl_nonblock_handshake(SSL *ssl, int fd, int& timeout);
|
||||
|
||||
// Returns difference between |a| and |b| in milliseconds, assuming
|
||||
// |a| is more recent than |b|.
|
||||
int64_t time_delta(const timeval& a, const timeval& b);
|
||||
|
@ -151,13 +93,6 @@ int get_time(timeval *tv);
|
|||
|
||||
void print_timer();
|
||||
|
||||
enum {
|
||||
WANT_READ = 1,
|
||||
WANT_WRITE = 1 << 1
|
||||
};
|
||||
|
||||
uint8_t get_ssl_io_demand(SSL *ssl, ssize_t r);
|
||||
|
||||
// Setting true will print characters with ANSI color escape codes
|
||||
// when printing SPDY frames. This function changes a static variable.
|
||||
void set_color_output(bool f);
|
||||
|
|
1033
src/spdycat.cc
1033
src/spdycat.cc
File diff suppressed because it is too large
Load Diff
185
src/spdyd.cc
185
src/spdyd.cc
|
@ -1,185 +0,0 @@
|
|||
/*
|
||||
* nghttp2 - HTTP/2.0 C 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 <unistd.h>
|
||||
#include <signal.h>
|
||||
#include <getopt.h>
|
||||
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <cassert>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#include <openssl/ssl.h>
|
||||
#include <openssl/err.h>
|
||||
#include <nghttp2/nghttp2.h>
|
||||
|
||||
#include "nghttp2_ssl.h"
|
||||
#include "SpdyServer.h"
|
||||
|
||||
namespace nghttp2 {
|
||||
|
||||
extern bool ssl_debug;
|
||||
|
||||
namespace {
|
||||
void print_usage(std::ostream& out)
|
||||
{
|
||||
out << "Usage: spdyd [-DVhv] [-d <PATH>] [--no-tls] <PORT> [<PRIVATE_KEY> <CERT>]"
|
||||
<< std::endl;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
namespace {
|
||||
void print_help(std::ostream& out)
|
||||
{
|
||||
print_usage(out);
|
||||
out << "\n"
|
||||
<< "OPTIONS:\n"
|
||||
<< " -D, --daemon Run in a background. If -D is used, the\n"
|
||||
<< " current working directory is changed to '/'.\n"
|
||||
<< " Therefore if this option is used, -d option\n"
|
||||
<< " must be specified.\n"
|
||||
<< " -V, --verify-client\n"
|
||||
<< " The server sends a client certificate\n"
|
||||
<< " request. If the client did not return a\n"
|
||||
<< " certificate, the handshake is terminated.\n"
|
||||
<< " Currently, this option just requests a\n"
|
||||
<< " client certificate and does not verify it.\n"
|
||||
<< " -d, --htdocs=<PATH>\n"
|
||||
<< " Specify document root. If this option is\n"
|
||||
<< " not specified, the document root is the\n"
|
||||
<< " current working directory.\n"
|
||||
<< " -v, --verbose Print debug information such as reception/\n"
|
||||
<< " transmission of frames and name/value pairs.\n"
|
||||
<< " --no-tls Disable SSL/TLS.\n"
|
||||
<< " -h, --help Print this help.\n"
|
||||
<< std::endl;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
Config config;
|
||||
while(1) {
|
||||
int flag;
|
||||
static option long_options[] = {
|
||||
{"daemon", no_argument, 0, 'D' },
|
||||
{"htdocs", required_argument, 0, 'd' },
|
||||
{"help", no_argument, 0, 'h' },
|
||||
{"verbose", no_argument, 0, 'v' },
|
||||
{"verify-client", no_argument, 0, 'V' },
|
||||
{"no-tls", no_argument, &flag, 1 },
|
||||
{0, 0, 0, 0 }
|
||||
};
|
||||
int option_index = 0;
|
||||
int c = getopt_long(argc, argv, "DVd:hv", long_options, &option_index);
|
||||
if(c == -1) {
|
||||
break;
|
||||
}
|
||||
switch(c) {
|
||||
case 'D':
|
||||
config.daemon = true;
|
||||
break;
|
||||
case 'V':
|
||||
config.verify_client = true;
|
||||
break;
|
||||
case 'd':
|
||||
config.htdocs = optarg;
|
||||
break;
|
||||
case 'h':
|
||||
print_help(std::cout);
|
||||
exit(EXIT_SUCCESS);
|
||||
case 'v':
|
||||
config.verbose = true;
|
||||
break;
|
||||
case '?':
|
||||
exit(EXIT_FAILURE);
|
||||
case 0:
|
||||
switch(flag) {
|
||||
case 1:
|
||||
// no-tls option
|
||||
config.no_tls = true;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(argc - optind < (config.no_tls ? 1 : 3)) {
|
||||
print_usage(std::cerr);
|
||||
std::cerr << "Too few arguments" << std::endl;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
config.port = strtol(argv[optind++], 0, 10);
|
||||
|
||||
if(!config.no_tls) {
|
||||
config.private_key_file = argv[optind++];
|
||||
config.cert_file = argv[optind++];
|
||||
}
|
||||
|
||||
if(config.daemon) {
|
||||
if(config.htdocs.empty()) {
|
||||
print_usage(std::cerr);
|
||||
std::cerr << "-d option must be specified when -D is used." << std::endl;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if(daemon(0, 0) == -1) {
|
||||
perror("daemon");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
if(config.htdocs.empty()) {
|
||||
config.htdocs = "./";
|
||||
}
|
||||
|
||||
set_color_output(isatty(fileno(stdout)));
|
||||
|
||||
struct sigaction act;
|
||||
memset(&act, 0, sizeof(struct sigaction));
|
||||
act.sa_handler = SIG_IGN;
|
||||
sigaction(SIGPIPE, &act, 0);
|
||||
OpenSSL_add_all_algorithms();
|
||||
SSL_load_error_strings();
|
||||
SSL_library_init();
|
||||
reset_timer();
|
||||
config.on_request_recv_callback = htdocs_on_request_recv_callback;
|
||||
ssl_debug = config.verbose;
|
||||
|
||||
SpdyServer server(&config);
|
||||
if(server.listen() == 0) {
|
||||
server.run();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
} // namespace nghttp2
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
return nghttp2::main(argc, argv);
|
||||
}
|
Loading…
Reference in New Issue