PageRenderTime 434ms CodeModel.GetById 181ms app.highlight 7ms RepoModel.GetById 244ms app.codeStats 0ms

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

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
C | 97 lines | 65 code | 12 blank | 20 comment | 22 complexity | 08257cc49eae94cf18ce53f2c34ea02e MD5 | raw file
 1/*
 2    http-server-bad-body.c - HTTP server that outputs a bad body
 3    Copyright (C) 2008 siliconforks.com
 4
 5    This program is free software; you can redistribute it and/or modify
 6    it under the terms of the GNU General Public License as published by
 7    the Free Software Foundation; either version 2 of the License, or
 8    (at your option) any later version.
 9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License along
16    with this program; if not, write to the Free Software Foundation, Inc.,
17    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18*/
19
20#include <config.h>
21
22#include <assert.h>
23#include <string.h>
24
25#include "http-server.h"
26
27int main(void) {
28#ifdef __MINGW32__
29  WSADATA data;
30  if (WSAStartup(MAKEWORD(1, 1), &data) != 0) {
31    return 1;
32  }
33#endif
34
35  SOCKET s = socket(PF_INET, SOCK_STREAM, 0);
36  assert(s != INVALID_SOCKET);
37
38  int optval = 1;
39  setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (const char *) &optval, sizeof(optval));
40
41  struct sockaddr_in a;
42  a.sin_family = AF_INET;
43  a.sin_port = htons(8000);
44  a.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
45  int result = bind(s, (struct sockaddr *) &a, sizeof(a));
46  assert(result == 0);
47
48  result = listen(s, 5);
49  assert(result == 0);
50
51  for (;;) {
52    struct sockaddr_in client_address;
53    size_t size = sizeof(client_address);
54    int client_socket = accept(s, (struct sockaddr *) &client_address, &size);
55    assert(client_socket > 0);
56
57    /* read request */
58    int state = 0;
59    while (state != 2) {
60      uint8_t buffer[8192];
61      ssize_t bytes_read = recv(client_socket, buffer, 8192, 0);
62      assert(bytes_read > 0);
63      for (int i = 0; i < bytes_read && state != 2; i++) {
64        uint8_t byte = buffer[i];
65        switch (state) {
66        case 0:
67          if (byte == '\n') {
68            state = 1;
69          }
70          else {
71            state = 0;
72          }
73          break;
74        case 1:
75          if (byte == '\n') {
76            state = 2;
77          }
78          else if (byte == '\r') {
79            state = 1;
80          }
81          else {
82            state = 0;
83          }
84        }
85      }
86    }
87
88    /* send response */
89    char * message = "HTTP/1.1 200 OK\r\nConnection: close\r\nContent-type: text/html\r\nTransfer-Encoding: chunked\r\n\r\nHello\n";
90    size_t message_length = strlen(message);
91    ssize_t bytes_sent = send(client_socket, message, message_length, 0);
92    assert(bytes_sent == (ssize_t) message_length);
93
94    closesocket(client_socket);
95  }
96  return 0;
97}