PageRenderTime 29ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
C | 144 lines | 111 code | 13 blank | 20 comment | 43 complexity | 1875d54b0f3afabf3ecae4500501177a MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. /*
  2. http-server-charset.c - HTTP server that outputs different charset values
  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. break;
  62. case 1:
  63. if (byte == '\n') {
  64. state = 2;
  65. }
  66. else if (byte == '\r') {
  67. state = 1;
  68. }
  69. else {
  70. state = 0;
  71. }
  72. break;
  73. }
  74. }
  75. }
  76. char * method;
  77. char * url;
  78. char * request_line = (char *) stream->data;
  79. char * first_space = strchr(request_line, ' ');
  80. assert(first_space != NULL);
  81. char * second_space = strchr(first_space + 1, ' ');
  82. assert(second_space != NULL);
  83. method = xstrndup(request_line, first_space - request_line);
  84. url = xstrndup(first_space + 1, second_space - (first_space + 1));
  85. /* send response */
  86. char * message;
  87. if (strcmp(url, "http://127.0.0.1:8000/utf-8.js") == 0 || strcmp(url, "/utf-8.js") == 0) {
  88. message = "HTTP/1.1 200 OK\r\n"
  89. "Connection: close\r\n"
  90. "Content-type: text/javascript; charset=UTF-8\r\n"
  91. "\r\n"
  92. "var s = 'e?¨?Š??';\n"
  93. "var r = /e?¨?Š??/;\n";
  94. }
  95. else if (strcmp(url, "http://127.0.0.1:8000/iso-8859-1.js") == 0 || strcmp(url, "/iso-8859-1.js") == 0) {
  96. message = "HTTP/1.1 200 OK\r\n"
  97. "Connection: close\r\n"
  98. "Content-type: text/javascript; charset=ISO-8859-1\r\n"
  99. "\r\n"
  100. "var s = 'e???';\n"
  101. "var r = /e???/;\n";
  102. }
  103. else if (strcmp(url, "http://127.0.0.1:8000/bogus.js") == 0 || strcmp(url, "/bogus.js") == 0) {
  104. message = "HTTP/1.1 200 OK\r\n"
  105. "Connection: close\r\n"
  106. "Content-type: text/javascript; charset=BOGUS\r\n"
  107. "\r\n"
  108. "var s = 'e???';\n"
  109. "var r = /e???/;\n";
  110. }
  111. else if (strcmp(url, "http://127.0.0.1:8000/malformed.js") == 0 || strcmp(url, "/malformed.js") == 0) {
  112. message = "HTTP/1.1 200 OK\r\n"
  113. "Connection: close\r\n"
  114. "Content-type: text/javascript; charset=UTF-8\r\n"
  115. "\r\n"
  116. "var s = 'e???';\n"
  117. "var r = /e???/;\n";
  118. }
  119. else {
  120. abort();
  121. }
  122. size_t message_length = strlen(message);
  123. ssize_t bytes_sent = send(client_socket, message, message_length, 0);
  124. assert(bytes_sent == (ssize_t) message_length);
  125. closesocket(client_socket);
  126. }
  127. return 0;
  128. }