Header Only Library
HTTP Server
How to use HTTP server class
References
#include "ip.hpp"
#include "ip/http/httpserver.hpp"
Syntax
namespace internetprotocol
class http_server_c
Example code
#include <iostream>
#include "ip.hpp">
using namespace InternetProtocol;
int main()
{
http_server_c net;
http_request_t req;
net.get("/", [&](const http_request_t &request, const std::shared_ptr<http_remote_c> &response) {
http_response_t &res = response->headers();
res.body = "hello, world!";
response->write([](const asio::error_code &ec, const size_t bytes_sent) {
// Check if response has been sent
});
});
std::string input;
while (std::getline(std::cin, input)) {
if (input == "quit") {
net.close();
break;
}
}
join_threads();
return 0;
}
Variables### Iddle Timeout
Set/Get the idle timeout for the connection in seconds.
uint16_t iddle_timeout
Backlog
Set/Get the maximum number of simultaneous client connections the server will accept in queue.
int backlog
Functions
Is Open
- Return true if socket is open.
bool is_open() const
Local Endpoint
- Get the local endpoint of the socket. Use this function only after open connection.
tcp::endpoint local_endpoint() const
Clients
- Gets a constant reference to the set of clients connected to the server.
const std::set<std::shared_ptr<http_remote_c>> &clients() const
Get Error Code
- Return a const ref of the latest error code returned by asio.
const asio::error_code &get_error_code() const
All
- Create a callback to receive requests of any methods for a specific path.
void all(const std::string &path, const std::function<void(const http_request_t &, const std::shared_ptr<http_remote_c> &)> &callback)
Get
- Create a callback to receive requests of get method for a specific path.
void get(const std::string &path, const std::function<void(const http_request_t &, const std::shared_ptr<http_remote_c> &)> &callback)
Post
- Create a callback to receive requests of post method for a specific path.
void post(const std::string &path, const std::function<void(const http_request_t &, const std::shared_ptr<http_remote_c> &)> &callback)
Put
- Create a callback to receive requests of put method for a specific path.
void put(const std::string &path, const std::function<void(const http_request_t &, const std::shared_ptr<http_remote_c> &)> &callback)
Del
- Create a callback to receive requests of del method for a specific path.
void del(const std::string &path, const std::function<void(const http_request_t &, const std::shared_ptr<http_remote_c> &)> &callback)
Head
- Create a callback to receive requests of head method for a specific path.
void head(const std::string &path, const std::function<void(const http_request_t &, const std::shared_ptr<http_remote_c> &)> &callback)
Options
- Create a callback to receive requests of options method for a specific path.
void options(const std::string &path, const std::function<void(const http_request_t &, const std::shared_ptr<http_remote_c> &)> &callback)
Patch
- Create a callback to receive requests of patch method for a specific path.
void patch(const std::string &path, const std::function<void(const http_request_t &, const std::shared_ptr<http_remote_c> &)> &callback)
Open
- Open connection
- Return false if socket is already open or any error occurs
- Event
on_error
is triggered if any error occurs
bool open(const server_bind_options_t &bind_opts = { "", 8080, v4, true })
Close
- Close connection.
void close()
Events
On Close
- Event triggered when socket is closed
std::function<void()> on_close
On Error
- Event triggered if any error occur during async process
const asio::error_code &
: if error occur, error code will be different from 0- Normally, when an error occurs, the socket is automatically closed, triggering the
on_close
event.
std::function<void(const asio::error_code &)> on_error