22 #include "microhttpd_compat.h"
24 #include <core/exception.h>
25 #include <netinet/in.h>
26 #include <sys/select.h>
27 #include <sys/types.h>
28 #include <webview/request.h>
38 cookie_iterator(
void *cls,
enum MHD_ValueKind kind,
const char *key,
const char *value)
40 WebRequest *request =
static_cast<WebRequest *
>(cls);
41 request->set_cookie(key, value);
46 get_argument_iterator(
void *cls,
enum MHD_ValueKind kind,
const char *key,
const char *value)
48 WebRequest *request =
static_cast<WebRequest *
>(cls);
50 request->set_get_value(key,
"");
52 request->set_get_value(key, value);
57 header_iterator(
void *cls,
enum MHD_ValueKind kind,
const char *key,
const char *value)
59 WebRequest *request =
static_cast<WebRequest *
>(cls);
61 request->set_header(key,
"");
63 request->set_header(key, value);
91 WebRequest::setup(
const char * url,
94 MHD_Connection *connection)
98 if (0 == strcmp(
method, MHD_HTTP_METHOD_GET)) {
100 }
else if (0 == strcmp(
method, MHD_HTTP_METHOD_POST)) {
102 }
else if (0 == strcmp(
method, MHD_HTTP_METHOD_HEAD)) {
104 }
else if (0 == strcmp(
method, MHD_HTTP_METHOD_DELETE)) {
106 }
else if (0 == strcmp(
method, MHD_HTTP_METHOD_PUT)) {
108 }
else if (0 == strcmp(
method, MHD_HTTP_METHOD_CONNECT)) {
110 }
else if (0 == strcmp(
method, MHD_HTTP_METHOD_OPTIONS)) {
112 }
else if (0 == strcmp(
method, MHD_HTTP_METHOD_TRACE)) {
114 }
else if (0 == strcmp(
method, MHD_HTTP_METHOD_PATCH)) {
118 if (0 == strcmp(version, MHD_HTTP_VERSION_1_0)) {
119 http_version_ = HTTP_VERSION_1_0;
120 }
else if (0 == strcmp(version, MHD_HTTP_VERSION_1_1)) {
121 http_version_ = HTTP_VERSION_1_1;
124 MHD_get_connection_values(connection, MHD_HEADER_KIND, &header_iterator,
this);
125 MHD_get_connection_values(connection, MHD_COOKIE_KIND, &cookie_iterator,
this);
126 MHD_get_connection_values(connection, MHD_GET_ARGUMENT_KIND, &get_argument_iterator,
this);
129 if (headers_.find(
"X-Forwarded-For") != headers_.end()) {
130 std::string forwarded_for{headers_[
"X-Forwarded-For"]};
131 std::string::size_type comma_pos = forwarded_for.find(
",");
132 if (comma_pos != std::string::npos) {
133 forwarded_for = forwarded_for.substr(0, comma_pos);
135 client_addr_ = forwarded_for;
139 MHD_get_connection_info(connection, MHD_CONNECTION_INFO_CLIENT_ADDRESS)->client_addr;
141 char addr_str[INET6_ADDRSTRLEN];
152 &(((
struct sockaddr_in6 *)
client_addr)->sin6_addr),
157 default: strncpy(addr_str,
"Unknown AF", INET6_ADDRSTRLEN);
160 client_addr_ = addr_str;
170 MHD_destroy_post_processor(pp_);
183 std::string val_add(data, size);
184 if (post_values_.find(key) != post_values_.end()) {
185 post_values_[key] += val_add;
187 post_values_[key] = val_add;
200 body_ = std::string(data, data_size);
212 body_ += std::string(data, data_size);
221 if (body_.length() == 0)
223 if (body_[body_.length() - 1] != 0) {
234 reply_size_ += increment_by;
261 default:
return "UNKNOWN_METHOD";
271 switch (http_version_) {
272 case HTTP_VERSION_1_0:
return MHD_HTTP_VERSION_1_0;
273 case HTTP_VERSION_1_1:
return MHD_HTTP_VERSION_1_1;
274 default:
return "UNKNOWN_VERSION";