4 #include <condition_variable> 14 #define SERVER_PORT "8080" 21 "abcdefghijklmnopqrstuvwxyz" 24 static inline bool is_base64(
unsigned char c);
36 mg_send_header(conn,
"Access-Control-Allow-Methods",
"GET, POST, PUT, DELETE, PATCH, OPTIONS");
55 mg_send_header(conn,
"Access-Control-Allow-Methods",
"GET, OPTIONS");
68 std::this_thread::sleep_for(std::chrono::milliseconds(10));
80 std::this_thread::sleep_for(std::chrono::seconds(2));
89 for (
auto i = 0;
i < 20; ++
i) {
90 std::this_thread::sleep_for(std::chrono::milliseconds(100));
100 time_t
t = time(NULL) + 5;
101 char expire[100], expire_epoch[100];
102 snprintf(expire_epoch,
sizeof(expire_epoch),
"%lu", static_cast<unsigned long>(t));
103 strftime(expire,
sizeof(expire),
"%a, %d %b %Y %H:%M:%S GMT", gmtime(&t));
116 time_t
t = time(NULL) + 5;
117 char expire[100], expire_epoch[100];
118 snprintf(expire_epoch,
sizeof(expire_epoch),
"%lu", static_cast<unsigned long>(t));
119 strftime(expire,
sizeof(expire),
"%a, %d %b %Y %H:%M:%S GMT", gmtime(&t));
120 std::string v1cookie{
"cookie=\"value with spaces (v1 cookie)\"; expires=\"" +
128 const char* request_cookies;
129 if ((request_cookies =
mg_get_header(conn,
"Cookie")) == NULL)
133 if (cookie_str.find(
"cookie=chocolate;") == cookie_str.npos ||
134 cookie_str.find(
"icecream=vanilla;") == cookie_str.npos) {
146 const char* request_cookies;
147 if ((request_cookies =
mg_get_header(conn,
"Cookie")) == NULL)
151 if (cookie_str.find(
"cookie=\"value with spaces (v1 cookie)\";") == cookie_str.npos) {
164 const char* requested_auth;
166 if ((requested_auth =
mg_get_header(conn,
"Authorization")) == NULL ||
171 auto basic_token = auth_string.find(
' ') + 1;
172 auth_string = auth_string.substr(basic_token, auth_string.length() - basic_token);
174 auto colon = auth_string.find(
':');
175 auto username = auth_string.substr(0, colon);
176 auto password = auth_string.substr(colon + 1, auth_string.length() - colon - 1);
177 if (username ==
"user" &&
password ==
"password") {
188 if ((fp = fopen(
"digest.txt",
"w")) != NULL) {
189 fprintf(fp,
"user:mydomain.com:0cf722ef3dd136b48da83758c5d855f8\n");
196 if ((fp = fopen(
"digest.txt",
"r")) != NULL) {
208 " \"first_key\": \"first_value\",\n" 209 " \"second_key\": \"second_value\"\n" 215 if (raw_header != NULL) {
218 if (!header.empty() && header ==
"application/json") {
234 for (
int i = 0;
i < num_headers; ++
i) {
235 auto name = headers[
i].name;
270 mg_get_var(conn,
"Message", message,
sizeof(message));
287 if (y_string.empty()) {
289 " \"x\": " + x_string +
"\n" 293 std::ostringstream
s;
294 s << (atoi(x) + atoi(y));
296 " \"x\": " + x_string +
",\n" 297 " \"y\": " + y_string +
",\n" 298 " \"sum\": " + s.str() +
"\n" 308 auto has_json_header =
false;
309 for (
int i = 0;
i < num_headers; ++
i) {
312 has_json_header =
true;
315 if (!has_json_header) {
316 auto response =
std::string{
"Unsupported Media Type"};
333 std::map<std::string, std::string> forms;
336 char*
data =
new char[10000];
342 filename,
sizeof(filename),
343 const_cast<const char**>(&data), &data_len);
350 content_len -= read_len;
352 if (strlen(data) == 0) {
362 if (forms.find(
"y") == forms.end()) {
364 " \"x\": " + forms[
"x"] +
"\n" 368 std::ostringstream
s;
369 s << (atoi(forms[
"x"].
data()) + atoi(forms[
"y"].
data()));
371 " \"x\": " + forms[
"x"] +
",\n" 372 " \"y\": " + forms[
"y"] +
",\n" 373 " \"sum\": " + s.str() +
"\n" 383 auto has_json_header =
false;
384 for (
int i = 0;
i < num_headers; ++
i) {
387 has_json_header =
true;
391 if (!has_json_header) {
436 if (y_string.empty()) {
438 " \"x\": " + x_string +
"\n" 442 std::ostringstream
s;
443 s << (atoi(x) + atoi(y));
445 " \"x\": " + x_string +
",\n" 446 " \"y\": " + y_string +
",\n" 447 " \"sum\": " + s.str() +
"\n" 485 if (y_string.empty()) {
487 " \"x\": " + x_string +
"\n" 491 std::ostringstream
s;
492 s << (atoi(x) + atoi(y));
494 " \"x\": " + x_string +
",\n" 495 " \"y\": " + y_string +
",\n" 496 " \"sum\": " + s.str() +
"\n" 527 if (
Url{conn->
uri} ==
"/basic_auth.html") {
529 }
else if (
Url{conn->
uri} ==
"/digest_auth.html") {
534 if (
Url{conn->
uri} ==
"/") {
536 }
else if (
Url{conn->
uri} ==
"/hello.html") {
538 }
else if (
Url{conn->
uri} ==
"/timeout.html") {
540 }
else if (
Url{conn->
uri} ==
"/low_speed.html") {
542 }
else if (
Url{conn->
uri} ==
"/low_speed_bytes.html") {
544 }
else if (
Url{conn->
uri} ==
"/basic_cookies.html") {
546 }
else if (
Url{conn->
uri} ==
"/check_cookies.html") {
548 }
else if (
Url{conn->
uri} ==
"/v1_cookies.html") {
550 }
else if (
Url{conn->
uri} ==
"/check_v1_cookies.html") {
552 }
else if (
Url{conn->
uri} ==
"/basic_auth.html") {
554 }
else if (
Url{conn->
uri} ==
"/digest_auth.html") {
556 }
else if (
Url{conn->
uri} ==
"/basic.json") {
558 }
else if (
Url{conn->
uri} ==
"/header_reflect.html") {
560 }
else if (
Url{conn->
uri} ==
"/temporary_redirect.html") {
562 }
else if (
Url{conn->
uri} ==
"/permanent_redirect.html") {
564 }
else if (
Url{conn->
uri} ==
"/two_redirects.html") {
566 }
else if (
Url{conn->
uri} ==
"/url_post.html") {
568 }
else if (
Url{conn->
uri} ==
"/body_get.html") {
570 }
else if (
Url{conn->
uri} ==
"/json_post.html") {
572 }
else if (
Url{conn->
uri} ==
"/form_post.html") {
574 }
else if (
Url{conn->
uri} ==
"/delete.html") {
576 }
else if (
Url{conn->
uri} ==
"/delete_unallowed.html") {
578 }
else if (
Url{conn->
uri} ==
"/put.html") {
580 }
else if (
Url{conn->
uri} ==
"/put_unallowed.html") {
582 }
else if (
Url{conn->
uri} ==
"/patch.html") {
584 }
else if (
Url{conn->
uri} ==
"/patch_unallowed.html") {
634 return (isalnum(c) || (c ==
'+') || (c ==
'/'));
638 int in_len = encoded_string.size();
642 unsigned char char_array_4[4], char_array_3[3];
645 while (in_len-- && ( encoded_string[in_] !=
'=') &&
is_base64(encoded_string[in_])) {
646 char_array_4[i++] = encoded_string[in_]; in_++;
648 for (i = 0; i <4; i++) {
652 char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
653 char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
654 char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
656 for (i = 0; (i < 3); i++) {
657 ret += char_array_3[
i];
665 for (j = i; j <4; j++) {
669 for (j = 0; j <4; j++) {
673 char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
674 char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
675 char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
677 for (j = 0; (j < i - 1); j++) {
678 ret += char_array_3[
j];
686 return tolower(* reinterpret_cast<const unsigned char *>(s));
695 }
while (diff == 0 && s1[-1] !=
'\0' && --len > 0);
struct mg_connection::mg_header http_headers[30]
int mg_get_var(const struct mg_connection *conn, const char *name, char *dst, size_t dst_len)
void mg_send_header(struct mg_connection *c, const char *name, const char *v)
static int lowercase(const char *s)
static int options(struct mg_connection *conn)
static pthread_mutex_t mutex
static int checkBasicCookies(struct mg_connection *conn)
static int basicAuth(struct mg_connection *conn)
const char * mg_get_header(const struct mg_connection *ri, const char *s)
IMETHOD Vector diff(const Vector &p_w_a, const Vector &p_w_b, double dt=1)
void runServer(struct mg_server *server)
static int mg_strncasecmp(const char *s1, const char *s2, size_t len)
struct mg_server * mg_create_server(void *server_data, mg_handler_t handler)
void mg_send_status(struct mg_connection *c, int status)
TFSIMD_FORCE_INLINE const tfScalar & y() const
static int twoRedirects(struct mg_connection *conn)
geometry_msgs::TransformStamped t
UNITTEST_START int result
static int deleteRequest(struct mg_connection *conn)
static int headerReflect(struct mg_connection *conn)
static int digestAuth(struct mg_connection *conn)
static int jsonPost(struct mg_connection *conn)
time_t mg_poll_server(struct mg_server *server, int milliseconds)
static int patchUnallowed(struct mg_connection *conn)
std::condition_variable server_cv
static int patch(struct mg_connection *conn)
static int putUnallowed(struct mg_connection *conn)
static int formPost(struct mg_connection *conn)
UNITTEST_START struct Curl_easy data
static struct mg_server * server
static int basicJson(struct mg_connection *conn)
static const std::string base64_chars
TFSIMD_FORCE_INLINE const tfScalar & x() const
static int lowSpeedBytes(struct mg_connection *conn)
std::mutex shutdown_mutex
int mg_authorize_digest(struct mg_connection *c, FILE *fp)
static int put(struct mg_connection *conn)
static int lowSpeed(struct mg_connection *conn)
static int urlPost(struct mg_connection *conn)
static int bodyGet(struct mg_connection *conn)
const char * mg_set_option(struct mg_server *server, const char *name, const char *value)
UNITTEST_START int * value
std::string base64_decode(std::string const &encoded_string)
static int basicCookies(struct mg_connection *conn)
static int v1Cookies(struct mg_connection *conn)
static int deleteUnallowedRequest(struct mg_connection *conn)
static int hello(struct mg_connection *conn)
static int evHandler(struct mg_connection *conn, enum mg_event ev)
static int timeout(struct mg_connection *conn)
const char * request_method
int mg_parse_multipart(const char *buf, int buf_len, char *var_name, int var_name_len, char *file_name, int file_name_len, const char **data, int *data_len)
static int temporaryRedirect(struct mg_connection *conn)
void mg_destroy_server(struct mg_server **server)
static int checkV1Cookies(struct mg_connection *conn)
static bool is_base64(unsigned char c)
size_t mg_send_data(struct mg_connection *c, const void *data, int data_len)
static int permanentRedirect(struct mg_connection *conn)