| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429 |
- //
- // httpsvrkit.h
- //
- // Copyright (c) 2012 Yuji Hirose. All rights reserved.
- // The Boost Software License 1.0
- //
- #ifndef HTTPSVRKIT_H
- #define HTTPSVRKIT_H
- #ifdef _WIN32
- //#define _CRT_SECURE_NO_WARNINGS
- #define _CRT_NONSTDC_NO_DEPRECATE
- #ifndef SO_SYNCHRONOUS_NONALERT
- #define SO_SYNCHRONOUS_NONALERT 0x20;
- #endif
- #ifndef SO_OPENTYPE
- #define SO_OPENTYPE 0x7008
- #endif
- #include <fcntl.h>
- #include <io.h>
- #include <winsock2.h>
- typedef SOCKET socket_t;
- #else
- #include <pthread.h>
- #include <unistd.h>
- #include <netdb.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <sys/socket.h>
- typedef int socket_t;
- #endif
- #include <functional>
- #include <map>
- #include <regex>
- #include <string>
- #include <assert.h>
- namespace httpsvrkit
- {
- typedef std::map<std::string, std::string> Map;
- typedef std::vector<std::string> Array;
- typedef std::multimap<std::string, std::string> MultiMap;
- // HTTP request
- struct Request {
- std::string method;
- std::string url;
- Map headers;
- std::string body;
- Map query;
- Array params;
- };
- // HTTP response
- struct Response {
- int status;
- MultiMap headers;
- std::string body;
- void set_redirect(const char* url);
- void set_content(const std::string& s, const char* content_type = "text/plain");
- };
- struct Context {
- Request request;
- Response response;
- };
- // HTTP server
- class Server {
- public:
- typedef std::function<void (Context& context)> Handler;
- Server(const char* ipaddr_or_hostname, int port);
- ~Server();
- void get(const char* pattern, Handler handler);
- void post(const char* pattern, Handler handler);
- bool run();
- void stop();
- private:
- void process_request(FILE* fp_read, FILE* fp_write);
- const std::string ipaddr_or_hostname_;
- const int port_;
- socket_t sock_;
- std::vector<std::pair<std::regex, Handler>> get_handlers_;
- std::vector<std::pair<std::string, Handler>> post_handlers_;
- };
- // Implementation
- 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]);
- }
- }
- inline socket_t create_server_socket(const char* ipaddr_or_hostname, int port)
- {
- #ifdef _WIN32
- int opt = SO_SYNCHRONOUS_NONALERT;
- setsockopt(INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE, (char*)&opt, sizeof(opt));
- #endif
- // Create a server socket
- socket_t sock = socket(AF_INET, SOCK_STREAM, 0);
- if (sock == -1) {
- return -1;
- }
- // Make 'reuse address' option available
- int yes = 1;
- setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char*)&yes, sizeof(yes));
- // Get a host entry info
- struct hostent* hp;
- if (!(hp = gethostbyname(ipaddr_or_hostname))) {
- return -1;
- }
- // Bind the socket to the given address
- struct sockaddr_in addr;
- memset(&addr, 0, sizeof(addr));
- memcpy(&addr.sin_addr, hp->h_addr, hp->h_length);
- addr.sin_family = AF_INET;
- addr.sin_port = htons(port);
- if (::bind(sock, (struct sockaddr*)&addr, sizeof(addr)) != 0) {
- 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
- }
- void Response::set_redirect(const char* url)
- {
- headers.insert(std::make_pair("Location", url));
- status = 302;
- }
- void Response::set_content(const std::string& s, const char* content_type)
- {
- body = s;
- headers.insert(std::make_pair("Content-Type", content_type));
- status = 200;
- }
- inline Server::Server(const char* ipaddr_or_hostname, int port)
- : ipaddr_or_hostname_(ipaddr_or_hostname)
- , port_(port)
- , sock_(-1)
- {
- #ifdef _WIN32
- WSADATA wsaData;
- WSAStartup(0x0002, &wsaData);
- #endif
- }
- inline Server::~Server()
- {
- #ifdef _WIN32
- WSACleanup();
- #endif
- }
- inline void Server::get(const char* pattern, Handler handler)
- {
- get_handlers_.push_back(std::make_pair(pattern, handler));
- }
- inline void Server::post(const char* pattern, Handler handler)
- {
- post_handlers_.push_back(std::make_pair(pattern, handler));
- }
- inline bool Server::run()
- {
- sock_ = create_server_socket(ipaddr_or_hostname_.c_str(), port_);
- 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;
- }
- #ifdef _WIN32
- int osfhandle = _open_osfhandle(fd, _O_RDONLY);
- FILE* fp_read = fdopen(osfhandle, "rb");
- FILE* fp_write = fdopen(osfhandle, "wb");
- #else
- FILE* fp_read = fdopen(fd, "rb");
- FILE* fp_write = fdopen(fd, "wb");
- #endif
- process_request(fp_read, fp_write);
- fflush(fp_write);
- close_socket(fd);
- }
- // NOTREACHED
- }
- inline void Server::stop()
- {
- close_socket(sock_);
- sock_ = -1;
- }
- inline bool read_request_line(FILE* fp, Request& request)
- {
- static std::regex re("(GET|POST) ([^?]+)(?:\\?(.+?))? HTTP/1\\.1\r\n");
- const size_t BUFSIZ_REQUESTLINE = 2048;
- char buf[BUFSIZ_REQUESTLINE];
- fgets(buf, BUFSIZ_REQUESTLINE, fp);
- std::cmatch m;
- if (std::regex_match(buf, m, re)) {
- 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;
- });
- }
- return true;
- }
- return false;
- }
- 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;
- }
- }
- }
- inline const char* get_header_value(const MultiMap& map, const char* key, const char* def)
- {
- auto it = map.find(key);
- if (it != map.end()) {
- return it->second.c_str();
- }
- return def;
- }
- inline void write_response(FILE* fp, const Response& response)
- {
- fprintf(fp, "HTTP/1.0 %d OK\r\n", response.status);
- fprintf(fp, "Connection: close\r\n");
- for (auto it = response.headers.begin(); it != response.headers.end(); ++it) {
- if (it->first != "Content-Type" && it->second != "Content-Length") {
- fprintf(fp, "%s: %s\r\n", it->first.c_str(), it->second.c_str());
- }
- }
- if (!response.body.empty()) {
- auto content_type = get_header_value(response.headers, "Content-Type", "text/plain");
- fprintf(fp, "Content-Type: %s\r\n", content_type);
- fprintf(fp, "Content-Length: %ld\r\n", response.body.size());
- }
- fprintf(fp, "\r\n");
- if (!response.body.empty()) {
- fprintf(fp, "%s", response.body.c_str());
- }
- }
- inline void write_error(FILE* fp, int status)
- {
- const char* msg = NULL;
- switch (status) {
- case 400:
- msg = "Bad Request";
- break;
- case 404:
- msg = "Not Found";
- break;
- default:
- status = 500;
- msg = "Internal Server Error";
- break;
- }
- assert(msg);
- fprintf(fp, "HTTP/1.0 %d %s\r\n", status, msg);
- fprintf(fp, "Content-type: text/plain\r\n");
- fprintf(fp, "Connection: close\r\n");
- fprintf(fp, "\r\n");
- fprintf(fp, "Status: %d\r\n", status);
- }
- inline void Server::process_request(FILE* fp_read, FILE* fp_write)
- {
- 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
- cxt.response.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]);
- }
- handler(cxt);
- break;
- }
- }
- } else if (cxt.request.method == "POST") {
- // TODO: parse body
- } else {
- cxt.response.status = 400;
- }
- if (200 <= cxt.response.status && cxt.response.status < 400) {
- write_response(fp_write, cxt.response);
- } else {
- write_error(fp_write, cxt.response.status);
- }
- }
- #define HTTP_SERVER(host, port) \
- for (std::shared_ptr<httpsvrkit::Server> svr = std::make_shared<httpsvrkit::Server>(host, port); \
- svr; \
- svr->run(), svr.reset())
- #define GET(url, body) \
- svr->get(url, [&](httpsvrkit::Context& cxt) { \
- const auto& req = cxt.request; \
- auto& res = cxt.response; \
- body \
- });
- } // namespace httpsvrkit
- #endif
- // vim: et ts=4 sw=4 cin cino={1s ff=unix
|