1 |
siliconforks |
114 |
/* |
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 <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 "util.h" |
32 |
|
|
|
33 |
|
|
int main(int argc, char ** argv) { |
34 |
|
|
int result; |
35 |
|
|
|
36 |
|
|
if (argc < 3) { |
37 |
|
|
fprintf(stderr, "Usage: %s PORT URL\n", argv[0]); |
38 |
|
|
exit(EXIT_FAILURE); |
39 |
|
|
} |
40 |
|
|
|
41 |
|
|
uint16_t connect_port = atoi(argv[1]); |
42 |
|
|
char * url = argv[2]; |
43 |
|
|
|
44 |
|
|
struct sockaddr_in a; |
45 |
|
|
a.sin_family = AF_INET; |
46 |
|
|
a.sin_port = htons(connect_port); |
47 |
|
|
a.sin_addr.s_addr = htonl(INADDR_LOOPBACK); |
48 |
|
|
|
49 |
|
|
int s = socket(PF_INET, SOCK_STREAM, 0); |
50 |
|
|
assert(s > 0); |
51 |
|
|
|
52 |
|
|
result = connect(s, (struct sockaddr *) &a, sizeof(a)); |
53 |
|
|
assert(result == 0); |
54 |
|
|
|
55 |
|
|
/* send request */ |
56 |
|
|
char * message; |
57 |
|
|
xasprintf(&message, "GET %s HTTP/1.1\r\nConnection: close\r\n\r\n", url); |
58 |
|
|
size_t message_length = strlen(message); |
59 |
|
|
ssize_t bytes_sent = send(s, message, message_length, 0); |
60 |
|
|
assert(bytes_sent == (ssize_t) message_length); |
61 |
|
|
|
62 |
|
|
/* read response */ |
63 |
|
|
for (;;) { |
64 |
|
|
uint8_t buffer[8192]; |
65 |
|
|
ssize_t bytes_read = recv(s, buffer, 8192, 0); |
66 |
|
|
assert(bytes_read >= 0); |
67 |
|
|
if (bytes_read == 0) { |
68 |
|
|
break; |
69 |
|
|
} |
70 |
|
|
} |
71 |
|
|
|
72 |
|
|
close(s); |
73 |
|
|
return 0; |
74 |
|
|
} |