embed.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. if (!strcmp(ri->request_method, "POST") && cl != NULL) {
  47. buf_len = atoi(cl);
  48. buf = malloc(buf_len);
  49. /* Read in two pieces, to test continuation */
  50. if (buf_len > 2) {
  51. mg_read(conn, buf, 2);
  52. mg_read(conn, buf + 2, buf_len - 2);
  53. } else {
  54. mg_read(conn, buf, buf_len);
  55. }
  56. } else if (ri->query_string != NULL) {
  57. buf_len = strlen(ri->query_string);
  58. buf = malloc(buf_len + 1);
  59. strcpy(buf, ri->query_string);
  60. }
  61. var = malloc(buf_len + 1);
  62. var_len = mg_get_var(buf, buf_len, "my_var", var, buf_len + 1);
  63. mg_printf(conn, "Value: [%s]\n", var);
  64. mg_printf(conn, "Value size: [%d]\n", var_len);
  65. free(buf);
  66. free(var);
  67. }
  68. static void test_get_header(struct mg_connection *conn,
  69. const struct mg_request_info *ri) {
  70. const char *value;
  71. int i;
  72. mg_printf(conn, "%s", standard_reply);
  73. printf("HTTP headers: %d\n", ri->num_headers);
  74. for (i = 0; i < ri->num_headers; i++) {
  75. printf("[%s]: [%s]\n", ri->http_headers[i].name, ri->http_headers[i].value);
  76. }
  77. value = mg_get_header(conn, "Host");
  78. if (value != NULL) {
  79. mg_printf(conn, "Value: [%s]", value);
  80. }
  81. }
  82. static void test_get_request_info(struct mg_connection *conn,
  83. const struct mg_request_info *ri) {
  84. int i;
  85. mg_printf(conn, "%s", standard_reply);
  86. mg_printf(conn, "Method: [%s]\n", ri->request_method);
  87. mg_printf(conn, "URI: [%s]\n", ri->uri);
  88. mg_printf(conn, "HTTP version: [%s]\n", ri->http_version);
  89. for (i = 0; i < ri->num_headers; i++) {
  90. mg_printf(conn, "HTTP header [%s]: [%s]\n",
  91. ri->http_headers[i].name,
  92. ri->http_headers[i].value);
  93. }
  94. mg_printf(conn, "Query string: [%s]\n",
  95. ri->query_string ? ri->query_string: "");
  96. mg_printf(conn, "Remote IP: [%lu]\n", ri->remote_ip);
  97. mg_printf(conn, "Remote port: [%d]\n", ri->remote_port);
  98. mg_printf(conn, "Remote user: [%s]\n",
  99. ri->remote_user ? ri->remote_user : "");
  100. }
  101. static void test_error(struct mg_connection *conn,
  102. const struct mg_request_info *ri) {
  103. mg_printf(conn, "HTTP/1.1 %d XX\r\n"
  104. "Conntection: close\r\n\r\n", ri->status_code);
  105. mg_printf(conn, "Error: [%d]", ri->status_code);
  106. }
  107. static void test_post(struct mg_connection *conn,
  108. const struct mg_request_info *ri) {
  109. const char *cl;
  110. char *buf;
  111. int len;
  112. mg_printf(conn, "%s", standard_reply);
  113. if (strcmp(ri->request_method, "POST") == 0 &&
  114. (cl = mg_get_header(conn, "Content-Length")) != NULL) {
  115. len = atoi(cl);
  116. if ((buf = malloc(len)) != NULL) {
  117. mg_write(conn, buf, len);
  118. free(buf);
  119. }
  120. }
  121. }
  122. static const struct test_config {
  123. enum mg_event event;
  124. const char *uri;
  125. void (*func)(struct mg_connection *, const struct mg_request_info *);
  126. } test_config[] = {
  127. {MG_NEW_REQUEST, "/test_get_header", &test_get_header},
  128. {MG_NEW_REQUEST, "/test_get_var", &test_get_var},
  129. {MG_NEW_REQUEST, "/test_get_request_info", &test_get_request_info},
  130. {MG_NEW_REQUEST, "/test_post", &test_post},
  131. {MG_HTTP_ERROR, "", &test_error},
  132. {0, NULL, NULL}
  133. };
  134. static void *callback(enum mg_event event,
  135. struct mg_connection *conn,
  136. const struct mg_request_info *request_info) {
  137. int i;
  138. for (i = 0; test_config[i].uri != NULL; i++) {
  139. if (event == test_config[i].event &&
  140. (event == MG_HTTP_ERROR ||
  141. !strcmp(request_info->uri, test_config[i].uri))) {
  142. test_config[i].func(conn, request_info);
  143. return "processed";
  144. }
  145. }
  146. return NULL;
  147. }
  148. int main(void) {
  149. struct mg_context *ctx;
  150. const char *options[] = {"listening_ports", LISTENING_PORT, NULL};
  151. ctx = mg_start(callback, NULL, options);
  152. pause();
  153. return 0;
  154. }