22 #include <core/exception.h>
23 #include <netinet/in.h>
24 #include <sys/select.h>
25 #include <sys/types.h>
26 #include <webview/request.h>
29 #include <microhttpd.h>
37 cookie_iterator(
void *cls,
enum MHD_ValueKind kind,
const char *key,
const char *value)
39 WebRequest *request =
static_cast<WebRequest *
>(cls);
40 request->set_cookie(key, value);
45 get_argument_iterator(
void *cls,
enum MHD_ValueKind kind,
const char *key,
const char *value)
47 WebRequest *request =
static_cast<WebRequest *
>(cls);
49 request->set_get_value(key,
"");
51 request->set_get_value(key, value);
56 header_iterator(
void *cls,
enum MHD_ValueKind kind,
const char *key,
const char *value)
58 WebRequest *request =
static_cast<WebRequest *
>(cls);
60 request->set_header(key,
"");
62 request->set_header(key, value);
90 WebRequest::setup(
const char * url,
93 MHD_Connection *connection)
97 if (0 == strcmp(
method, MHD_HTTP_METHOD_GET)) {
99 }
else if (0 == strcmp(
method, MHD_HTTP_METHOD_POST)) {
101 }
else if (0 == strcmp(
method, MHD_HTTP_METHOD_HEAD)) {
103 }
else if (0 == strcmp(
method, MHD_HTTP_METHOD_DELETE)) {
105 }
else if (0 == strcmp(
method, MHD_HTTP_METHOD_PUT)) {
107 }
else if (0 == strcmp(
method, MHD_HTTP_METHOD_CONNECT)) {
109 }
else if (0 == strcmp(
method, MHD_HTTP_METHOD_OPTIONS)) {
111 }
else if (0 == strcmp(
method, MHD_HTTP_METHOD_TRACE)) {
113 }
else if (0 == strcmp(
method, MHD_HTTP_METHOD_PATCH)) {
117 if (0 == strcmp(version, MHD_HTTP_VERSION_1_0)) {
118 http_version_ = HTTP_VERSION_1_0;
119 }
else if (0 == strcmp(version, MHD_HTTP_VERSION_1_1)) {
120 http_version_ = HTTP_VERSION_1_1;
123 MHD_get_connection_values(connection, MHD_HEADER_KIND, &header_iterator,
this);
124 MHD_get_connection_values(connection, MHD_COOKIE_KIND, &cookie_iterator,
this);
125 MHD_get_connection_values(connection, MHD_GET_ARGUMENT_KIND, &get_argument_iterator,
this);
128 if (headers_.find(
"X-Forwarded-For") != headers_.end()) {
129 std::string forwarded_for{headers_[
"X-Forwarded-For"]};
130 std::string::size_type comma_pos = forwarded_for.find(
",");
131 if (comma_pos != std::string::npos) {
132 forwarded_for = forwarded_for.substr(0, comma_pos);
134 client_addr_ = forwarded_for;
138 MHD_get_connection_info(connection, MHD_CONNECTION_INFO_CLIENT_ADDRESS)->client_addr;
140 char addr_str[INET6_ADDRSTRLEN];
151 &(((
struct sockaddr_in6 *)
client_addr)->sin6_addr),
156 default: strncpy(addr_str,
"Unknown AF", INET6_ADDRSTRLEN);
159 client_addr_ = addr_str;
169 MHD_destroy_post_processor(pp_);
182 std::string val_add(data, size);
183 if (post_values_.find(key) != post_values_.end()) {
184 post_values_[key] += val_add;
186 post_values_[key] = val_add;
199 body_ = std::string(data, data_size);
211 body_ += std::string(data, data_size);
220 if (body_.length() == 0)
222 if (body_[body_.length() - 1] != 0) {
233 reply_size_ += increment_by;
260 default:
return "UNKNOWN_METHOD";
270 switch (http_version_) {
271 case HTTP_VERSION_1_0:
return MHD_HTTP_VERSION_1_0;
272 case HTTP_VERSION_1_1:
return MHD_HTTP_VERSION_1_1;
273 default:
return "UNKNOWN_VERSION";