]>
Commit | Line | Data |
---|---|---|
52e1d7e2 | 1 | |
30a4f2a8 | 2 | /* |
b3b64e58 | 3 | * $Id: client.cc,v 1.16 1996/12/17 07:16:52 wessels Exp $ |
30a4f2a8 | 4 | * |
5 | * DEBUG: section 0 WWW Client | |
6 | * AUTHOR: Harvest Derived | |
7 | * | |
42c04c16 | 8 | * SQUID Internet Object Cache http://squid.nlanr.net/Squid/ |
30a4f2a8 | 9 | * -------------------------------------------------------- |
10 | * | |
11 | * Squid is the result of efforts by numerous individuals from the | |
12 | * Internet community. Development is led by Duane Wessels of the | |
13 | * National Laboratory for Applied Network Research and funded by | |
14 | * the National Science Foundation. | |
15 | * | |
16 | * This program is free software; you can redistribute it and/or modify | |
17 | * it under the terms of the GNU General Public License as published by | |
18 | * the Free Software Foundation; either version 2 of the License, or | |
19 | * (at your option) any later version. | |
20 | * | |
21 | * This program is distributed in the hope that it will be useful, | |
22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
24 | * GNU General Public License for more details. | |
25 | * | |
26 | * You should have received a copy of the GNU General Public License | |
27 | * along with this program; if not, write to the Free Software | |
28 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
29 | * | |
30 | */ | |
31 | ||
32 | /* | |
33 | * Copyright (c) 1994, 1995. All rights reserved. | |
34 | * | |
35 | * The Harvest software was developed by the Internet Research Task | |
36 | * Force Research Group on Resource Discovery (IRTF-RD): | |
37 | * | |
38 | * Mic Bowman of Transarc Corporation. | |
39 | * Peter Danzig of the University of Southern California. | |
40 | * Darren R. Hardy of the University of Colorado at Boulder. | |
41 | * Udi Manber of the University of Arizona. | |
42 | * Michael F. Schwartz of the University of Colorado at Boulder. | |
43 | * Duane Wessels of the University of Colorado at Boulder. | |
44 | * | |
45 | * This copyright notice applies to software in the Harvest | |
46 | * ``src/'' directory only. Users should consult the individual | |
47 | * copyright notices in the ``components/'' subdirectories for | |
48 | * copyright information about other software bundled with the | |
49 | * Harvest source code distribution. | |
50 | * | |
51 | * TERMS OF USE | |
52 | * | |
53 | * The Harvest software may be used and re-distributed without | |
54 | * charge, provided that the software origin and research team are | |
55 | * cited in any use of the system. Most commonly this is | |
56 | * accomplished by including a link to the Harvest Home Page | |
57 | * (http://harvest.cs.colorado.edu/) from the query page of any | |
58 | * Broker you deploy, as well as in the query result pages. These | |
59 | * links are generated automatically by the standard Broker | |
60 | * software distribution. | |
61 | * | |
62 | * The Harvest software is provided ``as is'', without express or | |
63 | * implied warranty, and with no support nor obligation to assist | |
64 | * in its use, correction, modification or enhancement. We assume | |
65 | * no liability with respect to the infringement of copyrights, | |
66 | * trade secrets, or any patents, and are not responsible for | |
67 | * consequential damages. Proper use of the Harvest software is | |
68 | * entirely the responsibility of the user. | |
69 | * | |
70 | * DERIVATIVE WORKS | |
71 | * | |
72 | * Users may make derivative works from the Harvest software, subject | |
73 | * to the following constraints: | |
74 | * | |
75 | * - You must include the above copyright notice and these | |
76 | * accompanying paragraphs in all forms of derivative works, | |
77 | * and any documentation and other materials related to such | |
78 | * distribution and use acknowledge that the software was | |
79 | * developed at the above institutions. | |
80 | * | |
81 | * - You must notify IRTF-RD regarding your distribution of | |
82 | * the derivative work. | |
83 | * | |
84 | * - You must clearly notify users that your are distributing | |
85 | * a modified version and not the original Harvest software. | |
86 | * | |
87 | * - Any derivative product is also subject to these copyright | |
88 | * and use restrictions. | |
89 | * | |
90 | * Note that the Harvest software is NOT in the public domain. We | |
91 | * retain copyright, as specified above. | |
92 | * | |
93 | * HISTORY OF FREE SOFTWARE STATUS | |
94 | * | |
95 | * Originally we required sites to license the software in cases | |
96 | * where they were going to build commercial products/services | |
97 | * around Harvest. In June 1995 we changed this policy. We now | |
98 | * allow people to use the core Harvest software (the code found in | |
99 | * the Harvest ``src/'' directory) for free. We made this change | |
100 | * in the interest of encouraging the widest possible deployment of | |
101 | * the technology. The Harvest software is really a reference | |
102 | * implementation of a set of protocols and formats, some of which | |
103 | * we intend to standardize. We encourage commercial | |
104 | * re-implementations of code complying to this set of standards. | |
105 | */ | |
090089c4 | 106 | |
44a47c6e | 107 | #include "squid.h" |
090089c4 | 108 | |
109 | #ifndef BUFSIZ | |
110 | #define BUFSIZ 8192 | |
111 | #endif | |
112 | ||
113 | /* Local functions */ | |
67508012 | 114 | static int client_comm_connect _PARAMS((int sock, char *dest_host, u_short dest_port)); |
0ee4272b | 115 | static void usage _PARAMS((const char *progname)); |
090089c4 | 116 | |
b8d8561b | 117 | static void |
0ee4272b | 118 | usage(const char *progname) |
090089c4 | 119 | { |
0ee4272b | 120 | fprintf(stderr, |
fe4e214f | 121 | "Usage: %s [-rs] [-i IMS_time] [-h host] [-p port] [-m method] url\n" |
122 | "Options:\n" | |
123 | " -r Force cache to reload URL.\n" | |
124 | " -s Silent. Do not print data to stdout.\n" | |
125 | " -i IMS If-Modified-Since time (in Epoch seconds).\n" | |
126 | " -h host Retrieve URL from cache on hostname. Default is localhost.\n" | |
127 | " -p port Port number of cache. Default is %d.\n" | |
128 | " -m method Request method, default is GET.\n", | |
129 | progname, CACHE_HTTP_PORT); | |
090089c4 | 130 | exit(1); |
131 | } | |
132 | ||
b8d8561b | 133 | int |
134 | main(int argc, char *argv[]) | |
090089c4 | 135 | { |
136 | int conn, c, len, bytesWritten; | |
137 | int port, to_stdout, reload; | |
138 | char url[BUFSIZ], msg[BUFSIZ], buf[BUFSIZ], hostname[BUFSIZ]; | |
0ee4272b | 139 | const char *method = "GET"; |
090089c4 | 140 | extern char *optarg; |
234967c9 | 141 | time_t ims = 0; |
b3b64e58 | 142 | int max_forwards = -1; |
090089c4 | 143 | |
144 | /* set the defaults */ | |
145 | strcpy(hostname, "localhost"); | |
146 | port = CACHE_HTTP_PORT; | |
147 | to_stdout = 1; | |
148 | reload = 0; | |
149 | ||
150 | if (argc < 2) { | |
151 | usage(argv[0]); /* need URL */ | |
152 | } else if (argc >= 2) { | |
153 | strcpy(url, argv[argc - 1]); | |
154 | if (url[0] == '-') | |
155 | usage(argv[0]); | |
b3b64e58 | 156 | while ((c = getopt(argc, argv, "fsrnp:c:h:i:m:t:?")) != -1) |
090089c4 | 157 | switch (c) { |
158 | case 'h': /* host:arg */ | |
159 | case 'c': /* backward compat */ | |
160 | if (optarg != NULL) | |
161 | strcpy(hostname, optarg); | |
162 | break; | |
163 | case 's': /* silent */ | |
164 | case 'n': /* backward compat */ | |
165 | to_stdout = 0; | |
166 | break; | |
167 | case 'r': /* reload */ | |
168 | reload = 1; | |
169 | break; | |
170 | case 'p': /* port number */ | |
171 | sscanf(optarg, "%d", &port); | |
172 | if (port < 1) | |
173 | port = CACHE_HTTP_PORT; /* default */ | |
174 | break; | |
234967c9 | 175 | case 'i': /* IMS */ |
176 | ims = (time_t) atoi(optarg); | |
177 | break; | |
178 | case 'm': | |
179 | method = xstrdup(optarg); | |
180 | break; | |
b3b64e58 | 181 | case 't': |
182 | method = xstrdup("TRACE"); | |
183 | max_forwards = atoi(optarg); | |
184 | break; | |
090089c4 | 185 | case '?': /* usage */ |
186 | default: | |
187 | usage(argv[0]); | |
188 | break; | |
189 | } | |
190 | } | |
191 | /* Connect to the server */ | |
192 | if ((conn = socket(PF_INET, SOCK_STREAM, 0)) < 0) { | |
193 | perror("client: socket"); | |
194 | exit(1); | |
195 | } | |
196 | if (client_comm_connect(conn, hostname, port) < 0) { | |
197 | if (errno == 0) { | |
198 | fprintf(stderr, "client: ERROR: Cannot connect to %s:%d: Host unknown.\n", hostname, port); | |
199 | } else { | |
200 | char tbuf[BUFSIZ]; | |
201 | sprintf(tbuf, "client: ERROR: Cannot connect to %s:%d", | |
202 | hostname, port); | |
203 | perror(tbuf); | |
204 | } | |
205 | exit(1); | |
206 | } | |
207 | /* Build the HTTP request */ | |
234967c9 | 208 | sprintf(msg, "%s %s HTTP/1.0\r\n", method, url); |
090089c4 | 209 | if (reload) { |
234967c9 | 210 | sprintf(buf, "Pragma: no-cache\r\n"); |
211 | strcat(msg, buf); | |
212 | } | |
213 | sprintf(buf, "Accept: */*\r\n"); | |
214 | strcat(msg, buf); | |
215 | if (ims) { | |
cf49df4b | 216 | sprintf(buf, "If-Modified-Since: %s\r\n", mkrfc1123(ims)); |
234967c9 | 217 | strcat(msg, buf); |
090089c4 | 218 | } |
b3b64e58 | 219 | if (max_forwards > -1) { |
220 | sprintf(buf, "Max-Forwards: %d\r\n", max_forwards); | |
221 | strcat(msg, buf); | |
222 | } | |
234967c9 | 223 | sprintf(buf, "\r\n"); |
224 | strcat(msg, buf); | |
090089c4 | 225 | |
226 | /* Send the HTTP request */ | |
227 | bytesWritten = write(conn, msg, strlen(msg)); | |
228 | if (bytesWritten < 0) { | |
229 | perror("client: ERROR: write"); | |
230 | exit(1); | |
231 | } else if (bytesWritten != strlen(msg)) { | |
232 | fprintf(stderr, "client: ERROR: Cannot send request?: %s\n", msg); | |
233 | exit(1); | |
234 | } | |
235 | /* Read the data */ | |
236 | while ((len = read(conn, buf, sizeof(buf))) > 0) { | |
237 | if (to_stdout) | |
238 | fwrite(buf, len, 1, stdout); | |
239 | } | |
240 | (void) close(conn); /* done with socket */ | |
241 | exit(0); | |
242 | /*NOTREACHED */ | |
983061ed | 243 | return 0; |
090089c4 | 244 | } |
245 | ||
b8d8561b | 246 | static int |
247 | client_comm_connect(int sock, char *dest_host, u_short dest_port) | |
090089c4 | 248 | { |
0ee4272b | 249 | const struct hostent *hp; |
090089c4 | 250 | static struct sockaddr_in to_addr; |
251 | ||
252 | /* Set up the destination socket address for message to send to. */ | |
253 | to_addr.sin_family = AF_INET; | |
254 | ||
255 | if ((hp = gethostbyname(dest_host)) == 0) { | |
256 | return (-1); | |
257 | } | |
30a4f2a8 | 258 | xmemcpy(&to_addr.sin_addr, hp->h_addr, hp->h_length); |
090089c4 | 259 | to_addr.sin_port = htons(dest_port); |
260 | return connect(sock, (struct sockaddr *) &to_addr, sizeof(struct sockaddr_in)); | |
261 | } |