embed.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * Copyright (c) 2004-2009 Sergey Lyubka
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. * THE SOFTWARE.
  21. *
  22. * $Id: embed.c 471 2009-08-30 14:30:21Z valenok $
  23. * Unit test for the mongoose web server. Tests embedded API.
  24. */
  25. #include <stdlib.h>
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include "mongoose.h"
  29. #if !defined(LISTENING_PORT)
  30. #define LISTENING_PORT "23456"
  31. #endif /* !LISTENING_PORT */
  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
  36. test_get_var(struct mg_connection *conn, const struct mg_request_info *ri,
  37. void *user_data)
  38. {
  39. char *value;
  40. mg_printf(conn, "%s", standard_reply);
  41. value = mg_get_var(conn, "my_var");
  42. if (value != NULL) {
  43. mg_printf(conn, "Value: [%s]\n", value);
  44. mg_printf(conn, "Value size: [%u]\n", (unsigned) strlen(value));
  45. free(value);
  46. }
  47. }
  48. static void
  49. test_get_header(struct mg_connection *conn, const struct mg_request_info *ri,
  50. void *user_data)
  51. {
  52. const char *value;
  53. mg_printf(conn, "%s", standard_reply);
  54. {
  55. int i;
  56. printf("HTTP headers: %d\n", ri->num_headers);
  57. for (i = 0; i < ri->num_headers; i++)
  58. printf("[%s]: [%s]\n",
  59. ri->http_headers[i].name,
  60. ri->http_headers[i].value);
  61. }
  62. value = mg_get_header(conn, "Host");
  63. if (value != NULL)
  64. mg_printf(conn, "Value: [%s]", value);
  65. }
  66. static void
  67. test_get_ri(struct mg_connection *conn, const struct mg_request_info *ri,
  68. void *user_data)
  69. {
  70. int i;
  71. mg_printf(conn, "%s", standard_reply);
  72. mg_printf(conn, "Method: [%s]\n", ri->request_method);
  73. mg_printf(conn, "URI: [%s]\n", ri->uri);
  74. mg_printf(conn, "HTTP version: [%s]\n", ri->http_version);
  75. for (i = 0; i < ri->num_headers; i++)
  76. mg_printf(conn, "HTTP header [%s]: [%s]\n",
  77. ri->http_headers[i].name,
  78. ri->http_headers[i].value);
  79. mg_printf(conn, "Query string: [%s]\n",
  80. ri->query_string ? ri->query_string: "");
  81. mg_printf(conn, "POST data: [%.*s]\n",
  82. ri->post_data_len, ri->post_data);
  83. mg_printf(conn, "Remote IP: [%lu]\n", ri->remote_ip);
  84. mg_printf(conn, "Remote port: [%d]\n", ri->remote_port);
  85. mg_printf(conn, "Remote user: [%s]\n",
  86. ri->remote_user ? ri->remote_user : "");
  87. }
  88. static void
  89. test_error(struct mg_connection *conn, const struct mg_request_info *ri,
  90. void *user_data)
  91. {
  92. const char *value;
  93. mg_printf(conn, "HTTP/1.1 %d XX\r\n"
  94. "Conntection: close\r\n\r\n", ri->status_code);
  95. mg_printf(conn, "Error: [%d]", ri->status_code);
  96. }
  97. static void
  98. test_user_data(struct mg_connection *conn, const struct mg_request_info *ri,
  99. void *user_data)
  100. {
  101. const char *value;
  102. mg_printf(conn, "%s", standard_reply);
  103. mg_printf(conn, "User data: [%d]", * (int *) user_data);
  104. }
  105. static void
  106. test_protect(struct mg_connection *conn, const struct mg_request_info *ri,
  107. void *user_data)
  108. {
  109. const char *allowed_user = * (char **) user_data;
  110. const char *remote_user = ri->remote_user;
  111. int allowed;
  112. allowed = remote_user != NULL && !strcmp(allowed_user, remote_user);
  113. * (long *) user_data = allowed ? 1 : 0;
  114. }
  115. static void
  116. test_post(struct mg_connection *conn, const struct mg_request_info *ri,
  117. void *user_data)
  118. {
  119. mg_printf(conn, "%s", standard_reply);
  120. mg_write(conn, ri->post_data, ri->post_data_len);
  121. }
  122. static void
  123. test_put(struct mg_connection *conn, const struct mg_request_info *ri,
  124. void *user_data)
  125. {
  126. mg_printf(conn, "%s", standard_reply);
  127. mg_write(conn, ri->post_data, ri->post_data_len);
  128. }
  129. static void
  130. test_remove_callback(struct mg_connection *conn,
  131. const struct mg_request_info *ri, void *user_data)
  132. {
  133. struct mg_context *ctx = (struct mg_context *) user_data;
  134. const char *uri_regex = "/foo/*";
  135. mg_printf(conn, "%sRemoving callbacks bound to [%s]",
  136. standard_reply, uri_regex);
  137. /* Un-bind bound callback */
  138. mg_set_uri_callback(ctx, uri_regex, NULL, NULL);
  139. }
  140. int main(void)
  141. {
  142. int user_data = 1234;
  143. struct mg_context *ctx;
  144. ctx = mg_start();
  145. mg_set_option(ctx, "ports", LISTENING_PORT);
  146. mg_set_uri_callback(ctx, "/test_get_header", &test_get_header, NULL);
  147. mg_set_uri_callback(ctx, "/test_get_var", &test_get_var, NULL);
  148. mg_set_uri_callback(ctx, "/test_get_request_info", &test_get_ri, NULL);
  149. mg_set_uri_callback(ctx, "/foo/*", &test_get_ri, NULL);
  150. mg_set_uri_callback(ctx, "/test_user_data",
  151. &test_user_data, &user_data);
  152. mg_set_uri_callback(ctx, "/p", &test_post, NULL);
  153. mg_set_uri_callback(ctx, "/put", &test_put, NULL);
  154. mg_set_uri_callback(ctx, "/test_remove_callback",
  155. &test_remove_callback, ctx);
  156. mg_set_error_callback(ctx, 404, &test_error, NULL);
  157. mg_set_error_callback(ctx, 0, &test_error, NULL);
  158. mg_set_auth_callback(ctx, "/foo/secret", &test_protect, (void *) "joe");
  159. for (;;)
  160. (void) getchar();
  161. }