1 |
/* |
2 |
http-client-bad-body.c - HTTP client that outputs a bad body |
3 |
Copyright (C) 2008, 2009 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 <stdlib.h> |
25 |
#include <string.h> |
26 |
|
27 |
#ifdef HAVE_ARPA_INET_H |
28 |
#include <arpa/inet.h> |
29 |
#endif |
30 |
#ifdef HAVE_NETDB_H |
31 |
#include <netdb.h> |
32 |
#endif |
33 |
#ifdef HAVE_NETINET_IN_H |
34 |
#include <netinet/in.h> |
35 |
#endif |
36 |
#ifdef HAVE_SYS_SOCKET_H |
37 |
#include <sys/socket.h> |
38 |
#endif |
39 |
#include <unistd.h> |
40 |
|
41 |
#include "http-server.h" |
42 |
#include "util.h" |
43 |
|
44 |
int main(int argc, char ** argv) { |
45 |
#ifdef __MINGW32__ |
46 |
WSADATA data; |
47 |
if (WSAStartup(MAKEWORD(1, 1), &data) != 0) { |
48 |
return 1; |
49 |
} |
50 |
#endif |
51 |
|
52 |
int result; |
53 |
|
54 |
if (argc < 3) { |
55 |
fprintf(stderr, "Usage: %s PORT URL\n", argv[0]); |
56 |
exit(EXIT_FAILURE); |
57 |
} |
58 |
|
59 |
uint16_t connect_port = atoi(argv[1]); |
60 |
char * url = argv[2]; |
61 |
char * host; |
62 |
uint16_t port; |
63 |
char * abs_path; |
64 |
char * query; |
65 |
result = URL_parse(url, &host, &port, &abs_path, &query); |
66 |
assert(result == 0); |
67 |
|
68 |
struct sockaddr_in a; |
69 |
a.sin_family = AF_INET; |
70 |
a.sin_port = htons(connect_port); |
71 |
a.sin_addr.s_addr = htonl(INADDR_LOOPBACK); |
72 |
|
73 |
SOCKET s = socket(PF_INET, SOCK_STREAM, 0); |
74 |
assert(s != INVALID_SOCKET); |
75 |
|
76 |
result = connect(s, (struct sockaddr *) &a, sizeof(a)); |
77 |
assert(result == 0); |
78 |
|
79 |
/* send request */ |
80 |
char * message; |
81 |
xasprintf(&message, "POST %s HTTP/1.1\r\nConnection: close\r\nTransfer-Encoding: chunked\r\n\r\nHello\n", url); |
82 |
size_t message_length = strlen(message); |
83 |
ssize_t bytes_sent = send(s, message, message_length, 0); |
84 |
assert(bytes_sent == (ssize_t) message_length); |
85 |
|
86 |
/* read response */ |
87 |
for (;;) { |
88 |
uint8_t buffer[8192]; |
89 |
ssize_t bytes_read = recv(s, buffer, 8192, 0); |
90 |
assert(bytes_read >= 0); |
91 |
if (bytes_read == 0) { |
92 |
break; |
93 |
} |
94 |
} |
95 |
|
96 |
closesocket(s); |
97 |
return 0; |
98 |
} |