/js/lib/Socket.IO-node/support/expresso/deps/jscoverage/tests/http-client-bad-url.c
C | 77 lines | 44 code | 13 blank | 20 comment | 9 complexity | 40460125c03cb4627e4c6e35f61e4ca1 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
1/*
2 http-client-bad-url.c - HTTP client that sends bad URLs
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 <stdio.h>
24#include <string.h>
25
26#include "http-server.h"
27#include "util.h"
28
29int main(int argc, char ** argv) {
30#ifdef __MINGW32__
31 WSADATA data;
32 if (WSAStartup(MAKEWORD(1, 1), &data) != 0) {
33 return 1;
34 }
35#endif
36
37 int result;
38
39 if (argc < 3) {
40 fprintf(stderr, "Usage: %s PORT URL\n", argv[0]);
41 exit(EXIT_FAILURE);
42 }
43
44 uint16_t connect_port = atoi(argv[1]);
45 char * url = argv[2];
46
47 struct sockaddr_in a;
48 a.sin_family = AF_INET;
49 a.sin_port = htons(connect_port);
50 a.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
51
52 SOCKET s = socket(PF_INET, SOCK_STREAM, 0);
53 assert(s != INVALID_SOCKET);
54
55 result = connect(s, (struct sockaddr *) &a, sizeof(a));
56 assert(result == 0);
57
58 /* send request */
59 char * message;
60 xasprintf(&message, "GET %s HTTP/1.1\r\nConnection: close\r\n\r\n", url);
61 size_t message_length = strlen(message);
62 ssize_t bytes_sent = send(s, message, message_length, 0);
63 assert(bytes_sent == (ssize_t) message_length);
64
65 /* read response */
66 for (;;) {
67 uint8_t buffer[8192];
68 ssize_t bytes_read = recv(s, buffer, 8192, 0);
69 assert(bytes_read >= 0);
70 if (bytes_read == 0) {
71 break;
72 }
73 }
74
75 closesocket(s);
76 return 0;
77}