2024-05-24 12:23:42 +08:00
|
|
|
#ifndef HV_HTTP_SERVICE_H_
|
|
|
|
#define HV_HTTP_SERVICE_H_
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <map>
|
|
|
|
#include <unordered_map>
|
2024-05-24 12:29:09 +08:00
|
|
|
#include <vector>
|
2024-05-24 12:23:42 +08:00
|
|
|
#include <list>
|
|
|
|
#include <memory>
|
|
|
|
#include <functional>
|
|
|
|
|
|
|
|
#include "hexport.h"
|
|
|
|
#include "HttpMessage.h"
|
|
|
|
#include "HttpResponseWriter.h"
|
|
|
|
#include "HttpContext.h"
|
|
|
|
|
|
|
|
#define DEFAULT_BASE_URL "/api/v1"
|
|
|
|
#define DEFAULT_DOCUMENT_ROOT "/var/www/html"
|
|
|
|
#define DEFAULT_HOME_PAGE "index.html"
|
|
|
|
#define DEFAULT_ERROR_PAGE "error.html"
|
|
|
|
#define DEFAULT_INDEXOF_DIR "/downloads/"
|
|
|
|
#define DEFAULT_KEEPALIVE_TIMEOUT 75000 // ms
|
|
|
|
|
|
|
|
// for FileCache
|
|
|
|
#define MAX_FILE_CACHE_SIZE (1 << 22) // 4M
|
|
|
|
#define DEFAULT_FILE_CACHE_STAT_INTERVAL 10 // s
|
|
|
|
#define DEFAULT_FILE_CACHE_EXPIRED_TIME 60 // s
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @param[in] req: parsed structured http request
|
|
|
|
* @param[out] resp: structured http response
|
2024-05-24 12:29:09 +08:00
|
|
|
* @return 0: handle next
|
2024-05-24 12:23:42 +08:00
|
|
|
* http_status_code: handle done
|
|
|
|
*/
|
2024-05-24 12:29:09 +08:00
|
|
|
#define HTTP_STATUS_NEXT 0
|
2024-05-24 12:23:42 +08:00
|
|
|
#define HTTP_STATUS_UNFINISHED 0
|
|
|
|
// NOTE: http_sync_handler run on IO thread
|
|
|
|
typedef std::function<int(HttpRequest* req, HttpResponse* resp)> http_sync_handler;
|
|
|
|
// NOTE: http_async_handler run on hv::async threadpool
|
|
|
|
typedef std::function<void(const HttpRequestPtr& req, const HttpResponseWriterPtr& writer)> http_async_handler;
|
|
|
|
// NOTE: http_ctx_handler run on IO thread, you can easily post HttpContextPtr to your consumer thread for processing.
|
|
|
|
typedef std::function<int(const HttpContextPtr& ctx)> http_ctx_handler;
|
2024-05-24 12:29:09 +08:00
|
|
|
// NOTE: http_state_handler run on IO thread
|
|
|
|
typedef std::function<int(const HttpContextPtr& ctx, http_parser_state state, const char* data, size_t size)> http_state_handler;
|
2024-05-24 12:23:42 +08:00
|
|
|
|
|
|
|
struct http_handler {
|
|
|
|
http_sync_handler sync_handler;
|
|
|
|
http_async_handler async_handler;
|
|
|
|
http_ctx_handler ctx_handler;
|
2024-05-24 12:29:09 +08:00
|
|
|
http_state_handler state_handler;
|
2024-05-24 12:23:42 +08:00
|
|
|
|
|
|
|
http_handler() {}
|
|
|
|
http_handler(http_sync_handler fn) : sync_handler(std::move(fn)) {}
|
|
|
|
http_handler(http_async_handler fn) : async_handler(std::move(fn)) {}
|
|
|
|
http_handler(http_ctx_handler fn) : ctx_handler(std::move(fn)) {}
|
2024-05-24 12:29:09 +08:00
|
|
|
http_handler(http_state_handler fn) : state_handler(std::move(fn)) {}
|
2024-05-24 12:23:42 +08:00
|
|
|
http_handler(const http_handler& rhs)
|
2024-05-24 12:29:09 +08:00
|
|
|
: sync_handler(std::move(const_cast<http_handler&>(rhs).sync_handler))
|
|
|
|
, async_handler(std::move(const_cast<http_handler&>(rhs).async_handler))
|
|
|
|
, ctx_handler(std::move(const_cast<http_handler&>(rhs).ctx_handler))
|
|
|
|
, state_handler(std::move(const_cast<http_handler&>(rhs).state_handler))
|
2024-05-24 12:23:42 +08:00
|
|
|
{}
|
|
|
|
|
|
|
|
const http_handler& operator=(http_sync_handler fn) {
|
|
|
|
sync_handler = std::move(fn);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
const http_handler& operator=(http_async_handler fn) {
|
|
|
|
async_handler = std::move(fn);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
const http_handler& operator=(http_ctx_handler fn) {
|
|
|
|
ctx_handler = std::move(fn);
|
|
|
|
return *this;
|
|
|
|
}
|
2024-05-24 12:29:09 +08:00
|
|
|
const http_handler& operator=(http_state_handler fn) {
|
|
|
|
state_handler = std::move(fn);
|
|
|
|
return *this;
|
|
|
|
}
|
2024-05-24 12:23:42 +08:00
|
|
|
|
|
|
|
bool isNull() {
|
|
|
|
return sync_handler == NULL &&
|
|
|
|
async_handler == NULL &&
|
|
|
|
ctx_handler == NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
operator bool() {
|
|
|
|
return !isNull();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-05-24 12:29:09 +08:00
|
|
|
typedef std::vector<http_handler> http_handlers;
|
|
|
|
|
2024-05-24 12:23:42 +08:00
|
|
|
struct http_method_handler {
|
|
|
|
http_method method;
|
|
|
|
http_handler handler;
|
|
|
|
|
|
|
|
http_method_handler() {}
|
|
|
|
http_method_handler(http_method m, const http_handler& h) : method(m), handler(h) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
// method => http_method_handler
|
|
|
|
typedef std::list<http_method_handler> http_method_handlers;
|
2024-05-24 12:29:09 +08:00
|
|
|
// path => http_method_handlers
|
|
|
|
typedef std::unordered_map<std::string, std::shared_ptr<http_method_handlers>> http_path_handlers;
|
2024-05-24 12:23:42 +08:00
|
|
|
|
|
|
|
namespace hv {
|
|
|
|
|
|
|
|
struct HV_EXPORT HttpService {
|
2024-05-24 12:29:09 +08:00
|
|
|
/* handler chain */
|
|
|
|
// preprocessor -> middleware -> processor -> postprocessor
|
2024-05-24 12:23:42 +08:00
|
|
|
http_handler preprocessor;
|
2024-05-24 12:29:09 +08:00
|
|
|
http_handlers middleware;
|
|
|
|
// processor: pathHandlers -> staticHandler -> errorHandler
|
2024-05-24 12:23:42 +08:00
|
|
|
http_handler processor;
|
|
|
|
http_handler postprocessor;
|
|
|
|
|
2024-05-24 12:29:09 +08:00
|
|
|
/* API handlers */
|
2024-05-24 12:23:42 +08:00
|
|
|
std::string base_url;
|
2024-05-24 12:29:09 +08:00
|
|
|
http_path_handlers pathHandlers;
|
2024-05-24 12:23:42 +08:00
|
|
|
|
2024-05-24 12:29:09 +08:00
|
|
|
/* Static file service */
|
2024-05-24 12:23:42 +08:00
|
|
|
http_handler staticHandler;
|
|
|
|
http_handler largeFileHandler;
|
|
|
|
std::string document_root;
|
|
|
|
std::string home_page;
|
|
|
|
std::string error_page;
|
2024-05-24 12:29:09 +08:00
|
|
|
// nginx: location => root
|
|
|
|
std::map<std::string, std::string, std::greater<std::string>> staticDirs;
|
|
|
|
/* Indexof directory service */
|
2024-05-24 12:23:42 +08:00
|
|
|
std::string index_of;
|
|
|
|
http_handler errorHandler;
|
|
|
|
|
2024-05-24 12:29:09 +08:00
|
|
|
/* Proxy service */
|
|
|
|
/* Reverse proxy service */
|
|
|
|
// nginx: location => proxy_pass
|
|
|
|
std::map<std::string, std::string, std::greater<std::string>> proxies;
|
|
|
|
/* Forward proxy service */
|
|
|
|
StringList trustProxies;
|
|
|
|
StringList noProxies;
|
|
|
|
int proxy_connect_timeout;
|
|
|
|
int proxy_read_timeout;
|
|
|
|
int proxy_write_timeout;
|
|
|
|
|
2024-05-24 12:23:42 +08:00
|
|
|
// options
|
|
|
|
int keepalive_timeout;
|
|
|
|
int max_file_cache_size; // cache small file
|
|
|
|
int file_cache_stat_interval; // stat file is modified
|
|
|
|
int file_cache_expired_time; // remove expired file cache
|
|
|
|
/*
|
|
|
|
* @test limit_rate
|
|
|
|
* @build make examples
|
|
|
|
* @server bin/httpd -c etc/httpd.conf -s restart -d
|
|
|
|
* @client bin/wget http://127.0.0.1:8080/downloads/test.zip
|
|
|
|
*/
|
|
|
|
int limit_rate; // limit send rate, unit: KB/s
|
|
|
|
|
2024-05-24 12:29:09 +08:00
|
|
|
unsigned enable_access_log :1;
|
|
|
|
unsigned enable_forward_proxy :1;
|
|
|
|
|
2024-05-24 12:23:42 +08:00
|
|
|
HttpService() {
|
|
|
|
// base_url = DEFAULT_BASE_URL;
|
|
|
|
|
|
|
|
document_root = DEFAULT_DOCUMENT_ROOT;
|
|
|
|
home_page = DEFAULT_HOME_PAGE;
|
|
|
|
// error_page = DEFAULT_ERROR_PAGE;
|
|
|
|
// index_of = DEFAULT_INDEXOF_DIR;
|
|
|
|
|
2024-05-24 12:29:09 +08:00
|
|
|
proxy_connect_timeout = DEFAULT_CONNECT_TIMEOUT;
|
|
|
|
proxy_read_timeout = 0;
|
|
|
|
proxy_write_timeout = 0;
|
|
|
|
|
2024-05-24 12:23:42 +08:00
|
|
|
keepalive_timeout = DEFAULT_KEEPALIVE_TIMEOUT;
|
|
|
|
max_file_cache_size = MAX_FILE_CACHE_SIZE;
|
|
|
|
file_cache_stat_interval = DEFAULT_FILE_CACHE_STAT_INTERVAL;
|
|
|
|
file_cache_expired_time = DEFAULT_FILE_CACHE_EXPIRED_TIME;
|
|
|
|
limit_rate = -1; // unlimited
|
2024-05-24 12:29:09 +08:00
|
|
|
|
|
|
|
enable_access_log = 1;
|
|
|
|
enable_forward_proxy = 0;
|
2024-05-24 12:23:42 +08:00
|
|
|
}
|
|
|
|
|
2024-05-24 12:29:09 +08:00
|
|
|
void AddRoute(const char* path, http_method method, const http_handler& handler);
|
2024-05-24 12:23:42 +08:00
|
|
|
// @retval 0 OK, else HTTP_STATUS_NOT_FOUND, HTTP_STATUS_METHOD_NOT_ALLOWED
|
2024-05-24 12:29:09 +08:00
|
|
|
int GetRoute(const char* url, http_method method, http_handler** handler);
|
2024-05-24 12:23:42 +08:00
|
|
|
// RESTful API /:field/ => req->query_params["field"]
|
2024-05-24 12:29:09 +08:00
|
|
|
int GetRoute(HttpRequest* req, http_handler** handler);
|
|
|
|
|
|
|
|
// Static("/", "/var/www/html")
|
|
|
|
void Static(const char* path, const char* dir);
|
|
|
|
// @retval / => /var/www/html/index.html
|
|
|
|
std::string GetStaticFilepath(const char* path);
|
|
|
|
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
|
|
|
|
void AllowCORS();
|
|
|
|
|
|
|
|
// proxy
|
|
|
|
// forward proxy
|
|
|
|
void EnableForwardProxy() { enable_forward_proxy = 1; }
|
|
|
|
void AddTrustProxy(const char* host);
|
|
|
|
void AddNoProxy(const char* host);
|
|
|
|
bool IsTrustProxy(const char* host);
|
|
|
|
// reverse proxy
|
|
|
|
// Proxy("/api/v1/", "http://www.httpbin.org/");
|
|
|
|
void Proxy(const char* path, const char* url);
|
|
|
|
// @retval /api/v1/test => http://www.httpbin.org/test
|
|
|
|
std::string GetProxyUrl(const char* path);
|
2024-05-24 12:23:42 +08:00
|
|
|
|
|
|
|
hv::StringList Paths() {
|
|
|
|
hv::StringList paths;
|
2024-05-24 12:29:09 +08:00
|
|
|
for (auto& pair : pathHandlers) {
|
2024-05-24 12:23:42 +08:00
|
|
|
paths.emplace_back(pair.first);
|
|
|
|
}
|
|
|
|
return paths;
|
|
|
|
}
|
|
|
|
|
2024-05-24 12:29:09 +08:00
|
|
|
// Handler = [ http_sync_handler, http_ctx_handler ]
|
|
|
|
template<typename Handler>
|
|
|
|
void Use(Handler handlerFunc) {
|
|
|
|
middleware.emplace_back(handlerFunc);
|
2024-05-24 12:23:42 +08:00
|
|
|
}
|
2024-05-24 12:29:09 +08:00
|
|
|
|
|
|
|
// Inspired by github.com/gin-gonic/gin
|
|
|
|
// Handler = [ http_sync_handler, http_async_handler, http_ctx_handler, http_state_handler ]
|
|
|
|
template<typename Handler>
|
|
|
|
void Handle(const char* httpMethod, const char* relativePath, Handler handlerFunc) {
|
|
|
|
AddRoute(relativePath, http_method_enum(httpMethod), http_handler(handlerFunc));
|
2024-05-24 12:23:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// HEAD
|
2024-05-24 12:29:09 +08:00
|
|
|
template<typename Handler>
|
|
|
|
void HEAD(const char* relativePath, Handler handlerFunc) {
|
2024-05-24 12:23:42 +08:00
|
|
|
Handle("HEAD", relativePath, handlerFunc);
|
|
|
|
}
|
|
|
|
|
|
|
|
// GET
|
2024-05-24 12:29:09 +08:00
|
|
|
template<typename Handler>
|
|
|
|
void GET(const char* relativePath, Handler handlerFunc) {
|
2024-05-24 12:23:42 +08:00
|
|
|
Handle("GET", relativePath, handlerFunc);
|
|
|
|
}
|
|
|
|
|
|
|
|
// POST
|
2024-05-24 12:29:09 +08:00
|
|
|
template<typename Handler>
|
|
|
|
void POST(const char* relativePath, Handler handlerFunc) {
|
2024-05-24 12:23:42 +08:00
|
|
|
Handle("POST", relativePath, handlerFunc);
|
|
|
|
}
|
|
|
|
|
|
|
|
// PUT
|
2024-05-24 12:29:09 +08:00
|
|
|
template<typename Handler>
|
|
|
|
void PUT(const char* relativePath, Handler handlerFunc) {
|
2024-05-24 12:23:42 +08:00
|
|
|
Handle("PUT", relativePath, handlerFunc);
|
|
|
|
}
|
|
|
|
|
|
|
|
// DELETE
|
|
|
|
// NOTE: Windows <winnt.h> #define DELETE as a macro, we have to replace DELETE with Delete.
|
2024-05-24 12:29:09 +08:00
|
|
|
template<typename Handler>
|
|
|
|
void Delete(const char* relativePath, Handler handlerFunc) {
|
2024-05-24 12:23:42 +08:00
|
|
|
Handle("DELETE", relativePath, handlerFunc);
|
|
|
|
}
|
|
|
|
|
|
|
|
// PATCH
|
2024-05-24 12:29:09 +08:00
|
|
|
template<typename Handler>
|
|
|
|
void PATCH(const char* relativePath, Handler handlerFunc) {
|
2024-05-24 12:23:42 +08:00
|
|
|
Handle("PATCH", relativePath, handlerFunc);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Any
|
2024-05-24 12:29:09 +08:00
|
|
|
template<typename Handler>
|
|
|
|
void Any(const char* relativePath, Handler handlerFunc) {
|
2024-05-24 12:23:42 +08:00
|
|
|
Handle("HEAD", relativePath, handlerFunc);
|
|
|
|
Handle("GET", relativePath, handlerFunc);
|
|
|
|
Handle("POST", relativePath, handlerFunc);
|
|
|
|
Handle("PUT", relativePath, handlerFunc);
|
|
|
|
Handle("DELETE", relativePath, handlerFunc);
|
|
|
|
Handle("PATCH", relativePath, handlerFunc);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // HV_HTTP_SERVICE_H_
|