embed.c 5.6 KB

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