embed.c 5.4 KB

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