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