#ifndef SERVER_HTTP_HPP #define SERVER_HTTP_HPP #include #include #include #include #include #include #ifdef NO_BOOST #include #include #include namespace asio_ns = asio; namespace error_ns = std; namespace merror_ns = std; # ifndef CASE_INSENSITIVE_EQUALS_AND_HASH # define CASE_INSENSITIVE_EQUALS_AND_HASH class case_insensitive_equals { public: bool operator()(const std::string &Left, const std::string &Right) const { return Left.size() == Right.size() && std::equal ( Left.begin() , Left.end() , Right.begin() , []( char a , char b ) { return tolower(a) == tolower(b); }); } }; bool IEQUALS(const std::string& Left, const std::string& Right) { return Left.size() == Right.size() && std::equal ( Left.begin() , Left.end() , Right.begin() , []( char a , char b ) { return tolower(a) == tolower(b); }); } class case_insensitive_hash { public: size_t operator()(const std::string& Keyval) const { //You might need a better hash function than this size_t h = 0; std::for_each( Keyval.begin() , Keyval.end() , [&](char c ) { h += tolower(c); }); return h; } }; # endif #else #include #include #include namespace asio_ns = boost::asio; namespace error_ns = boost::system; namespace merror_ns = boost::system::errc; # ifndef CASE_INSENSITIVE_EQUALS_AND_HASH # define CASE_INSENSITIVE_EQUALS_AND_HASH # define IEQUALS boost::iequals //Based on http://www.boost.org/doc/libs/1_60_0/doc/html/unordered/hash_equality.html class case_insensitive_equals { public: bool operator()(const std::string &key1, const std::string &key2) const { return boost::algorithm::iequals(key1, key2); } }; class case_insensitive_hash { public: size_t operator()(const std::string &key) const { std::size_t seed=0; for(auto &c: key) boost::hash_combine(seed, std::tolower(c)); return seed; } }; # endif #endif // Late 2017 TODO: remove the following checks and always use std::regex #ifdef USE_BOOST_REGEX #include namespace regex_ns = boost; #else #include namespace regex_ns = std; #endif // TODO when switching to c++14, use [[deprecated]] instead #ifndef DEPRECATED #ifdef __GNUC__ #define DEPRECATED __attribute__((deprecated)) #elif defined(_MSC_VER) #define DEPRECATED __declspec(deprecated) #else #define DEPRECATED #endif #endif namespace SimpleWeb { template class Server; template class ServerBase { public: virtual ~ServerBase() {} class Response : public std::ostream { friend class ServerBase; asio_ns::streambuf streambuf; std::shared_ptr socket; Response(const std::shared_ptr &socket): std::ostream(&streambuf), socket(socket) {} public: size_t size() { return streambuf.size(); } /// If true, force server to close the connection after the response have been sent. /// /// This is useful when implementing a HTTP/1.0-server sending content /// without specifying the content length. bool close_connection_after_response = false; }; class Content : public std::istream { friend class ServerBase; public: size_t size() { return streambuf.size(); } std::string string() { std::stringstream ss; ss << rdbuf(); return ss.str(); } private: asio_ns::streambuf &streambuf; Content(asio_ns::streambuf &streambuf): std::istream(&streambuf), streambuf(streambuf) {} }; class Request { friend class ServerBase; friend class Server; public: std::string method, path, http_version; Content content; std::unordered_multimap header; regex_ns::smatch path_match; std::string remote_endpoint_address; unsigned short remote_endpoint_port; /// Returns query keys with percent-decoded values. std::unordered_multimap parse_query_string() { std::unordered_multimap result; auto qs_start_pos = path.find('?'); if (qs_start_pos != std::string::npos && qs_start_pos + 1 < path.size()) { ++qs_start_pos; static regex_ns::regex pattern("([\\w+%]+)=?([^&]*)"); int submatches[] = {1, 2}; auto it_begin = regex_ns::sregex_token_iterator(path.begin() + qs_start_pos, path.end(), pattern, submatches); auto it_end = regex_ns::sregex_token_iterator(); for (auto it = it_begin; it != it_end; ++it) { auto submatch1=it->str(); auto submatch2=(++it)->str(); auto query_it = result.emplace(submatch1, submatch2); auto &value = query_it->second; for (size_t c = 0; c < value.size(); ++c) { if (value[c] == '+') value[c] = ' '; else if (value[c] == '%' && c + 2 < value.size()) { auto hex = value.substr(c + 1, 2); auto chr = static_cast(std::strtol(hex.c_str(), nullptr, 16)); value.replace(c, 3, &chr, 1); } } } } return result; } private: Request(const socket_type &socket): content(streambuf) { try { remote_endpoint_address=socket.lowest_layer().remote_endpoint().address().to_string(); remote_endpoint_port=socket.lowest_layer().remote_endpoint().port(); } catch(...) {} } asio_ns::streambuf streambuf; }; class Config { friend class ServerBase; Config(unsigned short port): port(port) {} public: /// Port number to use. Defaults to 80 for HTTP and 443 for HTTPS. unsigned short port; /// Number of threads that the server will use when start() is called. Defaults to 1 thread. size_t thread_pool_size=1; /// Timeout on request handling. Defaults to 5 seconds. size_t timeout_request=5; /// Timeout on content handling. Defaults to 300 seconds. size_t timeout_content=300; /// IPv4 address in dotted decimal form or IPv6 address in hexadecimal notation. /// If empty, the address will be any address. std::string address; /// Set to false to avoid binding the socket to an address that is already in use. Defaults to true. bool reuse_address=true; }; ///Set before calling start(). Config config; private: class regex_orderable : public regex_ns::regex { std::string str; public: regex_orderable(const char *regex_cstr) : regex_ns::regex(regex_cstr), str(regex_cstr) {} regex_orderable(const std::string ®ex_str) : regex_ns::regex(regex_str), str(regex_str) {} bool operator<(const regex_orderable &rhs) const { return str::Response>, std::shared_ptr::Request>)> > > resource; std::map::Response>, std::shared_ptr::Request>)> > default_resource; std::function::Request>, const error_ns::error_code&)> on_error; std::function socket, std::shared_ptr::Request>)> on_upgrade; virtual void start() { if(!io_service) io_service=std::make_shared(); if(io_service->stopped()) io_service->reset(); asio_ns::ip::tcp::endpoint endpoint; if(config.address.size()>0) endpoint=asio_ns::ip::tcp::endpoint(asio_ns::ip::address::from_string(config.address), config.port); else endpoint=asio_ns::ip::tcp::endpoint(asio_ns::ip::tcp::v4(), config.port); if(!acceptor) acceptor=std::unique_ptr(new asio_ns::ip::tcp::acceptor(*io_service)); acceptor->open(endpoint.protocol()); acceptor->set_option(asio_ns::socket_base::reuse_address(config.reuse_address)); acceptor->bind(endpoint); acceptor->listen(); accept(); //If thread_pool_size>1, start m_io_service.run() in (thread_pool_size-1) threads for thread-pooling threads.clear(); for(size_t c=1;crun(); }); } //Main thread if(config.thread_pool_size>0) io_service->run(); //Wait for the rest of the threads, if any, to finish as well for(auto& t: threads) { t.join(); } } void stop() { acceptor->close(); if(config.thread_pool_size>0) io_service->stop(); } ///Use this function if you need to recursively send parts of a longer message void send(const std::shared_ptr &response, const std::function& callback=nullptr) const { asio_ns::async_write(*response->socket, response->streambuf, [this, response, callback](const error_ns::error_code& ec, size_t /*bytes_transferred*/) { if(callback) callback(ec); }); } /// If you have your own asio_ns::io_service, store its pointer here before running start(). /// You might also want to set config.thread_pool_size to 0. std::shared_ptr io_service; protected: std::unique_ptr acceptor; std::vector threads; ServerBase(unsigned short port) : config(port) {} virtual void accept()=0; std::shared_ptr get_timeout_timer(const std::shared_ptr &socket, long seconds) { if(seconds==0) return nullptr; auto timer=std::make_shared(*io_service); timer->expires_from_now(boost::posix_time::seconds(seconds)); timer->async_wait([socket](const error_ns::error_code& ec){ if(!ec) { error_ns::error_code ec; socket->lowest_layer().shutdown(asio_ns::ip::tcp::socket::shutdown_both, ec); socket->lowest_layer().close(); } }); return timer; } void read_request_and_content(const std::shared_ptr &socket) { //Create new streambuf (Request::streambuf) for async_read_until() //shared_ptr is used to pass temporary objects to the asynchronous functions std::shared_ptr request(new Request(*socket)); //Set timeout on the following asio_ns::async-read or write function auto timer=this->get_timeout_timer(socket, config.timeout_request); asio_ns::async_read_until(*socket, request->streambuf, "\r\n\r\n", [this, socket, request, timer](const error_ns::error_code& ec, size_t bytes_transferred) { if(timer) timer->cancel(); if(!ec) { //request->streambuf.size() is not necessarily the same as bytes_transferred, from Boost-docs: //"After a successful async_read_until operation, the streambuf may contain additional data beyond the delimiter" //The chosen solution is to extract lines from the stream directly when parsing the header. What is left of the //streambuf (maybe some bytes of the content) is appended to in the async_read-function below (for retrieving content). size_t num_additional_bytes=request->streambuf.size()-bytes_transferred; if(!this->parse_request(request)) return; //If content, read that as well auto it=request->header.find("Content-Length"); if(it!=request->header.end()) { unsigned long long content_length; try { content_length=stoull(it->second); } catch(const std::exception &e) { if(on_error) on_error(request, merror_ns::make_error_code(error_ns::errc::protocol_error)); return; } if(content_length>num_additional_bytes) { //Set timeout on the following asio_ns::async-read or write function auto timer=this->get_timeout_timer(socket, config.timeout_content); asio_ns::async_read(*socket, request->streambuf, asio_ns::transfer_exactly(content_length-num_additional_bytes), [this, socket, request, timer] (const error_ns::error_code& ec, size_t /*bytes_transferred*/) { if(timer) timer->cancel(); if(!ec) this->find_resource(socket, request); else if(on_error) on_error(request, ec); }); } else this->find_resource(socket, request); } else this->find_resource(socket, request); } else if(on_error) on_error(request, ec); }); } bool parse_request(const std::shared_ptr &request) const { std::string line; getline(request->content, line); size_t method_end; if((method_end=line.find(' '))!=std::string::npos) { size_t path_end; if((path_end=line.find(' ', method_end+1))!=std::string::npos) { request->method=line.substr(0, method_end); request->path=line.substr(method_end+1, path_end-method_end-1); size_t protocol_end; if((protocol_end=line.find('/', path_end+1))!=std::string::npos) { if(line.compare(path_end+1, protocol_end-path_end-1, "HTTP")!=0) return false; request->http_version=line.substr(protocol_end+1, line.size()-protocol_end-2); } else return false; getline(request->content, line); size_t param_end; while((param_end=line.find(':'))!=std::string::npos) { size_t value_start=param_end+1; if((value_start)header.emplace(line.substr(0, param_end), line.substr(value_start, line.size()-value_start-1)); } getline(request->content, line); } } else return false; } else return false; return true; } void find_resource(const std::shared_ptr &socket, const std::shared_ptr &request) { //Upgrade connection if(on_upgrade) { auto it=request->header.find("Upgrade"); if(it!=request->header.end()) { on_upgrade(socket, request); return; } } //Find path- and method-match, and call write_response for(auto ®ex_method: resource) { auto it=regex_method.second.find(request->method); if(it!=regex_method.second.end()) { regex_ns::smatch sm_res; if(regex_ns::regex_match(request->path, sm_res, regex_method.first)) { request->path_match=std::move(sm_res); write_response(socket, request, it->second); return; } } } auto it=default_resource.find(request->method); if(it!=default_resource.end()) { write_response(socket, request, it->second); } } void write_response(const std::shared_ptr &socket, const std::shared_ptr &request, std::function::Response>, std::shared_ptr::Request>)>& resource_function) { //Set timeout on the following asio_ns::async-read or write function auto timer=this->get_timeout_timer(socket, config.timeout_content); auto response=std::shared_ptr(new Response(socket), [this, request, timer](Response *response_ptr) { auto response=std::shared_ptr(response_ptr); this->send(response, [this, response, request, timer](const error_ns::error_code& ec) { if(timer) timer->cancel(); if(!ec) { if (response->close_connection_after_response) return; auto range=request->header.equal_range("Connection"); for(auto it=range.first;it!=range.second;it++) { if(IEQUALS(it->second, "close")) { return; } else if (IEQUALS(it->second, "keep-alive")) { this->read_request_and_content(response->socket); return; } } if(request->http_version >= "1.1") this->read_request_and_content(response->socket); } else if(on_error) on_error(request, ec); }); }); try { resource_function(response, request); } catch(const std::exception &e) { if(on_error) on_error(request, merror_ns::make_error_code(error_ns::errc::operation_canceled)); return; } } }; template class Server : public ServerBase {}; typedef asio_ns::ip::tcp::socket HTTP; template<> class Server : public ServerBase { public: DEPRECATED Server(unsigned short port, size_t thread_pool_size=1, long timeout_request=5, long timeout_content=300) : Server() { config.port=port; config.thread_pool_size=thread_pool_size; config.timeout_request=timeout_request; config.timeout_content=timeout_content; } Server() : ServerBase::ServerBase(80) {} protected: void accept() { //Create new socket for this connection //Shared_ptr is used to pass temporary objects to the asynchronous functions auto socket=std::make_shared(*io_service); acceptor->async_accept(*socket, [this, socket](const error_ns::error_code& ec){ //Immediately start accepting a new connection (if io_service hasn't been stopped) if (ec != asio_ns::error::operation_aborted) accept(); if(!ec) { asio_ns::ip::tcp::no_delay option(true); socket->set_option(option); this->read_request_and_content(socket); } else if(on_error) on_error(std::shared_ptr(new Request(*socket)), ec); }); } }; } #endif /* SERVER_HTTP_HPP */