/js/lib/Socket.IO-node/support/expresso/deps/jscoverage/tests/http-server-chunked.c

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs · C · 196 lines · 163 code · 13 blank · 20 comment · 50 complexity · 1981363caefe226652cb94e1485b5ba6 MD5 · raw file

  1. /*
  2. http-server-chunked.c - HTTP server that outputs chunked response
  3. Copyright (C) 2008 siliconforks.com
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include <config.h>
  17. #include <assert.h>
  18. #include <string.h>
  19. #include "http-server.h"
  20. #include "stream.h"
  21. #include "util.h"
  22. int main(void) {
  23. #ifdef __MINGW32__
  24. WSADATA data;
  25. if (WSAStartup(MAKEWORD(1, 1), &data) != 0) {
  26. return 1;
  27. }
  28. #endif
  29. SOCKET s = socket(PF_INET, SOCK_STREAM, 0);
  30. assert(s != INVALID_SOCKET);
  31. int optval = 1;
  32. setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (const char *) &optval, sizeof(optval));
  33. struct sockaddr_in a;
  34. a.sin_family = AF_INET;
  35. a.sin_port = htons(8000);
  36. a.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  37. int result = bind(s, (struct sockaddr *) &a, sizeof(a));
  38. assert(result == 0);
  39. result = listen(s, 5);
  40. assert(result == 0);
  41. for (;;) {
  42. struct sockaddr_in client_address;
  43. size_t size = sizeof(client_address);
  44. int client_socket = accept(s, (struct sockaddr *) &client_address, &size);
  45. assert(client_socket > 0);
  46. /* read request */
  47. Stream * stream = Stream_new(0);
  48. int state = 0;
  49. while (state != 2) {
  50. uint8_t buffer[8192];
  51. ssize_t bytes_read = recv(client_socket, buffer, 8192, 0);
  52. assert(bytes_read > 0);
  53. Stream_write(stream, buffer, bytes_read);
  54. for (int i = 0; i < bytes_read && state != 2; i++) {
  55. uint8_t byte = buffer[i];
  56. switch (state) {
  57. case 0:
  58. if (byte == '\n') {
  59. state = 1;
  60. }
  61. else {
  62. state = 0;
  63. }
  64. break;
  65. case 1:
  66. if (byte == '\n') {
  67. state = 2;
  68. }
  69. else if (byte == '\r') {
  70. state = 1;
  71. }
  72. else {
  73. state = 0;
  74. }
  75. break;
  76. }
  77. }
  78. }
  79. char * method;
  80. char * url;
  81. char * request_line = (char *) stream->data;
  82. char * first_space = strchr(request_line, ' ');
  83. assert(first_space != NULL);
  84. char * second_space = strchr(first_space + 1, ' ');
  85. assert(second_space != NULL);
  86. method = xstrndup(request_line, first_space - request_line);
  87. url = xstrndup(first_space + 1, second_space - (first_space + 1));
  88. /* send response */
  89. char * message;
  90. if (strcmp(url, "http://127.0.0.1:8000/lower") == 0 || strcmp(url, "/lower") == 0) {
  91. message = "HTTP/1.1 200 OK\r\n"
  92. "Connection: close\r\n"
  93. "Content-type: text/html\r\n"
  94. "Transfer-Encoding: chunked\r\n"
  95. "\r\n"
  96. "b\r\n"
  97. "hello world\r\n"
  98. "1\r\n"
  99. "\n\r\n"
  100. "0\r\n"
  101. "\r\n";
  102. }
  103. else if (strcmp(url, "http://127.0.0.1:8000/upper") == 0 || strcmp(url, "/upper") == 0) {
  104. message = "HTTP/1.1 200 OK\r\n"
  105. "Connection: close\r\n"
  106. "Content-type: text/html\r\n"
  107. "Transfer-Encoding: chunked\r\n"
  108. "\r\n"
  109. "B\r\n"
  110. "HELLO WORLD\r\n"
  111. "1\r\n"
  112. "\n\r\n"
  113. "0\r\n"
  114. "\r\n";
  115. }
  116. else if (strcmp(url, "http://127.0.0.1:8000/javascript") == 0 || strcmp(url, "/javascript") == 0) {
  117. message = "HTTP/1.1 200 OK\r\n"
  118. "Connection: close\r\n"
  119. "Content-Type: text/javascript\r\n"
  120. "Transfer-Encoding: chunked\r\n"
  121. "\r\n"
  122. "B\r\n"
  123. "hello = 10;\r\n"
  124. "1\r\n"
  125. "\n\r\n"
  126. "0\r\n"
  127. "\r\n";
  128. }
  129. else if (strcmp(url, "http://127.0.0.1:8000/trailer") == 0 || strcmp(url, "/trailer") == 0) {
  130. message = "HTTP/1.1 200 OK\r\n"
  131. "Connection: close\r\n"
  132. "Content-type: text/html\r\n"
  133. "Transfer-Encoding: chunked\r\n"
  134. "\r\n"
  135. "b\r\n"
  136. "hello world\r\n"
  137. "1\r\n"
  138. "\n\r\n"
  139. "0\r\n"
  140. "X-Foo: bar\r\n"
  141. "X-Bar: foo\r\n"
  142. "\r\n";
  143. }
  144. else if (strcmp(url, "http://127.0.0.1:8000/overflow") == 0) {
  145. message = "HTTP/1.1 200 OK\r\n"
  146. "Connection: close\r\n"
  147. "Content-type: text/html\r\n"
  148. "Transfer-Encoding: chunked\r\n"
  149. "\r\n"
  150. "100000000\r\n"
  151. "hello world\r\n"
  152. "1\r\n"
  153. "\n\r\n"
  154. "0\r\n"
  155. "\r\n";
  156. }
  157. else if (strcmp(url, "http://127.0.0.1:8000/multiple") == 0) {
  158. message = "HTTP/1.1 200 OK\r\n"
  159. "Connection: close\r\n"
  160. "Content-type: text/html\r\n"
  161. "Transfer-Encoding: foo; foo = bar, bar; foo = \"bar\"\r\n"
  162. "Transfer-Encoding: foobar; foo = \"\\\"bar\\\"\", chunked\r\n"
  163. "\r\n"
  164. "b\r\n"
  165. "hello world\r\n"
  166. "1\r\n"
  167. "\n\r\n"
  168. "0\r\n"
  169. "\r\n";
  170. }
  171. else {
  172. abort();
  173. }
  174. size_t message_length = strlen(message);
  175. ssize_t bytes_sent = send(client_socket, message, message_length, 0);
  176. assert(bytes_sent == (ssize_t) message_length);
  177. closesocket(client_socket);
  178. }
  179. return 0;
  180. }