2012-09-22 21:40:46 -07:00
|
|
|
//
|
2012-09-27 18:05:36 -07:00
|
|
|
// httplib.h
|
2012-09-22 21:40:46 -07:00
|
|
|
//
|
2017-04-21 20:00:00 -07:00
|
|
|
// Copyright (c) 2017 Yuji Hirose. All rights reserved.
|
2012-09-22 21:40:46 -07:00
|
|
|
// The Boost Software License 1.0
|
|
|
|
//
|
|
|
|
|
2017-04-21 20:00:00 -07:00
|
|
|
#ifndef _CPPHTTPLIB_HTTPLIB_H_
|
|
|
|
#define _CPPHTTPLIB_HTTPLIB_H_
|
2012-09-25 19:09:56 -07:00
|
|
|
|
2013-07-04 15:18:52 -07:00
|
|
|
#ifdef _MSC_VER
|
2017-11-22 09:07:59 -07:00
|
|
|
#ifndef _CRT_SECURE_NO_WARNINGS
|
2012-09-26 08:49:04 -07:00
|
|
|
#define _CRT_SECURE_NO_WARNINGS
|
2017-11-22 09:07:59 -07:00
|
|
|
#endif //_CRT_SECURE_NO_WARNINGS
|
|
|
|
#ifndef _CRT_NONSTDC_NO_DEPRECATE
|
2012-09-24 06:39:13 -07:00
|
|
|
#define _CRT_NONSTDC_NO_DEPRECATE
|
2017-11-22 09:07:59 -07:00
|
|
|
#endif //_CRT_NONSTDC_NO_DEPRECATE
|
2012-09-24 06:39:13 -07:00
|
|
|
|
2012-09-25 19:09:56 -07:00
|
|
|
#ifndef SO_SYNCHRONOUS_NONALERT
|
2016-03-04 14:49:15 -07:00
|
|
|
#define SO_SYNCHRONOUS_NONALERT 0x20
|
2012-09-25 19:09:56 -07:00
|
|
|
#endif
|
|
|
|
#ifndef SO_OPENTYPE
|
|
|
|
#define SO_OPENTYPE 0x7008
|
|
|
|
#endif
|
2015-08-04 15:13:39 -07:00
|
|
|
#if (_MSC_VER < 1900)
|
2012-10-12 13:09:39 -07:00
|
|
|
#define snprintf _snprintf_s
|
|
|
|
#endif
|
2013-07-04 15:18:52 -07:00
|
|
|
|
2015-07-15 13:41:11 -07:00
|
|
|
#define S_ISREG(m) (((m)&S_IFREG)==S_IFREG)
|
|
|
|
#define S_ISDIR(m) (((m)&S_IFDIR)==S_IFDIR)
|
2012-09-25 19:09:56 -07:00
|
|
|
|
2012-09-24 06:39:13 -07:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <io.h>
|
2012-09-22 21:40:46 -07:00
|
|
|
#include <winsock2.h>
|
2015-01-14 16:51:53 -07:00
|
|
|
#include <ws2tcpip.h>
|
2012-09-22 21:40:46 -07:00
|
|
|
|
2015-07-13 12:42:21 -07:00
|
|
|
#undef min
|
|
|
|
#undef max
|
|
|
|
|
2012-09-22 21:40:46 -07:00
|
|
|
typedef SOCKET socket_t;
|
|
|
|
#else
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <unistd.h>
|
2012-09-24 17:49:00 -07:00
|
|
|
#include <netdb.h>
|
2013-04-24 03:09:19 -07:00
|
|
|
#include <cstring>
|
2012-09-22 21:40:46 -07:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
2017-07-07 09:43:57 -07:00
|
|
|
#include <signal.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
|
|
|
|
|
2013-07-04 15:18:52 -07:00
|
|
|
#include <fstream>
|
2012-09-24 06:39:13 -07:00
|
|
|
#include <functional>
|
|
|
|
#include <map>
|
2013-04-24 03:09:19 -07:00
|
|
|
#include <memory>
|
2012-09-24 06:39:13 -07:00
|
|
|
#include <regex>
|
|
|
|
#include <string>
|
2015-01-14 16:51:53 -07:00
|
|
|
#include <sys/stat.h>
|
2012-09-24 06:39:13 -07:00
|
|
|
#include <assert.h>
|
|
|
|
|
2017-04-21 20:00:00 -07:00
|
|
|
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
|
|
|
|
#include <openssl/ssl.h>
|
|
|
|
#endif
|
|
|
|
|
2012-09-27 18:05:36 -07:00
|
|
|
namespace httplib
|
2012-09-22 21:40:46 -07:00
|
|
|
{
|
|
|
|
|
2017-12-03 19:25:38 -07:00
|
|
|
enum class HttpVersion { v1_0 = 0, v1_1 };
|
|
|
|
|
2017-12-05 21:19:39 -07:00
|
|
|
typedef std::multimap<std::string, std::string> MultiMap;
|
|
|
|
typedef std::smatch Match;
|
2017-11-22 09:31:19 -07:00
|
|
|
typedef std::function<void (int64_t current, int64_t total)> Progress;
|
2012-09-22 21:40:46 -07:00
|
|
|
|
2017-12-05 17:19:07 -07:00
|
|
|
struct MultipartFile {
|
|
|
|
std::string filename;
|
|
|
|
std::string content_type;
|
|
|
|
size_t offset = 0;
|
|
|
|
size_t length = 0;
|
|
|
|
};
|
|
|
|
typedef std::multimap<std::string, MultipartFile> MultipartFiles;
|
|
|
|
|
2012-09-24 04:57:09 -07:00
|
|
|
struct Request {
|
2017-12-05 17:19:07 -07:00
|
|
|
std::string method;
|
|
|
|
std::string path;
|
|
|
|
MultiMap headers;
|
|
|
|
std::string body;
|
2017-12-05 21:19:39 -07:00
|
|
|
MultiMap params;
|
2017-12-05 17:19:07 -07:00
|
|
|
MultipartFiles files;
|
|
|
|
Match matches;
|
|
|
|
Progress progress;
|
2012-10-03 17:11:22 -07:00
|
|
|
|
|
|
|
bool has_header(const char* key) const;
|
|
|
|
std::string get_header_value(const char* key) const;
|
2012-10-03 20:47:59 -07:00
|
|
|
void set_header(const char* key, const char* val);
|
|
|
|
|
|
|
|
bool has_param(const char* key) const;
|
2017-12-05 21:19:39 -07:00
|
|
|
std::string get_param_value(const char* key) const;
|
2017-12-05 17:19:07 -07:00
|
|
|
|
|
|
|
bool has_file(const char* key) const;
|
|
|
|
MultipartFile get_file_value(const char* key) const;
|
2012-09-22 21:40:46 -07:00
|
|
|
};
|
|
|
|
|
2012-09-24 04:57:09 -07:00
|
|
|
struct Response {
|
2012-09-25 19:09:56 -07:00
|
|
|
int status;
|
2012-09-24 04:57:09 -07:00
|
|
|
MultiMap headers;
|
|
|
|
std::string body;
|
2012-09-25 19:09:56 -07:00
|
|
|
|
2012-10-03 17:11:22 -07:00
|
|
|
bool has_header(const char* key) const;
|
|
|
|
std::string get_header_value(const char* key) const;
|
|
|
|
void set_header(const char* key, const char* val);
|
|
|
|
|
2012-09-25 19:09:56 -07:00
|
|
|
void set_redirect(const char* url);
|
2015-07-15 13:40:40 -07:00
|
|
|
void set_content(const char* s, size_t n, const char* content_type);
|
2012-10-03 17:11:22 -07:00
|
|
|
void set_content(const std::string& s, const char* content_type);
|
2012-10-03 21:38:32 -07:00
|
|
|
|
|
|
|
Response() : status(-1) {}
|
2012-09-24 04:57:09 -07:00
|
|
|
};
|
|
|
|
|
2017-04-21 20:00:00 -07:00
|
|
|
class Stream {
|
|
|
|
public:
|
|
|
|
virtual ~Stream() {}
|
|
|
|
virtual int read(char* ptr, size_t size) = 0;
|
|
|
|
virtual int write(const char* ptr, size_t size1) = 0;
|
|
|
|
virtual int write(const char* ptr) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
class SocketStream : public Stream {
|
|
|
|
public:
|
|
|
|
SocketStream(socket_t sock);
|
|
|
|
virtual ~SocketStream();
|
|
|
|
|
|
|
|
virtual int read(char* ptr, size_t size);
|
|
|
|
virtual int write(const char* ptr, size_t size);
|
|
|
|
virtual int write(const char* ptr);
|
|
|
|
|
|
|
|
private:
|
|
|
|
socket_t sock_;
|
|
|
|
};
|
|
|
|
|
2012-09-22 21:40:46 -07:00
|
|
|
class Server {
|
|
|
|
public:
|
2012-10-12 13:09:39 -07:00
|
|
|
typedef std::function<void (const Request&, Response&)> Handler;
|
2012-10-12 14:41:58 -07:00
|
|
|
typedef std::function<void (const Request&, const Response&)> Logger;
|
2012-09-22 21:40:46 -07:00
|
|
|
|
2012-10-12 13:09:39 -07:00
|
|
|
Server();
|
2017-04-21 20:00:00 -07:00
|
|
|
virtual ~Server();
|
2012-09-22 21:40:46 -07:00
|
|
|
|
2017-11-25 09:58:09 -07:00
|
|
|
Server& get(const char* pattern, Handler handler);
|
|
|
|
Server& post(const char* pattern, Handler handler);
|
2012-09-22 21:40:46 -07:00
|
|
|
|
2013-07-04 19:08:06 -07:00
|
|
|
bool set_base_dir(const char* path);
|
2013-07-04 15:18:52 -07:00
|
|
|
|
2012-10-03 17:11:22 -07:00
|
|
|
void set_error_handler(Handler handler);
|
2012-10-12 14:41:58 -07:00
|
|
|
void set_logger(Logger logger);
|
2012-09-27 17:54:54 -07:00
|
|
|
|
2017-08-29 10:20:20 -07:00
|
|
|
bool listen(const char* host, int port, int socket_flags = 0);
|
2012-09-22 21:40:46 -07:00
|
|
|
void stop();
|
|
|
|
|
2017-04-21 20:00:00 -07:00
|
|
|
protected:
|
|
|
|
void process_request(Stream& strm);
|
|
|
|
|
2012-09-22 21:40:46 -07:00
|
|
|
private:
|
2012-10-03 18:55:01 -07:00
|
|
|
typedef std::vector<std::pair<std::regex, Handler>> Handlers;
|
2012-09-22 21:40:46 -07:00
|
|
|
|
2012-10-12 13:09:39 -07:00
|
|
|
bool routing(Request& req, Response& res);
|
2015-07-15 13:41:11 -07:00
|
|
|
bool handle_file_request(Request& req, Response& res);
|
|
|
|
bool dispatch_request(Request& req, Response& res, Handlers& handlers);
|
2013-07-04 15:18:52 -07:00
|
|
|
|
2017-04-21 20:00:00 -07:00
|
|
|
bool read_request_line(Stream& strm, Request& req);
|
2017-12-02 08:24:41 -07:00
|
|
|
void write_response(Stream& strm, const Request& req, Response& res);
|
2017-04-21 20:00:00 -07:00
|
|
|
|
|
|
|
virtual bool read_and_close_socket(socket_t sock);
|
|
|
|
|
2013-07-04 15:18:52 -07:00
|
|
|
socket_t svr_sock_;
|
|
|
|
std::string base_dir_;
|
|
|
|
Handlers get_handlers_;
|
|
|
|
Handlers post_handlers_;
|
|
|
|
Handler error_handler_;
|
|
|
|
Logger logger_;
|
2012-10-02 17:39:13 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
class Client {
|
|
|
|
public:
|
2017-12-03 19:25:38 -07:00
|
|
|
Client(const char* host, int port, HttpVersion http_version = HttpVersion::v1_0);
|
2017-04-21 20:00:00 -07:00
|
|
|
virtual ~Client();
|
2012-10-02 17:39:13 -07:00
|
|
|
|
2017-11-22 09:31:19 -07:00
|
|
|
std::shared_ptr<Response> get(const char* path, Progress callback = [](int64_t,int64_t){});
|
2017-05-13 20:43:29 -07:00
|
|
|
std::shared_ptr<Response> head(const char* path);
|
|
|
|
std::shared_ptr<Response> post(const char* path, const std::string& body, const char* content_type);
|
2017-12-05 21:19:39 -07:00
|
|
|
std::shared_ptr<Response> post(const char* path, const MultiMap& params);
|
2012-10-05 10:58:56 -07:00
|
|
|
|
|
|
|
bool send(const Request& req, Response& res);
|
2012-10-03 22:18:18 -07:00
|
|
|
|
2017-04-21 20:00:00 -07:00
|
|
|
protected:
|
|
|
|
bool process_request(Stream& strm, const Request& req, Response& res);
|
|
|
|
|
2017-09-07 11:24:33 -07:00
|
|
|
const std::string host_;
|
|
|
|
const int port_;
|
2017-12-03 19:25:38 -07:00
|
|
|
const HttpVersion http_version_;
|
2017-11-06 11:24:55 -07:00
|
|
|
const std::string host_and_port_;
|
2017-09-07 11:24:33 -07:00
|
|
|
|
2012-10-02 17:39:13 -07:00
|
|
|
private:
|
2017-04-21 20:00:00 -07:00
|
|
|
bool read_response_line(Stream& strm, Response& res);
|
2017-06-28 17:12:11 -07:00
|
|
|
void add_default_headers(Request& req);
|
2017-04-21 20:00:00 -07:00
|
|
|
|
|
|
|
virtual bool read_and_close_socket(socket_t sock, const Request& req, Response& res);
|
2012-09-22 21:40:46 -07:00
|
|
|
};
|
|
|
|
|
2017-04-21 20:00:00 -07:00
|
|
|
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
|
|
|
|
class SSLSocketStream : public Stream {
|
|
|
|
public:
|
|
|
|
SSLSocketStream(SSL* ssl);
|
|
|
|
virtual ~SSLSocketStream();
|
|
|
|
|
|
|
|
virtual int read(char* ptr, size_t size);
|
|
|
|
virtual int write(const char* ptr, size_t size);
|
|
|
|
virtual int write(const char* ptr);
|
|
|
|
|
|
|
|
private:
|
|
|
|
SSL* ssl_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class SSLServer : public Server {
|
|
|
|
public:
|
|
|
|
SSLServer(const char* cert_path, const char* private_key_path);
|
|
|
|
virtual ~SSLServer();
|
|
|
|
|
|
|
|
private:
|
|
|
|
virtual bool read_and_close_socket(socket_t sock);
|
|
|
|
|
|
|
|
SSL_CTX* ctx_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class SSLClient : public Client {
|
|
|
|
public:
|
2017-12-05 17:15:52 -07:00
|
|
|
SSLClient(const char* host, int port, HttpVersion http_version = HttpVersion::v1_0);
|
2017-04-21 20:00:00 -07:00
|
|
|
virtual ~SSLClient();
|
|
|
|
|
|
|
|
private:
|
|
|
|
virtual bool read_and_close_socket(socket_t sock, const Request& req, Response& res);
|
|
|
|
|
|
|
|
SSL_CTX* ctx_;
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Implementation
|
|
|
|
*/
|
2012-10-05 10:58:56 -07:00
|
|
|
namespace detail {
|
2012-09-22 21:40:46 -07:00
|
|
|
|
2017-12-03 19:25:38 -07:00
|
|
|
static std::vector<const char*> http_version_strings = { "HTTP/1.0", "HTTP/1.1" };
|
|
|
|
|
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++;
|
|
|
|
}
|
|
|
|
|
2012-10-02 17:39:13 -07:00
|
|
|
if (i) {
|
2012-09-24 20:13:01 -07:00
|
|
|
fn(&b[beg], &b[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-06 21:52:34 -07:00
|
|
|
class socket_reader {
|
|
|
|
public:
|
|
|
|
socket_reader(Stream& strm, char* fixed_buffer, size_t fixed_buffer_size)
|
|
|
|
: strm_(strm)
|
|
|
|
, fixed_buffer_(fixed_buffer)
|
|
|
|
, fixed_buffer_size_(fixed_buffer_size) {
|
|
|
|
}
|
2015-01-27 14:45:11 -07:00
|
|
|
|
2017-12-06 21:52:34 -07:00
|
|
|
const char* ptr() const {
|
|
|
|
if (glowable_buffer_.empty()) {
|
|
|
|
return fixed_buffer_;
|
|
|
|
} else {
|
|
|
|
return glowable_buffer_.data();
|
2016-09-16 13:12:52 -07:00
|
|
|
}
|
2017-12-06 21:52:34 -07:00
|
|
|
}
|
2016-09-16 13:12:52 -07:00
|
|
|
|
2017-12-06 21:52:34 -07:00
|
|
|
bool getline() {
|
|
|
|
fixed_buffer_used_size_ = 0;
|
|
|
|
glowable_buffer_.clear();
|
2017-12-02 08:24:41 -07:00
|
|
|
|
2017-12-06 21:52:34 -07:00
|
|
|
size_t i = 0;
|
2016-09-16 13:12:52 -07:00
|
|
|
|
2017-12-06 21:52:34 -07:00
|
|
|
for (;;) {
|
|
|
|
char byte;
|
|
|
|
auto n = strm_.read(&byte, 1);
|
|
|
|
|
|
|
|
if (n < 1) {
|
|
|
|
if (i == 0) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
append(byte);
|
|
|
|
|
|
|
|
if (byte == '\n') {
|
|
|
|
break;
|
|
|
|
}
|
2016-09-16 13:12:52 -07:00
|
|
|
}
|
2017-12-06 21:52:34 -07:00
|
|
|
|
|
|
|
append('\0');
|
|
|
|
return true;
|
2016-09-16 13:12:52 -07:00
|
|
|
}
|
|
|
|
|
2017-12-06 21:52:34 -07:00
|
|
|
private:
|
|
|
|
void append(char c) {
|
|
|
|
if (fixed_buffer_used_size_ < fixed_buffer_size_) {
|
|
|
|
fixed_buffer_[fixed_buffer_used_size_++] = c;
|
|
|
|
} else {
|
|
|
|
if (glowable_buffer_.empty()) {
|
|
|
|
glowable_buffer_.assign(fixed_buffer_, fixed_buffer_size_);
|
|
|
|
}
|
|
|
|
glowable_buffer_ += c;
|
|
|
|
}
|
2017-12-02 08:24:41 -07:00
|
|
|
}
|
|
|
|
|
2017-12-06 21:52:34 -07:00
|
|
|
Stream& strm_;
|
|
|
|
char* fixed_buffer_;
|
|
|
|
const size_t fixed_buffer_size_;
|
|
|
|
size_t fixed_buffer_used_size_;
|
|
|
|
std::string glowable_buffer_;
|
|
|
|
};
|
2012-10-02 17:39:13 -07:00
|
|
|
|
2016-09-16 13:12:52 -07:00
|
|
|
template <typename ...Args>
|
2017-04-21 20:00:00 -07:00
|
|
|
inline void socket_printf(Stream& strm, const char* fmt, const Args& ...args)
|
2015-01-14 16:51:53 -07:00
|
|
|
{
|
2016-09-16 13:12:52 -07:00
|
|
|
char buf[BUFSIZ];
|
|
|
|
auto n = snprintf(buf, BUFSIZ, fmt, args...);
|
|
|
|
if (n > 0) {
|
|
|
|
if (n >= BUFSIZ) {
|
2017-12-02 08:24:41 -07:00
|
|
|
std::vector<char> glowable_buf(BUFSIZ);
|
|
|
|
|
2017-12-03 06:17:05 -07:00
|
|
|
while (n >= static_cast<int>(glowable_buf.size())) {
|
2017-12-02 08:24:41 -07:00
|
|
|
glowable_buf.resize(glowable_buf.size() * 2);
|
|
|
|
n = snprintf(&glowable_buf[0], glowable_buf.size(), fmt, args...);
|
|
|
|
}
|
|
|
|
strm.write(&glowable_buf[0], n);
|
2016-09-16 13:12:52 -07:00
|
|
|
} else {
|
2017-04-21 20:00:00 -07:00
|
|
|
strm.write(buf, n);
|
2016-09-16 13:12:52 -07:00
|
|
|
}
|
|
|
|
}
|
2015-01-14 16:51:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
inline int close_socket(socket_t sock)
|
|
|
|
{
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
return closesocket(sock);
|
|
|
|
#else
|
|
|
|
return close(sock);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-09-16 13:12:52 -07:00
|
|
|
template <typename T>
|
|
|
|
inline bool read_and_close_socket(socket_t sock, T callback)
|
|
|
|
{
|
2017-04-21 20:00:00 -07:00
|
|
|
SocketStream strm(sock);
|
|
|
|
auto ret = callback(strm);
|
2016-09-16 13:12:52 -07:00
|
|
|
close_socket(sock);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int shutdown_socket(socket_t sock)
|
|
|
|
{
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
return shutdown(sock, SD_BOTH);
|
|
|
|
#else
|
|
|
|
return shutdown(sock, SHUT_RDWR);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-10-02 17:39:13 -07:00
|
|
|
template <typename Fn>
|
2017-08-29 10:20:20 -07:00
|
|
|
socket_t create_socket(const char* host, int port, Fn fn, int socket_flags = 0)
|
2012-09-22 21:40:46 -07:00
|
|
|
{
|
2013-07-04 15:18:52 -07:00
|
|
|
#ifdef _MSC_VER
|
2012-09-25 19:09:56 -07:00
|
|
|
int opt = SO_SYNCHRONOUS_NONALERT;
|
|
|
|
setsockopt(INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE, (char*)&opt, sizeof(opt));
|
|
|
|
#endif
|
|
|
|
|
2015-01-14 16:51:53 -07:00
|
|
|
// Get address info
|
|
|
|
struct addrinfo hints;
|
|
|
|
struct addrinfo *result;
|
2012-09-22 21:40:46 -07:00
|
|
|
|
2015-01-14 16:51:53 -07:00
|
|
|
memset(&hints, 0, sizeof(struct addrinfo));
|
|
|
|
hints.ai_family = AF_UNSPEC;
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
2017-08-29 10:20:20 -07:00
|
|
|
hints.ai_flags = socket_flags;
|
2015-01-14 16:51:53 -07:00
|
|
|
hints.ai_protocol = 0;
|
2012-09-22 21:40:46 -07:00
|
|
|
|
2015-01-14 16:51:53 -07:00
|
|
|
auto service = std::to_string(port);
|
|
|
|
|
|
|
|
if (getaddrinfo(host, service.c_str(), &hints, &result)) {
|
2012-09-24 17:49:00 -07:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-01-14 16:51:53 -07:00
|
|
|
for (auto rp = result; rp; rp = rp->ai_next) {
|
|
|
|
// Create a socket
|
|
|
|
auto sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
|
|
|
|
if (sock == -1) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make 'reuse address' option available
|
|
|
|
int yes = 1;
|
|
|
|
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char*)&yes, sizeof(yes));
|
|
|
|
|
|
|
|
// bind or connect
|
|
|
|
if (fn(sock, *rp)) {
|
|
|
|
freeaddrinfo(result);
|
|
|
|
return sock;
|
|
|
|
}
|
2012-09-22 21:40:46 -07:00
|
|
|
|
2015-01-14 16:51:53 -07:00
|
|
|
close_socket(sock);
|
|
|
|
}
|
|
|
|
|
|
|
|
freeaddrinfo(result);
|
|
|
|
return -1;
|
2012-10-02 17:39:13 -07:00
|
|
|
}
|
2012-09-22 21:40:46 -07:00
|
|
|
|
2017-08-29 10:20:20 -07:00
|
|
|
inline socket_t create_server_socket(const char* host, int port, int socket_flags)
|
2012-10-02 17:39:13 -07:00
|
|
|
{
|
2015-01-14 16:51:53 -07:00
|
|
|
return create_socket(host, port, [](socket_t sock, struct addrinfo& ai) -> socket_t {
|
|
|
|
if (::bind(sock, ai.ai_addr, ai.ai_addrlen)) {
|
|
|
|
return false;
|
2012-10-02 17:39:13 -07:00
|
|
|
}
|
2012-10-05 10:58:56 -07:00
|
|
|
if (listen(sock, 5)) { // Listen through 5 channels
|
2015-01-14 16:51:53 -07:00
|
|
|
return false;
|
2012-10-02 17:39:13 -07:00
|
|
|
}
|
2015-01-14 16:51:53 -07:00
|
|
|
return true;
|
2017-08-29 10:20:20 -07:00
|
|
|
}, socket_flags);
|
2012-09-22 21:40:46 -07:00
|
|
|
}
|
|
|
|
|
2012-10-02 17:39:13 -07:00
|
|
|
inline socket_t create_client_socket(const char* host, int port)
|
2012-09-25 20:21:57 -07:00
|
|
|
{
|
2015-01-14 16:51:53 -07:00
|
|
|
return create_socket(host, port, [](socket_t sock, struct addrinfo& ai) -> socket_t {
|
|
|
|
if (connect(sock, ai.ai_addr, ai.ai_addrlen)) {
|
|
|
|
return false;
|
2012-10-02 17:39:13 -07:00
|
|
|
}
|
2015-01-14 16:51:53 -07:00
|
|
|
return true;
|
2012-10-02 17:39:13 -07:00
|
|
|
});
|
|
|
|
}
|
2012-09-25 20:21:57 -07:00
|
|
|
|
2017-11-25 20:32:09 -07:00
|
|
|
inline bool is_file(const std::string& path)
|
2013-07-04 15:18:52 -07:00
|
|
|
{
|
2015-07-15 13:41:11 -07:00
|
|
|
struct stat st;
|
2017-11-25 20:32:09 -07:00
|
|
|
return stat(path.c_str(), &st) >= 0 && S_ISREG(st.st_mode);
|
2013-07-04 19:08:06 -07:00
|
|
|
}
|
|
|
|
|
2017-11-25 20:32:09 -07:00
|
|
|
inline bool is_dir(const std::string& path)
|
2013-07-04 19:08:06 -07:00
|
|
|
{
|
2015-07-15 13:41:11 -07:00
|
|
|
struct stat st;
|
2017-11-25 20:32:09 -07:00
|
|
|
return stat(path.c_str(), &st) >= 0 && S_ISDIR(st.st_mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool is_valid_path(const std::string& path) {
|
|
|
|
size_t level = 0;
|
|
|
|
size_t i = 0;
|
|
|
|
|
|
|
|
// Skip slash
|
|
|
|
while (i < path.size() && path[i] == '/') {
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (i < path.size()) {
|
|
|
|
// Read component
|
|
|
|
auto beg = i;
|
|
|
|
while (i < path.size() && path[i] != '/') {
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto len = i - beg;
|
|
|
|
assert(len > 0);
|
|
|
|
|
|
|
|
if (!path.compare(beg, len, ".")) {
|
|
|
|
;
|
|
|
|
} else if (!path.compare(beg, len, "..")) {
|
|
|
|
if (level == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
level--;
|
|
|
|
} else {
|
|
|
|
level++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip slash
|
|
|
|
while (i < path.size() && path[i] == '/') {
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2013-07-04 15:18:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void read_file(const std::string& path, std::string& out)
|
|
|
|
{
|
2015-07-16 17:26:26 -07:00
|
|
|
std::ifstream fs(path, std::ios_base::binary);
|
2015-07-15 13:41:11 -07:00
|
|
|
fs.seekg(0, std::ios_base::end);
|
|
|
|
auto size = fs.tellg();
|
|
|
|
fs.seekg(0);
|
2016-09-16 13:12:52 -07:00
|
|
|
out.resize(static_cast<size_t>(size));
|
2015-07-15 13:41:11 -07:00
|
|
|
fs.read(&out[0], size);
|
2013-07-04 15:18:52 -07:00
|
|
|
}
|
|
|
|
|
2017-05-13 21:46:40 -07:00
|
|
|
inline std::string file_extension(const std::string& path)
|
2013-07-04 15:18:52 -07:00
|
|
|
{
|
2015-07-15 13:41:11 -07:00
|
|
|
std::smatch m;
|
|
|
|
auto pat = std::regex("\\.([a-zA-Z0-9]+)$");
|
2017-05-13 21:46:40 -07:00
|
|
|
if (std::regex_search(path, m, pat)) {
|
2015-07-15 13:41:11 -07:00
|
|
|
return m[1].str();
|
|
|
|
}
|
|
|
|
return std::string();
|
2013-07-04 15:18:52 -07:00
|
|
|
}
|
|
|
|
|
2017-05-13 21:46:40 -07:00
|
|
|
inline const char* content_type(const std::string& path)
|
2013-07-04 15:18:52 -07:00
|
|
|
{
|
2017-05-13 21:46:40 -07:00
|
|
|
auto ext = detail::file_extension(path);
|
|
|
|
if (ext == "txt") {
|
|
|
|
return "text/plain";
|
|
|
|
} else if (ext == "html") {
|
2015-07-15 13:41:11 -07:00
|
|
|
return "text/html";
|
2017-05-13 21:46:40 -07:00
|
|
|
} else if (ext == "js") {
|
|
|
|
return "text/javascript";
|
|
|
|
} else if (ext == "css") {
|
|
|
|
return "text/css";
|
|
|
|
} else if (ext == "xml") {
|
|
|
|
return "text/xml";
|
|
|
|
} else if (ext == "jpeg" || ext == "jpg") {
|
|
|
|
return "image/jpg";
|
|
|
|
} else if (ext == "png") {
|
|
|
|
return "image/png";
|
|
|
|
} else if (ext == "gif") {
|
|
|
|
return "image/gif";
|
|
|
|
} else if (ext == "svg") {
|
|
|
|
return "image/svg+xml";
|
|
|
|
} else if (ext == "ico") {
|
|
|
|
return "image/x-icon";
|
|
|
|
} else if (ext == "json") {
|
|
|
|
return "application/json";
|
|
|
|
} else if (ext == "pdf") {
|
|
|
|
return "application/pdf";
|
|
|
|
} else if (ext == "xhtml") {
|
|
|
|
return "application/xhtml+xml";
|
2015-07-15 13:41:11 -07:00
|
|
|
}
|
2017-05-13 21:46:40 -07:00
|
|
|
return nullptr;
|
2013-07-04 15:18:52 -07:00
|
|
|
}
|
|
|
|
|
2012-10-02 19:37:14 -07:00
|
|
|
inline const char* status_message(int status)
|
|
|
|
{
|
|
|
|
switch (status) {
|
2012-10-12 14:41:58 -07:00
|
|
|
case 200: return "OK";
|
2012-10-05 10:58:56 -07:00
|
|
|
case 400: return "Bad Request";
|
|
|
|
case 404: return "Not Found";
|
2012-10-02 19:37:14 -07:00
|
|
|
default:
|
2012-10-12 14:41:58 -07:00
|
|
|
case 500: return "Internal Server Error";
|
2012-10-02 19:37:14 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-05 21:19:39 -07:00
|
|
|
inline const char* get_header_value(const MultiMap& headers, const char* key, const char* def)
|
2012-10-02 17:39:13 -07:00
|
|
|
{
|
2017-12-05 21:19:39 -07:00
|
|
|
auto it = headers.find(key);
|
|
|
|
if (it != headers.end()) {
|
2012-10-02 17:39:13 -07:00
|
|
|
return it->second.c_str();
|
|
|
|
}
|
|
|
|
return def;
|
|
|
|
}
|
|
|
|
|
2017-12-05 21:19:39 -07:00
|
|
|
inline int get_header_value_int(const MultiMap& headers, const char* key, int def)
|
2012-10-02 17:39:13 -07:00
|
|
|
{
|
2017-12-05 21:19:39 -07:00
|
|
|
auto it = headers.find(key);
|
|
|
|
if (it != headers.end()) {
|
2013-04-24 03:09:19 -07:00
|
|
|
return std::stoi(it->second);
|
2012-10-02 17:39:13 -07:00
|
|
|
}
|
|
|
|
return def;
|
|
|
|
}
|
|
|
|
|
2017-04-21 20:00:00 -07:00
|
|
|
inline bool read_headers(Stream& strm, MultiMap& headers)
|
2012-10-02 17:39:13 -07:00
|
|
|
{
|
|
|
|
static std::regex re("(.+?): (.+?)\r\n");
|
|
|
|
|
2017-12-06 21:52:34 -07:00
|
|
|
const auto bufsiz = 2048;
|
|
|
|
char buf[bufsiz];
|
|
|
|
|
|
|
|
socket_reader reader(strm, buf, bufsiz);
|
2012-10-02 17:39:13 -07:00
|
|
|
|
2012-10-03 18:55:01 -07:00
|
|
|
for (;;) {
|
2017-12-06 21:52:34 -07:00
|
|
|
if (!reader.getline()) {
|
2012-10-03 18:55:01 -07:00
|
|
|
return false;
|
|
|
|
}
|
2017-12-06 21:52:34 -07:00
|
|
|
if (!strcmp(reader.ptr(), "\r\n")) {
|
2012-10-03 18:55:01 -07:00
|
|
|
break;
|
|
|
|
}
|
2012-10-02 17:39:13 -07:00
|
|
|
std::cmatch m;
|
2017-12-06 21:52:34 -07:00
|
|
|
if (std::regex_match(reader.ptr(), m, re)) {
|
2012-10-02 17:39:13 -07:00
|
|
|
auto key = std::string(m[1]);
|
|
|
|
auto val = std::string(m[2]);
|
2017-12-05 21:19:39 -07:00
|
|
|
headers.emplace(key, val);
|
2012-10-02 17:39:13 -07:00
|
|
|
}
|
2012-09-25 20:21:57 -07:00
|
|
|
}
|
2012-10-03 18:55:01 -07:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2017-12-03 19:25:38 -07:00
|
|
|
bool read_content_with_length(Stream& strm, T& x, size_t len, Progress progress)
|
|
|
|
{
|
|
|
|
x.body.assign(len, 0);
|
|
|
|
size_t r = 0;
|
|
|
|
while (r < len){
|
|
|
|
auto r_incr = strm.read(&x.body[r], len - r);
|
|
|
|
if (r_incr <= 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
r += r_incr;
|
|
|
|
if (progress) {
|
|
|
|
progress(r, len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
bool read_content_without_length(Stream& strm, T& x)
|
|
|
|
{
|
|
|
|
for (;;) {
|
|
|
|
char byte;
|
|
|
|
auto n = strm.read(&byte, 1);
|
|
|
|
if (n < 0) {
|
|
|
|
return false;
|
|
|
|
} else if (n == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
x.body += byte;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
bool read_content_chunked(Stream& strm, T& x)
|
|
|
|
{
|
2017-12-06 21:52:34 -07:00
|
|
|
const auto bufsiz = 16;
|
|
|
|
char buf[bufsiz];
|
2017-12-03 19:25:38 -07:00
|
|
|
|
2017-12-06 21:52:34 -07:00
|
|
|
socket_reader reader(strm, buf, bufsiz);
|
|
|
|
|
|
|
|
if (!reader.getline()) {
|
2017-12-03 19:25:38 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-12-06 21:52:34 -07:00
|
|
|
auto chunk_len = std::stoi(reader.ptr(), 0, 16);
|
2017-12-03 19:25:38 -07:00
|
|
|
|
|
|
|
while (chunk_len > 0){
|
|
|
|
std::string chunk(chunk_len, 0);
|
|
|
|
|
|
|
|
auto n = strm.read(&chunk[0], chunk_len);
|
|
|
|
if (n <= 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-12-06 21:52:34 -07:00
|
|
|
if (!reader.getline()) {
|
2017-12-03 19:25:38 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-12-06 21:52:34 -07:00
|
|
|
if (strcmp(reader.ptr(), "\r\n")) {
|
2017-12-03 19:25:38 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
x.body += chunk;
|
|
|
|
|
2017-12-06 21:52:34 -07:00
|
|
|
if (!reader.getline()) {
|
2017-12-03 19:25:38 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-12-06 21:52:34 -07:00
|
|
|
chunk_len = std::stoi(reader.ptr(), 0, 16);
|
2017-12-03 19:25:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
bool read_content(Stream& strm, T& x, Progress progress = [](int64_t,int64_t){})
|
2012-10-03 18:55:01 -07:00
|
|
|
{
|
|
|
|
auto len = get_header_value_int(x.headers, "Content-Length", 0);
|
2017-12-03 19:25:38 -07:00
|
|
|
|
2012-10-03 18:55:01 -07:00
|
|
|
if (len) {
|
2017-12-03 19:25:38 -07:00
|
|
|
return read_content_with_length(strm, x, len, progress);
|
|
|
|
} else {
|
|
|
|
auto encoding = get_header_value(x.headers, "Transfer-Encoding", "");
|
|
|
|
|
|
|
|
if (!strcmp(encoding, "chunked")) {
|
|
|
|
return read_content_chunked(strm, x);
|
|
|
|
} else {
|
|
|
|
return read_content_without_length(strm, x);
|
2017-09-08 09:59:00 -07:00
|
|
|
}
|
2012-10-03 18:55:01 -07:00
|
|
|
}
|
2017-12-03 19:25:38 -07:00
|
|
|
|
2012-10-03 18:55:01 -07:00
|
|
|
return true;
|
2012-10-02 17:39:13 -07:00
|
|
|
}
|
|
|
|
|
2012-10-03 20:47:59 -07:00
|
|
|
template <typename T>
|
2017-04-21 20:00:00 -07:00
|
|
|
inline void write_headers(Stream& strm, const T& res)
|
2012-10-03 20:47:59 -07:00
|
|
|
{
|
2017-04-21 20:00:00 -07:00
|
|
|
strm.write("Connection: close\r\n");
|
2012-10-03 20:47:59 -07:00
|
|
|
|
2013-05-11 13:11:30 -07:00
|
|
|
for (const auto& x: res.headers) {
|
|
|
|
if (x.first != "Content-Type" && x.first != "Content-Length") {
|
2017-04-21 20:00:00 -07:00
|
|
|
socket_printf(strm, "%s: %s\r\n", x.first.c_str(), x.second.c_str());
|
2012-10-03 20:47:59 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-14 16:51:53 -07:00
|
|
|
auto t = get_header_value(res.headers, "Content-Type", "text/plain");
|
2017-04-21 20:00:00 -07:00
|
|
|
socket_printf(strm, "Content-Type: %s\r\n", t);
|
|
|
|
socket_printf(strm, "Content-Length: %ld\r\n", res.body.size());
|
|
|
|
strm.write("\r\n");
|
2012-10-03 20:47:59 -07:00
|
|
|
}
|
|
|
|
|
2017-04-21 20:00:00 -07:00
|
|
|
inline void write_response(Stream& strm, const Request& req, const Response& res)
|
2012-10-03 20:47:59 -07:00
|
|
|
{
|
2017-04-21 20:00:00 -07:00
|
|
|
socket_printf(strm, "HTTP/1.0 %d %s\r\n", res.status, status_message(res.status));
|
2012-10-03 20:47:59 -07:00
|
|
|
|
2017-04-21 20:00:00 -07:00
|
|
|
write_headers(strm, res);
|
2012-10-03 20:47:59 -07:00
|
|
|
|
2012-10-12 14:41:58 -07:00
|
|
|
if (!res.body.empty() && req.method != "HEAD") {
|
2017-04-21 20:00:00 -07:00
|
|
|
strm.write(res.body.c_str(), res.body.size());
|
2012-10-03 20:47:59 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-11 20:52:34 -07:00
|
|
|
inline std::string encode_url(const std::string& s)
|
|
|
|
{
|
|
|
|
std::string result;
|
|
|
|
|
2012-11-07 18:54:39 -07:00
|
|
|
for (auto i = 0; s[i]; i++) {
|
2012-10-11 20:52:34 -07:00
|
|
|
switch (s[i]) {
|
2012-10-11 21:01:56 -07:00
|
|
|
case ' ': result += "+"; break;
|
2012-10-11 20:52:34 -07:00
|
|
|
case '\'': result += "%27"; break;
|
|
|
|
case ',': result += "%2C"; break;
|
|
|
|
case ':': result += "%3A"; break;
|
|
|
|
case ';': result += "%3B"; break;
|
|
|
|
default:
|
|
|
|
if (s[i] < 0) {
|
|
|
|
result += '%';
|
|
|
|
char hex[4];
|
2012-10-12 13:09:39 -07:00
|
|
|
size_t len = snprintf(hex, sizeof(hex), "%02X", (unsigned char)s[i]);
|
2012-10-11 20:52:34 -07:00
|
|
|
assert(len == 2);
|
|
|
|
result.append(hex, len);
|
|
|
|
} else {
|
|
|
|
result += s[i];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2012-11-07 18:54:39 -07:00
|
|
|
}
|
2012-10-11 20:52:34 -07:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool is_hex(char c, int& v)
|
|
|
|
{
|
|
|
|
if (0x20 <= c && isdigit(c)) {
|
|
|
|
v = c - '0';
|
|
|
|
return true;
|
|
|
|
} else if ('A' <= c && c <= 'F') {
|
|
|
|
v = c - 'A' + 10;
|
|
|
|
return true;
|
|
|
|
} else if ('a' <= c && c <= 'f') {
|
|
|
|
v = c - 'a' + 10;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-12-03 20:31:00 -07:00
|
|
|
inline bool from_hex_to_i(const std::string& s, int i, int cnt, int& val)
|
2012-10-11 20:52:34 -07:00
|
|
|
{
|
|
|
|
val = 0;
|
2017-12-03 20:31:00 -07:00
|
|
|
for (; cnt; i++, cnt--) {
|
|
|
|
if (!s[i]) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-10-11 20:52:34 -07:00
|
|
|
int v = 0;
|
|
|
|
if (is_hex(s[i], v)) {
|
|
|
|
val = val * 16 + v;
|
|
|
|
} else {
|
2017-12-03 20:31:00 -07:00
|
|
|
return false;
|
2012-10-11 20:52:34 -07:00
|
|
|
}
|
|
|
|
}
|
2017-12-03 20:31:00 -07:00
|
|
|
return true;
|
2012-10-11 20:52:34 -07:00
|
|
|
}
|
|
|
|
|
2015-01-14 16:51:53 -07:00
|
|
|
inline size_t to_utf8(int code, char* buff)
|
2012-10-11 20:52:34 -07:00
|
|
|
{
|
|
|
|
if (code < 0x0080) {
|
2012-10-12 13:09:39 -07:00
|
|
|
buff[0] = (code & 0x7F);
|
2012-10-11 20:52:34 -07:00
|
|
|
return 1;
|
|
|
|
} else if (code < 0x0800) {
|
2012-10-12 13:09:39 -07:00
|
|
|
buff[0] = (0xC0 | ((code >> 6) & 0x1F));
|
|
|
|
buff[1] = (0x80 | (code & 0x3F));
|
2012-10-11 20:52:34 -07:00
|
|
|
return 2;
|
|
|
|
} else if (code < 0xD800) {
|
2012-10-12 13:09:39 -07:00
|
|
|
buff[0] = (0xE0 | ((code >> 12) & 0xF));
|
|
|
|
buff[1] = (0x80 | ((code >> 6) & 0x3F));
|
|
|
|
buff[2] = (0x80 | (code & 0x3F));
|
2012-10-11 20:52:34 -07:00
|
|
|
return 3;
|
|
|
|
} else if (code < 0xE000) { // D800 - DFFF is invalid...
|
|
|
|
return 0;
|
|
|
|
} else if (code < 0x10000) {
|
2012-10-12 13:09:39 -07:00
|
|
|
buff[0] = (0xE0 | ((code >> 12) & 0xF));
|
|
|
|
buff[1] = (0x80 | ((code >> 6) & 0x3F));
|
|
|
|
buff[2] = (0x80 | (code & 0x3F));
|
2012-10-11 20:52:34 -07:00
|
|
|
return 3;
|
|
|
|
} else if (code < 0x110000) {
|
2012-10-12 13:09:39 -07:00
|
|
|
buff[0] = (0xF0 | ((code >> 18) & 0x7));
|
|
|
|
buff[1] = (0x80 | ((code >> 12) & 0x3F));
|
|
|
|
buff[2] = (0x80 | ((code >> 6) & 0x3F));
|
|
|
|
buff[3] = (0x80 | (code & 0x3F));
|
2012-10-11 20:52:34 -07:00
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTREACHED
|
2012-10-12 13:09:39 -07:00
|
|
|
return 0;
|
2012-10-11 20:52:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
inline std::string decode_url(const std::string& s)
|
|
|
|
{
|
|
|
|
std::string result;
|
|
|
|
|
|
|
|
for (int i = 0; s[i]; i++) {
|
|
|
|
if (s[i] == '%') {
|
2017-12-03 20:31:00 -07:00
|
|
|
if (s[i + 1] && s[i + 1] == 'u') {
|
2012-10-11 20:52:34 -07:00
|
|
|
int val = 0;
|
2017-12-03 20:31:00 -07:00
|
|
|
if (from_hex_to_i(s, i + 2, 4, val)) {
|
|
|
|
// 4 digits Unicode codes
|
|
|
|
char buff[4];
|
|
|
|
size_t len = to_utf8(val, buff);
|
|
|
|
if (len > 0) {
|
|
|
|
result.append(buff, len);
|
|
|
|
}
|
|
|
|
i += 5; // 'u0000'
|
|
|
|
} else {
|
|
|
|
result += s[i];
|
2012-10-11 20:52:34 -07:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
int val = 0;
|
2017-12-03 20:31:00 -07:00
|
|
|
if (from_hex_to_i(s, i + 1, 2, val)) {
|
|
|
|
// 2 digits hex codes
|
|
|
|
result += val;
|
|
|
|
i += 2; // '00'
|
|
|
|
} else {
|
|
|
|
result += s[i];
|
|
|
|
}
|
2012-10-11 20:52:34 -07:00
|
|
|
}
|
|
|
|
} else if (s[i] == '+') {
|
|
|
|
result += ' ';
|
|
|
|
} else {
|
|
|
|
result += s[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-12-03 19:25:38 -07:00
|
|
|
inline void write_request(Stream& strm, const Request& req, const char* ver)
|
2012-10-03 20:47:59 -07:00
|
|
|
{
|
2017-05-13 20:43:29 -07:00
|
|
|
auto path = encode_url(req.path);
|
2017-12-03 19:25:38 -07:00
|
|
|
socket_printf(strm, "%s %s %s\r\n", req.method.c_str(), path.c_str(), ver);
|
2012-10-03 20:47:59 -07:00
|
|
|
|
2017-04-21 20:00:00 -07:00
|
|
|
write_headers(strm, req);
|
2012-10-03 20:47:59 -07:00
|
|
|
|
|
|
|
if (!req.body.empty()) {
|
2012-10-11 20:52:34 -07:00
|
|
|
if (req.has_header("application/x-www-form-urlencoded")) {
|
2016-09-16 13:12:52 -07:00
|
|
|
auto str = encode_url(req.body);
|
2017-04-21 20:00:00 -07:00
|
|
|
strm.write(str.c_str(), str.size());
|
2012-10-11 20:52:34 -07:00
|
|
|
} else {
|
2017-04-21 20:00:00 -07:00
|
|
|
strm.write(req.body.c_str(), req.body.size());
|
2012-10-11 20:52:34 -07:00
|
|
|
}
|
2012-10-03 20:47:59 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-05 21:19:39 -07:00
|
|
|
inline void parse_query_text(const std::string& s, MultiMap& params)
|
2012-10-03 20:47:59 -07:00
|
|
|
{
|
2012-10-11 20:52:34 -07:00
|
|
|
split(&s[0], &s[s.size()], '&', [&](const char* b, const char* e) {
|
2012-10-03 20:47:59 -07:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
});
|
2017-12-05 21:19:39 -07:00
|
|
|
params.emplace(key, detail::decode_url(val));
|
2012-10-03 20:47:59 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-12-05 17:19:07 -07:00
|
|
|
inline bool parse_multipart_boundary(const std::string& content_type, std::string& boundary)
|
|
|
|
{
|
|
|
|
auto pos = content_type.find("boundary=");
|
|
|
|
if (pos == std::string::npos) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
boundary = content_type.substr(pos + 9);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool parse_multipart_formdata(
|
|
|
|
const std::string& boundary, const std::string& body, MultipartFiles& files)
|
|
|
|
{
|
|
|
|
static std::string dash = "--";
|
|
|
|
static std::string crlf = "\r\n";
|
|
|
|
|
|
|
|
static std::regex re_content_type(
|
|
|
|
"Content-Type: (.*?)");
|
|
|
|
|
|
|
|
static std::regex re_content_disposition(
|
|
|
|
"Content-Disposition: form-data; name=\"(.*?)\"(?:; filename=\"(.*?)\")?");
|
|
|
|
|
|
|
|
auto dash_boundary = dash + boundary;
|
|
|
|
|
|
|
|
auto pos = body.find(dash_boundary);
|
|
|
|
if (pos != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
pos += dash_boundary.size();
|
|
|
|
|
|
|
|
auto next_pos = body.find(crlf, pos);
|
|
|
|
if (next_pos == std::string::npos) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
pos = next_pos + crlf.size();
|
|
|
|
|
|
|
|
while (pos < body.size()) {
|
|
|
|
next_pos = body.find(crlf, pos);
|
|
|
|
if (next_pos == std::string::npos) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string name;
|
|
|
|
MultipartFile file;
|
|
|
|
|
|
|
|
auto header = body.substr(pos, (next_pos - pos));
|
|
|
|
|
|
|
|
while (pos != next_pos) {
|
|
|
|
std::smatch m;
|
|
|
|
if (std::regex_match(header, m, re_content_type)) {
|
|
|
|
file.content_type = m[1];
|
|
|
|
} else if (std::regex_match(header, m, re_content_disposition)) {
|
|
|
|
name = m[1];
|
|
|
|
file.filename = m[2];
|
|
|
|
}
|
|
|
|
|
|
|
|
pos = next_pos + crlf.size();
|
|
|
|
|
|
|
|
next_pos = body.find(crlf, pos);
|
|
|
|
if (next_pos == std::string::npos) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
header = body.substr(pos, (next_pos - pos));
|
|
|
|
}
|
|
|
|
|
|
|
|
pos = next_pos + crlf.size();
|
|
|
|
|
|
|
|
next_pos = body.find(crlf + dash_boundary, pos);
|
|
|
|
|
|
|
|
if (next_pos == std::string::npos) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
file.offset = pos;
|
|
|
|
file.length = next_pos - pos;
|
|
|
|
|
|
|
|
pos = next_pos + crlf.size() + dash_boundary.size();
|
|
|
|
|
|
|
|
next_pos = body.find(crlf, pos);
|
|
|
|
if (next_pos == std::string::npos) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-12-05 21:19:39 -07:00
|
|
|
files.emplace(name, file);
|
2017-12-05 17:19:07 -07:00
|
|
|
|
|
|
|
pos = next_pos + crlf.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-07-04 15:18:52 -07:00
|
|
|
#ifdef _MSC_VER
|
2012-11-07 18:54:04 -07:00
|
|
|
class WSInit {
|
|
|
|
public:
|
2016-03-04 14:49:15 -07:00
|
|
|
WSInit() {
|
2012-11-07 18:54:04 -07:00
|
|
|
WSADATA wsaData;
|
|
|
|
WSAStartup(0x0002, &wsaData);
|
|
|
|
}
|
|
|
|
|
2016-03-04 14:49:15 -07:00
|
|
|
~WSInit() {
|
2012-11-07 18:54:04 -07:00
|
|
|
WSACleanup();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static WSInit wsinit_;
|
|
|
|
#endif
|
|
|
|
|
2012-10-05 10:58:56 -07:00
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
// Request implementation
|
2012-10-03 17:11:22 -07:00
|
|
|
inline bool Request::has_header(const char* key) const
|
2012-10-02 17:39:13 -07:00
|
|
|
{
|
2012-10-03 17:11:22 -07:00
|
|
|
return headers.find(key) != headers.end();
|
|
|
|
}
|
2012-09-25 20:21:57 -07:00
|
|
|
|
2012-10-03 17:11:22 -07:00
|
|
|
inline std::string Request::get_header_value(const char* key) const
|
|
|
|
{
|
2013-05-11 13:11:30 -07:00
|
|
|
return detail::get_header_value(headers, key, "");
|
2012-10-03 17:11:22 -07:00
|
|
|
}
|
2012-09-25 20:21:57 -07:00
|
|
|
|
2012-10-03 20:47:59 -07:00
|
|
|
inline void Request::set_header(const char* key, const char* val)
|
|
|
|
{
|
2017-12-05 21:19:39 -07:00
|
|
|
headers.emplace(key, val);
|
2012-10-03 20:47:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool Request::has_param(const char* key) const
|
|
|
|
{
|
|
|
|
return params.find(key) != params.end();
|
|
|
|
}
|
|
|
|
|
2017-12-05 21:19:39 -07:00
|
|
|
inline std::string Request::get_param_value(const char* key) const
|
|
|
|
{
|
|
|
|
auto it = params.find(key);
|
|
|
|
if (it != params.end()) {
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
return std::string();
|
|
|
|
}
|
|
|
|
|
2017-12-05 17:19:07 -07:00
|
|
|
inline bool Request::has_file(const char* key) const
|
|
|
|
{
|
|
|
|
return files.find(key) != files.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline MultipartFile Request::get_file_value(const char* key) const
|
|
|
|
{
|
|
|
|
auto it = files.find(key);
|
|
|
|
if (it != files.end()) {
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
return MultipartFile();
|
|
|
|
}
|
|
|
|
|
2012-10-05 10:58:56 -07:00
|
|
|
// Response implementation
|
2012-10-03 17:11:22 -07:00
|
|
|
inline bool Response::has_header(const char* key) const
|
|
|
|
{
|
|
|
|
return headers.find(key) != headers.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline std::string Response::get_header_value(const char* key) const
|
|
|
|
{
|
2013-05-11 13:11:30 -07:00
|
|
|
return detail::get_header_value(headers, key, "");
|
2012-10-03 17:11:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void Response::set_header(const char* key, const char* val)
|
|
|
|
{
|
2017-12-05 21:19:39 -07:00
|
|
|
headers.emplace(key, val);
|
2012-09-25 20:21:57 -07:00
|
|
|
}
|
|
|
|
|
2012-10-02 17:39:13 -07:00
|
|
|
inline void Response::set_redirect(const char* url)
|
2012-09-25 19:09:56 -07:00
|
|
|
{
|
2012-10-03 17:11:22 -07:00
|
|
|
set_header("Location", url);
|
2012-09-25 19:09:56 -07:00
|
|
|
status = 302;
|
|
|
|
}
|
|
|
|
|
2015-07-15 13:40:40 -07:00
|
|
|
inline void Response::set_content(const char* s, size_t n, const char* content_type)
|
|
|
|
{
|
|
|
|
body.assign(s, n);
|
|
|
|
set_header("Content-Type", content_type);
|
|
|
|
}
|
|
|
|
|
2012-10-02 17:39:13 -07:00
|
|
|
inline void Response::set_content(const std::string& s, const char* content_type)
|
2012-09-25 19:09:56 -07:00
|
|
|
{
|
|
|
|
body = s;
|
2012-10-03 17:11:22 -07:00
|
|
|
set_header("Content-Type", content_type);
|
2012-09-25 19:09:56 -07:00
|
|
|
}
|
|
|
|
|
2017-04-21 20:00:00 -07:00
|
|
|
// Socket stream implementation
|
|
|
|
inline SocketStream::SocketStream(socket_t sock): sock_(sock)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
inline SocketStream::~SocketStream()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int SocketStream::read(char* ptr, size_t size)
|
|
|
|
{
|
|
|
|
return recv(sock_, ptr, size, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int SocketStream::write(const char* ptr, size_t size)
|
|
|
|
{
|
|
|
|
return send(sock_, ptr, size, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int SocketStream::write(const char* ptr)
|
|
|
|
{
|
|
|
|
return write(ptr, strlen(ptr));
|
|
|
|
}
|
|
|
|
|
2012-10-05 10:58:56 -07:00
|
|
|
// HTTP server implementation
|
2012-10-12 13:09:39 -07:00
|
|
|
inline Server::Server()
|
|
|
|
: svr_sock_(-1)
|
2012-09-22 21:40:46 -07:00
|
|
|
{
|
2017-07-06 19:04:59 -07:00
|
|
|
#ifndef _MSC_VER
|
|
|
|
signal(SIGPIPE, SIG_IGN);
|
|
|
|
#endif
|
2012-09-22 21:40:46 -07:00
|
|
|
}
|
|
|
|
|
2017-04-21 20:00:00 -07:00
|
|
|
inline Server::~Server()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-11-25 09:58:09 -07:00
|
|
|
inline Server& Server::get(const char* pattern, Handler handler)
|
2012-09-22 21:40:46 -07:00
|
|
|
{
|
2012-10-11 20:52:34 -07:00
|
|
|
get_handlers_.push_back(std::make_pair(std::regex(pattern), handler));
|
2017-11-25 09:58:09 -07:00
|
|
|
return *this;
|
2012-09-22 21:40:46 -07:00
|
|
|
}
|
|
|
|
|
2017-11-25 09:58:09 -07:00
|
|
|
inline Server& Server::post(const char* pattern, Handler handler)
|
2012-09-22 21:40:46 -07:00
|
|
|
{
|
2012-10-11 20:52:34 -07:00
|
|
|
post_handlers_.push_back(std::make_pair(std::regex(pattern), handler));
|
2017-11-25 09:58:09 -07:00
|
|
|
return *this;
|
2012-09-22 21:40:46 -07:00
|
|
|
}
|
|
|
|
|
2013-07-04 19:08:06 -07:00
|
|
|
inline bool Server::set_base_dir(const char* path)
|
2013-07-04 15:18:52 -07:00
|
|
|
{
|
2013-07-04 19:08:06 -07:00
|
|
|
if (detail::is_dir(path)) {
|
|
|
|
base_dir_ = path;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2013-07-04 15:18:52 -07:00
|
|
|
}
|
|
|
|
|
2012-10-03 17:11:22 -07:00
|
|
|
inline void Server::set_error_handler(Handler handler)
|
2012-09-27 17:54:54 -07:00
|
|
|
{
|
2012-10-02 19:37:14 -07:00
|
|
|
error_handler_ = handler;
|
2012-09-27 17:54:54 -07:00
|
|
|
}
|
|
|
|
|
2012-10-12 14:41:58 -07:00
|
|
|
inline void Server::set_logger(Logger logger)
|
2012-10-02 17:39:13 -07:00
|
|
|
{
|
|
|
|
logger_ = logger;
|
|
|
|
}
|
|
|
|
|
2017-08-29 10:20:20 -07:00
|
|
|
inline bool Server::listen(const char* host, int port, int socket_flags)
|
2012-09-22 21:40:46 -07:00
|
|
|
{
|
2017-08-29 10:20:20 -07:00
|
|
|
svr_sock_ = detail::create_server_socket(host, port, socket_flags);
|
2012-10-05 10:58:56 -07:00
|
|
|
if (svr_sock_ == -1) {
|
2012-09-22 21:40:46 -07:00
|
|
|
return false;
|
|
|
|
}
|
2015-01-27 14:45:11 -07:00
|
|
|
|
2012-10-11 20:52:34 -07:00
|
|
|
auto ret = true;
|
|
|
|
|
2012-09-22 21:40:46 -07:00
|
|
|
for (;;) {
|
2012-10-05 10:58:56 -07:00
|
|
|
socket_t sock = accept(svr_sock_, NULL, NULL);
|
2012-10-11 20:52:34 -07:00
|
|
|
|
2012-10-05 10:58:56 -07:00
|
|
|
if (sock == -1) {
|
2012-10-11 20:52:34 -07:00
|
|
|
if (svr_sock_ != -1) {
|
|
|
|
detail::close_socket(svr_sock_);
|
|
|
|
ret = false;
|
2012-10-05 10:58:56 -07:00
|
|
|
} else {
|
2012-10-11 20:52:34 -07:00
|
|
|
; // The server socket was closed by user.
|
2012-10-05 10:58:56 -07:00
|
|
|
}
|
2012-10-11 20:52:34 -07:00
|
|
|
break;
|
2012-09-22 21:40:46 -07:00
|
|
|
}
|
|
|
|
|
2012-10-05 10:58:56 -07:00
|
|
|
// TODO: should be async
|
2017-04-21 20:00:00 -07:00
|
|
|
read_and_close_socket(sock);
|
2012-09-22 21:40:46 -07:00
|
|
|
}
|
|
|
|
|
2012-10-11 20:52:34 -07:00
|
|
|
return ret;
|
2012-09-22 21:40:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void Server::stop()
|
|
|
|
{
|
2012-10-11 20:52:34 -07:00
|
|
|
detail::shutdown_socket(svr_sock_);
|
|
|
|
detail::close_socket(svr_sock_);
|
2012-10-05 10:58:56 -07:00
|
|
|
svr_sock_ = -1;
|
2012-09-22 21:40:46 -07:00
|
|
|
}
|
|
|
|
|
2017-04-21 20:00:00 -07:00
|
|
|
inline bool Server::read_request_line(Stream& strm, Request& req)
|
2012-09-22 21:40:46 -07:00
|
|
|
{
|
2017-12-06 21:52:34 -07:00
|
|
|
const auto bufsiz = 2048;
|
|
|
|
char buf[bufsiz];
|
|
|
|
|
|
|
|
detail::socket_reader reader(strm, buf, bufsiz);
|
|
|
|
|
|
|
|
if (!reader.getline()) {
|
2012-10-02 17:39:13 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-10-12 14:41:58 -07:00
|
|
|
static std::regex re("(GET|HEAD|POST) ([^?]+)(?:\\?(.+?))? HTTP/1\\.[01]\r\n");
|
2012-09-24 04:57:09 -07:00
|
|
|
|
2012-09-22 21:40:46 -07:00
|
|
|
std::cmatch m;
|
2017-12-06 21:52:34 -07:00
|
|
|
if (std::regex_match(reader.ptr(), m, re)) {
|
2012-10-02 19:37:14 -07:00
|
|
|
req.method = std::string(m[1]);
|
2017-05-13 20:43:29 -07:00
|
|
|
req.path = detail::decode_url(m[2]);
|
2012-09-24 20:13:01 -07:00
|
|
|
|
|
|
|
// Parse query text
|
|
|
|
auto len = std::distance(m[3].first, m[3].second);
|
|
|
|
if (len > 0) {
|
2015-07-13 12:42:21 -07:00
|
|
|
detail::parse_query_text(m[3], req.params);
|
2012-09-24 20:13:01 -07:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-12-02 08:24:41 -07:00
|
|
|
inline void Server::write_response(Stream& strm, const Request& req, Response& res)
|
|
|
|
{
|
|
|
|
assert(res.status != -1);
|
|
|
|
|
|
|
|
if (400 <= res.status && error_handler_) {
|
|
|
|
error_handler_(req, res);
|
|
|
|
}
|
|
|
|
|
|
|
|
detail::write_response(strm, req, res);
|
|
|
|
|
|
|
|
if (logger_) {
|
|
|
|
logger_(req, res);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-04 15:18:52 -07:00
|
|
|
inline bool Server::handle_file_request(Request& req, Response& res)
|
|
|
|
{
|
2017-11-25 20:32:09 -07:00
|
|
|
if (!base_dir_.empty() && detail::is_valid_path(req.path)) {
|
2017-05-13 20:43:29 -07:00
|
|
|
std::string path = base_dir_ + req.path;
|
2013-07-04 15:18:52 -07:00
|
|
|
|
2013-07-04 19:08:06 -07:00
|
|
|
if (!path.empty() && path.back() == '/') {
|
|
|
|
path += "index.html";
|
|
|
|
}
|
2013-07-04 15:18:52 -07:00
|
|
|
|
2013-07-04 19:08:06 -07:00
|
|
|
if (detail::is_file(path)) {
|
|
|
|
detail::read_file(path, res.body);
|
2017-05-13 21:46:40 -07:00
|
|
|
auto type = detail::content_type(path);
|
|
|
|
if (type) {
|
|
|
|
res.set_header("Content-Type", type);
|
|
|
|
}
|
2013-07-04 19:08:06 -07:00
|
|
|
res.status = 200;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2013-07-04 15:18:52 -07:00
|
|
|
|
2015-07-15 13:41:11 -07:00
|
|
|
return false;
|
2013-07-04 15:18:52 -07:00
|
|
|
}
|
|
|
|
|
2012-10-12 13:09:39 -07:00
|
|
|
inline bool Server::routing(Request& req, Response& res)
|
2012-10-03 21:38:32 -07:00
|
|
|
{
|
2015-07-15 13:41:11 -07:00
|
|
|
if (req.method == "GET" && handle_file_request(req, res)) {
|
|
|
|
return true;
|
2013-07-04 15:18:52 -07:00
|
|
|
}
|
|
|
|
|
2012-10-12 14:41:58 -07:00
|
|
|
if (req.method == "GET" || req.method == "HEAD") {
|
2012-10-12 13:09:39 -07:00
|
|
|
return dispatch_request(req, res, get_handlers_);
|
|
|
|
} else if (req.method == "POST") {
|
|
|
|
return dispatch_request(req, res, post_handlers_);
|
2012-10-03 21:38:32 -07:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-10-12 13:09:39 -07:00
|
|
|
inline bool Server::dispatch_request(Request& req, Response& res, Handlers& handlers)
|
2012-10-03 18:55:01 -07:00
|
|
|
{
|
2013-05-11 13:11:30 -07:00
|
|
|
for (const auto& x: handlers) {
|
|
|
|
const auto& pattern = x.first;
|
|
|
|
const auto& handler = x.second;
|
2012-10-03 18:55:01 -07:00
|
|
|
|
2017-05-13 20:43:29 -07:00
|
|
|
if (std::regex_match(req.path, req.matches, pattern)) {
|
2012-10-12 13:09:39 -07:00
|
|
|
handler(req, res);
|
2012-10-03 21:38:32 -07:00
|
|
|
return true;
|
2012-10-03 18:55:01 -07:00
|
|
|
}
|
|
|
|
}
|
2012-10-03 21:38:32 -07:00
|
|
|
return false;
|
2012-10-03 18:55:01 -07:00
|
|
|
}
|
|
|
|
|
2017-04-21 20:00:00 -07:00
|
|
|
inline void Server::process_request(Stream& strm)
|
2012-09-22 21:40:46 -07:00
|
|
|
{
|
2012-10-12 13:09:39 -07:00
|
|
|
Request req;
|
|
|
|
Response res;
|
2012-09-24 20:13:01 -07:00
|
|
|
|
2017-12-02 08:24:41 -07:00
|
|
|
if (!read_request_line(strm, req) || !detail::read_headers(strm, req.headers)) {
|
|
|
|
res.status = 400;
|
|
|
|
write_response(strm, req, res);
|
2012-09-24 20:13:01 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-10-12 13:09:39 -07:00
|
|
|
if (req.method == "POST") {
|
2017-12-03 19:25:38 -07:00
|
|
|
if (!detail::read_content(strm, req)) {
|
2017-12-02 08:24:41 -07:00
|
|
|
res.status = 400;
|
|
|
|
write_response(strm, req, res);
|
2012-10-03 18:55:01 -07:00
|
|
|
return;
|
|
|
|
}
|
2017-12-02 08:24:41 -07:00
|
|
|
|
2017-12-05 17:19:07 -07:00
|
|
|
const auto& content_type = req.get_header_value("Content-Type");
|
|
|
|
|
|
|
|
if (!content_type.find("application/x-www-form-urlencoded")) {
|
2015-07-13 12:42:21 -07:00
|
|
|
detail::parse_query_text(req.body, req.params);
|
2017-12-05 17:19:07 -07:00
|
|
|
} else if(!content_type.find("multipart/form-data")) {
|
|
|
|
std::string boundary;
|
|
|
|
if (!detail::parse_multipart_boundary(content_type, boundary) ||
|
|
|
|
!detail::parse_multipart_formdata(boundary, req.body, req.files)) {
|
|
|
|
res.status = 400;
|
|
|
|
write_response(strm, req, res);
|
|
|
|
return;
|
|
|
|
}
|
2012-10-03 20:47:59 -07:00
|
|
|
}
|
2012-09-24 20:13:01 -07:00
|
|
|
}
|
2015-01-27 14:45:11 -07:00
|
|
|
|
2012-10-12 13:09:39 -07:00
|
|
|
if (routing(req, res)) {
|
|
|
|
if (res.status == -1) {
|
|
|
|
res.status = 200;
|
2012-10-03 21:38:32 -07:00
|
|
|
}
|
|
|
|
} else {
|
2012-10-12 13:09:39 -07:00
|
|
|
res.status = 404;
|
2012-10-02 19:37:14 -07:00
|
|
|
}
|
|
|
|
|
2017-12-02 08:24:41 -07:00
|
|
|
write_response(strm, req, res);
|
2012-09-22 21:40:46 -07:00
|
|
|
}
|
|
|
|
|
2017-04-21 20:00:00 -07:00
|
|
|
inline bool Server::read_and_close_socket(socket_t sock)
|
|
|
|
{
|
|
|
|
return detail::read_and_close_socket(sock, [this](Stream& strm) {
|
|
|
|
process_request(strm);
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2012-10-02 17:39:13 -07:00
|
|
|
// HTTP client implementation
|
2017-12-03 19:25:38 -07:00
|
|
|
inline Client::Client(const char* host, int port, HttpVersion http_version)
|
2012-10-02 17:39:13 -07:00
|
|
|
: host_(host)
|
|
|
|
, port_(port)
|
2017-12-03 19:25:38 -07:00
|
|
|
, http_version_(http_version)
|
2017-11-06 11:24:55 -07:00
|
|
|
, host_and_port_(host_ + ":" + std::to_string(port_))
|
2012-10-02 17:39:13 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-04-21 20:00:00 -07:00
|
|
|
inline Client::~Client()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool Client::read_response_line(Stream& strm, Response& res)
|
2012-10-02 17:39:13 -07:00
|
|
|
{
|
2017-12-06 21:52:34 -07:00
|
|
|
const auto bufsiz = 2048;
|
|
|
|
char buf[bufsiz];
|
|
|
|
|
|
|
|
detail::socket_reader reader(strm, buf, bufsiz);
|
|
|
|
|
|
|
|
if (!reader.getline()) {
|
2012-10-02 17:39:13 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-04-24 03:09:19 -07:00
|
|
|
const static std::regex re("HTTP/1\\.[01] (\\d+?) .+\r\n");
|
2012-10-02 17:39:13 -07:00
|
|
|
|
|
|
|
std::cmatch m;
|
2017-12-06 21:52:34 -07:00
|
|
|
if (std::regex_match(reader.ptr(), m, re)) {
|
2013-04-24 03:09:19 -07:00
|
|
|
res.status = std::stoi(std::string(m[1]));
|
2012-10-02 17:39:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-10-03 20:47:59 -07:00
|
|
|
inline bool Client::send(const Request& req, Response& res)
|
2012-10-02 17:39:13 -07:00
|
|
|
{
|
2017-12-06 22:05:43 -07:00
|
|
|
if (req.path.empty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-11-07 18:54:39 -07:00
|
|
|
auto sock = detail::create_client_socket(host_.c_str(), port_);
|
2012-10-02 17:39:13 -07:00
|
|
|
if (sock == -1) {
|
2012-10-02 20:24:23 -07:00
|
|
|
return false;
|
2012-10-02 17:39:13 -07:00
|
|
|
}
|
|
|
|
|
2017-04-21 20:00:00 -07:00
|
|
|
return read_and_close_socket(sock, req, res);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool Client::process_request(Stream& strm, const Request& req, Response& res)
|
|
|
|
{
|
|
|
|
// Send request
|
2017-12-03 19:25:38 -07:00
|
|
|
auto ver = detail::http_version_strings[static_cast<size_t>(http_version_)];
|
|
|
|
detail::write_request(strm, req, ver);
|
2015-07-15 13:41:11 -07:00
|
|
|
|
2017-04-21 20:00:00 -07:00
|
|
|
// Receive response
|
|
|
|
if (!read_response_line(strm, res) ||
|
|
|
|
!detail::read_headers(strm, res.headers)) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-12-02 08:24:41 -07:00
|
|
|
|
2017-04-21 20:00:00 -07:00
|
|
|
if (req.method != "HEAD") {
|
2017-12-03 19:25:38 -07:00
|
|
|
if (!detail::read_content(strm, res, req.progress)) {
|
2015-07-15 13:41:11 -07:00
|
|
|
return false;
|
|
|
|
}
|
2017-04-21 20:00:00 -07:00
|
|
|
}
|
2012-10-02 17:39:13 -07:00
|
|
|
|
2017-04-21 20:00:00 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool Client::read_and_close_socket(socket_t sock, const Request& req, Response& res)
|
|
|
|
{
|
|
|
|
return detail::read_and_close_socket(sock, [&](Stream& strm) {
|
|
|
|
return process_request(strm, req, res);
|
2015-01-27 14:45:11 -07:00
|
|
|
});
|
2012-10-02 17:39:13 -07:00
|
|
|
}
|
|
|
|
|
2017-06-28 17:12:11 -07:00
|
|
|
inline void Client::add_default_headers(Request& req)
|
|
|
|
{
|
2017-11-06 11:24:55 -07:00
|
|
|
req.set_header("Host", host_and_port_.c_str());
|
2017-06-28 17:12:11 -07:00
|
|
|
req.set_header("Accept", "*/*");
|
|
|
|
req.set_header("User-Agent", "cpp-httplib/0.1");
|
|
|
|
}
|
|
|
|
|
2017-11-22 09:31:19 -07:00
|
|
|
inline std::shared_ptr<Response> Client::get(const char* path, Progress callback)
|
2012-10-03 22:18:18 -07:00
|
|
|
{
|
2012-10-05 10:58:56 -07:00
|
|
|
Request req;
|
|
|
|
req.method = "GET";
|
2017-05-13 20:43:29 -07:00
|
|
|
req.path = path;
|
2017-11-22 09:31:19 -07:00
|
|
|
req.progress = callback;
|
2017-06-28 17:12:11 -07:00
|
|
|
add_default_headers(req);
|
2012-10-05 10:58:56 -07:00
|
|
|
|
2012-10-03 22:18:18 -07:00
|
|
|
auto res = std::make_shared<Response>();
|
2012-10-05 10:58:56 -07:00
|
|
|
|
|
|
|
return send(req, *res) ? res : nullptr;
|
2012-10-03 22:18:18 -07:00
|
|
|
}
|
|
|
|
|
2017-05-13 20:43:29 -07:00
|
|
|
inline std::shared_ptr<Response> Client::head(const char* path)
|
2012-10-12 14:41:58 -07:00
|
|
|
{
|
|
|
|
Request req;
|
|
|
|
req.method = "HEAD";
|
2017-05-13 20:43:29 -07:00
|
|
|
req.path = path;
|
2017-06-28 17:12:11 -07:00
|
|
|
add_default_headers(req);
|
2012-10-12 14:41:58 -07:00
|
|
|
|
|
|
|
auto res = std::make_shared<Response>();
|
|
|
|
|
|
|
|
return send(req, *res) ? res : nullptr;
|
|
|
|
}
|
|
|
|
|
2012-10-03 22:18:18 -07:00
|
|
|
inline std::shared_ptr<Response> Client::post(
|
2017-05-13 20:43:29 -07:00
|
|
|
const char* path, const std::string& body, const char* content_type)
|
2012-10-03 22:18:18 -07:00
|
|
|
{
|
2012-10-05 10:58:56 -07:00
|
|
|
Request req;
|
|
|
|
req.method = "POST";
|
2017-05-13 20:43:29 -07:00
|
|
|
req.path = path;
|
2017-06-28 17:12:11 -07:00
|
|
|
add_default_headers(req);
|
|
|
|
|
2012-10-05 10:58:56 -07:00
|
|
|
req.set_header("Content-Type", content_type);
|
|
|
|
req.body = body;
|
|
|
|
|
2012-10-03 22:18:18 -07:00
|
|
|
auto res = std::make_shared<Response>();
|
2012-10-05 10:58:56 -07:00
|
|
|
|
|
|
|
return send(req, *res) ? res : nullptr;
|
2012-10-03 22:18:18 -07:00
|
|
|
}
|
|
|
|
|
2012-10-11 20:52:34 -07:00
|
|
|
inline std::shared_ptr<Response> Client::post(
|
2017-12-05 21:19:39 -07:00
|
|
|
const char* path, const MultiMap& params)
|
2012-10-11 20:52:34 -07:00
|
|
|
{
|
|
|
|
std::string query;
|
|
|
|
for (auto it = params.begin(); it != params.end(); ++it) {
|
|
|
|
if (it != params.begin()) {
|
|
|
|
query += "&";
|
|
|
|
}
|
|
|
|
query += it->first;
|
|
|
|
query += "=";
|
|
|
|
query += it->second;
|
|
|
|
}
|
|
|
|
|
2017-05-13 20:43:29 -07:00
|
|
|
return post(path, query, "application/x-www-form-urlencoded");
|
2012-10-11 20:52:34 -07:00
|
|
|
}
|
|
|
|
|
2017-04-21 20:00:00 -07:00
|
|
|
/*
|
|
|
|
* SSL Implementation
|
|
|
|
*/
|
|
|
|
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
|
|
|
|
namespace detail {
|
|
|
|
|
2017-09-07 11:24:33 -07:00
|
|
|
template <typename U, typename V, typename T>
|
|
|
|
inline bool read_and_close_socket_ssl(socket_t sock, SSL_CTX* ctx, U SSL_connect_or_accept, V setup, T callback)
|
2017-04-21 20:00:00 -07:00
|
|
|
{
|
|
|
|
auto ssl = SSL_new(ctx);
|
2017-05-13 20:43:09 -07:00
|
|
|
|
|
|
|
auto bio = BIO_new_socket(sock, BIO_NOCLOSE);
|
|
|
|
SSL_set_bio(ssl, bio, bio);
|
|
|
|
|
2017-09-07 11:24:33 -07:00
|
|
|
setup(ssl);
|
|
|
|
|
2017-04-21 20:00:00 -07:00
|
|
|
SSL_connect_or_accept(ssl);
|
|
|
|
|
|
|
|
SSLSocketStream strm(ssl);
|
|
|
|
auto ret = callback(strm);
|
|
|
|
|
|
|
|
SSL_shutdown(ssl);
|
|
|
|
SSL_free(ssl);
|
|
|
|
close_socket(sock);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
class SSLInit {
|
|
|
|
public:
|
|
|
|
SSLInit() {
|
|
|
|
SSL_load_error_strings();
|
|
|
|
SSL_library_init();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static SSLInit sslinit_;
|
|
|
|
|
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
// SSL socket stream implementation
|
|
|
|
inline SSLSocketStream::SSLSocketStream(SSL* ssl): ssl_(ssl)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
inline SSLSocketStream::~SSLSocketStream()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int SSLSocketStream::read(char* ptr, size_t size)
|
|
|
|
{
|
|
|
|
return SSL_read(ssl_, ptr, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int SSLSocketStream::write(const char* ptr, size_t size)
|
|
|
|
{
|
|
|
|
return SSL_write(ssl_, ptr, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int SSLSocketStream::write(const char* ptr)
|
|
|
|
{
|
|
|
|
return write(ptr, strlen(ptr));
|
|
|
|
}
|
|
|
|
|
|
|
|
// SSL HTTP server implementation
|
|
|
|
inline SSLServer::SSLServer(const char* cert_path, const char* private_key_path)
|
|
|
|
{
|
|
|
|
ctx_ = SSL_CTX_new(SSLv23_server_method());
|
|
|
|
|
|
|
|
if (ctx_) {
|
|
|
|
SSL_CTX_set_options(ctx_,
|
|
|
|
SSL_OP_ALL | SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 |
|
|
|
|
SSL_OP_NO_COMPRESSION |
|
|
|
|
SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION);
|
|
|
|
|
|
|
|
// auto ecdh = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
|
|
|
|
// SSL_CTX_set_tmp_ecdh(ctx_, ecdh);
|
|
|
|
// EC_KEY_free(ecdh);
|
|
|
|
|
|
|
|
if (SSL_CTX_use_certificate_file(ctx_, cert_path, SSL_FILETYPE_PEM) != 1 ||
|
|
|
|
SSL_CTX_use_PrivateKey_file(ctx_, private_key_path, SSL_FILETYPE_PEM) != 1) {
|
|
|
|
SSL_CTX_free(ctx_);
|
|
|
|
ctx_ = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline SSLServer::~SSLServer()
|
|
|
|
{
|
|
|
|
if (ctx_) {
|
|
|
|
SSL_CTX_free(ctx_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool SSLServer::read_and_close_socket(socket_t sock)
|
|
|
|
{
|
2017-09-07 11:24:33 -07:00
|
|
|
return detail::read_and_close_socket_ssl(
|
|
|
|
sock, ctx_,
|
|
|
|
SSL_accept,
|
2017-12-03 19:25:38 -07:00
|
|
|
[](SSL* /*ssl*/) {},
|
2017-09-07 11:24:33 -07:00
|
|
|
[this](Stream& strm) {
|
|
|
|
process_request(strm);
|
|
|
|
return true;
|
|
|
|
});
|
2017-04-21 20:00:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// SSL HTTP client implementation
|
2017-12-03 19:25:38 -07:00
|
|
|
inline SSLClient::SSLClient(const char* host, int port, HttpVersion http_version)
|
|
|
|
: Client(host, port, http_version)
|
2017-04-21 20:00:00 -07:00
|
|
|
{
|
|
|
|
ctx_ = SSL_CTX_new(SSLv23_client_method());
|
|
|
|
}
|
|
|
|
|
|
|
|
inline SSLClient::~SSLClient()
|
|
|
|
{
|
|
|
|
if (ctx_) {
|
|
|
|
SSL_CTX_free(ctx_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool SSLClient::read_and_close_socket(socket_t sock, const Request& req, Response& res)
|
|
|
|
{
|
2017-09-07 11:24:33 -07:00
|
|
|
return detail::read_and_close_socket_ssl(
|
|
|
|
sock, ctx_,
|
|
|
|
SSL_connect,
|
|
|
|
[&](SSL* ssl) {
|
|
|
|
SSL_set_tlsext_host_name(ssl, host_.c_str());
|
|
|
|
},
|
|
|
|
[&](Stream& strm) {
|
|
|
|
return process_request(strm, req, res);
|
|
|
|
});
|
2017-04-21 20:00:00 -07:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-09-27 18:05:36 -07:00
|
|
|
} // namespace httplib
|
2012-09-22 21:40:46 -07:00
|
|
|
|
2012-09-25 19:09:56 -07:00
|
|
|
#endif
|
|
|
|
|
2012-09-22 21:40:46 -07:00
|
|
|
// vim: et ts=4 sw=4 cin cino={1s ff=unix
|