Commit dfce262f authored by Tatsuhiro Tsujikawa's avatar Tatsuhiro Tsujikawa

Added non-blocking SPDY server spdyd. It only handles static contents.

parent 3bfe0553
......@@ -26,6 +26,8 @@ AM_CPPFLAGS = -I$(srcdir)/../lib/includes -I$(builddir)/../lib/includes `pkg-con
AM_LDFLAGS = `pkg-config --libs libssl libcrypto`
LDADD = $(top_builddir)/lib/libspdylay.la
bin_PROGRAMS = spdycat
bin_PROGRAMS = spdycat spdyd
spdycat_SOURCES = uri.cc spdylay_ssl.cc util.cc spdycat.cc
spdyd_SOURCES = uri.cc spdylay_ssl.cc util.cc spdyd.cc
This diff is collapsed.
......@@ -159,6 +159,52 @@ int connect_to(const std::string& host, uint16_t port)
return fd;
}
int make_listen_socket(uint16_t port)
{
addrinfo hints;
int fd = -1;
int r;
char service[10];
snprintf(service, sizeof(service), "%u", port);
memset(&hints, 0, sizeof(addrinfo));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_ADDRCONFIG | AI_PASSIVE;
addrinfo *res, *rp;
r = getaddrinfo(0, 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;
}
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;
......@@ -173,6 +219,12 @@ int make_non_block(int fd)
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(spdylay_session *session,
const uint8_t *data, size_t len, int flags,
void *user_data)
......@@ -250,6 +302,7 @@ void print_ctrl_hd(const spdylay_ctrl_hd& hd)
}
} // namespace
namespace {
void print_frame(spdylay_frame_type type, spdylay_frame *frame)
{
printf("%s frame ", ctrl_names[type-1]);
......@@ -300,6 +353,7 @@ void print_frame(spdylay_frame_type type, spdylay_frame *frame)
break;
}
}
} // namespace
void on_ctrl_recv_callback
(spdylay_session *session, spdylay_frame_type type, spdylay_frame *frame,
......@@ -311,23 +365,41 @@ void on_ctrl_recv_callback
fflush(stdout);
}
void on_ctrl_send_callback
(spdylay_session *session, spdylay_frame_type type, spdylay_frame *frame,
void *user_data)
{
print_timer();
printf(" send ");
print_frame(type, frame);
fflush(stdout);
}
namespace {
void print_data_frame(uint8_t flags, int32_t stream_id, int32_t length)
{
printf("DATA frame (stream_id=%d, flags=%d, length=%d)\n",
stream_id, flags, length);
}
} // namespace
void on_data_recv_callback
(spdylay_session *session, uint8_t flags, int32_t stream_id, int32_t length,
void *user_data)
{
print_timer();
printf(" recv DATA frame (stream_id=%d, flags=%d, length=%d)\n",
stream_id, flags, length);
printf(" recv ");
print_data_frame(flags, stream_id, length);
fflush(stdout);
}
void on_ctrl_send_callback
(spdylay_session *session, spdylay_frame_type type, spdylay_frame *frame,
void on_data_send_callback
(spdylay_session *session, uint8_t flags, int32_t stream_id, int32_t length,
void *user_data)
{
print_timer();
printf(" send ");
print_frame(type, frame);
print_data_frame(flags, stream_id, length);
fflush(stdout);
}
......
......@@ -62,8 +62,12 @@ private:
int connect_to(const std::string& host, uint16_t port);
int make_listen_socket(uint16_t port);
int make_non_block(int fd);
int set_tcp_nodelay(int fd);
ssize_t send_callback(spdylay_session *session,
const uint8_t *data, size_t len, int flags,
void *user_data);
......@@ -77,12 +81,16 @@ void on_ctrl_recv_callback
(spdylay_session *session, spdylay_frame_type type, spdylay_frame *frame,
void *user_data);
void on_ctrl_send_callback
(spdylay_session *session, spdylay_frame_type type, spdylay_frame *frame,
void *user_data);
void on_data_recv_callback
(spdylay_session *session, uint8_t flags, int32_t stream_id, int32_t length,
void *user_data);
void on_ctrl_send_callback
(spdylay_session *session, spdylay_frame_type type, spdylay_frame *frame,
void on_data_send_callback
(spdylay_session *session, uint8_t flags, int32_t stream_id, int32_t length,
void *user_data);
void ctl_poll(pollfd *pollfd, Spdylay *sc);
......@@ -100,6 +108,8 @@ void reset_timer();
void get_timer(timeval *tv);
void print_timer();
} // namespace spdylay
#endif // SPDYLAY_SSL_H
......@@ -24,7 +24,10 @@
*/
#include "util.h"
#include <time.h>
#include <cstdio>
#include <cstring>
namespace spdylay {
......@@ -97,6 +100,30 @@ std::string percentDecode
return result;
}
std::string http_date(time_t t)
{
char buf[32];
tm* tms = gmtime(&t); // returned struct is statically allocated.
size_t r = strftime(buf, sizeof(buf), "%a, %d %b %Y %H:%M:%S GMT", tms);
return std::string(&buf[0], &buf[r]);
}
time_t parse_http_date(const std::string& s)
{
tm tm;
memset(&tm, 0, sizeof(tm));
char* r = strptime(s.c_str(), "%a, %d %b %Y %H:%M:%S GMT", &tm);
if(r == 0) {
return 0;
}
return timegm(&tm);
}
bool endsWith(const std::string& a, const std::string& b)
{
return endsWith(a.begin(), a.end(), b.begin(), b.end());
}
} // namespace util
} // namespace spdylay
......@@ -28,6 +28,7 @@
#include <vector>
#include <string>
#include <algorithm>
#include <sstream>
namespace spdylay {
......@@ -161,6 +162,33 @@ std::string percentEncode(const std::string& target);
std::string percentDecode
(std::string::const_iterator first, std::string::const_iterator last);
std::string http_date(time_t t);
time_t parse_http_date(const std::string& s);
template<typename T>
std::string to_str(T value)
{
std::stringstream ss;
ss << value;
return ss.str();
}
template<typename InputIterator1, typename InputIterator2>
bool endsWith
(InputIterator1 first1,
InputIterator1 last1,
InputIterator2 first2,
InputIterator2 last2)
{
if(last1-first1 < last2-first2) {
return false;
}
return std::equal(first2, last2, last1-(last2-first2));
}
bool endsWith(const std::string& a, const std::string& b);
} // namespace util
} // namespace spdylay
......
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