1 |
/* Licensed to the Apache Software Foundation (ASF) under one or more |
2 |
* contributor license agreements. See the NOTICE file distributed with |
3 |
* this work for additional information regarding copyright ownership. |
4 |
* The ASF licenses this file to You under the Apache License, Version 2.0 |
5 |
* (the "License"); you may not use this file except in compliance with |
6 |
* the License. You may obtain a copy of the License at |
7 |
* |
8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
9 |
* |
10 |
* Unless required by applicable law or agreed to in writing, software |
11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 |
* See the License for the specific language governing permissions and |
14 |
* limitations under the License. |
15 |
* |
16 |
* Originally developed by Aaron Bannert and Justin Erenkrantz, eBuilt. |
17 |
*/ |
18 |
|
19 |
#include <apr_general.h> /* For apr_initialize */ |
20 |
#include <apr_strings.h> |
21 |
#include <apr_file_io.h> |
22 |
|
23 |
#if APR_HAVE_STDLIB_H |
24 |
#include <stdlib.h> /* atexit */ |
25 |
#endif |
26 |
|
27 |
#include "flood_profile.h" |
28 |
|
29 |
/* FIXME: Base this on RCS ID? */ |
30 |
#define FLOOD_VERSION ".001" |
31 |
|
32 |
#define CRLF "\r\n" |
33 |
|
34 |
/* FIXME: replace the apr_recv logic with something sane. */ |
35 |
#define MAX_DOC_LENGTH 8192 |
36 |
|
37 |
#define LOCAL_SOCKET_TIMEOUT 30 * APR_USEC_PER_SEC |
38 |
|
39 |
/* Win32 doesn't have stdout or stderr. */ |
40 |
apr_file_t *local_stdout, *local_stderr; |
41 |
|
42 |
/* Should be the new apr_sms_t struct? Not ready yet. */ |
43 |
apr_pool_t *local_pool; |
44 |
|
45 |
/* Construct a request */ |
46 |
static void construct_request(request_t *r) |
47 |
{ |
48 |
/* FIXME: Handle keepalives. */ |
49 |
switch (r->method) |
50 |
{ |
51 |
case GET: |
52 |
r->rbuf = apr_psprintf(local_pool, |
53 |
"GET %s HTTP/1.1" CRLF |
54 |
"User-Agent: Flood/" FLOOD_VERSION CRLF |
55 |
"Host: %s" CRLF CRLF, |
56 |
r->parsed_uri->path, |
57 |
r->parsed_uri->hostinfo); |
58 |
r->rbuftype = POOL; |
59 |
r->rbufsize = strlen(r->rbuf); |
60 |
break; |
61 |
case HEAD: |
62 |
r->rbuf = apr_psprintf(local_pool, |
63 |
"HEAD %s HTTP/1.1" CRLF |
64 |
"User-Agent: Flood/" FLOOD_VERSION CRLF |
65 |
"Host: %s" CRLF CRLF, |
66 |
r->parsed_uri->path, |
67 |
r->parsed_uri->hostinfo); |
68 |
r->rbuftype = POOL; |
69 |
r->rbufsize = strlen(r->rbuf); |
70 |
break; |
71 |
case POST: |
72 |
/* FIXME */ |
73 |
r->rbuf = apr_psprintf(local_pool, |
74 |
"POST %s HTTP/1.1" CRLF |
75 |
"User-Agent: Flood/" FLOOD_VERSION CRLF |
76 |
"Host: %s" CRLF CRLF, |
77 |
r->parsed_uri->path, |
78 |
r->parsed_uri->hostinfo); |
79 |
r->rbuftype = POOL; |
80 |
r->rbufsize = strlen(r->rbuf); |
81 |
break; |
82 |
} |
83 |
|
84 |
} |
85 |
|
86 |
/* Open the TCP connection to the server */ |
87 |
static apr_socket_t* open_socket(request_t *r) |
88 |
{ |
89 |
apr_status_t rv = 0; |
90 |
apr_sockaddr_t *destsa; |
91 |
/* FIXME: apr_socket_t == socket_t - fix profile.h */ |
92 |
apr_socket_t *socket; |
93 |
|
94 |
if ((rv = apr_sockaddr_info_get(&destsa, r->parsed_uri->hostname, APR_INET, |
95 |
r->parsed_uri->port, 0, local_pool)) |
96 |
!= APR_SUCCESS) { |
97 |
return NULL; |
98 |
} |
99 |
|
100 |
if ((rv = apr_socket_create(&socket, APR_INET, SOCK_STREAM, |
101 |
local_pool)) != APR_SUCCESS) { |
102 |
return NULL; |
103 |
} |
104 |
|
105 |
if ((rv = apr_connect(socket, destsa)) != APR_SUCCESS) { |
106 |
if (APR_STATUS_IS_EINPROGRESS(rv)) { |
107 |
/* FIXME: Handle better */ |
108 |
apr_socket_close(socket); |
109 |
return NULL; |
110 |
} |
111 |
else { |
112 |
/* FIXME: Handle */ |
113 |
apr_socket_close(socket); |
114 |
return NULL; |
115 |
} |
116 |
} |
117 |
|
118 |
apr_socket_timeout_set(socket, LOCAL_SOCKET_TIMEOUT); |
119 |
|
120 |
return socket; |
121 |
} |
122 |
|
123 |
/* close down TCP socket */ |
124 |
static void close_socket(apr_socket_t *s) |
125 |
{ |
126 |
/* FIXME: recording and other stuff here? */ |
127 |
apr_socket_close(s); |
128 |
} |
129 |
|
130 |
static apr_status_t write_socket(request_t *r, apr_socket_t *s) |
131 |
{ |
132 |
apr_size_t l; |
133 |
apr_status_t e; |
134 |
|
135 |
l = r->rbufsize; |
136 |
|
137 |
e = apr_send(s, r->rbuf, &l); |
138 |
|
139 |
/* FIXME: Better error and allow restarts? */ |
140 |
if (l != r->rbufsize) |
141 |
return APR_EGENERAL; |
142 |
|
143 |
return APR_SUCCESS; |
144 |
} |
145 |
|
146 |
/* FIXME: poll implementation? */ |
147 |
static response_t *read_socket(apr_socket_t *s) |
148 |
{ |
149 |
apr_status_t status; |
150 |
response_t *resp; |
151 |
|
152 |
resp = apr_pcalloc(local_pool, sizeof(response_t)); |
153 |
resp->rbuftype = POOL; |
154 |
resp->rbufsize = MAX_DOC_LENGTH; |
155 |
resp->rbuf = apr_pcalloc(local_pool, resp->rbufsize); |
156 |
|
157 |
status = apr_recv(s, resp->rbuf, &resp->rbufsize); |
158 |
|
159 |
if (status != APR_SUCCESS && status != APR_EOF) |
160 |
return NULL; |
161 |
|
162 |
return resp; |
163 |
} |
164 |
|
165 |
int main(int argc, char** argv) |
166 |
{ |
167 |
request_t r; |
168 |
apr_socket_t *s; |
169 |
|
170 |
/* FIXME: Where is Roy's change to return the global pool... */ |
171 |
apr_initialize(); |
172 |
atexit(apr_terminate); |
173 |
|
174 |
apr_pool_create(&local_pool, NULL); |
175 |
|
176 |
/* The pool should close these file descriptors */ |
177 |
apr_file_open_stdout(&local_stdout, local_pool); |
178 |
apr_file_open_stderr(&local_stderr, local_pool); |
179 |
|
180 |
r.uri = "http://www.apachelabs.org/"; |
181 |
r.method = GET; |
182 |
|
183 |
r.parsed_uri = apr_pcalloc(local_pool, sizeof(*r.parsed_uri)); |
184 |
|
185 |
/* FIXME: This is the private copy. */ |
186 |
apr_uri_parse(local_pool, r.uri, r.parsed_uri); |
187 |
if (!r.parsed_uri->port) |
188 |
r.parsed_uri->port = 80; |
189 |
|
190 |
apr_file_printf(local_stdout, "%s\n", r.parsed_uri->hostname); |
191 |
|
192 |
construct_request(&r); |
193 |
|
194 |
s = open_socket(&r); |
195 |
|
196 |
if (s) |
197 |
{ |
198 |
response_t *resp; |
199 |
|
200 |
if (write_socket(&r, s) == APR_SUCCESS) |
201 |
{ |
202 |
/* Wait .25 seconds... */ |
203 |
/* FIXME: replace with poll. */ |
204 |
apr_sleep(0.25 * APR_USEC_PER_SEC); |
205 |
|
206 |
resp = read_socket(s); |
207 |
|
208 |
if (resp) |
209 |
apr_file_printf(local_stdout, "%s\n", (char*)resp->rbuf); |
210 |
} |
211 |
close_socket(s); |
212 |
} |
213 |
|
214 |
return EXIT_SUCCESS; |
215 |
} |