8 #include "httpconnection.h"
12 enum class HttpState {
14 WAITING_FOR_REQUEST, SENDING_RESPONSE,
15 READING_POST_DATA, WEBSOCKET
31 const char *ip, uint16_t
port);
43 void eraseQueryPar(
const std::string &name) {
44 current_query_pars.erase(name);
50 std::string
getQueryVal(
const std::string &name)
const;
52 const std::multimap<std::string, std::string> ¤tQueryPars()
const {
53 return current_query_pars;
56 std::map<std::string, std::string> currentRequestCookies()
const;
59 std::string
cookieVal(
const std::string &name)
const;
67 typedef std::multimap<std::string, std::string>::const_iterator Hit;
68 std::pair<Hit, Hit> getHeaderVals(
const std::string &name)
const {
69 return http_request_headers.equal_range(name);
74 const std::string &mime,
75 const std::string &contents);
79 const std::string &mime,
80 size_t content_length);
85 const std::string &mime);
88 size_t sendChunk(
const std::string &content);
93 bool sendingChunkedResponse()
const {
94 return sending_chunked_response;
103 bool prepareFileResponse(
const std::string &url);
105 HttpState sendHttpFileResponse(
const std::string &headers,
106 const std::string &mime);
110 const std::string ¤tUri()
const {
114 const std::string currentFullUrl()
const {
115 if (current_query_string.empty())
117 return current_uri +
'?' + current_query_string;
120 const std::string ¤tQueryString()
const {
121 return current_query_string;
126 return remaining_post_data;
136 buffer_post_data =
true;
141 notify_after_ws_handshake =
true;
150 return current_request;
156 HttpState state = HttpState::WAITING_FOR_REQUEST;
157 std::string current_request;
158 std::multimap<std::string, std::string> http_request_headers;
159 std::string current_method, current_uri;
160 std::string current_query_string;
161 std::multimap<std::string, std::string> current_query_pars;
162 static void get_all_parameters(
const char *qpos,
163 std::multimap<std::string, std::string> &pars);
166 size_t remaining_bytes;
173 bool sending_static_file =
false;
177 bool sending_chunked_response =
false;
180 bool notify_after_ws_handshake =
false;
183 bool buffer_post_data;
184 size_t remaining_post_data;
186 bool parse_request();
187 PollState got_post_data(
const char *buf,
size_t len);
Definition: httpconnection.h:11
HTTP/1.1 server protocol.
Definition: httpserverconnection.h:28
size_t remainingPostData() const
Number of bytes left of the current HTTP POST.
Definition: httpserverconnection.h:125
void notifyWsHandshake()
Call this if the owner is to be notified after handshake:
Definition: httpserverconnection.h:140
bool hasQueryPar(const std::string &name) const
Return true if query string in current request has parameter name:
Definition: httpserverconnection.cpp:407
size_t sendChunkedResponseHeader(const std::string &headers, const std::string &mime)
Definition: httpserverconnection.cpp:374
PollState readData(char *buf, size_t len) final
Callback, called when data has arrived; len > 0.
Definition: httpserverconnection.cpp:155
std::string getHeaderVal(const std::string &name) const
Definition: httpserverconnection.cpp:418
void setOwner(Task *new_owner) override
Definition: httpserverconnection.cpp:47
std::string getQueryVal(const std::string &name) const
Definition: httpserverconnection.cpp:411
size_t sendHttpResponse(const std::string &headers, const std::string &mime, const std::string &contents)
Async send response. Return length of what's to be sent.
Definition: httpserverconnection.cpp:349
PollState connected() final
Will be called when the connection is established.
Definition: httpserverconnection.h:33
size_t chunkedResponseComplete()
To notify that all chunks have been sent.
Definition: httpserverconnection.cpp:396
size_t sendChunk(const std::string &content)
Send chunk. May only be used if we sent chunked headers.
Definition: httpserverconnection.cpp:385
size_t sendHttpResponseHeader(const std::string &headers, const std::string &mime, size_t content_length)
Async send response headers. Return length of what's to be sent:
Definition: httpserverconnection.cpp:361
std::string cookieVal(const std::string &name) const
Return value of cookie if it exists, otherwise empty string.
Definition: httpserverconnection.cpp:445
std::string currentRequest() const
Current request (first line and headers) in raw form.
Definition: httpserverconnection.h:149
PollState writeData() override
Callback, called when socket is writable.
Definition: httpserverconnection.cpp:125
void bufferPostData()
Definition: httpserverconnection.h:135
std::string label() const
Return the object's log label.
Definition: logger.h:251
uint16_t port() const
Return port number to which the socket is supposed to connect.
Definition: socket.h:37
The purpose of a task is to manage socket connections, and/or to execute timers.
Definition: task.h:39
API for HTTP servers.
Definition: webservertask.h:16
PollState
Definition: pollstate.h:11