PageRenderTime 30ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
C | 97 lines | 65 code | 12 blank | 20 comment | 22 complexity | 00e88f0e03d1be60e734ed594692c11f 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-bad-headers.c - HTTP server that outputs bad headers
  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. int main(void) {
  21. #ifdef __MINGW32__
  22. WSADATA data;
  23. if (WSAStartup(MAKEWORD(1, 1), &data) != 0) {
  24. return 1;
  25. }
  26. #endif
  27. SOCKET s = socket(PF_INET, SOCK_STREAM, 0);
  28. assert(s != INVALID_SOCKET);
  29. int optval = 1;
  30. setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (const char *) &optval, sizeof(optval));
  31. struct sockaddr_in a;
  32. a.sin_family = AF_INET;
  33. a.sin_port = htons(8000);
  34. a.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  35. int result = bind(s, (struct sockaddr *) &a, sizeof(a));
  36. assert(result == 0);
  37. result = listen(s, 5);
  38. assert(result == 0);
  39. for (;;) {
  40. struct sockaddr_in client_address;
  41. size_t size = sizeof(client_address);
  42. int client_socket = accept(s, (struct sockaddr *) &client_address, &size);
  43. assert(client_socket > 0);
  44. /* read request */
  45. int state = 0;
  46. while (state != 2) {
  47. uint8_t buffer[8192];
  48. ssize_t bytes_read = recv(client_socket, buffer, 8192, 0);
  49. assert(bytes_read > 0);
  50. for (int i = 0; i < bytes_read && state != 2; i++) {
  51. uint8_t byte = buffer[i];
  52. switch (state) {
  53. case 0:
  54. if (byte == '\n') {
  55. state = 1;
  56. }
  57. else {
  58. state = 0;
  59. }
  60. break;
  61. case 1:
  62. if (byte == '\n') {
  63. state = 2;
  64. }
  65. else if (byte == '\r') {
  66. state = 1;
  67. }
  68. else {
  69. state = 0;
  70. }
  71. }
  72. }
  73. }
  74. /* send response */
  75. char * message = "HTTP/1.1 200 OK\r\nHere is some bogosity!\r\nConnection: close\r\nContent-type: text/html\r\n\r\nHello\n";
  76. size_t message_length = strlen(message);
  77. ssize_t bytes_sent = send(client_socket, message, message_length, 0);
  78. assert(bytes_sent == (ssize_t) message_length);
  79. closesocket(client_socket);
  80. }
  81. return 0;
  82. }