embed.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // Copyright (c) 2004-2009 Sergey Lyubka
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. // THE SOFTWARE.
  20. //
  21. // Unit test for the mongoose web server. Tests embedded API.
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <string.h>
  25. #ifndef _WIN32
  26. #include <unistd.h>
  27. #endif
  28. #include "mongoose.h"
  29. #if !defined(LISTENING_PORT)
  30. #define LISTENING_PORT "23456"
  31. #endif
  32. static const char *standard_reply = "HTTP/1.1 200 OK\r\n"
  33. "Content-Type: text/plain\r\n"
  34. "Connection: close\r\n\r\n";
  35. static void test_get_var(struct mg_connection *conn,
  36. const struct mg_request_info *ri) {
  37. char *var, *buf;
  38. size_t buf_len;
  39. const char *cl;
  40. int var_len;
  41. mg_printf(conn, "%s", standard_reply);
  42. buf_len = 0;
  43. var = buf = NULL;
  44. cl = mg_get_header(conn, "Content-Length");
  45. mg_printf(conn, "cl: %p\n", cl);
  46. printf("reqeust method = %s\n", ri->request_method);
  47. if ((!strcmp(ri->request_method, "POST") || !strcmp(ri->request_method, "PUT"))
  48. && cl != NULL) {
  49. buf_len = atoi(cl);
  50. buf = malloc(buf_len);
  51. mg_read(conn, buf, buf_len);
  52. } else if (ri->query_string != NULL) {
  53. buf_len = strlen(ri->query_string);
  54. buf = malloc(buf_len + 1);
  55. strcpy(buf, ri->query_string);
  56. }
  57. var = malloc(buf_len + 1);
  58. var_len = mg_get_var(buf, buf_len, "my_var", var, buf_len + 1);
  59. mg_printf(conn, "Value: [%s]\n", var);
  60. mg_printf(conn, "Value size: [%d]\n", var_len);
  61. free(buf);
  62. free(var);
  63. }
  64. static void test_get_header(struct mg_connection *conn,
  65. const struct mg_request_info *ri) {
  66. const char *value;
  67. int i;
  68. mg_printf(conn, "%s", standard_reply);
  69. printf("HTTP headers: %d\n", ri->num_headers);
  70. for (i = 0; i < ri->num_headers; i++) {
  71. printf("[%s]: [%s]\n", ri->http_headers[i].name, ri->http_headers[i].value);
  72. }
  73. value = mg_get_header(conn, "Host");
  74. if (value != NULL) {
  75. mg_printf(conn, "Value: [%s]", value);
  76. }
  77. }
  78. static void test_get_request_info(struct mg_connection *conn,
  79. const struct mg_request_info *ri) {
  80. int i;
  81. mg_printf(conn, "%s", standard_reply);
  82. mg_printf(conn, "Method: [%s]\n", ri->request_method);
  83. mg_printf(conn, "URI: [%s]\n", ri->uri);
  84. mg_printf(conn, "HTTP version: [%s]\n", ri->http_version);
  85. for (i = 0; i < ri->num_headers; i++) {
  86. mg_printf(conn, "HTTP header [%s]: [%s]\n",
  87. ri->http_headers[i].name,
  88. ri->http_headers[i].value);
  89. }
  90. mg_printf(conn, "Query string: [%s]\n",
  91. ri->query_string ? ri->query_string: "");
  92. mg_printf(conn, "Remote IP: [%lu]\n", ri->remote_ip);
  93. mg_printf(conn, "Remote port: [%d]\n", ri->remote_port);
  94. mg_printf(conn, "Remote user: [%s]\n",
  95. ri->remote_user ? ri->remote_user : "");
  96. }
  97. static void test_error(struct mg_connection *conn,
  98. const struct mg_request_info *ri) {
  99. mg_printf(conn, "HTTP/1.1 %d XX\r\n"
  100. "Conntection: close\r\n\r\n", ri->status_code);
  101. mg_printf(conn, "Error: [%d]", ri->status_code);
  102. }
  103. static void test_post(struct mg_connection *conn,
  104. const struct mg_request_info *ri) {
  105. const char *cl;
  106. char *buf;
  107. int len;
  108. mg_printf(conn, "%s", standard_reply);
  109. if (strcmp(ri->request_method, "POST") == 0 &&
  110. (cl = mg_get_header(conn, "Content-Length")) != NULL) {
  111. len = atoi(cl);
  112. if ((buf = malloc(len)) != NULL) {
  113. mg_write(conn, buf, len);
  114. free(buf);
  115. }
  116. }
  117. }
  118. static const struct test_config {
  119. enum mg_event event;
  120. const char *uri;
  121. void (*func)(struct mg_connection *, const struct mg_request_info *);
  122. } test_config[] = {
  123. {MG_NEW_REQUEST, "/test_get_header", &test_get_header},
  124. {MG_NEW_REQUEST, "/test_get_var", &test_get_var},
  125. {MG_NEW_REQUEST, "/test_get_request_info", &test_get_request_info},
  126. {MG_NEW_REQUEST, "/test_post", &test_post},
  127. {MG_HTTP_ERROR, "", &test_error},
  128. {0, NULL, NULL}
  129. };
  130. static void *callback(enum mg_event event,
  131. struct mg_connection *conn,
  132. const struct mg_request_info *request_info) {
  133. int i;
  134. for (i = 0; test_config[i].uri != NULL; i++) {
  135. if (event == test_config[i].event &&
  136. (event == MG_HTTP_ERROR ||
  137. !strcmp(request_info->uri, test_config[i].uri))) {
  138. test_config[i].func(conn, request_info);
  139. return "processed";
  140. }
  141. }
  142. return NULL;
  143. }
  144. int main(void) {
  145. struct mg_context *ctx;
  146. const char *options[] = {"listening_ports", LISTENING_PORT, NULL};
  147. ctx = mg_start(callback, NULL, options);
  148. pause();
  149. return 0;
  150. }