2012-09-22 21:40:46 -07:00
|
|
|
//
|
|
|
|
// httpsvrkit.h
|
|
|
|
//
|
|
|
|
// Copyright (c) 2012 Yuji Hirose. All rights reserved.
|
|
|
|
// The Boost Software License 1.0
|
|
|
|
//
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
2012-09-24 06:39:13 -07:00
|
|
|
//#define _CRT_SECURE_NO_WARNINGS
|
|
|
|
#define _CRT_NONSTDC_NO_DEPRECATE
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <io.h>
|
2012-09-22 21:40:46 -07:00
|
|
|
#include <winsock2.h>
|
|
|
|
|
|
|
|
typedef SOCKET socket_t;
|
|
|
|
#else
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <unistd.h>
|
2012-09-24 17:49:00 -07:00
|
|
|
#include <netdb.h>
|
2012-09-22 21:40:46 -07:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
2012-09-24 17:49:00 -07:00
|
|
|
#include <sys/socket.h>
|
2012-09-22 21:40:46 -07:00
|
|
|
|
|
|
|
typedef int socket_t;
|
|
|
|
#endif
|
|
|
|
|
2012-09-24 06:39:13 -07:00
|
|
|
#include <functional>
|
|
|
|
#include <map>
|
|
|
|
#include <regex>
|
|
|
|
#include <string>
|
|
|
|
#include <assert.h>
|
|
|
|
|
2012-09-22 21:40:46 -07:00
|
|
|
namespace httpsvrkit
|
|
|
|
{
|
|
|
|
|
2012-09-24 04:57:09 -07:00
|
|
|
typedef std::map<std::string, std::string> Map;
|
2012-09-24 20:13:01 -07:00
|
|
|
typedef std::vector<std::string> Array;
|
2012-09-24 04:57:09 -07:00
|
|
|
typedef std::multimap<std::string, std::string> MultiMap;
|
2012-09-22 21:40:46 -07:00
|
|
|
|
2012-09-24 04:57:09 -07:00
|
|
|
// HTTP request
|
|
|
|
struct Request {
|
2012-09-24 20:13:01 -07:00
|
|
|
std::string method;
|
|
|
|
std::string url;
|
2012-09-24 04:57:09 -07:00
|
|
|
Map headers;
|
|
|
|
std::string body;
|
2012-09-24 20:13:01 -07:00
|
|
|
Map query;
|
|
|
|
Array params;
|
2012-09-22 21:40:46 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
// HTTP response
|
2012-09-24 04:57:09 -07:00
|
|
|
struct Response {
|
|
|
|
MultiMap headers;
|
|
|
|
std::string body;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Context {
|
2012-09-24 20:13:01 -07:00
|
|
|
Request request;
|
|
|
|
Response response;
|
2012-09-22 21:40:46 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
// HTTP server
|
|
|
|
class Server {
|
|
|
|
public:
|
2012-09-24 20:13:01 -07:00
|
|
|
typedef std::function<int (Context& context)> Handler;
|
2012-09-22 21:40:46 -07:00
|
|
|
|
|
|
|
Server();
|
|
|
|
~Server();
|
|
|
|
|
2012-09-24 17:49:00 -07:00
|
|
|
void get(const char* pattern, Handler handler);
|
|
|
|
void post(const char* pattern, Handler handler);
|
2012-09-22 21:40:46 -07:00
|
|
|
|
2012-09-24 17:49:00 -07:00
|
|
|
bool run(const char* ipaddr_or_hostname, int port);
|
2012-09-22 21:40:46 -07:00
|
|
|
void stop();
|
|
|
|
|
|
|
|
private:
|
2012-09-24 06:39:13 -07:00
|
|
|
void process_request(FILE* fp_read, FILE* fp_write);
|
2012-09-22 21:40:46 -07:00
|
|
|
|
|
|
|
socket_t sock_;
|
2012-09-24 20:13:01 -07:00
|
|
|
std::vector<std::pair<std::regex, Handler>> get_handlers_;
|
|
|
|
std::vector<std::pair<std::string, Handler>> post_handlers_;
|
2012-09-22 21:40:46 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
// Implementation
|
|
|
|
|
2012-09-24 20:13:01 -07:00
|
|
|
template <class Fn>
|
|
|
|
void split(const char* b, const char* e, char d, Fn fn)
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
int beg = 0;
|
|
|
|
|
|
|
|
while (e ? (b + i != e) : (b[i] != '\0')) {
|
|
|
|
if (b[i] == d) {
|
|
|
|
fn(&b[beg], &b[i]);
|
|
|
|
beg = i + 1;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i != 0) {
|
|
|
|
fn(&b[beg], &b[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-24 17:49:00 -07:00
|
|
|
inline socket_t create_server_socket(const const char* ipaddr_or_hostname, int port)
|
2012-09-22 21:40:46 -07:00
|
|
|
{
|
|
|
|
// Create a server socket
|
|
|
|
socket_t sock = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
if (sock == -1) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make 'reuse address' option available
|
2012-09-24 06:39:13 -07:00
|
|
|
int opt = 1;
|
|
|
|
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char*)&opt, sizeof(opt));
|
2012-09-22 21:40:46 -07:00
|
|
|
|
2012-09-24 17:49:00 -07:00
|
|
|
// Get a host entry info
|
|
|
|
struct hostent* hp;
|
|
|
|
if (!(hp = gethostbyname(ipaddr_or_hostname))) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-09-22 21:40:46 -07:00
|
|
|
// Bind the socket to the given address
|
|
|
|
struct sockaddr_in addr;
|
2012-09-24 17:49:00 -07:00
|
|
|
memset(&addr, 0, sizeof(addr));
|
|
|
|
memcpy(&addr.sin_addr, hp->h_addr, hp->h_length);
|
2012-09-22 21:40:46 -07:00
|
|
|
addr.sin_family = AF_INET;
|
2012-09-24 17:49:00 -07:00
|
|
|
addr.sin_port = htons(port);
|
2012-09-22 21:40:46 -07:00
|
|
|
|
|
|
|
if (::bind(sock, (struct sockaddr*)&addr, sizeof(addr)) != 0) {
|
2012-09-24 17:49:00 -07:00
|
|
|
puts("(error)\n");
|
2012-09-22 21:40:46 -07:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Listen through 5 channels
|
|
|
|
if (listen(sock, 5) != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return sock;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void close_socket(socket_t sock)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
closesocket(sock);
|
|
|
|
#else
|
|
|
|
shutdown(sock, SHUT_RDWR);
|
|
|
|
close(sock);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
inline Server::Server()
|
|
|
|
: sock_(-1)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
WSADATA wsaData;
|
|
|
|
WSAStartup(0x0002, &wsaData);
|
2012-09-24 06:39:13 -07:00
|
|
|
|
|
|
|
#ifndef SO_SYNCHRONOUS_NONALERT
|
|
|
|
#define SO_SYNCHRONOUS_NONALERT 0x20;
|
|
|
|
#endif
|
|
|
|
#ifndef SO_OPENTYPE
|
|
|
|
#define SO_OPENTYPE 0x7008
|
|
|
|
#endif
|
|
|
|
int opt = SO_SYNCHRONOUS_NONALERT;
|
|
|
|
setsockopt(INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE, (char*)&opt, sizeof(opt));
|
2012-09-22 21:40:46 -07:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
inline Server::~Server()
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
WSACleanup();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-09-24 17:49:00 -07:00
|
|
|
inline void Server::get(const char* pattern, Handler handler)
|
2012-09-22 21:40:46 -07:00
|
|
|
{
|
2012-09-24 20:13:01 -07:00
|
|
|
get_handlers_.push_back(std::make_pair(pattern, handler));
|
2012-09-22 21:40:46 -07:00
|
|
|
}
|
|
|
|
|
2012-09-24 17:49:00 -07:00
|
|
|
inline void Server::post(const char* pattern, Handler handler)
|
2012-09-22 21:40:46 -07:00
|
|
|
{
|
2012-09-24 20:13:01 -07:00
|
|
|
post_handlers_.push_back(std::make_pair(pattern, handler));
|
2012-09-22 21:40:46 -07:00
|
|
|
}
|
|
|
|
|
2012-09-24 17:49:00 -07:00
|
|
|
inline bool Server::run(const const char*ipaddr_or_hostname, int port)
|
2012-09-22 21:40:46 -07:00
|
|
|
{
|
2012-09-24 17:49:00 -07:00
|
|
|
sock_ = create_server_socket(ipaddr_or_hostname, port);
|
2012-09-22 21:40:46 -07:00
|
|
|
if (sock_ == -1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
socket_t fd = accept(sock_, NULL, NULL);
|
|
|
|
if (fd == -1) {
|
|
|
|
// The server socket was closed by user.
|
|
|
|
if (sock_ == -1) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
close_socket(sock_);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-09-24 06:39:13 -07:00
|
|
|
#ifdef _WIN32
|
|
|
|
int osfhandle = _open_osfhandle(fd, _O_RDONLY);
|
|
|
|
FILE* fp_read = fdopen(osfhandle, "r");
|
|
|
|
FILE* fp_write = fdopen(osfhandle, "w");
|
|
|
|
#else
|
|
|
|
FILE* fp_read = fdopen(fd, "r");
|
|
|
|
FILE* fp_write = fdopen(fd, "w");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
process_request(fp_read, fp_write);
|
|
|
|
|
|
|
|
fflush(fp_write);
|
|
|
|
close_socket(fd);
|
2012-09-22 21:40:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// NOTREACHED
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void Server::stop()
|
|
|
|
{
|
|
|
|
close_socket(sock_);
|
|
|
|
sock_ = -1;
|
|
|
|
}
|
|
|
|
|
2012-09-24 20:13:01 -07:00
|
|
|
inline bool read_request_line(FILE* fp, Request& request)
|
2012-09-22 21:40:46 -07:00
|
|
|
{
|
2012-09-24 20:13:01 -07:00
|
|
|
static std::regex re("(GET|POST) ([^?]+)(?:\\?(.+?))? HTTP/1\\.1\r\n");
|
2012-09-24 04:57:09 -07:00
|
|
|
|
|
|
|
const size_t BUFSIZ_REQUESTLINE = 2048;
|
|
|
|
char buf[BUFSIZ_REQUESTLINE];
|
|
|
|
fgets(buf, BUFSIZ_REQUESTLINE, fp);
|
|
|
|
|
2012-09-22 21:40:46 -07:00
|
|
|
std::cmatch m;
|
2012-09-24 04:57:09 -07:00
|
|
|
if (std::regex_match(buf, m, re)) {
|
2012-09-24 20:13:01 -07:00
|
|
|
request.method = std::string(m[1]);
|
|
|
|
request.url = std::string(m[2]);
|
|
|
|
|
|
|
|
// Parse query text
|
|
|
|
auto len = std::distance(m[3].first, m[3].second);
|
|
|
|
if (len > 0) {
|
|
|
|
const auto& pos = m[3];
|
|
|
|
split(pos.first, pos.second, '&', [&](const char* b, const char* e) {
|
|
|
|
std::string key;
|
|
|
|
std::string val;
|
|
|
|
split(b, e, '=', [&](const char* b, const char* e) {
|
|
|
|
if (key.empty()) {
|
|
|
|
key.assign(b, e);
|
|
|
|
} else {
|
|
|
|
val.assign(b, e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
request.query[key] = val;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2012-09-22 21:40:46 -07:00
|
|
|
return true;
|
|
|
|
}
|
2012-09-24 04:57:09 -07:00
|
|
|
|
2012-09-22 21:40:46 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-09-24 04:57:09 -07:00
|
|
|
inline void read_headers(FILE* fp, Map& headers)
|
|
|
|
{
|
|
|
|
static std::regex re("(.+?): (.+?)\r\n");
|
|
|
|
|
|
|
|
const size_t BUFSIZ_HEADER = 2048;
|
|
|
|
char buf[BUFSIZ_HEADER];
|
|
|
|
|
|
|
|
while (fgets(buf, BUFSIZ_HEADER, fp) && strcmp(buf, "\r\n")) {
|
|
|
|
std::cmatch m;
|
|
|
|
if (std::regex_match(buf, m, re)) {
|
|
|
|
auto key = std::string(m[1]);
|
|
|
|
auto val = std::string(m[2]);
|
|
|
|
headers[key] = val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-24 06:39:13 -07:00
|
|
|
inline void write_plain_text(FILE* fp, const char* s)
|
2012-09-22 21:40:46 -07:00
|
|
|
{
|
2012-09-24 06:39:13 -07:00
|
|
|
fprintf(fp, "HTTP/1.0 200 OK\r\n");
|
|
|
|
fprintf(fp, "Content-type: text/plain\r\n");
|
|
|
|
fprintf(fp, "Connection: close\r\n");
|
|
|
|
fprintf(fp, "\r\n");
|
|
|
|
fprintf(fp, "%s", s);
|
2012-09-22 21:40:46 -07:00
|
|
|
}
|
|
|
|
|
2012-09-24 20:13:01 -07:00
|
|
|
inline void write_error(FILE* fp, int status)
|
2012-09-22 21:40:46 -07:00
|
|
|
{
|
|
|
|
const char* msg = NULL;
|
|
|
|
|
2012-09-24 20:13:01 -07:00
|
|
|
switch (status) {
|
2012-09-22 21:40:46 -07:00
|
|
|
case 400:
|
|
|
|
msg = "Bad Request";
|
|
|
|
break;
|
|
|
|
case 404:
|
|
|
|
msg = "Not Found";
|
|
|
|
break;
|
|
|
|
default:
|
2012-09-24 20:13:01 -07:00
|
|
|
status = 500;
|
2012-09-22 21:40:46 -07:00
|
|
|
msg = "Internal Server Error";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(msg);
|
|
|
|
|
2012-09-24 20:13:01 -07:00
|
|
|
fprintf(fp, "HTTP/1.0 %d %s\r\n", status, msg);
|
2012-09-24 06:39:13 -07:00
|
|
|
fprintf(fp, "Content-type: text/plain\r\n");
|
|
|
|
fprintf(fp, "Connection: close\r\n");
|
|
|
|
fprintf(fp, "\r\n");
|
2012-09-24 20:13:01 -07:00
|
|
|
fprintf(fp, "Status: %d\r\n", status);
|
2012-09-22 21:40:46 -07:00
|
|
|
}
|
|
|
|
|
2012-09-24 06:39:13 -07:00
|
|
|
inline void Server::process_request(FILE* fp_read, FILE* fp_write)
|
2012-09-22 21:40:46 -07:00
|
|
|
{
|
2012-09-24 20:13:01 -07:00
|
|
|
Context cxt;
|
|
|
|
|
|
|
|
// Read and parse request line
|
|
|
|
if (!read_request_line(fp_read, cxt.request)) {
|
|
|
|
write_error(fp_write, 400);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read headers
|
|
|
|
read_headers(fp_read, cxt.request.headers);
|
|
|
|
|
|
|
|
// Routing
|
|
|
|
int status = 404;
|
|
|
|
|
|
|
|
if (cxt.request.method == "GET") {
|
|
|
|
for (auto it = get_handlers_.begin(); it != get_handlers_.end(); ++it) {
|
|
|
|
const auto& pattern = it->first;
|
|
|
|
const auto& handler = it->second;
|
|
|
|
|
|
|
|
std::smatch m;
|
|
|
|
if (std::regex_match(cxt.request.url, m, pattern)) {
|
|
|
|
for (size_t i = 1; i < m.size(); i++) {
|
|
|
|
cxt.request.params.push_back(m[i]);
|
|
|
|
}
|
|
|
|
status = handler(cxt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (cxt.request.method == "POST") {
|
|
|
|
// TODO: parse body
|
|
|
|
} else {
|
|
|
|
status = 400;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (status == 200) {
|
|
|
|
write_plain_text(fp_write, cxt.response.body.c_str());
|
|
|
|
} else {
|
|
|
|
write_error(fp_write, status);
|
|
|
|
}
|
2012-09-22 21:40:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace httpsvrkit
|
|
|
|
|
|
|
|
// vim: et ts=4 sw=4 cin cino={1s ff=unix
|