]>
Commit | Line | Data |
---|---|---|
3c66d057 | 1 | |
dd11e0b7 | 2 | /* |
4361707c | 3 | * $Id: client_side.cc,v 1.679 2005/01/23 11:32:42 serassio Exp $ |
dd11e0b7 | 4 | * |
5 | * DEBUG: section 33 Client-side Routines | |
6 | * AUTHOR: Duane Wessels | |
7 | * | |
2b6662ba | 8 | * SQUID Web Proxy Cache http://www.squid-cache.org/ |
e25c139f | 9 | * ---------------------------------------------------------- |
dd11e0b7 | 10 | * |
2b6662ba | 11 | * Squid is the result of efforts by numerous individuals from |
12 | * the Internet community; see the CONTRIBUTORS file for full | |
13 | * details. Many organizations have provided support for Squid's | |
14 | * development; see the SPONSORS file for full details. Squid is | |
15 | * Copyrighted (C) 2001 by the Regents of the University of | |
16 | * California; see the COPYRIGHT file for full details. Squid | |
17 | * incorporates software developed and/or copyrighted by other | |
18 | * sources; see the CREDITS file for full details. | |
dd11e0b7 | 19 | * |
20 | * This program is free software; you can redistribute it and/or modify | |
21 | * it under the terms of the GNU General Public License as published by | |
22 | * the Free Software Foundation; either version 2 of the License, or | |
23 | * (at your option) any later version. | |
24 | * | |
25 | * This program is distributed in the hope that it will be useful, | |
26 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
27 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
28 | * GNU General Public License for more details. | |
29 | * | |
30 | * You should have received a copy of the GNU General Public License | |
31 | * along with this program; if not, write to the Free Software | |
cbdec147 | 32 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. |
e25c139f | 33 | * |
dd11e0b7 | 34 | */ |
f88bb09c | 35 | |
62e76326 | 36 | /* Errors and client side |
edce4d98 | 37 | * |
38 | * Problem the first: the store entry is no longer authoritative on the | |
39 | * reply status. EBITTEST (E_ABORT) is no longer a valid test outside | |
40 | * of client_side_reply.c. | |
41 | * Problem the second: resources are wasted if we delay in cleaning up. | |
42 | * Problem the third we can't depend on a connection close to clean up. | |
43 | * | |
44 | * Nice thing the first: Any step in the stream can callback with data | |
45 | * representing an error. | |
46 | * Nice thing the second: once you stop requesting reads from upstream, | |
47 | * upstream can be stopped too. | |
48 | * | |
49 | * Solution #1: Error has a callback mechanism to hand over a membuf | |
50 | * with the error content. The failing node pushes that back as the | |
51 | * reply. Can this be generalised to reduce duplicate efforts? | |
52 | * A: Possibly. For now, only one location uses this. | |
53 | * How to deal with pre-stream errors? | |
54 | * Tell client_side_reply that we *want* an error page before any | |
55 | * stream calls occur. Then we simply read as normal. | |
56 | */ | |
57 | ||
f88bb09c | 58 | #include "squid.h" |
a46d2c0e | 59 | #include "client_side.h" |
c8be6d7b | 60 | #include "clientStream.h" |
61 | #include "IPInterception.h" | |
f5691f9c | 62 | #include "AuthUserRequest.h" |
e6ccf245 | 63 | #include "Store.h" |
c4b7a5a9 | 64 | #include "comm.h" |
528b2c61 | 65 | #include "HttpReply.h" |
66 | #include "HttpRequest.h" | |
67 | #include "MemObject.h" | |
68 | #include "fde.h" | |
69 | #include "client_side_request.h" | |
4fb35c3c | 70 | #include "ACLChecklist.h" |
ee0989f2 | 71 | #include "ConnectionDetail.h" |
0655fa4d | 72 | #include "client_side_reply.h" |
5cafc1d6 | 73 | |
5492ad1d | 74 | #if LINGERING_CLOSE |
75 | #define comm_close comm_lingering_close | |
76 | #endif | |
77 | ||
edce4d98 | 78 | /* Persistent connection logic: |
79 | * | |
80 | * requests (httpClientRequest structs) get added to the connection | |
81 | * list, with the current one being chr | |
82 | * | |
83 | * The request is *immediately* kicked off, and data flows through | |
84 | * to clientSocketRecipient. | |
85 | * | |
86 | * If the data that arrives at clientSocketRecipient is not for the current | |
87 | * request, clientSocketRecipient simply returns, without requesting more | |
88 | * data, or sending it. | |
89 | * | |
90 | * ClientKeepAliveNextRequest will then detect the presence of data in | |
91 | * the next clientHttpRequest, and will send it, restablishing the | |
92 | * data flow. | |
93 | */ | |
94 | ||
95 | /* our socket-related context */ | |
62e76326 | 96 | |
62e76326 | 97 | |
0655fa4d | 98 | CBDATA_CLASS_INIT(ClientSocketContext); |
62e76326 | 99 | |
0655fa4d | 100 | void * |
101 | ClientSocketContext::operator new (size_t byteCount) | |
102 | { | |
103 | /* derived classes with different sizes must implement their own new */ | |
104 | assert (byteCount == sizeof (ClientSocketContext)); | |
105 | CBDATA_INIT_TYPE(ClientSocketContext); | |
106 | return cbdataAlloc(ClientSocketContext); | |
107 | } | |
62e76326 | 108 | |
0655fa4d | 109 | void |
110 | ClientSocketContext::operator delete (void *address) | |
111 | { | |
112 | cbdataFree (address); | |
113 | } | |
7a2f978b | 114 | |
edce4d98 | 115 | /* Local functions */ |
528b2c61 | 116 | /* ClientSocketContext */ |
528b2c61 | 117 | static ClientSocketContext *ClientSocketContextNew(clientHttpRequest *); |
edce4d98 | 118 | /* other */ |
fc5d6f7f | 119 | static CWCB clientWriteComplete; |
25f651c1 | 120 | static IOWCB clientWriteBodyComplete; |
c4b7a5a9 | 121 | static IOCB clientReadRequest; |
f900210a | 122 | static bool clientParseRequest(ConnStateData::Pointer conn, bool &do_next_read); |
123 | static void clientAfterReadingRequests(int fd, ConnStateData::Pointer &conn, int do_next_read); | |
7a2f978b | 124 | static PF connStateFree; |
125 | static PF requestTimeout; | |
b5c39993 | 126 | static PF clientLifetimeTimeout; |
a2ac85d9 | 127 | static ClientSocketContext *parseHttpRequestAbort(ConnStateData::Pointer & conn, |
62e76326 | 128 | const char *uri); |
a2ac85d9 | 129 | static ClientSocketContext *parseHttpRequest(ConnStateData::Pointer &, method_t *, |
62e76326 | 130 | char **, size_t *); |
3898f57f | 131 | #if USE_IDENT |
05832ae1 | 132 | static IDCB clientIdentDone; |
3898f57f | 133 | #endif |
edce4d98 | 134 | static CSCB clientSocketRecipient; |
135 | static CSD clientSocketDetach; | |
c68e9c6b | 136 | static void clientSetKeepaliveFlag(clientHttpRequest *); |
190154cf | 137 | static int clientIsContentLengthValid(HttpRequest * r); |
a46d2c0e | 138 | static bool okToAccept(); |
c8be6d7b | 139 | static int clientIsRequestBodyValid(int bodyLength); |
e6ccf245 | 140 | static int clientIsRequestBodyTooLargeForPolicy(size_t bodyLength); |
50c09fc4 | 141 | |
c8be6d7b | 142 | static void clientUpdateStatHistCounters(log_type logType, int svc_time); |
143 | static void clientUpdateStatCounters(log_type logType); | |
144 | static void clientUpdateHierCounters(HierarchyLogEntry *); | |
528b2c61 | 145 | static bool clientPingHasFinished(ping_data const *aPing); |
190154cf | 146 | static void clientPrepareLogWithRequestDetails(HttpRequest *, AccessLogEntry *); |
e4a67a80 | 147 | #ifndef PURIFY |
a2ac85d9 | 148 | static int connIsUsable(ConnStateData::Pointer conn); |
e4a67a80 | 149 | #endif |
528b2c61 | 150 | static int responseFinishedOrFailed(HttpReply * rep, StoreIOBuffer const &recievedData); |
a2ac85d9 | 151 | static void ClientSocketContextPushDeferredIfNeeded(ClientSocketContext::Pointer deferredRequest, ConnStateData::Pointer & conn); |
c8be6d7b | 152 | static void clientUpdateSocketStats(log_type logType, size_t size); |
153 | ||
a2ac85d9 | 154 | static ClientSocketContext *clientParseRequestMethod(char *inbuf, method_t * method_p, ConnStateData::Pointer & conn); |
c8be6d7b | 155 | static char *skipLeadingSpace(char *aString); |
156 | static char *findTrailingHTTPVersion(char *uriAndHTTPVersion); | |
3f38a55e | 157 | #if UNUSED_CODE |
c8be6d7b | 158 | static void trimTrailingSpaces(char *aString, size_t len); |
3f38a55e | 159 | #endif |
450e0c10 | 160 | static ClientSocketContext *parseURIandHTTPVersion(char **url_p, HttpVersion * http_ver_p, ConnStateData::Pointer& conn, char *http_version_str); |
c4b7a5a9 | 161 | static void setLogUri(clientHttpRequest * http, char const *uri); |
a2ac85d9 | 162 | static int connReadWasError(ConnStateData::Pointer& conn, comm_err_t, int size, int xerrno); |
163 | static int connFinishedWithConn(ConnStateData::Pointer& conn, int size); | |
164 | static void connNoteUseOfBuffer(ConnStateData::Pointer & conn, size_t byteCount); | |
165 | static int connKeepReadingIncompleteRequest(ConnStateData::Pointer & conn); | |
166 | static void connCancelIncompleteRequests(ConnStateData::Pointer & conn); | |
62e76326 | 167 | |
3f38a55e | 168 | static ConnStateData *connStateCreate(struct sockaddr_in *peer, struct sockaddr_in *me, int fd, http_port_list *port); |
528b2c61 | 169 | |
170 | int | |
171 | ClientSocketContext::fd() const | |
172 | { | |
173 | assert (http); | |
a2ac85d9 | 174 | assert (http->getConn().getRaw() != NULL); |
98242069 | 175 | return http->getConn()->fd; |
528b2c61 | 176 | } |
c8be6d7b | 177 | |
178 | clientStreamNode * | |
528b2c61 | 179 | ClientSocketContext::getTail() const |
c8be6d7b | 180 | { |
0655fa4d | 181 | if (http->client_stream.tail) |
182 | return (clientStreamNode *)http->client_stream.tail->data; | |
183 | ||
184 | return NULL; | |
c8be6d7b | 185 | } |
186 | ||
187 | clientStreamNode * | |
528b2c61 | 188 | ClientSocketContext::getClientReplyContext() const |
c8be6d7b | 189 | { |
528b2c61 | 190 | return (clientStreamNode *)http->client_stream.tail->prev->data; |
c8be6d7b | 191 | } |
192 | ||
c4b7a5a9 | 193 | /* |
194 | * This routine should be called to grow the inbuf and then | |
195 | * call comm_read(). | |
196 | */ | |
197 | void | |
a46d2c0e | 198 | ConnStateData::readSomeData() |
c4b7a5a9 | 199 | { |
a46d2c0e | 200 | if (reading()) |
62e76326 | 201 | return; |
202 | ||
a46d2c0e | 203 | reading(true); |
c4b7a5a9 | 204 | |
a46d2c0e | 205 | debug(33, 4) ("clientReadSomeData: FD %d: reading request...\n", fd); |
62e76326 | 206 | |
a46d2c0e | 207 | makeSpaceAvailable(); |
62e76326 | 208 | |
0e783891 | 209 | /* Make sure we are not still reading from the client side! */ |
210 | /* XXX this could take a bit of CPU time! aiee! -- adrian */ | |
a46d2c0e | 211 | assert(!comm_has_pending_read(fd)); |
62e76326 | 212 | |
a46d2c0e | 213 | comm_read(fd, in.addressToReadInto(), getAvailableBufferLength(), clientReadRequest, this); |
c4b7a5a9 | 214 | } |
62e76326 | 215 | |
c4b7a5a9 | 216 | |
c8be6d7b | 217 | void |
a2ac85d9 | 218 | ClientSocketContext::removeFromConnectionList(ConnStateData::Pointer conn) |
c8be6d7b | 219 | { |
0655fa4d | 220 | ClientSocketContext::Pointer *tempContextPointer; |
a2ac85d9 | 221 | assert(conn.getRaw() != NULL); |
0655fa4d | 222 | assert(conn->getCurrentContext().getRaw() != NULL); |
c8be6d7b | 223 | /* Unlink us from the connection request list */ |
0655fa4d | 224 | tempContextPointer = & conn->currentobject; |
62e76326 | 225 | |
0655fa4d | 226 | while (tempContextPointer->getRaw()) { |
62e76326 | 227 | if (*tempContextPointer == this) |
228 | break; | |
229 | ||
230 | tempContextPointer = &(*tempContextPointer)->next; | |
c8be6d7b | 231 | } |
62e76326 | 232 | |
0655fa4d | 233 | assert(tempContextPointer->getRaw() != NULL); |
528b2c61 | 234 | *tempContextPointer = next; |
235 | next = NULL; | |
c8be6d7b | 236 | } |
ea6f43cd | 237 | |
0655fa4d | 238 | ClientSocketContext::~ClientSocketContext() |
f88bb09c | 239 | { |
0655fa4d | 240 | clientStreamNode *node = getTail(); |
241 | ||
242 | if (node) { | |
243 | ClientSocketContext *streamContext = dynamic_cast<ClientSocketContext *> (node->data.getRaw()); | |
244 | ||
245 | if (streamContext) { | |
246 | /* We are *always* the tail - prevent recursive free */ | |
247 | assert(this == streamContext); | |
248 | node->data = NULL; | |
249 | } | |
250 | } | |
251 | ||
252 | if (connRegistered_) | |
253 | deRegisterWithConn(); | |
254 | ||
255 | httpRequestFree(http); | |
256 | ||
edce4d98 | 257 | /* clean up connection links to us */ |
0655fa4d | 258 | assert(this != next.getRaw()); |
259 | } | |
62e76326 | 260 | |
0655fa4d | 261 | void |
262 | ClientSocketContext::registerWithConn() | |
263 | { | |
264 | assert (!connRegistered_); | |
265 | assert (http); | |
a2ac85d9 | 266 | assert (http->getConn().getRaw() != NULL); |
0655fa4d | 267 | connRegistered_ = true; |
98242069 | 268 | http->getConn()->addContextToQueue(this); |
0655fa4d | 269 | } |
270 | ||
271 | void | |
272 | ClientSocketContext::deRegisterWithConn() | |
273 | { | |
274 | assert (connRegistered_); | |
98242069 | 275 | removeFromConnectionList(http->getConn()); |
0655fa4d | 276 | connRegistered_ = false; |
277 | } | |
278 | ||
279 | void | |
280 | ClientSocketContext::connIsFinished() | |
281 | { | |
282 | assert (http); | |
a2ac85d9 | 283 | assert (http->getConn().getRaw() != NULL); |
0655fa4d | 284 | deRegisterWithConn(); |
285 | /* we can't handle any more stream data - detach */ | |
286 | clientStreamDetach(getTail(), http); | |
287 | } | |
288 | ||
289 | ClientSocketContext::ClientSocketContext() : http(NULL), next(NULL), | |
290 | writtenToSocket(0), | |
291 | mayUseConnection_ (false), | |
292 | connRegistered_ (false) | |
293 | { | |
294 | memset (reqbuf, '\0', sizeof (reqbuf)); | |
295 | flags.deferred = 0; | |
296 | flags.parsed_ok = 0; | |
297 | deferredparams.node = NULL; | |
298 | deferredparams.rep = NULL; | |
f88bb09c | 299 | } |
300 | ||
528b2c61 | 301 | ClientSocketContext * |
302 | ClientSocketContextNew(clientHttpRequest * http) | |
f88bb09c | 303 | { |
528b2c61 | 304 | ClientSocketContext *newContext; |
edce4d98 | 305 | assert(http != NULL); |
0655fa4d | 306 | newContext = new ClientSocketContext; |
c8be6d7b | 307 | newContext->http = http; |
308 | return newContext; | |
4d55827a | 309 | } |
310 | ||
edce4d98 | 311 | #if USE_IDENT |
4d55827a | 312 | static void |
edce4d98 | 313 | clientIdentDone(const char *ident, void *data) |
4d55827a | 314 | { |
cc59d02a | 315 | ConnStateData *conn = (ConnStateData *)data; |
edce4d98 | 316 | xstrncpy(conn->rfc931, ident ? ident : dash_str, USER_IDENT_SZ); |
e81957b7 | 317 | } |
318 | ||
447e176b | 319 | #endif |
7a2f978b | 320 | |
c8be6d7b | 321 | void |
322 | clientUpdateStatCounters(log_type logType) | |
a7c05555 | 323 | { |
83704487 | 324 | statCounter.client_http.requests++; |
62e76326 | 325 | |
c8be6d7b | 326 | if (logTypeIsATcpHit(logType)) |
62e76326 | 327 | statCounter.client_http.hits++; |
328 | ||
c8be6d7b | 329 | if (logType == LOG_TCP_HIT) |
62e76326 | 330 | statCounter.client_http.disk_hits++; |
c8be6d7b | 331 | else if (logType == LOG_TCP_MEM_HIT) |
62e76326 | 332 | statCounter.client_http.mem_hits++; |
c8be6d7b | 333 | } |
334 | ||
335 | void | |
336 | clientUpdateStatHistCounters(log_type logType, int svc_time) | |
337 | { | |
83704487 | 338 | statHistCount(&statCounter.client_http.all_svc_time, svc_time); |
ee1679df | 339 | /* |
340 | * The idea here is not to be complete, but to get service times | |
341 | * for only well-defined types. For example, we don't include | |
342 | * LOG_TCP_REFRESH_FAIL_HIT because its not really a cache hit | |
343 | * (we *tried* to validate it, but failed). | |
344 | */ | |
62e76326 | 345 | |
c8be6d7b | 346 | switch (logType) { |
62e76326 | 347 | |
7c9c84ad | 348 | case LOG_TCP_REFRESH_HIT: |
62e76326 | 349 | statHistCount(&statCounter.client_http.nh_svc_time, svc_time); |
350 | break; | |
351 | ||
ee1679df | 352 | case LOG_TCP_IMS_HIT: |
62e76326 | 353 | statHistCount(&statCounter.client_http.nm_svc_time, svc_time); |
354 | break; | |
355 | ||
ee1679df | 356 | case LOG_TCP_HIT: |
62e76326 | 357 | |
ee1679df | 358 | case LOG_TCP_MEM_HIT: |
62e76326 | 359 | |
b540e168 | 360 | case LOG_TCP_OFFLINE_HIT: |
62e76326 | 361 | statHistCount(&statCounter.client_http.hit_svc_time, svc_time); |
362 | break; | |
363 | ||
ee1679df | 364 | case LOG_TCP_MISS: |
62e76326 | 365 | |
ee1679df | 366 | case LOG_TCP_CLIENT_REFRESH_MISS: |
62e76326 | 367 | statHistCount(&statCounter.client_http.miss_svc_time, svc_time); |
368 | break; | |
369 | ||
ee1679df | 370 | default: |
62e76326 | 371 | /* make compiler warnings go away */ |
372 | break; | |
ee1679df | 373 | } |
c8be6d7b | 374 | } |
375 | ||
528b2c61 | 376 | bool |
c8be6d7b | 377 | clientPingHasFinished(ping_data const *aPing) |
378 | { | |
379 | if (0 != aPing->stop.tv_sec && 0 != aPing->start.tv_sec) | |
62e76326 | 380 | return true; |
381 | ||
528b2c61 | 382 | return false; |
c8be6d7b | 383 | } |
384 | ||
385 | void | |
386 | clientUpdateHierCounters(HierarchyLogEntry * someEntry) | |
387 | { | |
388 | ping_data *i; | |
62e76326 | 389 | |
c8547a11 | 390 | switch (someEntry->code) { |
391 | #if USE_CACHE_DIGESTS | |
62e76326 | 392 | |
c8547a11 | 393 | case CD_PARENT_HIT: |
62e76326 | 394 | |
a196e1e4 | 395 | case CD_SIBLING_HIT: |
62e76326 | 396 | statCounter.cd.times_used++; |
397 | break; | |
c8547a11 | 398 | #endif |
62e76326 | 399 | |
c8547a11 | 400 | case SIBLING_HIT: |
62e76326 | 401 | |
c8547a11 | 402 | case PARENT_HIT: |
62e76326 | 403 | |
c8547a11 | 404 | case FIRST_PARENT_MISS: |
62e76326 | 405 | |
c8547a11 | 406 | case CLOSEST_PARENT_MISS: |
62e76326 | 407 | statCounter.icp.times_used++; |
408 | i = &someEntry->ping; | |
409 | ||
410 | if (clientPingHasFinished(i)) | |
411 | statHistCount(&statCounter.icp.query_svc_time, | |
412 | tvSubUsec(i->start, i->stop)); | |
413 | ||
414 | if (i->timeout) | |
415 | statCounter.icp.query_timeouts++; | |
416 | ||
417 | break; | |
418 | ||
c8547a11 | 419 | case CLOSEST_PARENT: |
62e76326 | 420 | |
c8547a11 | 421 | case CLOSEST_DIRECT: |
62e76326 | 422 | statCounter.netdb.times_used++; |
423 | ||
424 | break; | |
425 | ||
69c95dd3 | 426 | default: |
62e76326 | 427 | break; |
17b6e784 | 428 | } |
a7c05555 | 429 | } |
430 | ||
528b2c61 | 431 | void |
432 | ClientHttpRequest::updateCounters() | |
c8be6d7b | 433 | { |
528b2c61 | 434 | clientUpdateStatCounters(logType); |
62e76326 | 435 | |
528b2c61 | 436 | if (request->errType != ERR_NONE) |
62e76326 | 437 | statCounter.client_http.errors++; |
438 | ||
528b2c61 | 439 | clientUpdateStatHistCounters(logType, |
62e76326 | 440 | tvSubMsec(start, current_time)); |
441 | ||
528b2c61 | 442 | clientUpdateHierCounters(&request->hier); |
c8be6d7b | 443 | } |
444 | ||
edce4d98 | 445 | void |
190154cf | 446 | clientPrepareLogWithRequestDetails(HttpRequest * request, AccessLogEntry * aLogEntry) |
7a2f978b | 447 | { |
c8be6d7b | 448 | assert(request); |
449 | assert(aLogEntry); | |
7684c4b1 | 450 | |
451 | if (Config.onoff.log_mime_hdrs) { | |
452 | Packer p; | |
453 | MemBuf mb; | |
454 | memBufDefInit(&mb); | |
455 | packerToMemInit(&p, &mb); | |
456 | httpHeaderPackInto(&request->header, &p); | |
457 | aLogEntry->headers.request = xstrdup(mb.buf); | |
458 | packerClean(&p); | |
459 | memBufClean(&mb); | |
460 | } | |
461 | ||
c8be6d7b | 462 | aLogEntry->http.method = request->method; |
463 | aLogEntry->http.version = request->http_ver; | |
c8be6d7b | 464 | aLogEntry->hier = request->hier; |
62e76326 | 465 | |
abb929f0 | 466 | aLogEntry->cache.extuser = request->extacl_user.buf(); |
467 | ||
c8be6d7b | 468 | if (request->auth_user_request) { |
f5691f9c | 469 | |
470 | if (request->auth_user_request->username()) | |
f5292c64 | 471 | aLogEntry->cache.authuser = |
f5691f9c | 472 | xstrdup(request->auth_user_request->username()); |
f5292c64 | 473 | |
f5691f9c | 474 | request->auth_user_request->unlock(); |
f5292c64 | 475 | |
62e76326 | 476 | request->auth_user_request = NULL; |
7a2f978b | 477 | } |
c8be6d7b | 478 | } |
479 | ||
480 | void | |
528b2c61 | 481 | ClientHttpRequest::logRequest() |
482 | { | |
483 | if (out.size || logType) { | |
62e76326 | 484 | al.icp.opcode = ICP_INVALID; |
485 | al.url = log_uri; | |
486 | debug(33, 9) ("clientLogRequest: al.url='%s'\n", al.url); | |
487 | ||
0976f8db | 488 | if (loggingEntry() && loggingEntry()->mem_obj) { |
489 | al.http.code = loggingEntry()->mem_obj->getReply()->sline.status; | |
490 | al.http.content_type = loggingEntry()->mem_obj->getReply()->content_type.buf(); | |
491 | al.cache.objectSize = contentLen(loggingEntry()); | |
62e76326 | 492 | } |
493 | ||
a2ac85d9 | 494 | al.cache.caddr = getConn().getRaw() != NULL ? getConn()->log_addr : no_addr; |
62e76326 | 495 | al.cache.size = out.size; |
0976f8db | 496 | al.cache.highOffset = out.offset; |
62e76326 | 497 | al.cache.code = logType; |
498 | al.cache.msec = tvSubMsec(start, current_time); | |
499 | ||
500 | if (request) | |
501 | clientPrepareLogWithRequestDetails(request, &al); | |
502 | ||
a2ac85d9 | 503 | if (getConn().getRaw() != NULL && getConn()->rfc931[0]) |
98242069 | 504 | al.cache.rfc931 = getConn()->rfc931; |
62e76326 | 505 | |
a7ad6e4e | 506 | #if USE_SSL |
62e76326 | 507 | |
c588c5ba | 508 | if (getConn().getRaw() != NULL) |
98242069 | 509 | al.cache.ssluser = sslGetUserEmail(fd_table[getConn()->fd].ssl); |
62e76326 | 510 | |
a7ad6e4e | 511 | #endif |
62e76326 | 512 | |
7684c4b1 | 513 | ACLChecklist *checklist = clientAclChecklistCreate(Config.accessList.log, this); |
62e76326 | 514 | |
7684c4b1 | 515 | checklist->reply = al.reply; |
62e76326 | 516 | |
b448c119 | 517 | if (!Config.accessList.log || checklist->fastCheck()) { |
7684c4b1 | 518 | al.request = requestLink(request); |
519 | accessLogLog(&al, checklist); | |
520 | updateCounters(); | |
62e76326 | 521 | |
a2ac85d9 | 522 | if (getConn().getRaw() != NULL) |
98242069 | 523 | clientdbUpdate(getConn()->peer.sin_addr, logType, PROTO_HTTP, out.size); |
7684c4b1 | 524 | } |
525 | ||
526 | delete checklist; | |
527 | ||
528 | accessLogFreeMemory(&al); | |
7a2f978b | 529 | } |
c8be6d7b | 530 | } |
531 | ||
532 | void | |
528b2c61 | 533 | ClientHttpRequest::freeResources() |
c8be6d7b | 534 | { |
528b2c61 | 535 | safe_free(uri); |
536 | safe_free(log_uri); | |
537 | safe_free(redirect.location); | |
538 | range_iter.boundary.clean(); | |
539 | requestUnlink(request); | |
540 | request = NULL; | |
62e76326 | 541 | |
528b2c61 | 542 | if (client_stream.tail) |
62e76326 | 543 | clientStreamAbort((clientStreamNode *)client_stream.tail->data, this); |
c8be6d7b | 544 | } |
545 | ||
546 | void | |
547 | httpRequestFree(void *data) | |
548 | { | |
e6ccf245 | 549 | clientHttpRequest *http = (clientHttpRequest *)data; |
c8be6d7b | 550 | assert(http != NULL); |
528b2c61 | 551 | delete http; |
7a2f978b | 552 | } |
553 | ||
a46d2c0e | 554 | bool |
555 | ConnStateData::areAllContextsForThisConnection() const | |
c8be6d7b | 556 | { |
a46d2c0e | 557 | assert(this != NULL); |
0655fa4d | 558 | ClientSocketContext::Pointer context = getCurrentContext(); |
62e76326 | 559 | |
0655fa4d | 560 | while (context.getRaw()) { |
98242069 | 561 | if (context->http->getConn() != this) |
a46d2c0e | 562 | return false; |
62e76326 | 563 | |
564 | context = context->next; | |
c8be6d7b | 565 | } |
62e76326 | 566 | |
a46d2c0e | 567 | return true; |
c8be6d7b | 568 | } |
569 | ||
570 | void | |
a46d2c0e | 571 | ConnStateData::freeAllContexts() |
c8be6d7b | 572 | { |
0655fa4d | 573 | ClientSocketContext::Pointer context; |
62e76326 | 574 | |
0655fa4d | 575 | while ((context = getCurrentContext()).getRaw() != NULL) { |
576 | assert(getCurrentContext() != | |
577 | getCurrentContext()->next); | |
578 | context->connIsFinished(); | |
579 | assert (context != currentobject); | |
c8be6d7b | 580 | } |
581 | } | |
582 | ||
7a2f978b | 583 | /* This is a handler normally called by comm_close() */ |
584 | static void | |
585 | connStateFree(int fd, void *data) | |
586 | { | |
e6ccf245 | 587 | ConnStateData *connState = (ConnStateData *)data; |
a46d2c0e | 588 | assert (fd == connState->fd); |
a2ac85d9 | 589 | connState->close(); |
a46d2c0e | 590 | } |
62e76326 | 591 | |
a2ac85d9 | 592 | void |
593 | ConnStateData::close() | |
a46d2c0e | 594 | { |
a2ac85d9 | 595 | debug(33, 3) ("ConnStateData::close: FD %d\n", fd); |
7ce98c67 | 596 | openReference = NULL; |
a46d2c0e | 597 | clientdbEstablished(peer.sin_addr, -1); /* decrement */ |
598 | assert(areAllContextsForThisConnection()); | |
599 | freeAllContexts(); | |
f5691f9c | 600 | |
601 | if (auth_user_request != NULL) | |
602 | auth_user_request->onConnectionClose(this); | |
a2ac85d9 | 603 | } |
604 | ||
605 | bool | |
606 | ConnStateData::isOpen() const | |
607 | { | |
7ce98c67 | 608 | return openReference.getRaw() != NULL; |
a2ac85d9 | 609 | } |
610 | ||
611 | ConnStateData::~ConnStateData() | |
612 | { | |
613 | assert(this != NULL); | |
614 | debug(33, 3) ("ConnStateData::~ConnStateData: FD %d\n", fd); | |
615 | ||
616 | if (isOpen()) | |
617 | close(); | |
62e76326 | 618 | |
a46d2c0e | 619 | if (auth_user_request) |
f5691f9c | 620 | auth_user_request->unlock(); |
62e76326 | 621 | |
a46d2c0e | 622 | auth_user_request = NULL; |
62e76326 | 623 | |
a46d2c0e | 624 | pconnHistCount(0, nrequests); |
e0db3481 | 625 | |
626 | cbdataReferenceDone(port); | |
7a2f978b | 627 | } |
628 | ||
c68e9c6b | 629 | /* |
630 | * clientSetKeepaliveFlag() sets request->flags.proxy_keepalive. | |
631 | * This is the client-side persistent connection flag. We need | |
632 | * to set this relatively early in the request processing | |
633 | * to handle hacks for broken servers and clients. | |
634 | */ | |
635 | static void | |
636 | clientSetKeepaliveFlag(clientHttpRequest * http) | |
637 | { | |
190154cf | 638 | HttpRequest *request = http->request; |
c68e9c6b | 639 | const HttpHeader *req_hdr = &request->header; |
f6329bc3 | 640 | |
ccf44862 | 641 | debug(33, 3) ("clientSetKeepaliveFlag: http_ver = %d.%d\n", |
62e76326 | 642 | request->http_ver.major, request->http_ver.minor); |
c68e9c6b | 643 | debug(33, 3) ("clientSetKeepaliveFlag: method = %s\n", |
62e76326 | 644 | RequestMethodStr[request->method]); |
645 | ||
efd900cb | 646 | if (!Config.onoff.client_pconns) |
62e76326 | 647 | request->flags.proxy_keepalive = 0; |
f6329bc3 | 648 | else { |
450e0c10 | 649 | HttpVersion http_ver(1,0); |
62e76326 | 650 | /* we are HTTP/1.0, no matter what the client requests... */ |
651 | ||
652 | if (httpMsgIsPersistent(http_ver, req_hdr)) | |
653 | request->flags.proxy_keepalive = 1; | |
f6329bc3 | 654 | } |
c68e9c6b | 655 | } |
656 | ||
31be8b80 | 657 | static int |
190154cf | 658 | clientIsContentLengthValid(HttpRequest * r) |
31be8b80 | 659 | { |
ffc128c4 | 660 | switch (r->method) { |
62e76326 | 661 | |
ffc128c4 | 662 | case METHOD_PUT: |
62e76326 | 663 | |
ffc128c4 | 664 | case METHOD_POST: |
62e76326 | 665 | /* PUT/POST requires a request entity */ |
666 | return (r->content_length >= 0); | |
667 | ||
ffc128c4 | 668 | case METHOD_GET: |
62e76326 | 669 | |
ffc128c4 | 670 | case METHOD_HEAD: |
62e76326 | 671 | /* We do not want to see a request entity on GET/HEAD requests */ |
672 | return (r->content_length <= 0 || Config.onoff.request_entities); | |
673 | ||
ffc128c4 | 674 | default: |
62e76326 | 675 | /* For other types of requests we don't care */ |
676 | return 1; | |
ffc128c4 | 677 | } |
62e76326 | 678 | |
ffc128c4 | 679 | /* NOT REACHED */ |
31be8b80 | 680 | } |
681 | ||
cf50a0af | 682 | int |
c8be6d7b | 683 | clientIsRequestBodyValid(int bodyLength) |
7a2f978b | 684 | { |
c8be6d7b | 685 | if (bodyLength >= 0) |
62e76326 | 686 | return 1; |
687 | ||
7a2f978b | 688 | return 0; |
689 | } | |
690 | ||
c8be6d7b | 691 | int |
e6ccf245 | 692 | clientIsRequestBodyTooLargeForPolicy(size_t bodyLength) |
efd900cb | 693 | { |
c8be6d7b | 694 | if (Config.maxRequestBodySize && |
62e76326 | 695 | bodyLength > Config.maxRequestBodySize) |
696 | return 1; /* too large */ | |
697 | ||
efd900cb | 698 | return 0; |
699 | } | |
700 | ||
e4a67a80 | 701 | #ifndef PURIFY |
c8be6d7b | 702 | int |
a2ac85d9 | 703 | connIsUsable(ConnStateData::Pointer conn) |
c8be6d7b | 704 | { |
a2ac85d9 | 705 | if (conn.getRaw() == NULL || conn->fd == -1) |
62e76326 | 706 | return 0; |
707 | ||
c8be6d7b | 708 | return 1; |
709 | } | |
710 | ||
e4a67a80 | 711 | #endif |
712 | ||
0655fa4d | 713 | ClientSocketContext::Pointer |
714 | ConnStateData::getCurrentContext() const | |
c8be6d7b | 715 | { |
0655fa4d | 716 | assert(this); |
717 | return currentobject; | |
c8be6d7b | 718 | } |
719 | ||
720 | void | |
528b2c61 | 721 | ClientSocketContext::deferRecipientForLater(clientStreamNode * node, HttpReply * rep, StoreIOBuffer recievedData) |
722 | { | |
a46d2c0e | 723 | debug(33, 2) ("clientSocketRecipient: Deferring request %s\n", http->uri); |
528b2c61 | 724 | assert(flags.deferred == 0); |
725 | flags.deferred = 1; | |
726 | deferredparams.node = node; | |
727 | deferredparams.rep = rep; | |
728 | deferredparams.queuedBuffer = recievedData; | |
c8be6d7b | 729 | return; |
730 | } | |
731 | ||
732 | int | |
528b2c61 | 733 | responseFinishedOrFailed(HttpReply * rep, StoreIOBuffer const & recievedData) |
c8be6d7b | 734 | { |
735 | if (rep == NULL && recievedData.data == NULL && recievedData.length == 0) | |
62e76326 | 736 | return 1; |
737 | ||
c8be6d7b | 738 | return 0; |
739 | } | |
740 | ||
0655fa4d | 741 | bool |
742 | ClientSocketContext::startOfOutput() const | |
c8be6d7b | 743 | { |
0655fa4d | 744 | return http->out.size == 0; |
c8be6d7b | 745 | } |
746 | ||
528b2c61 | 747 | size_t |
2512d159 | 748 | ClientSocketContext::lengthToSend(Range<size_t> const &available) |
528b2c61 | 749 | { |
2512d159 | 750 | size_t maximum = available.size(); |
751 | ||
528b2c61 | 752 | if (!http->request->range) |
62e76326 | 753 | return maximum; |
754 | ||
528b2c61 | 755 | assert (canPackMoreRanges()); |
62e76326 | 756 | |
528b2c61 | 757 | if (http->range_iter.debt() == -1) |
62e76326 | 758 | return maximum; |
759 | ||
528b2c61 | 760 | assert (http->range_iter.debt() > 0); |
62e76326 | 761 | |
2512d159 | 762 | /* TODO this + the last line could be a range intersection calculation */ |
763 | if ((ssize_t)available.start < http->range_iter.currentSpec()->offset) | |
764 | return 0; | |
765 | ||
528b2c61 | 766 | return XMIN(http->range_iter.debt(), (ssize_t)maximum); |
767 | } | |
768 | ||
c8be6d7b | 769 | void |
528b2c61 | 770 | ClientSocketContext::noteSentBodyBytes(size_t bytes) |
771 | { | |
772 | http->out.offset += bytes; | |
62e76326 | 773 | |
528b2c61 | 774 | if (!http->request->range) |
62e76326 | 775 | return; |
776 | ||
528b2c61 | 777 | if (http->range_iter.debt() != -1) { |
62e76326 | 778 | http->range_iter.debt(http->range_iter.debt() - bytes); |
779 | assert (http->range_iter.debt() >= 0); | |
528b2c61 | 780 | } |
62e76326 | 781 | |
dd272b8e | 782 | /* debt() always stops at -1, below that is a bug */ |
783 | assert (http->range_iter.debt() >= -1); | |
528b2c61 | 784 | } |
62e76326 | 785 | |
528b2c61 | 786 | bool |
787 | ClientHttpRequest::multipartRangeRequest() const | |
788 | { | |
789 | return request->multipartRangeRequest(); | |
790 | } | |
791 | ||
792 | bool | |
793 | ClientSocketContext::multipartRangeRequest() const | |
794 | { | |
795 | return http->multipartRangeRequest(); | |
796 | } | |
797 | ||
798 | void | |
799 | ClientSocketContext::sendBody(HttpReply * rep, StoreIOBuffer bodyData) | |
c8be6d7b | 800 | { |
801 | assert(rep == NULL); | |
528b2c61 | 802 | |
803 | if (!multipartRangeRequest()) { | |
2512d159 | 804 | size_t length = lengthToSend(bodyData.range()); |
62e76326 | 805 | noteSentBodyBytes (length); |
806 | comm_write(fd(), bodyData.data, length, | |
807 | clientWriteBodyComplete, this); | |
808 | return; | |
528b2c61 | 809 | } |
810 | ||
811 | MemBuf mb; | |
812 | memBufDefInit(&mb); | |
2512d159 | 813 | packRange(bodyData, &mb); |
814 | ||
815 | if (mb.size) | |
816 | /* write */ | |
817 | comm_old_write_mbuf(fd(), mb, clientWriteComplete, this); | |
818 | else | |
819 | writeComplete(fd(), NULL, 0, COMM_OK); | |
c8be6d7b | 820 | } |
821 | ||
528b2c61 | 822 | /* put terminating boundary for multiparts */ |
823 | static void | |
824 | clientPackTermBound(String boundary, MemBuf * mb) | |
825 | { | |
826 | memBufPrintf(mb, "\r\n--%s--\r\n", boundary.buf()); | |
827 | debug(33, 6) ("clientPackTermBound: buf offset: %ld\n", (long int) mb->size); | |
828 | } | |
829 | ||
830 | /* appends a "part" HTTP header (as in a multi-part/range reply) to the buffer */ | |
831 | static void | |
832 | clientPackRangeHdr(const HttpReply * rep, const HttpHdrRangeSpec * spec, String boundary, MemBuf * mb) | |
833 | { | |
75faaa7a | 834 | HttpHeader hdr(hoReply); |
528b2c61 | 835 | Packer p; |
836 | assert(rep); | |
837 | assert(spec); | |
838 | ||
839 | /* put boundary */ | |
840 | debug(33, 5) ("clientPackRangeHdr: appending boundary: %s\n", boundary.buf()); | |
841 | /* rfc2046 requires to _prepend_ boundary with <crlf>! */ | |
842 | memBufPrintf(mb, "\r\n--%s\r\n", boundary.buf()); | |
843 | ||
844 | /* stuff the header with required entries and pack it */ | |
62e76326 | 845 | |
528b2c61 | 846 | if (httpHeaderHas(&rep->header, HDR_CONTENT_TYPE)) |
62e76326 | 847 | httpHeaderPutStr(&hdr, HDR_CONTENT_TYPE, httpHeaderGetStr(&rep->header, HDR_CONTENT_TYPE)); |
848 | ||
528b2c61 | 849 | httpHeaderAddContRange(&hdr, *spec, rep->content_length); |
62e76326 | 850 | |
528b2c61 | 851 | packerToMemInit(&p, mb); |
62e76326 | 852 | |
528b2c61 | 853 | httpHeaderPackInto(&hdr, &p); |
62e76326 | 854 | |
528b2c61 | 855 | packerClean(&p); |
62e76326 | 856 | |
528b2c61 | 857 | httpHeaderClean(&hdr); |
858 | ||
859 | /* append <crlf> (we packed a header, not a reply) */ | |
860 | memBufPrintf(mb, "\r\n"); | |
861 | } | |
862 | ||
863 | /* | |
864 | * extracts a "range" from *buf and appends them to mb, updating | |
865 | * all offsets and such. | |
866 | */ | |
c8be6d7b | 867 | void |
2512d159 | 868 | ClientSocketContext::packRange(StoreIOBuffer const &source, MemBuf * mb) |
528b2c61 | 869 | { |
870 | HttpHdrRangeIter * i = &http->range_iter; | |
2512d159 | 871 | Range<size_t> available (source.range()); |
b65351fa | 872 | char const *buf = source.data; |
62e76326 | 873 | |
2512d159 | 874 | while (i->currentSpec() && available.size()) { |
62e76326 | 875 | const size_t copy_sz = lengthToSend(available); |
62e76326 | 876 | |
2512d159 | 877 | if (copy_sz) { |
878 | /* | |
879 | * intersection of "have" and "need" ranges must not be empty | |
880 | */ | |
881 | assert(http->out.offset < i->currentSpec()->offset + i->currentSpec()->length); | |
882 | assert(http->out.offset + available.size() > (size_t)i->currentSpec()->offset); | |
883 | ||
884 | /* | |
885 | * put boundary and headers at the beginning of a range in a | |
886 | * multi-range | |
887 | */ | |
888 | ||
889 | if (http->multipartRangeRequest() && i->debt() == i->currentSpec()->length) { | |
890 | assert(http->memObject()); | |
891 | clientPackRangeHdr( | |
892 | http->memObject()->getReply(), /* original reply */ | |
893 | i->currentSpec(), /* current range */ | |
894 | i->boundary, /* boundary, the same for all */ | |
895 | mb); | |
896 | } | |
62e76326 | 897 | |
2512d159 | 898 | /* |
899 | * append content | |
900 | */ | |
901 | debug(33, 3) ("clientPackRange: appending %ld bytes\n", (long int) copy_sz); | |
62e76326 | 902 | |
2512d159 | 903 | noteSentBodyBytes (copy_sz); |
62e76326 | 904 | |
2512d159 | 905 | memBufAppend(mb, buf, copy_sz); |
62e76326 | 906 | |
2512d159 | 907 | /* |
908 | * update offsets | |
909 | */ | |
910 | available.start += copy_sz; | |
62e76326 | 911 | |
2512d159 | 912 | buf += copy_sz; |
62e76326 | 913 | |
2512d159 | 914 | } |
62e76326 | 915 | |
916 | /* | |
917 | * paranoid check | |
918 | */ | |
2512d159 | 919 | assert(available.size() >= 0 && i->debt() >= 0 || i->debt() == -1); |
62e76326 | 920 | |
c7329b75 | 921 | if (!canPackMoreRanges()) { |
922 | debug(33, 3) ("clientPackRange: Returning because !canPackMoreRanges.\n"); | |
923 | ||
924 | if (i->debt() == 0) | |
925 | /* put terminating boundary for multiparts */ | |
926 | clientPackTermBound(i->boundary, mb); | |
62e76326 | 927 | |
62e76326 | 928 | return; |
c7329b75 | 929 | } |
62e76326 | 930 | |
931 | off_t next = getNextRangeOffset(); | |
932 | ||
933 | assert (next >= http->out.offset); | |
934 | ||
935 | size_t skip = next - http->out.offset; | |
936 | ||
2512d159 | 937 | /* adjust for not to be transmitted bytes */ |
938 | http->out.offset = next; | |
939 | ||
940 | if (available.size() <= skip) | |
62e76326 | 941 | return; |
942 | ||
2512d159 | 943 | available.start += skip; |
62e76326 | 944 | |
2512d159 | 945 | buf += skip; |
946 | ||
947 | if (copy_sz == 0) | |
948 | return; | |
528b2c61 | 949 | } |
950 | } | |
951 | ||
952 | /* returns expected content length for multi-range replies | |
953 | * note: assumes that httpHdrRangeCanonize has already been called | |
954 | * warning: assumes that HTTP headers for individual ranges at the | |
955 | * time of the actuall assembly will be exactly the same as | |
956 | * the headers when clientMRangeCLen() is called */ | |
957 | int | |
958 | ClientHttpRequest::mRangeCLen() | |
c8be6d7b | 959 | { |
528b2c61 | 960 | int clen = 0; |
c8be6d7b | 961 | MemBuf mb; |
528b2c61 | 962 | |
86a2f789 | 963 | assert(memObject()); |
528b2c61 | 964 | |
965 | memBufDefInit(&mb); | |
966 | HttpHdrRange::iterator pos = request->range->begin(); | |
62e76326 | 967 | |
528b2c61 | 968 | while (pos != request->range->end()) { |
62e76326 | 969 | /* account for headers for this range */ |
970 | memBufReset(&mb); | |
86a2f789 | 971 | clientPackRangeHdr(memObject()->getReply(), |
62e76326 | 972 | *pos, range_iter.boundary, &mb); |
973 | clen += mb.size; | |
528b2c61 | 974 | |
62e76326 | 975 | /* account for range content */ |
976 | clen += (*pos)->length; | |
528b2c61 | 977 | |
62e76326 | 978 | debug(33, 6) ("clientMRangeCLen: (clen += %ld + %ld) == %d\n", |
979 | (long int) mb.size, (long int) (*pos)->length, clen); | |
980 | ++pos; | |
528b2c61 | 981 | } |
62e76326 | 982 | |
528b2c61 | 983 | /* account for the terminating boundary */ |
984 | memBufReset(&mb); | |
62e76326 | 985 | |
528b2c61 | 986 | clientPackTermBound(range_iter.boundary, &mb); |
62e76326 | 987 | |
528b2c61 | 988 | clen += mb.size; |
989 | ||
990 | memBufClean(&mb); | |
62e76326 | 991 | |
528b2c61 | 992 | return clen; |
993 | } | |
994 | ||
995 | /* | |
996 | * returns true if If-Range specs match reply, false otherwise | |
997 | */ | |
998 | static int | |
999 | clientIfRangeMatch(clientHttpRequest * http, HttpReply * rep) | |
1000 | { | |
1001 | const TimeOrTag spec = httpHeaderGetTimeOrTag(&http->request->header, HDR_IF_RANGE); | |
1002 | /* check for parsing falure */ | |
62e76326 | 1003 | |
528b2c61 | 1004 | if (!spec.valid) |
62e76326 | 1005 | return 0; |
1006 | ||
528b2c61 | 1007 | /* got an ETag? */ |
1008 | if (spec.tag.str) { | |
62e76326 | 1009 | ETag rep_tag = httpHeaderGetETag(&rep->header, HDR_ETAG); |
1010 | debug(33, 3) ("clientIfRangeMatch: ETags: %s and %s\n", | |
1011 | spec.tag.str, rep_tag.str ? rep_tag.str : "<none>"); | |
1012 | ||
1013 | if (!rep_tag.str) | |
1014 | return 0; /* entity has no etag to compare with! */ | |
1015 | ||
1016 | if (spec.tag.weak || rep_tag.weak) { | |
1017 | debug(33, 1) ("clientIfRangeMatch: Weak ETags are not allowed in If-Range: %s ? %s\n", | |
1018 | spec.tag.str, rep_tag.str); | |
1019 | return 0; /* must use strong validator for sub-range requests */ | |
1020 | } | |
1021 | ||
1022 | return etagIsEqual(&rep_tag, &spec.tag); | |
528b2c61 | 1023 | } |
62e76326 | 1024 | |
528b2c61 | 1025 | /* got modification time? */ |
1026 | if (spec.time >= 0) { | |
86a2f789 | 1027 | return http->storeEntry()->lastmod <= spec.time; |
528b2c61 | 1028 | } |
62e76326 | 1029 | |
528b2c61 | 1030 | assert(0); /* should not happen */ |
1031 | return 0; | |
1032 | } | |
1033 | ||
1034 | /* generates a "unique" boundary string for multipart responses | |
1035 | * the caller is responsible for cleaning the string */ | |
1036 | String | |
1037 | ClientHttpRequest::rangeBoundaryStr() const | |
1038 | { | |
1039 | assert(this); | |
1040 | const char *key; | |
1041 | String b (full_appname_string); | |
1042 | b.append (":",1); | |
86a2f789 | 1043 | key = storeEntry()->getMD5Text(); |
528b2c61 | 1044 | b.append(key, strlen(key)); |
1045 | return b; | |
1046 | } | |
1047 | ||
1048 | /* adds appropriate Range headers if needed */ | |
1049 | void | |
1050 | ClientSocketContext::buildRangeHeader(HttpReply * rep) | |
1051 | { | |
1052 | HttpHeader *hdr = rep ? &rep->header : 0; | |
1053 | const char *range_err = NULL; | |
190154cf | 1054 | HttpRequest *request = http->request; |
528b2c61 | 1055 | assert(request->range); |
1056 | /* check if we still want to do ranges */ | |
62e76326 | 1057 | |
528b2c61 | 1058 | if (!rep) |
62e76326 | 1059 | range_err = "no [parse-able] reply"; |
528b2c61 | 1060 | else if ((rep->sline.status != HTTP_OK) && (rep->sline.status != HTTP_PARTIAL_CONTENT)) |
62e76326 | 1061 | range_err = "wrong status code"; |
1062 | ||
528b2c61 | 1063 | #if 0 |
62e76326 | 1064 | |
528b2c61 | 1065 | else if (httpHeaderHas(hdr, HDR_CONTENT_RANGE)) |
62e76326 | 1066 | range_err = "origin server does ranges"; |
1067 | ||
528b2c61 | 1068 | #endif |
62e76326 | 1069 | |
528b2c61 | 1070 | else if (rep->content_length < 0) |
62e76326 | 1071 | range_err = "unknown length"; |
86a2f789 | 1072 | else if (rep->content_length != http->memObject()->getReply()->content_length) |
62e76326 | 1073 | range_err = "INCONSISTENT length"; /* a bug? */ |
96cbbe03 | 1074 | |
1075 | /* hits only - upstream peer determines correct behaviour on misses, and client_side_reply determines | |
1076 | * hits candidates | |
1077 | */ | |
1078 | else if (logTypeIsATcpHit(http->logType) && httpHeaderHas(&http->request->header, HDR_IF_RANGE) && !clientIfRangeMatch(http, rep)) | |
62e76326 | 1079 | range_err = "If-Range match failed"; |
528b2c61 | 1080 | else if (!http->request->range->canonize(rep)) |
62e76326 | 1081 | range_err = "canonization failed"; |
528b2c61 | 1082 | else if (http->request->range->isComplex()) |
62e76326 | 1083 | range_err = "too complex range header"; |
62e76326 | 1084 | |
528b2c61 | 1085 | #if 0 |
62e76326 | 1086 | |
528b2c61 | 1087 | else if (!logTypeIsATcpHit(http->logType); && http->request->range->offsetLimitExceeded()) |
62e76326 | 1088 | range_err = "range outside range_offset_limit"; |
1089 | ||
c8be6d7b | 1090 | #endif |
528b2c61 | 1091 | /* get rid of our range specs on error */ |
1092 | if (range_err) { | |
b631f31c | 1093 | /* XXX We do this here because we need canonisation etc. However, this current |
1094 | * code will lead to incorrect store offset requests - the store will have the | |
1095 | * offset data, but we won't be requesting it. | |
1096 | * So, we can either re-request, or generate an error | |
1097 | */ | |
62e76326 | 1098 | debug(33, 3) ("clientBuildRangeHeader: will not do ranges: %s.\n", range_err); |
00d77d6b | 1099 | delete http->request->range; |
62e76326 | 1100 | http->request->range = NULL; |
c8be6d7b | 1101 | } else { |
3cff087f | 1102 | /* XXX: TODO: Review, this unconditional set may be wrong. - TODO: review. */ |
1103 | httpStatusLineSet(&rep->sline, rep->sline.version, | |
1104 | HTTP_PARTIAL_CONTENT, NULL); | |
62e76326 | 1105 | const int spec_count = http->request->range->specs.count; |
1106 | int actual_clen = -1; | |
1107 | ||
1108 | debug(33, 3) ("clientBuildRangeHeader: range spec count: %d virgin clen: %d\n", | |
1109 | spec_count, rep->content_length); | |
1110 | assert(spec_count > 0); | |
1111 | /* ETags should not be returned with Partial Content replies? */ | |
1112 | httpHeaderDelById(hdr, HDR_ETAG); | |
1113 | /* append appropriate header(s) */ | |
1114 | ||
1115 | if (spec_count == 1) { | |
1116 | HttpHdrRange::iterator pos = http->request->range->begin(); | |
1117 | assert(*pos); | |
1118 | /* append Content-Range */ | |
1119 | ||
1120 | if (!httpHeaderHas(hdr, HDR_CONTENT_RANGE)) { | |
1121 | /* No content range, so this was a full object we are | |
1122 | * sending parts of. | |
1123 | */ | |
1124 | httpHeaderAddContRange(hdr, **pos, rep->content_length); | |
1125 | } | |
1126 | ||
1127 | /* set new Content-Length to the actual number of bytes | |
1128 | * transmitted in the message-body */ | |
1129 | actual_clen = (*pos)->length; | |
1130 | } else { | |
1131 | /* multipart! */ | |
1132 | /* generate boundary string */ | |
1133 | http->range_iter.boundary = http->rangeBoundaryStr(); | |
1134 | /* delete old Content-Type, add ours */ | |
1135 | httpHeaderDelById(hdr, HDR_CONTENT_TYPE); | |
1136 | httpHeaderPutStrf(hdr, HDR_CONTENT_TYPE, | |
1137 | "multipart/byteranges; boundary=\"%s\"", | |
1138 | http->range_iter.boundary.buf()); | |
1139 | /* Content-Length is not required in multipart responses | |
1140 | * but it is always nice to have one */ | |
1141 | actual_clen = http->mRangeCLen(); | |
2512d159 | 1142 | /* http->out needs to start where we want data at */ |
1143 | http->out.offset = http->range_iter.currentSpec()->offset; | |
62e76326 | 1144 | } |
1145 | ||
1146 | /* replace Content-Length header */ | |
1147 | assert(actual_clen >= 0); | |
1148 | ||
1149 | httpHeaderDelById(hdr, HDR_CONTENT_LENGTH); | |
1150 | ||
1151 | httpHeaderPutInt(hdr, HDR_CONTENT_LENGTH, actual_clen); | |
1152 | ||
1153 | debug(33, 3) ("clientBuildRangeHeader: actual content length: %d\n", actual_clen); | |
1154 | ||
1155 | /* And start the range iter off */ | |
1156 | http->range_iter.updateSpec(); | |
c8be6d7b | 1157 | } |
528b2c61 | 1158 | } |
1159 | ||
1160 | void | |
1161 | ClientSocketContext::prepareReply(HttpReply * rep) | |
1162 | { | |
1163 | if (http->request->range) | |
62e76326 | 1164 | buildRangeHeader(rep); |
528b2c61 | 1165 | } |
1166 | ||
1167 | void | |
1168 | ClientSocketContext::sendStartOfMessage(HttpReply * rep, StoreIOBuffer bodyData) | |
1169 | { | |
1170 | prepareReply(rep); | |
1171 | /* init mb; put status line and headers if any */ | |
1172 | assert (rep); | |
1173 | MemBuf mb = httpReplyPack(rep); | |
1174 | /* Save length of headers for persistent conn checks */ | |
1175 | http->out.headers_sz = mb.size; | |
1176 | #if HEADERS_LOG | |
62e76326 | 1177 | |
528b2c61 | 1178 | headersLog(0, 0, http->request->method, rep); |
1179 | #endif | |
62e76326 | 1180 | |
c8be6d7b | 1181 | if (bodyData.data && bodyData.length) { |
62e76326 | 1182 | if (!multipartRangeRequest()) { |
2512d159 | 1183 | size_t length = lengthToSend(bodyData.range()); |
62e76326 | 1184 | noteSentBodyBytes (length); |
1185 | ||
1186 | memBufAppend(&mb, bodyData.data, length); | |
1187 | } else { | |
2512d159 | 1188 | packRange(bodyData, &mb); |
62e76326 | 1189 | } |
c8be6d7b | 1190 | } |
62e76326 | 1191 | |
c8be6d7b | 1192 | /* write */ |
528b2c61 | 1193 | comm_old_write_mbuf(fd(), mb, clientWriteComplete, this); |
62e76326 | 1194 | |
c8be6d7b | 1195 | /* if we don't do it, who will? */ |
1196 | } | |
1197 | ||
2246b732 | 1198 | /* |
edce4d98 | 1199 | * Write a chunk of data to a client socket. If the reply is present, send the reply headers down the wire too, |
1200 | * and clean them up when finished. | |
1201 | * Pre-condition: | |
1202 | * The request is one backed by a connection, not an internal request. | |
1203 | * data context is not NULL | |
1204 | * There are no more entries in the stream chain. | |
2246b732 | 1205 | */ |
edce4d98 | 1206 | static void |
1207 | clientSocketRecipient(clientStreamNode * node, clientHttpRequest * http, | |
62e76326 | 1208 | HttpReply * rep, StoreIOBuffer recievedData) |
edce4d98 | 1209 | { |
1210 | int fd; | |
edce4d98 | 1211 | /* Test preconditions */ |
1212 | assert(node != NULL); | |
62e76326 | 1213 | /* TODO: handle this rather than asserting |
c8be6d7b | 1214 | * - it should only ever happen if we cause an abort and |
edce4d98 | 1215 | * the callback chain loops back to here, so we can simply return. |
1216 | * However, that itself shouldn't happen, so it stays as an assert for now. | |
1217 | */ | |
1218 | assert(cbdataReferenceValid(node)); | |
edce4d98 | 1219 | assert(node->node.next == NULL); |
0655fa4d | 1220 | ClientSocketContext::Pointer context = dynamic_cast<ClientSocketContext *>(node->data.getRaw()); |
1221 | assert(context.getRaw() != NULL); | |
98242069 | 1222 | assert(connIsUsable(http->getConn())); |
1223 | fd = http->getConn()->fd; | |
528b2c61 | 1224 | /* TODO: check offset is what we asked for */ |
62e76326 | 1225 | |
98242069 | 1226 | if (context != http->getConn()->getCurrentContext()) { |
62e76326 | 1227 | context->deferRecipientForLater(node, rep, recievedData); |
1228 | return; | |
edce4d98 | 1229 | } |
62e76326 | 1230 | |
c8be6d7b | 1231 | if (responseFinishedOrFailed(rep, recievedData)) { |
0655fa4d | 1232 | context->writeComplete(fd, NULL, 0, COMM_OK); |
62e76326 | 1233 | return; |
edce4d98 | 1234 | } |
62e76326 | 1235 | |
0655fa4d | 1236 | if (!context->startOfOutput()) |
62e76326 | 1237 | context->sendBody(rep, recievedData); |
7684c4b1 | 1238 | else { |
1239 | http->al.reply = rep; | |
62e76326 | 1240 | context->sendStartOfMessage(rep, recievedData); |
7684c4b1 | 1241 | } |
edce4d98 | 1242 | } |
1243 | ||
62e76326 | 1244 | /* Called when a downstream node is no longer interested in |
edce4d98 | 1245 | * our data. As we are a terminal node, this means on aborts |
1246 | * only | |
1247 | */ | |
1248 | void | |
1249 | clientSocketDetach(clientStreamNode * node, clientHttpRequest * http) | |
1250 | { | |
edce4d98 | 1251 | /* Test preconditions */ |
1252 | assert(node != NULL); | |
62e76326 | 1253 | /* TODO: handle this rather than asserting |
c8be6d7b | 1254 | * - it should only ever happen if we cause an abort and |
edce4d98 | 1255 | * the callback chain loops back to here, so we can simply return. |
1256 | * However, that itself shouldn't happen, so it stays as an assert for now. | |
1257 | */ | |
1258 | assert(cbdataReferenceValid(node)); | |
1259 | /* Set null by ContextFree */ | |
edce4d98 | 1260 | assert(node->node.next == NULL); |
0655fa4d | 1261 | /* this is the assert discussed above */ |
e4a67a80 | 1262 | assert(NULL == dynamic_cast<ClientSocketContext *>(node->data.getRaw())); |
edce4d98 | 1263 | /* We are only called when the client socket shutsdown. |
1264 | * Tell the prev pipeline member we're finished | |
1265 | */ | |
1266 | clientStreamDetach(node, http); | |
7a2f978b | 1267 | } |
1268 | ||
f4f278b5 | 1269 | static void |
25f651c1 | 1270 | clientWriteBodyComplete(int fd, char *buf, size_t size, comm_err_t errflag, int xerrno, void *data) |
f4f278b5 | 1271 | { |
c8be6d7b | 1272 | clientWriteComplete(fd, NULL, size, errflag, data); |
1273 | } | |
1274 | ||
1275 | void | |
a46d2c0e | 1276 | ConnStateData::readNextRequest() |
c8be6d7b | 1277 | { |
a46d2c0e | 1278 | debug(33, 5) ("ConnStateData::readNextRequest: FD %d reading next req\n", fd); |
1279 | fd_note(fd, "Waiting for next request"); | |
f4f278b5 | 1280 | /* |
c8be6d7b | 1281 | * Set the timeout BEFORE calling clientReadRequest(). |
1282 | */ | |
a46d2c0e | 1283 | commSetTimeout(fd, Config.Timeout.persistent_request, |
1284 | requestTimeout, this); | |
1285 | readSomeData(); | |
c4b7a5a9 | 1286 | /* Please don't do anything with the FD past here! */ |
c8be6d7b | 1287 | } |
1288 | ||
1289 | void | |
a2ac85d9 | 1290 | ClientSocketContextPushDeferredIfNeeded(ClientSocketContext::Pointer deferredRequest, ConnStateData::Pointer & conn) |
c8be6d7b | 1291 | { |
528b2c61 | 1292 | debug(33, 2) ("ClientSocketContextPushDeferredIfNeeded: FD %d Sending next\n", |
62e76326 | 1293 | conn->fd); |
c8be6d7b | 1294 | /* If the client stream is waiting on a socket write to occur, then */ |
62e76326 | 1295 | |
c8be6d7b | 1296 | if (deferredRequest->flags.deferred) { |
62e76326 | 1297 | /* NO data is allowed to have been sent */ |
1298 | assert(deferredRequest->http->out.size == 0); | |
1299 | clientSocketRecipient(deferredRequest->deferredparams.node, | |
1300 | deferredRequest->http, | |
1301 | deferredRequest->deferredparams.rep, | |
1302 | deferredRequest->deferredparams.queuedBuffer); | |
c8be6d7b | 1303 | } |
62e76326 | 1304 | |
c8be6d7b | 1305 | /* otherwise, the request is still active in a callbacksomewhere, |
1306 | * and we are done | |
f4f278b5 | 1307 | */ |
f4f278b5 | 1308 | } |
1309 | ||
0655fa4d | 1310 | void |
1311 | ClientSocketContext::keepaliveNextRequest() | |
1a92a1e2 | 1312 | { |
a2ac85d9 | 1313 | ConnStateData::Pointer conn = http->getConn(); |
f900210a | 1314 | bool do_next_read = false; |
bd4e6ec8 | 1315 | |
0655fa4d | 1316 | debug(33, 3) ("ClientSocketContext::keepaliveNextRequest: FD %d\n", conn->fd); |
1317 | connIsFinished(); | |
1318 | ||
f900210a | 1319 | /* |
1320 | * Attempt to parse a request from the request buffer. | |
1321 | * If we've been fed a pipelined request it may already | |
1322 | * be in our read buffer. | |
1323 | * | |
1324 | * This needs to fall through - if we're unlucky and parse the _last_ request | |
1325 | * from our read buffer we may never re-register for another client read. | |
1326 | */ | |
1327 | ||
1328 | if (clientParseRequest(conn, do_next_read)) { | |
1329 | debug(33, 3) ("clientSocketContext::keepaliveNextRequest: FD %d: parsed next request from buffer\n", conn->fd); | |
1330 | } | |
1331 | ||
1332 | /* | |
1333 | * Either we need to kick-start another read or, if we have | |
1334 | * a half-closed connection, kill it after the last request. | |
1335 | * This saves waiting for half-closed connections to finished being | |
1336 | * half-closed _AND_ then, sometimes, spending "Timeout" time in | |
1337 | * the keepalive "Waiting for next request" state. | |
1338 | */ | |
1339 | if (commIsHalfClosed(conn->fd) && (conn->getConcurrentRequestCount() == 0)) { | |
1340 | debug(33, 3) ("ClientSocketContext::keepaliveNextRequest: half-closed client with no pending requests, closing\n"); | |
1341 | comm_close(conn->fd); | |
1342 | return; | |
1343 | } | |
1344 | ||
0655fa4d | 1345 | ClientSocketContext::Pointer deferredRequest; |
62e76326 | 1346 | |
f900210a | 1347 | /* |
1348 | * At this point we either have a parsed request (which we've | |
1349 | * kicked off the processing for) or not. If we have a deferred | |
1350 | * request (parsed but deferred for pipeling processing reasons) | |
1351 | * then look at processing it. If not, simply kickstart | |
1352 | * another read. | |
1353 | */ | |
1354 | ||
1355 | if ((deferredRequest = conn->getCurrentContext()).getRaw()) { | |
1356 | debug(33, 3) ("ClientSocketContext:: FD %d: calling PushDeferredIfNeeded\n", conn->fd); | |
62e76326 | 1357 | ClientSocketContextPushDeferredIfNeeded(deferredRequest, conn); |
f900210a | 1358 | } else { |
1359 | debug(33, 3) ("ClientSocketContext:: FD %d: calling conn->readNextRequest()\n", conn->fd); | |
1360 | conn->readNextRequest(); | |
1361 | } | |
1a92a1e2 | 1362 | } |
1363 | ||
c8be6d7b | 1364 | void |
1365 | clientUpdateSocketStats(log_type logType, size_t size) | |
1366 | { | |
1367 | if (size == 0) | |
62e76326 | 1368 | return; |
1369 | ||
c8be6d7b | 1370 | kb_incr(&statCounter.client_http.kbytes_out, size); |
62e76326 | 1371 | |
c8be6d7b | 1372 | if (logTypeIsATcpHit(logType)) |
62e76326 | 1373 | kb_incr(&statCounter.client_http.hit_kbytes_out, size); |
c8be6d7b | 1374 | } |
1375 | ||
528b2c61 | 1376 | /* returns true if there is still data available to pack more ranges |
1377 | * increments iterator "i" | |
1378 | * used by clientPackMoreRanges */ | |
1379 | bool | |
1380 | ClientSocketContext::canPackMoreRanges() const | |
1381 | { | |
1382 | /* first update "i" if needed */ | |
62e76326 | 1383 | |
528b2c61 | 1384 | if (!http->range_iter.debt()) { |
62e76326 | 1385 | debug (33,5)("ClientSocketContext::canPackMoreRanges: At end of current range spec for fd %d\n",fd()); |
1386 | ||
1387 | if (http->range_iter.pos.incrementable()) | |
1388 | ++http->range_iter.pos; | |
1389 | ||
1390 | http->range_iter.updateSpec(); | |
528b2c61 | 1391 | } |
62e76326 | 1392 | |
528b2c61 | 1393 | assert(!http->range_iter.debt() == !http->range_iter.currentSpec()); |
1394 | /* paranoid sync condition */ | |
1395 | /* continue condition: need_more_data */ | |
1396 | debug (33,5)("ClientSocketContext::canPackMoreRanges: returning %d\n", http->range_iter.currentSpec() ? true : false); | |
1397 | return http->range_iter.currentSpec() ? true : false; | |
1398 | } | |
1399 | ||
1400 | off_t | |
1401 | ClientSocketContext::getNextRangeOffset() const | |
1402 | { | |
1403 | if (http->request->range) { | |
62e76326 | 1404 | /* offset in range specs does not count the prefix of an http msg */ |
3d882db7 | 1405 | debug (33,5) ("ClientSocketContext::getNextRangeOffset: http offset %lu\n", (long unsigned)http->out.offset); |
62e76326 | 1406 | /* check: reply was parsed and range iterator was initialized */ |
1407 | assert(http->range_iter.valid); | |
1408 | /* filter out data according to range specs */ | |
1409 | assert (canPackMoreRanges()); | |
1410 | { | |
1411 | off_t start; /* offset of still missing data */ | |
1412 | assert(http->range_iter.currentSpec()); | |
1413 | start = http->range_iter.currentSpec()->offset + http->range_iter.currentSpec()->length - http->range_iter.debt(); | |
1414 | debug(33, 3) ("clientPackMoreRanges: in: offset: %ld\n", | |
1415 | (long int) http->out.offset); | |
1416 | debug(33, 3) ("clientPackMoreRanges: out: start: %ld spec[%ld]: [%ld, %ld), len: %ld debt: %ld\n", | |
1417 | (long int) start, (long int) (http->range_iter.pos - http->request->range->begin()), (long int) http->range_iter.currentSpec()->offset, (long int) (http->range_iter.currentSpec()->offset + http->range_iter.currentSpec()->length), (long int) http->range_iter.currentSpec()->length, (long int) http->range_iter.debt()); | |
1418 | ||
1419 | if (http->range_iter.currentSpec()->length != -1) | |
1420 | assert(http->out.offset <= start); /* we did not miss it */ | |
1421 | ||
1422 | return start; | |
1423 | } | |
1424 | ||
528b2c61 | 1425 | } |
1426 | ||
1427 | return http->out.offset; | |
1428 | } | |
1429 | ||
c8be6d7b | 1430 | void |
528b2c61 | 1431 | ClientSocketContext::pullData() |
c8be6d7b | 1432 | { |
528b2c61 | 1433 | debug (33,5)("ClientSocketContext::pullData: FD %d attempting to pull upstream data\n", fd()); |
c8be6d7b | 1434 | /* More data will be coming from the stream. */ |
528b2c61 | 1435 | StoreIOBuffer readBuffer; |
1436 | /* XXX: Next requested byte in the range sequence */ | |
1437 | /* XXX: length = getmaximumrangelenfgth */ | |
1438 | readBuffer.offset = getNextRangeOffset(); | |
c8be6d7b | 1439 | readBuffer.length = HTTP_REQBUF_SZ; |
528b2c61 | 1440 | readBuffer.data = reqbuf; |
1441 | /* we may note we have reached the end of the wanted ranges */ | |
1442 | clientStreamRead(getTail(), http, readBuffer); | |
1443 | } | |
1444 | ||
62e76326 | 1445 | clientStream_status_t |
528b2c61 | 1446 | ClientSocketContext::socketState() |
1447 | { | |
1448 | switch (clientStreamStatus(getTail(), http)) { | |
62e76326 | 1449 | |
1450 | case STREAM_NONE: | |
528b2c61 | 1451 | /* check for range support ending */ |
62e76326 | 1452 | |
528b2c61 | 1453 | if (http->request->range) { |
62e76326 | 1454 | /* check: reply was parsed and range iterator was initialized */ |
1455 | assert(http->range_iter.valid); | |
1456 | /* filter out data according to range specs */ | |
1457 | ||
1458 | if (!canPackMoreRanges()) { | |
1459 | debug (33,5)("ClientSocketContext::socketState: Range request has hit end of returnable range sequence on fd %d\n", fd()); | |
1460 | ||
1461 | if (http->request->flags.proxy_keepalive) | |
1462 | return STREAM_COMPLETE; | |
1463 | else | |
1464 | return STREAM_UNPLANNED_COMPLETE; | |
1465 | } | |
1466 | } | |
1467 | ||
1468 | return STREAM_NONE; | |
1469 | ||
1470 | case STREAM_COMPLETE: | |
528b2c61 | 1471 | return STREAM_COMPLETE; |
62e76326 | 1472 | |
1473 | case STREAM_UNPLANNED_COMPLETE: | |
1474 | return STREAM_UNPLANNED_COMPLETE; | |
1475 | ||
1476 | case STREAM_FAILED: | |
1477 | return STREAM_FAILED; | |
528b2c61 | 1478 | } |
62e76326 | 1479 | |
528b2c61 | 1480 | fatal ("unreachable code\n"); |
1481 | return STREAM_NONE; | |
c8be6d7b | 1482 | } |
edce4d98 | 1483 | |
1484 | /* A write has just completed to the client, or we have just realised there is | |
1485 | * no more data to send. | |
1486 | */ | |
e6ccf245 | 1487 | void |
1488 | clientWriteComplete(int fd, char *bufnotused, size_t size, comm_err_t errflag, void *data) | |
7a2f978b | 1489 | { |
528b2c61 | 1490 | ClientSocketContext *context = (ClientSocketContext *)data; |
0655fa4d | 1491 | context->writeComplete (fd, bufnotused, size, errflag); |
1492 | } | |
1493 | ||
55e44db9 | 1494 | void |
1495 | ClientSocketContext::doClose() | |
1496 | { | |
1497 | comm_close(fd()); | |
1498 | } | |
1499 | ||
1500 | void | |
1501 | ClientSocketContext::initiateClose() | |
1502 | { | |
1503 | if (!http || !http->getConn().getRaw()) { | |
1504 | doClose(); | |
1505 | return; | |
1506 | } | |
1507 | ||
1508 | if (http->getConn()->body.size_left > 0) { | |
1509 | debug(33, 5) ("ClientSocketContext::initiateClose: closing, but first we need to read the rest of the request\n"); | |
1510 | /* XXX We assumes the reply does fit in the TCP transmit window. | |
1511 | * If not the connection may stall while sending the reply | |
1512 | * (before reaching here) if the client does not try to read the | |
1513 | * response while sending the request body. As of yet we have | |
1514 | * not received any complaints indicating this may be an issue. | |
1515 | */ | |
1516 | http->getConn()->closing(true); | |
1517 | clientAbortBody(http->request); | |
1518 | return; | |
1519 | } | |
1520 | ||
1521 | doClose(); | |
1522 | } | |
1523 | ||
0655fa4d | 1524 | void |
1525 | ClientSocketContext::writeComplete(int fd, char *bufnotused, size_t size, comm_err_t errflag) | |
1526 | { | |
86a2f789 | 1527 | StoreEntry *entry = http->storeEntry(); |
7a2f978b | 1528 | http->out.size += size; |
c8be6d7b | 1529 | assert(fd > -1); |
e4049756 | 1530 | debugs(33, 5, "clientWriteComplete: FD " << fd << ", sz " << size << |
1531 | ", err " << errflag << ", off " << http->out.size << ", len " << | |
1532 | entry ? objectLen(entry) : 0); | |
c8be6d7b | 1533 | clientUpdateSocketStats(http->logType, size); |
55e44db9 | 1534 | assert (this->fd() == fd); |
62e76326 | 1535 | |
c8be6d7b | 1536 | if (errflag || clientHttpRequestStatus(fd, http)) { |
62e76326 | 1537 | debug (33,5)("clientWriteComplete: FD %d, closing connection due to failure, or true requeststatus\n", fd); |
55e44db9 | 1538 | initiateClose(); |
62e76326 | 1539 | /* Do we leak here ? */ |
1540 | return; | |
edce4d98 | 1541 | } |
62e76326 | 1542 | |
0655fa4d | 1543 | switch (socketState()) { |
62e76326 | 1544 | |
edce4d98 | 1545 | case STREAM_NONE: |
0655fa4d | 1546 | pullData(); |
62e76326 | 1547 | break; |
1548 | ||
edce4d98 | 1549 | case STREAM_COMPLETE: |
62e76326 | 1550 | debug(33, 5) ("clientWriteComplete: FD %d Keeping Alive\n", fd); |
0655fa4d | 1551 | keepaliveNextRequest(); |
62e76326 | 1552 | return; |
1553 | ||
edce4d98 | 1554 | case STREAM_UNPLANNED_COMPLETE: |
62e76326 | 1555 | /* fallthrough */ |
1556 | ||
edce4d98 | 1557 | case STREAM_FAILED: |
55e44db9 | 1558 | initiateClose(); |
62e76326 | 1559 | return; |
1560 | ||
edce4d98 | 1561 | default: |
62e76326 | 1562 | fatal("Hit unreachable code in clientWriteComplete\n"); |
7a2f978b | 1563 | } |
1564 | } | |
1565 | ||
e6ccf245 | 1566 | extern "C" CSR clientGetMoreData; |
1567 | extern "C" CSS clientReplyStatus; | |
1568 | extern "C" CSD clientReplyDetach; | |
edce4d98 | 1569 | |
528b2c61 | 1570 | static ClientSocketContext * |
a2ac85d9 | 1571 | parseHttpRequestAbort(ConnStateData::Pointer & conn, const char *uri) |
038eb4ed | 1572 | { |
edce4d98 | 1573 | clientHttpRequest *http; |
528b2c61 | 1574 | ClientSocketContext *context; |
1575 | StoreIOBuffer tempBuffer; | |
1576 | http = new ClientHttpRequest; | |
98242069 | 1577 | http->setConn(conn); |
c8be6d7b | 1578 | http->req_sz = conn->in.notYetUsed; |
edce4d98 | 1579 | http->uri = xstrdup(uri); |
c4b7a5a9 | 1580 | setLogUri (http, uri); |
528b2c61 | 1581 | context = ClientSocketContextNew(http); |
c8be6d7b | 1582 | tempBuffer.data = context->reqbuf; |
1583 | tempBuffer.length = HTTP_REQBUF_SZ; | |
edce4d98 | 1584 | clientStreamInit(&http->client_stream, clientGetMoreData, clientReplyDetach, |
0655fa4d | 1585 | clientReplyStatus, new clientReplyContext(http), clientSocketRecipient, |
62e76326 | 1586 | clientSocketDetach, context, tempBuffer); |
edce4d98 | 1587 | dlinkAdd(http, &http->active, &ClientActiveRequests); |
1588 | return context; | |
038eb4ed | 1589 | } |
1590 | ||
528b2c61 | 1591 | ClientSocketContext * |
a2ac85d9 | 1592 | clientParseRequestMethod(char *inbuf, method_t * method_p, ConnStateData::Pointer & conn) |
c8be6d7b | 1593 | { |
1594 | char *mstr = NULL; | |
62e76326 | 1595 | |
edce4d98 | 1596 | if ((mstr = strtok(inbuf, "\t ")) == NULL) { |
62e76326 | 1597 | debug(33, 1) ("clientParseRequestMethod: Can't get request method\n"); |
1598 | return parseHttpRequestAbort(conn, "error:invalid-request"); | |
7ddc902f | 1599 | } |
62e76326 | 1600 | |
edce4d98 | 1601 | *method_p = urlParseMethod(mstr); |
62e76326 | 1602 | |
edce4d98 | 1603 | if (*method_p == METHOD_NONE) { |
62e76326 | 1604 | debug(33, 1) ("clientParseRequestMethod: Unsupported method '%s'\n", mstr); |
1605 | return parseHttpRequestAbort(conn, "error:unsupported-request-method"); | |
50ddd7a4 | 1606 | } |
62e76326 | 1607 | |
c8be6d7b | 1608 | debug(33, 5) ("clientParseRequestMethod: Method is '%s'\n", mstr); |
1609 | return NULL; | |
1610 | } | |
1611 | ||
1612 | char * | |
1613 | skipLeadingSpace(char *aString) | |
1614 | { | |
1615 | char *result = aString; | |
62e76326 | 1616 | |
c8be6d7b | 1617 | while (xisspace(*aString)) |
62e76326 | 1618 | ++aString; |
1619 | ||
c8be6d7b | 1620 | return result; |
1621 | } | |
1622 | ||
3f38a55e | 1623 | static char * |
c8be6d7b | 1624 | findTrailingHTTPVersion(char *uriAndHTTPVersion) |
1625 | { | |
3f38a55e | 1626 | char *token; |
62e76326 | 1627 | |
3f38a55e | 1628 | for (token = strchr(uriAndHTTPVersion, '\n'); token > uriAndHTTPVersion; token--) { |
62e76326 | 1629 | if (*token == '\n' || *token == '\r') |
1630 | continue; | |
1631 | ||
1632 | if (xisspace(*token)) { | |
1633 | if (strncasecmp(token + 1, "HTTP/", 5) == 0) | |
1634 | return token + 1; | |
1635 | else | |
1636 | break; | |
1637 | } | |
c8be6d7b | 1638 | } |
62e76326 | 1639 | |
3f38a55e | 1640 | return NULL; |
c8be6d7b | 1641 | } |
1642 | ||
3f38a55e | 1643 | #if UNUSED_CODE |
c8be6d7b | 1644 | void |
1645 | trimTrailingSpaces(char *aString, size_t len) | |
1646 | { | |
1647 | char *endPointer = aString + len; | |
62e76326 | 1648 | |
c8be6d7b | 1649 | while (endPointer > aString && xisspace(*endPointer)) |
62e76326 | 1650 | *(endPointer--) = '\0'; |
c8be6d7b | 1651 | } |
62e76326 | 1652 | |
3f38a55e | 1653 | #endif |
50ddd7a4 | 1654 | |
3f38a55e | 1655 | static ClientSocketContext * |
450e0c10 | 1656 | parseURIandHTTPVersion(char **url_p, HttpVersion * http_ver_p, |
a2ac85d9 | 1657 | ConnStateData::Pointer & conn, char *http_version_str) |
c8be6d7b | 1658 | { |
1659 | char *url; | |
3f38a55e | 1660 | char *t; |
1661 | /* look for URL (strtok initiated by clientParseRequestMethod) */ | |
62e76326 | 1662 | |
edce4d98 | 1663 | if ((url = strtok(NULL, "\n")) == NULL) { |
62e76326 | 1664 | debug(33, 1) ("parseHttpRequest: Missing URL\n"); |
1665 | return parseHttpRequestAbort(conn, "error:missing-url"); | |
7a2f978b | 1666 | } |
62e76326 | 1667 | |
c8be6d7b | 1668 | url = skipLeadingSpace(url); |
c8be6d7b | 1669 | |
3f38a55e | 1670 | if (!*url || (http_version_str && http_version_str <= url+1)) { |
62e76326 | 1671 | debug(33, 1) ("parseHttpRequest: Missing URL\n"); |
1672 | return parseHttpRequestAbort(conn, "error:missing-url"); | |
3f38a55e | 1673 | } |
62e76326 | 1674 | |
3f38a55e | 1675 | /* Terminate URL just before HTTP version (or at end of line) */ |
1676 | if (http_version_str) | |
62e76326 | 1677 | http_version_str[-1] = '\0'; |
3f38a55e | 1678 | else { |
62e76326 | 1679 | t = url + strlen(url) - 1; |
1680 | ||
1681 | while (t > url && *t == '\r') | |
1682 | *t-- = '\0'; | |
3f38a55e | 1683 | } |
62e76326 | 1684 | |
edce4d98 | 1685 | debug(33, 5) ("parseHttpRequest: URI is '%s'\n", url); |
1686 | *url_p = url; | |
62e76326 | 1687 | |
3f38a55e | 1688 | if (http_version_str) { |
62e76326 | 1689 | if (sscanf(http_version_str + 5, "%d.%d", &http_ver_p->major, |
1690 | &http_ver_p->minor) != 2) { | |
1691 | debug(33, 3) ("parseHttpRequest: Invalid HTTP identifier.\n"); | |
1692 | return parseHttpRequestAbort(conn, "error:invalid-http-ident"); | |
1693 | } | |
1694 | ||
1695 | debug(33, 6) ("parseHttpRequest: Client HTTP version %d.%d.\n", | |
1696 | http_ver_p->major, http_ver_p->minor); | |
3f38a55e | 1697 | } else { |
450e0c10 | 1698 | *http_ver_p = HttpVersion(0,9); /* wild guess */ |
6d38ef86 | 1699 | } |
62e76326 | 1700 | |
c8be6d7b | 1701 | return NULL; |
1702 | } | |
1703 | ||
1704 | /* Utility function to perform part of request parsing */ | |
528b2c61 | 1705 | static ClientSocketContext * |
a2ac85d9 | 1706 | clientParseHttpRequestLine(char *reqline, ConnStateData::Pointer &conn, |
450e0c10 | 1707 | method_t * method_p, char **url_p, HttpVersion * http_ver_p, char * http_version_str) |
c8be6d7b | 1708 | { |
528b2c61 | 1709 | ClientSocketContext *result = NULL; |
3f38a55e | 1710 | /* XXX: This sequence relies on strtok() */ |
62e76326 | 1711 | |
3f38a55e | 1712 | if ((result = clientParseRequestMethod(reqline, method_p, conn)) |
62e76326 | 1713 | || (result = parseURIandHTTPVersion(url_p, http_ver_p, conn, http_version_str))) |
1714 | return result; | |
7a2f978b | 1715 | |
edce4d98 | 1716 | return NULL; |
99edd1c3 | 1717 | } |
1718 | ||
c8be6d7b | 1719 | void |
c4b7a5a9 | 1720 | setLogUri(clientHttpRequest * http, char const *uri) |
c8be6d7b | 1721 | { |
a46d0227 | 1722 | safe_free(http->log_uri); |
62e76326 | 1723 | |
c8be6d7b | 1724 | if (!stringHasCntl(uri)) |
62e76326 | 1725 | http->log_uri = xstrndup(uri, MAX_URL); |
c8be6d7b | 1726 | else |
62e76326 | 1727 | http->log_uri = xstrndup(rfc1738_escape_unescaped(uri), MAX_URL); |
c8be6d7b | 1728 | } |
1729 | ||
3f38a55e | 1730 | static void |
a2ac85d9 | 1731 | prepareAcceleratedURL(ConnStateData::Pointer & conn, clientHttpRequest *http, char *url, const char *req_hdr) |
62e76326 | 1732 | { |
3f38a55e | 1733 | int vhost = conn->port->vhost; |
1734 | int vport = conn->port->vport; | |
1735 | char *host; | |
c8be6d7b | 1736 | |
3f38a55e | 1737 | http->flags.accel = 1; |
c8be6d7b | 1738 | |
3f38a55e | 1739 | /* BUG: Squid cannot deal with '*' URLs (RFC2616 5.1.2) */ |
c8be6d7b | 1740 | |
34399323 | 1741 | if (strncasecmp(url, "cache_object://", 15) == 0) |
1742 | return; /* already in good shape */ | |
1743 | ||
3f38a55e | 1744 | if (*url != '/') { |
62e76326 | 1745 | if (conn->port->vhost) |
1746 | return; /* already in good shape */ | |
1747 | ||
1748 | /* else we need to ignore the host name */ | |
1749 | url = strstr(url, "//"); | |
1750 | ||
3f38a55e | 1751 | #if SHOULD_REJECT_UNKNOWN_URLS |
62e76326 | 1752 | |
1753 | if (!url) | |
1754 | return parseHttpRequestAbort(conn, "error:invalid-request"); | |
1755 | ||
c8be6d7b | 1756 | #endif |
62e76326 | 1757 | |
1758 | if (url) | |
1759 | url = strchr(url + 2, '/'); | |
1760 | ||
1761 | if (!url) | |
1762 | url = (char *) "/"; | |
3f38a55e | 1763 | } |
1764 | ||
1765 | if (internalCheck(url)) { | |
62e76326 | 1766 | /* prepend our name & port */ |
1767 | http->uri = xstrdup(internalLocalUri(NULL, url)); | |
1768 | http->flags.internal = 1; | |
3f38a55e | 1769 | } else if (vhost && (host = mime_get_header(req_hdr, "Host")) != NULL) { |
62e76326 | 1770 | int url_sz = strlen(url) + 32 + Config.appendDomainLen + |
1771 | strlen(host); | |
1772 | http->uri = (char *)xcalloc(url_sz, 1); | |
1773 | snprintf(http->uri, url_sz, "%s://%s%s", | |
1774 | conn->port->protocol, host, url); | |
1775 | debug(33, 5) ("ACCEL VHOST REWRITE: '%s'\n", http->uri); | |
3f38a55e | 1776 | } else if (conn->port->defaultsite) { |
62e76326 | 1777 | int url_sz = strlen(url) + 32 + Config.appendDomainLen + |
1778 | strlen(conn->port->defaultsite); | |
1779 | http->uri = (char *)xcalloc(url_sz, 1); | |
1780 | snprintf(http->uri, url_sz, "%s://%s%s", | |
1781 | conn->port->protocol, conn->port->defaultsite, url); | |
1782 | debug(33, 5) ("ACCEL DEFAULTSITE REWRITE: '%s'\n", http->uri); | |
3f38a55e | 1783 | } else if (vport == -1) { |
62e76326 | 1784 | /* Put the local socket IP address as the hostname. */ |
1785 | int url_sz = strlen(url) + 32 + Config.appendDomainLen; | |
1786 | http->uri = (char *)xcalloc(url_sz, 1); | |
1787 | snprintf(http->uri, url_sz, "%s://%s:%d%s", | |
98242069 | 1788 | http->getConn()->port->protocol, |
1789 | inet_ntoa(http->getConn()->me.sin_addr), | |
1790 | ntohs(http->getConn()->me.sin_port), url); | |
62e76326 | 1791 | debug(33, 5) ("ACCEL VPORT REWRITE: '%s'\n", http->uri); |
3f38a55e | 1792 | } else if (vport > 0) { |
62e76326 | 1793 | /* Put the local socket IP address as the hostname, but static port */ |
1794 | int url_sz = strlen(url) + 32 + Config.appendDomainLen; | |
1795 | http->uri = (char *)xcalloc(url_sz, 1); | |
1796 | snprintf(http->uri, url_sz, "%s://%s:%d%s", | |
98242069 | 1797 | http->getConn()->port->protocol, |
1798 | inet_ntoa(http->getConn()->me.sin_addr), | |
62e76326 | 1799 | vport, url); |
1800 | debug(33, 5) ("ACCEL VPORT REWRITE: '%s'\n", http->uri); | |
3f38a55e | 1801 | } |
1802 | } | |
1803 | ||
1804 | static void | |
a2ac85d9 | 1805 | prepareTransparentURL(ConnStateData::Pointer & conn, clientHttpRequest *http, char *url, const char *req_hdr) |
62e76326 | 1806 | { |
3f38a55e | 1807 | char *host; |
1808 | ||
d048c262 | 1809 | http->flags.transparent = 1; |
3f38a55e | 1810 | |
1811 | if (*url != '/') | |
62e76326 | 1812 | return; /* already in good shape */ |
3f38a55e | 1813 | |
1814 | /* BUG: Squid cannot deal with '*' URLs (RFC2616 5.1.2) */ | |
1815 | ||
1816 | if (internalCheck(url)) { | |
62e76326 | 1817 | /* prepend our name & port */ |
1818 | http->uri = xstrdup(internalLocalUri(NULL, url)); | |
1819 | http->flags.internal = 1; | |
3f38a55e | 1820 | } else if ((host = mime_get_header(req_hdr, "Host")) != NULL) { |
62e76326 | 1821 | int url_sz = strlen(url) + 32 + Config.appendDomainLen + |
1822 | strlen(host); | |
1823 | http->uri = (char *)xcalloc(url_sz, 1); | |
1824 | snprintf(http->uri, url_sz, "%s://%s%s", | |
1825 | conn->port->protocol, host, url); | |
1826 | debug(33, 5) ("TRANSPARENT HOST REWRITE: '%s'\n", http->uri); | |
c8be6d7b | 1827 | } else { |
62e76326 | 1828 | /* Put the local socket IP address as the hostname. */ |
1829 | int url_sz = strlen(url) + 32 + Config.appendDomainLen; | |
1830 | http->uri = (char *)xcalloc(url_sz, 1); | |
1831 | snprintf(http->uri, url_sz, "%s://%s:%d%s", | |
98242069 | 1832 | http->getConn()->port->protocol, |
1833 | inet_ntoa(http->getConn()->me.sin_addr), | |
1834 | ntohs(http->getConn()->me.sin_port), url); | |
62e76326 | 1835 | debug(33, 5) ("TRANSPARENT REWRITE: '%s'\n", http->uri); |
c8be6d7b | 1836 | } |
c8be6d7b | 1837 | } |
1838 | ||
7a2f978b | 1839 | /* |
1840 | * parseHttpRequest() | |
1841 | * | |
1842 | * Returns | |
c4b7a5a9 | 1843 | * NULL on incomplete requests |
528b2c61 | 1844 | * a ClientSocketContext structure on success or failure. |
c4b7a5a9 | 1845 | * Sets result->flags.parsed_ok to 0 if failed to parse the request. |
1846 | * Sets result->flags.parsed_ok to 1 if we have a good request. | |
7a2f978b | 1847 | */ |
528b2c61 | 1848 | static ClientSocketContext * |
a2ac85d9 | 1849 | parseHttpRequest(ConnStateData::Pointer & conn, method_t * method_p, |
62e76326 | 1850 | char **prefix_p, size_t * req_line_sz_p) |
7a2f978b | 1851 | { |
1852 | char *inbuf = NULL; | |
7a2f978b | 1853 | char *url = NULL; |
1854 | char *req_hdr = NULL; | |
3f38a55e | 1855 | char *t; |
450e0c10 | 1856 | HttpVersion http_ver; |
2334c194 | 1857 | char *end; |
7a2f978b | 1858 | size_t header_sz; /* size of headers, not including first line */ |
ea285003 | 1859 | size_t prefix_sz; /* size of whole request (req-line + headers) */ |
c68e9c6b | 1860 | size_t req_sz; |
edce4d98 | 1861 | clientHttpRequest *http; |
528b2c61 | 1862 | ClientSocketContext *result; |
1863 | StoreIOBuffer tempBuffer; | |
3f38a55e | 1864 | char *http_version; |
7a2f978b | 1865 | |
6792f0d3 | 1866 | /* pre-set these values to make aborting simpler */ |
1867 | *prefix_p = NULL; | |
1868 | *method_p = METHOD_NONE; | |
6792f0d3 | 1869 | |
3f38a55e | 1870 | /* Read the HTTP message. HTTP/0.9 is detected by the absence of a HTTP signature */ |
62e76326 | 1871 | |
3f38a55e | 1872 | if ((t = (char *)memchr(conn->in.buf, '\n', conn->in.notYetUsed)) == NULL) { |
62e76326 | 1873 | debug(33, 5) ("Incomplete request, waiting for end of request line\n"); |
1874 | return NULL; | |
7a2f978b | 1875 | } |
62e76326 | 1876 | |
3f38a55e | 1877 | *req_line_sz_p = t - conn->in.buf + 1; |
1878 | http_version = findTrailingHTTPVersion(conn->in.buf); | |
62e76326 | 1879 | |
3f38a55e | 1880 | if (http_version) { |
62e76326 | 1881 | if ((req_sz = headersEnd(conn->in.buf, conn->in.notYetUsed)) == 0) { |
1882 | debug(33, 5) ("Incomplete request, waiting for end of headers\n"); | |
1883 | return NULL; | |
1884 | } | |
3f38a55e | 1885 | } else { |
62e76326 | 1886 | debug(33, 3) ("parseHttpRequest: Missing HTTP identifier\n"); |
1887 | req_sz = t - conn->in.buf + 1; /* HTTP/0.9 requests */ | |
3f38a55e | 1888 | } |
1889 | ||
c8be6d7b | 1890 | assert(req_sz <= conn->in.notYetUsed); |
c68e9c6b | 1891 | /* Use memcpy, not strdup! */ |
e6ccf245 | 1892 | inbuf = (char *)xmalloc(req_sz + 1); |
c68e9c6b | 1893 | xmemcpy(inbuf, conn->in.buf, req_sz); |
1894 | *(inbuf + req_sz) = '\0'; | |
3f38a55e | 1895 | /* and adjust http_version to point into the new copy */ |
62e76326 | 1896 | |
3f38a55e | 1897 | if (http_version) |
62e76326 | 1898 | http_version = inbuf + (http_version - conn->in.buf); |
3f38a55e | 1899 | |
5b648f60 | 1900 | /* Enforce max_request_size */ |
1901 | ||
1902 | if (req_sz >= Config.maxRequestHeaderSize) { | |
1903 | debug(33, 5) ("parseHttpRequest: Too large request\n"); | |
1904 | xfree(inbuf); | |
1905 | return parseHttpRequestAbort(conn, "error:request-too-large"); | |
1906 | } | |
1907 | ||
3f38a55e | 1908 | /* Barf on NULL characters in the headers */ |
1909 | if (strlen(inbuf) != req_sz) { | |
62e76326 | 1910 | debug(33, 1) ("parseHttpRequest: Requestheader contains NULL characters\n"); |
3f38a55e | 1911 | #if TRY_TO_IGNORE_THIS |
62e76326 | 1912 | |
1913 | return parseHttpRequestAbort(conn, "error:invalid-request"); | |
3f38a55e | 1914 | #endif |
62e76326 | 1915 | |
3f38a55e | 1916 | } |
7a2f978b | 1917 | |
edce4d98 | 1918 | /* Is there a legitimate first line to the headers ? */ |
3f38a55e | 1919 | if ((result = clientParseHttpRequestLine(inbuf, conn, method_p, &url, |
62e76326 | 1920 | &http_ver, http_version))) { |
1921 | /* something wrong, abort */ | |
1922 | xfree(inbuf); | |
1923 | return result; | |
99edd1c3 | 1924 | } |
62e76326 | 1925 | |
c68e9c6b | 1926 | /* |
1927 | * Process headers after request line | |
c8be6d7b | 1928 | * TODO: Use httpRequestParse here. |
c68e9c6b | 1929 | */ |
3f38a55e | 1930 | req_hdr = inbuf + *req_line_sz_p; |
62e76326 | 1931 | |
3f38a55e | 1932 | header_sz = req_sz - *req_line_sz_p; |
62e76326 | 1933 | |
754b9ce9 | 1934 | debug(33, 3) ("parseHttpRequest: req_hdr = {%s}\n", req_hdr); |
62e76326 | 1935 | |
2334c194 | 1936 | end = req_hdr + header_sz; |
62e76326 | 1937 | |
04990e2d | 1938 | debug(33, 3) ("parseHttpRequest: end = {%s}\n", end); |
99edd1c3 | 1939 | |
47ac2ebe | 1940 | if (strstr(req_hdr, "\r\r\n")) { |
1941 | debug(33, 1) ("WARNING: suspicious HTTP request contains double CR\n"); | |
1942 | xfree(inbuf); | |
1943 | return parseHttpRequestAbort(conn, "error:double-CR"); | |
1944 | } | |
1945 | ||
99edd1c3 | 1946 | prefix_sz = end - inbuf; |
62e76326 | 1947 | |
ea285003 | 1948 | debug(33, 3) ("parseHttpRequest: prefix_sz = %d, req_line_sz = %d\n", |
62e76326 | 1949 | (int) prefix_sz, (int) *req_line_sz_p); |
1950 | ||
c8be6d7b | 1951 | assert(prefix_sz <= conn->in.notYetUsed); |
7a2f978b | 1952 | |
1953 | /* Ok, all headers are received */ | |
528b2c61 | 1954 | http = new ClientHttpRequest; |
62e76326 | 1955 | |
7a2f978b | 1956 | http->http_ver = http_ver; |
62e76326 | 1957 | |
98242069 | 1958 | http->setConn(conn); |
62e76326 | 1959 | |
99edd1c3 | 1960 | http->req_sz = prefix_sz; |
62e76326 | 1961 | |
528b2c61 | 1962 | result = ClientSocketContextNew(http); |
62e76326 | 1963 | |
c8be6d7b | 1964 | tempBuffer.data = result->reqbuf; |
62e76326 | 1965 | |
c8be6d7b | 1966 | tempBuffer.length = HTTP_REQBUF_SZ; |
62e76326 | 1967 | |
0655fa4d | 1968 | ClientStreamData newServer = new clientReplyContext(http); |
1969 | ||
1970 | ClientStreamData newClient = result; | |
1971 | ||
edce4d98 | 1972 | clientStreamInit(&http->client_stream, clientGetMoreData, clientReplyDetach, |
0655fa4d | 1973 | clientReplyStatus, newServer, clientSocketRecipient, |
1974 | clientSocketDetach, newClient, tempBuffer); | |
62e76326 | 1975 | |
e6ccf245 | 1976 | *prefix_p = (char *)xmalloc(prefix_sz + 1); |
62e76326 | 1977 | |
99edd1c3 | 1978 | xmemcpy(*prefix_p, conn->in.buf, prefix_sz); |
62e76326 | 1979 | |
99edd1c3 | 1980 | *(*prefix_p + prefix_sz) = '\0'; |
62e76326 | 1981 | |
0f1bc304 | 1982 | dlinkAdd(http, &http->active, &ClientActiveRequests); |
7a2f978b | 1983 | |
edce4d98 | 1984 | debug(33, 5) ("parseHttpRequest: Request Header is\n%s\n", |
62e76326 | 1985 | (*prefix_p) + *req_line_sz_p); |
3f38a55e | 1986 | |
ba9ebd0a | 1987 | #if THIS_VIOLATES_HTTP_SPECS_ON_URL_TRANSFORMATION |
62e76326 | 1988 | |
7a2f978b | 1989 | if ((t = strchr(url, '#'))) /* remove HTML anchors */ |
62e76326 | 1990 | *t = '\0'; |
1991 | ||
ba9ebd0a | 1992 | #endif |
7a2f978b | 1993 | |
3f38a55e | 1994 | /* Rewrite the URL in transparent or accelerator mode */ |
a46d2c0e | 1995 | if (conn->transparent()) { |
62e76326 | 1996 | prepareTransparentURL(conn, http, url, req_hdr); |
3f38a55e | 1997 | } else if (conn->port->accel) { |
62e76326 | 1998 | prepareAcceleratedURL(conn, http, url, req_hdr); |
2f2749d7 | 1999 | } else if (internalCheck(url)) { |
2000 | /* prepend our name & port */ | |
2001 | http->uri = xstrdup(internalLocalUri(NULL, url)); | |
2002 | http->flags.internal = 1; | |
2003 | http->flags.accel = 1; | |
3f38a55e | 2004 | } |
2005 | ||
2006 | if (!http->uri) { | |
62e76326 | 2007 | /* No special rewrites have been applied above, use the |
2008 | * requested url. may be rewritten later, so make extra room */ | |
2009 | int url_sz = strlen(url) + Config.appendDomainLen + 5; | |
2010 | http->uri = (char *)xcalloc(url_sz, 1); | |
2011 | strcpy(http->uri, url); | |
3f38a55e | 2012 | } |
62e76326 | 2013 | |
c8be6d7b | 2014 | setLogUri(http, http->uri); |
93f9abd0 | 2015 | debug(33, 5) ("parseHttpRequest: Complete request received\n"); |
7a2f978b | 2016 | xfree(inbuf); |
c4b7a5a9 | 2017 | result->flags.parsed_ok = 1; |
c8be6d7b | 2018 | return result; |
3f38a55e | 2019 | |
7a2f978b | 2020 | } |
2021 | ||
c8be6d7b | 2022 | int |
a46d2c0e | 2023 | ConnStateData::getAvailableBufferLength() const |
c8be6d7b | 2024 | { |
1a419b96 | 2025 | int result = in.allocatedSize - in.notYetUsed - 1; |
2026 | assert (result >= 0); | |
2027 | return result; | |
c8be6d7b | 2028 | } |
2029 | ||
2030 | void | |
a46d2c0e | 2031 | ConnStateData::makeSpaceAvailable() |
c8be6d7b | 2032 | { |
a46d2c0e | 2033 | if (getAvailableBufferLength() < 2) { |
2034 | in.buf = (char *)memReallocBuf(in.buf, in.allocatedSize * 2, &in.allocatedSize); | |
62e76326 | 2035 | debug(33, 2) ("growing request buffer: notYetUsed=%ld size=%ld\n", |
a46d2c0e | 2036 | (long) in.notYetUsed, (long) in.allocatedSize); |
c8be6d7b | 2037 | } |
2038 | } | |
2039 | ||
2040 | void | |
0655fa4d | 2041 | ConnStateData::addContextToQueue(ClientSocketContext * context) |
c8be6d7b | 2042 | { |
0655fa4d | 2043 | ClientSocketContext::Pointer *S; |
62e76326 | 2044 | |
0655fa4d | 2045 | for (S = (ClientSocketContext::Pointer *) & currentobject; S->getRaw(); |
62e76326 | 2046 | S = &(*S)->next) |
2047 | ||
2048 | ; | |
c8be6d7b | 2049 | *S = context; |
62e76326 | 2050 | |
0655fa4d | 2051 | ++nrequests; |
c8be6d7b | 2052 | } |
2053 | ||
2054 | int | |
0655fa4d | 2055 | ConnStateData::getConcurrentRequestCount() const |
c8be6d7b | 2056 | { |
2057 | int result = 0; | |
0655fa4d | 2058 | ClientSocketContext::Pointer *T; |
62e76326 | 2059 | |
0655fa4d | 2060 | for (T = (ClientSocketContext::Pointer *) ¤tobject; |
2061 | T->getRaw(); T = &(*T)->next, ++result) | |
62e76326 | 2062 | |
2063 | ; | |
c8be6d7b | 2064 | return result; |
2065 | } | |
2066 | ||
2067 | int | |
a2ac85d9 | 2068 | connReadWasError(ConnStateData::Pointer & conn, comm_err_t flag, int size, int xerrno) |
c8be6d7b | 2069 | { |
c4b7a5a9 | 2070 | if (flag != COMM_OK) { |
62e76326 | 2071 | debug(50, 2) ("connReadWasError: FD %d: got flag %d\n", conn->fd, flag); |
2072 | return 1; | |
c4b7a5a9 | 2073 | } |
62e76326 | 2074 | |
c8be6d7b | 2075 | if (size < 0) { |
f3400a93 | 2076 | if (!ignoreErrno(xerrno)) { |
2077 | debug(50, 2) ("connReadWasError: FD %d: %s\n", conn->fd, xstrerr(xerrno)); | |
62e76326 | 2078 | return 1; |
2079 | } else if (conn->in.notYetUsed == 0) { | |
2080 | debug(50, 2) ("connReadWasError: FD %d: no data to process (%s)\n", | |
f3400a93 | 2081 | conn->fd, xstrerr(xerrno)); |
62e76326 | 2082 | } |
c8be6d7b | 2083 | } |
62e76326 | 2084 | |
c8be6d7b | 2085 | return 0; |
2086 | } | |
2087 | ||
2088 | int | |
a2ac85d9 | 2089 | connFinishedWithConn(ConnStateData::Pointer & conn, int size) |
c8be6d7b | 2090 | { |
2091 | if (size == 0) { | |
0655fa4d | 2092 | if (conn->getConcurrentRequestCount() == 0 && conn->in.notYetUsed == 0) { |
62e76326 | 2093 | /* no current or pending requests */ |
2094 | debug(33, 4) ("connFinishedWithConn: FD %d closed\n", conn->fd); | |
2095 | return 1; | |
2096 | } else if (!Config.onoff.half_closed_clients) { | |
2097 | /* admin doesn't want to support half-closed client sockets */ | |
2098 | debug(33, 3) ("connFinishedWithConn: FD %d aborted (half_closed_clients disabled)\n", conn->fd); | |
2099 | return 1; | |
2100 | } | |
c8be6d7b | 2101 | } |
62e76326 | 2102 | |
c8be6d7b | 2103 | return 0; |
2104 | } | |
2105 | ||
2106 | void | |
a2ac85d9 | 2107 | connNoteUseOfBuffer(ConnStateData::Pointer & conn, size_t byteCount) |
c8be6d7b | 2108 | { |
2109 | assert(byteCount > 0 && byteCount <= conn->in.notYetUsed); | |
2110 | conn->in.notYetUsed -= byteCount; | |
e6ccf245 | 2111 | debug(33, 5) ("conn->in.notYetUsed = %u\n", (unsigned) conn->in.notYetUsed); |
c8be6d7b | 2112 | /* |
2113 | * If there is still data that will be used, | |
2114 | * move it to the beginning. | |
2115 | */ | |
62e76326 | 2116 | |
c8be6d7b | 2117 | if (conn->in.notYetUsed > 0) |
62e76326 | 2118 | xmemmove(conn->in.buf, conn->in.buf + byteCount, |
2119 | conn->in.notYetUsed); | |
c8be6d7b | 2120 | } |
2121 | ||
2122 | int | |
a2ac85d9 | 2123 | connKeepReadingIncompleteRequest(ConnStateData::Pointer & conn) |
c8be6d7b | 2124 | { |
2125 | return conn->in.notYetUsed >= Config.maxRequestHeaderSize ? 0 : 1; | |
2126 | } | |
2127 | ||
2128 | void | |
a2ac85d9 | 2129 | connCancelIncompleteRequests(ConnStateData::Pointer & conn) |
c8be6d7b | 2130 | { |
528b2c61 | 2131 | ClientSocketContext *context = parseHttpRequestAbort(conn, "error:request-too-large"); |
2132 | clientStreamNode *node = context->getClientReplyContext(); | |
c8be6d7b | 2133 | assert(!connKeepReadingIncompleteRequest(conn)); |
e6ccf245 | 2134 | debug(33, 1) ("Request header is too large (%u bytes)\n", |
62e76326 | 2135 | (unsigned) conn->in.notYetUsed); |
c8be6d7b | 2136 | debug(33, 1) ("Config 'request_header_max_size'= %ld bytes.\n", |
62e76326 | 2137 | (long int) Config.maxRequestHeaderSize); |
0655fa4d | 2138 | clientReplyContext *repContext = dynamic_cast<clientReplyContext *>(node->data.getRaw()); |
2139 | assert (repContext); | |
2140 | repContext->setReplyToError(ERR_TOO_BIG, | |
2141 | HTTP_REQUEST_ENTITY_TOO_LARGE, METHOD_NONE, NULL, | |
2142 | &conn->peer.sin_addr, NULL, NULL, NULL); | |
2143 | context->registerWithConn(); | |
528b2c61 | 2144 | context->pullData(); |
c8be6d7b | 2145 | } |
2146 | ||
7a2f978b | 2147 | static void |
a2ac85d9 | 2148 | clientMaybeReadData(ConnStateData::Pointer &conn, int do_next_read) |
7a2f978b | 2149 | { |
c4b7a5a9 | 2150 | if (do_next_read) { |
62e76326 | 2151 | conn->flags.readMoreRequests = 1; |
a46d2c0e | 2152 | conn->readSomeData(); |
c4b7a5a9 | 2153 | } |
2154 | } | |
2155 | ||
2156 | static void | |
a2ac85d9 | 2157 | clientAfterReadingRequests(int fd, ConnStateData::Pointer &conn, int do_next_read) |
c4b7a5a9 | 2158 | { |
2159 | fde *F = &fd_table[fd]; | |
2160 | ||
2161 | /* Check if a half-closed connection was aborted in the middle */ | |
62e76326 | 2162 | |
c4b7a5a9 | 2163 | if (F->flags.socket_eof) { |
62e76326 | 2164 | if (conn->in.notYetUsed != conn->body.size_left) { |
2165 | /* != 0 when no request body */ | |
2166 | /* Partial request received. Abort client connection! */ | |
98242069 | 2167 | debug(33, 3) ("clientAfterReadingRequests: FD %d aborted, partial request\n", fd); |
62e76326 | 2168 | comm_close(fd); |
2169 | return; | |
2170 | } | |
c4b7a5a9 | 2171 | } |
2172 | ||
2173 | clientMaybeReadData (conn, do_next_read); | |
2174 | } | |
2175 | ||
c4b7a5a9 | 2176 | static void |
a2ac85d9 | 2177 | clientProcessRequest(ConnStateData::Pointer &conn, ClientSocketContext *context, method_t method, char *prefix, size_t req_line_sz) |
c4b7a5a9 | 2178 | { |
2179 | clientHttpRequest *http = context->http; | |
190154cf | 2180 | HttpRequest *request = NULL; |
c4b7a5a9 | 2181 | /* We have an initial client stream in place should it be needed */ |
2182 | /* setup our private context */ | |
2183 | connNoteUseOfBuffer(conn, http->req_sz); | |
2184 | ||
0655fa4d | 2185 | context->registerWithConn(); |
c4b7a5a9 | 2186 | |
2187 | if (context->flags.parsed_ok == 0) { | |
62e76326 | 2188 | clientStreamNode *node = context->getClientReplyContext(); |
50c09fc4 | 2189 | debug(33, 1) ("clientProcessRequest: Invalid Request\n"); |
0655fa4d | 2190 | clientReplyContext *repContext = dynamic_cast<clientReplyContext *>(node->data.getRaw()); |
2191 | assert (repContext); | |
2192 | repContext->setReplyToError(ERR_INVALID_REQ, HTTP_BAD_REQUEST, method, NULL, | |
2193 | &conn->peer.sin_addr, NULL, conn->in.buf, NULL); | |
62e76326 | 2194 | assert(context->http->out.offset == 0); |
2195 | context->pullData(); | |
2196 | conn->flags.readMoreRequests = 0; | |
2197 | return; | |
c4b7a5a9 | 2198 | } |
2199 | ||
2200 | if ((request = urlParse(method, http->uri)) == NULL) { | |
62e76326 | 2201 | clientStreamNode *node = context->getClientReplyContext(); |
2202 | debug(33, 5) ("Invalid URL: %s\n", http->uri); | |
0655fa4d | 2203 | clientReplyContext *repContext = dynamic_cast<clientReplyContext *>(node->data.getRaw()); |
2204 | assert (repContext); | |
2205 | repContext->setReplyToError( | |
2206 | ERR_INVALID_URL, HTTP_BAD_REQUEST, method, http->uri, | |
2207 | &conn->peer.sin_addr, NULL, NULL, NULL); | |
62e76326 | 2208 | assert(context->http->out.offset == 0); |
2209 | context->pullData(); | |
2210 | conn->flags.readMoreRequests = 0; | |
2211 | return; | |
2212 | } | |
c4b7a5a9 | 2213 | |
528b2c61 | 2214 | /* compile headers */ |
2215 | /* we should skip request line! */ | |
47ac2ebe | 2216 | if (!httpRequestParseHeader(request, prefix + req_line_sz)) { |
2217 | clientStreamNode *node = context->getClientReplyContext(); | |
2218 | debug(33, 5) ("Failed to parse request headers:\n%s\n", prefix); | |
2219 | clientReplyContext *repContext = dynamic_cast<clientReplyContext *>(node->data.getRaw()); | |
2220 | assert (repContext); | |
2221 | repContext->setReplyToError( | |
2222 | ERR_INVALID_URL, HTTP_BAD_REQUEST, method, http->uri, | |
2223 | &conn->peer.sin_addr, NULL, NULL, NULL); | |
2224 | assert(context->http->out.offset == 0); | |
2225 | context->pullData(); | |
2226 | conn->flags.readMoreRequests = 0; | |
2227 | return; | |
2228 | } | |
c4b7a5a9 | 2229 | |
2230 | request->flags.accelerated = http->flags.accel; | |
62e76326 | 2231 | |
d048c262 | 2232 | request->flags.transparent = http->flags.transparent; |
2233 | ||
c4b7a5a9 | 2234 | if (!http->flags.internal) { |
62e76326 | 2235 | if (internalCheck(request->urlpath.buf())) { |
2236 | if (internalHostnameIs(request->host) && | |
2237 | request->port == getMyPort()) { | |
2238 | http->flags.internal = 1; | |
2239 | } else if (internalStaticCheck(request->urlpath.buf())) { | |
2240 | xstrncpy(request->host, internalHostname(), | |
2241 | SQUIDHOSTNAMELEN); | |
2242 | request->port = getMyPort(); | |
2243 | http->flags.internal = 1; | |
2244 | } | |
2245 | } | |
e72a0ec0 | 2246 | |
4361707c | 2247 | if (http->flags.internal) { |
e72a0ec0 | 2248 | request->protocol = PROTO_HTTP; |
4361707c | 2249 | request->login[0] = '\0'; |
2250 | } | |
c4b7a5a9 | 2251 | } |
2252 | ||
c4b7a5a9 | 2253 | request->flags.internal = http->flags.internal; |
2254 | setLogUri (http, urlCanonicalClean(request)); | |
2255 | request->client_addr = conn->peer.sin_addr; | |
47b0c1fa | 2256 | request->client_port = ntohs(conn->peer.sin_port); |
c4b7a5a9 | 2257 | request->my_addr = conn->me.sin_addr; |
2258 | request->my_port = ntohs(conn->me.sin_port); | |
2259 | request->http_ver = http->http_ver; | |
62e76326 | 2260 | |
c4b7a5a9 | 2261 | if (!urlCheckRequest(request) || |
62e76326 | 2262 | httpHeaderHas(&request->header, HDR_TRANSFER_ENCODING)) { |
2263 | clientStreamNode *node = context->getClientReplyContext(); | |
0655fa4d | 2264 | clientReplyContext *repContext = dynamic_cast<clientReplyContext *>(node->data.getRaw()); |
2265 | assert (repContext); | |
2266 | repContext->setReplyToError(ERR_UNSUP_REQ, | |
2267 | HTTP_NOT_IMPLEMENTED, request->method, NULL, | |
2268 | &conn->peer.sin_addr, request, NULL, NULL); | |
62e76326 | 2269 | assert(context->http->out.offset == 0); |
2270 | context->pullData(); | |
2271 | conn->flags.readMoreRequests = 0; | |
2272 | return; | |
c4b7a5a9 | 2273 | } |
2274 | ||
2275 | ||
2276 | if (!clientIsContentLengthValid(request)) { | |
62e76326 | 2277 | clientStreamNode *node = context->getClientReplyContext(); |
0655fa4d | 2278 | clientReplyContext *repContext = dynamic_cast<clientReplyContext *>(node->data.getRaw()); |
2279 | assert (repContext); | |
2280 | repContext->setReplyToError(ERR_INVALID_REQ, | |
2281 | HTTP_LENGTH_REQUIRED, request->method, NULL, | |
2282 | &conn->peer.sin_addr, request, NULL, NULL); | |
62e76326 | 2283 | assert(context->http->out.offset == 0); |
2284 | context->pullData(); | |
2285 | conn->flags.readMoreRequests = 0; | |
2286 | return; | |
c4b7a5a9 | 2287 | } |
2288 | ||
2289 | http->request = requestLink(request); | |
2290 | clientSetKeepaliveFlag(http); | |
2291 | /* Do we expect a request-body? */ | |
62e76326 | 2292 | |
c4b7a5a9 | 2293 | if (request->content_length > 0) { |
62e76326 | 2294 | conn->body.size_left = request->content_length; |
2295 | request->body_connection = conn; | |
2296 | /* Is it too large? */ | |
2297 | ||
2298 | if (!clientIsRequestBodyValid(request->content_length) || | |
2299 | clientIsRequestBodyTooLargeForPolicy(request->content_length)) { | |
2300 | clientStreamNode *node = context->getClientReplyContext(); | |
0655fa4d | 2301 | clientReplyContext *repContext = dynamic_cast<clientReplyContext *>(node->data.getRaw()); |
2302 | assert (repContext); | |
2303 | repContext->setReplyToError(ERR_TOO_BIG, | |
2304 | HTTP_REQUEST_ENTITY_TOO_LARGE, METHOD_NONE, NULL, | |
2305 | &conn->peer.sin_addr, http->request, NULL, NULL); | |
62e76326 | 2306 | assert(context->http->out.offset == 0); |
2307 | context->pullData(); | |
2308 | conn->flags.readMoreRequests = 0; | |
2309 | return; | |
2310 | } | |
2311 | ||
2312 | context->mayUseConnection(true); | |
c4b7a5a9 | 2313 | } |
2314 | ||
2315 | /* If this is a CONNECT, don't schedule a read - ssl.c will handle it */ | |
2316 | if (http->request->method == METHOD_CONNECT) | |
62e76326 | 2317 | context->mayUseConnection(true); |
2318 | ||
c4b7a5a9 | 2319 | clientAccessCheck(http); |
2320 | } | |
2321 | ||
2322 | static void | |
a2ac85d9 | 2323 | connStripBufferWhitespace (ConnStateData::Pointer &conn) |
c4b7a5a9 | 2324 | { |
2325 | while (conn->in.notYetUsed > 0 && xisspace(conn->in.buf[0])) { | |
62e76326 | 2326 | xmemmove(conn->in.buf, conn->in.buf + 1, conn->in.notYetUsed - 1); |
2327 | --conn->in.notYetUsed; | |
c4b7a5a9 | 2328 | } |
2329 | } | |
2330 | ||
2331 | static int | |
a2ac85d9 | 2332 | connOkToAddRequest(ConnStateData::Pointer &conn) |
c4b7a5a9 | 2333 | { |
0655fa4d | 2334 | int result = conn->getConcurrentRequestCount() < (Config.onoff.pipeline_prefetch ? 2 : 1); |
62e76326 | 2335 | |
c4b7a5a9 | 2336 | if (!result) { |
50c09fc4 | 2337 | debug(33, 3) ("connOkToAddRequest: FD %d max concurrent requests reached\n", |
62e76326 | 2338 | conn->fd); |
50c09fc4 | 2339 | debug(33, 5) ("connOkToAddRequest: FD %d defering new request until one is done\n", |
62e76326 | 2340 | conn->fd); |
c4b7a5a9 | 2341 | } |
62e76326 | 2342 | |
c4b7a5a9 | 2343 | return result; |
2344 | } | |
2345 | ||
f900210a | 2346 | /* |
2347 | * Attempt to parse one or more requests from the input buffer. | |
2348 | * If a request is successfully parsed, even if the next request | |
2349 | * is only partially parsed, it will return TRUE. | |
2350 | * do_next_read is updated to indicate whether a read should be | |
2351 | * scheduled. | |
2352 | */ | |
2353 | static bool | |
2354 | clientParseRequest(ConnStateData::Pointer conn, bool &do_next_read) | |
2355 | { | |
2356 | method_t method; | |
2357 | char *prefix = NULL; | |
2358 | ClientSocketContext *context; | |
2359 | bool parsed_req = false; | |
2360 | ||
2361 | debug(33, 5) ("clientParseRequest: FD %d: attempting to parse\n", conn->fd); | |
2362 | ||
2363 | while (conn->in.notYetUsed > 0 && conn->body.size_left == 0) { | |
2364 | size_t req_line_sz; | |
2365 | connStripBufferWhitespace (conn); | |
2366 | ||
2367 | /* Limit the number of concurrent requests to 2 */ | |
2368 | ||
2369 | if (!connOkToAddRequest(conn)) { | |
2370 | break; | |
2371 | } | |
2372 | ||
2373 | /* Should not be needed anymore */ | |
2374 | /* Terminate the string */ | |
2375 | conn->in.buf[conn->in.notYetUsed] = '\0'; | |
2376 | ||
2377 | /* Process request */ | |
2378 | context = parseHttpRequest(conn, &method, &prefix, &req_line_sz); | |
2379 | ||
2380 | /* partial or incomplete request */ | |
2381 | if (!context) { | |
2382 | safe_free(prefix); | |
2383 | ||
2384 | if (!connKeepReadingIncompleteRequest(conn)) | |
2385 | connCancelIncompleteRequests(conn); | |
2386 | ||
2387 | break; | |
2388 | } | |
2389 | ||
2390 | /* status -1 or 1 */ | |
2391 | if (context) { | |
2392 | debug(33, 5) ("clientParseRequest: FD %d: parsed a request\n", conn->fd); | |
2393 | commSetTimeout(conn->fd, Config.Timeout.lifetime, clientLifetimeTimeout, | |
2394 | context->http); | |
2395 | ||
2396 | clientProcessRequest(conn, context, method, prefix, req_line_sz); | |
2397 | ||
2398 | safe_free(prefix); | |
2399 | parsed_req = true; | |
2400 | ||
2401 | if (context->mayUseConnection()) { | |
2402 | debug (33, 3) ("clientReadRequest: Not reading, as this request may need the connection\n"); | |
2403 | do_next_read = 0; | |
2404 | break; | |
2405 | } | |
2406 | ||
2407 | if (!conn->flags.readMoreRequests) { | |
2408 | conn->flags.readMoreRequests = 1; | |
2409 | break; | |
2410 | } | |
2411 | ||
2412 | continue; /* while offset > 0 && body.size_left == 0 */ | |
2413 | } | |
2414 | } /* while offset > 0 && conn->body.size_left == 0 */ | |
2415 | ||
2416 | return parsed_req; | |
2417 | } | |
2418 | ||
c4b7a5a9 | 2419 | static void |
2420 | clientReadRequest(int fd, char *buf, size_t size, comm_err_t flag, int xerrno, | |
62e76326 | 2421 | void *data) |
c4b7a5a9 | 2422 | { |
a2ac85d9 | 2423 | ConnStateData::Pointer conn ((ConnStateData *)data); |
a46d2c0e | 2424 | conn->reading(false); |
a46d2c0e | 2425 | bool do_next_read = 1; /* the default _is_ to read data! - adrian */ |
c4b7a5a9 | 2426 | |
2427 | assert (fd == conn->fd); | |
2428 | ||
2429 | /* Bail out quickly on COMM_ERR_CLOSING - close handlers will tidy up */ | |
62e76326 | 2430 | |
c4b7a5a9 | 2431 | if (flag == COMM_ERR_CLOSING) { |
2432 | return; | |
2433 | } | |
62e76326 | 2434 | |
44db45e8 | 2435 | /* |
2436 | * Don't reset the timeout value here. The timeout value will be | |
2437 | * set to Config.Timeout.request by httpAccept() and | |
2438 | * clientWriteComplete(), and should apply to the request as a | |
2439 | * whole, not individual read() calls. Plus, it breaks our | |
2440 | * lame half-close detection | |
2441 | */ | |
f3400a93 | 2442 | if (connReadWasError(conn, flag, size, xerrno)) { |
62e76326 | 2443 | comm_close(fd); |
2444 | return; | |
7a2f978b | 2445 | } |
c4b7a5a9 | 2446 | |
2447 | if (flag == COMM_OK) { | |
62e76326 | 2448 | if (size > 0) { |
95ac44e6 | 2449 | char *current_buf = conn->in.addressToReadInto(); |
62e76326 | 2450 | kb_incr(&statCounter.client_http.kbytes_in, size); |
95ac44e6 | 2451 | |
2452 | if (buf != current_buf) | |
2453 | xmemmove(current_buf, buf, size); | |
2454 | ||
62e76326 | 2455 | conn->in.notYetUsed += size; |
95ac44e6 | 2456 | |
d048c262 | 2457 | conn->in.buf[conn->in.notYetUsed] = '\0'; /* Terminate the string */ |
62e76326 | 2458 | } else if (size == 0) { |
2459 | debug(33, 5) ("clientReadRequest: FD %d closed?\n", fd); | |
2460 | ||
2461 | if (connFinishedWithConn(conn, size)) { | |
2462 | comm_close(fd); | |
2463 | return; | |
2464 | } | |
2465 | ||
2466 | /* It might be half-closed, we can't tell */ | |
2467 | fd_table[fd].flags.socket_eof = 1; | |
2468 | ||
a46d2c0e | 2469 | commMarkHalfClosed(fd); |
2470 | ||
2471 | do_next_read = 0; | |
62e76326 | 2472 | |
2473 | fd_note(fd, "half-closed"); | |
2474 | ||
2475 | /* There is one more close check at the end, to detect aborted | |
2476 | * (partial) requests. At this point we can't tell if the request | |
2477 | * is partial. | |
2478 | */ | |
2479 | /* Continue to process previously read data */ | |
2480 | } | |
c4b7a5a9 | 2481 | } |
2482 | ||
2483 | ||
94439e4e | 2484 | /* Process request body if any */ |
50c09fc4 | 2485 | if (conn->in.notYetUsed > 0 && conn->body.callback != NULL) { |
2486 | ClientBody body(conn); | |
2487 | body.process(); | |
2488 | } | |
62e76326 | 2489 | |
94439e4e | 2490 | /* Process next request */ |
0655fa4d | 2491 | if (conn->getConcurrentRequestCount() == 0) |
62e76326 | 2492 | fd_note(conn->fd, "Reading next request"); |
c8be6d7b | 2493 | |
f900210a | 2494 | if (! clientParseRequest(conn, do_next_read)) { |
2495 | /* | |
2496 | * If the client here is half closed and we failed | |
2497 | * to parse a request, close the connection. | |
2498 | * The above check with connFinishedWithConn() only | |
2499 | * succeeds _if_ the buffer is empty which it won't | |
2500 | * be if we have an incomplete request. | |
2501 | */ | |
62e76326 | 2502 | |
f900210a | 2503 | if (conn->getConcurrentRequestCount() == 0 && commIsHalfClosed(fd)) { |
2504 | debug(33, 5) ("clientReadRequest: FD %d: half-closed connection, no completed request parsed, connection closing.\n", fd); | |
2505 | comm_close(fd); | |
62e76326 | 2506 | } |
62e76326 | 2507 | |
f900210a | 2508 | clientAfterReadingRequests(fd, conn, do_next_read); |
2509 | } | |
94439e4e | 2510 | } |
2511 | ||
2512 | /* file_read like function, for reading body content */ | |
2513 | void | |
190154cf | 2514 | clientReadBody(HttpRequest * request, char *buf, size_t size, CBCB * callback, |
62e76326 | 2515 | void *cbdata) |
94439e4e | 2516 | { |
a2ac85d9 | 2517 | ConnStateData::Pointer conn = request->body_connection; |
62e76326 | 2518 | |
a2ac85d9 | 2519 | if (conn.getRaw() == NULL) { |
62e76326 | 2520 | debug(33, 5) ("clientReadBody: no body to read, request=%p\n", request); |
2521 | callback(buf, 0, cbdata); /* Signal end of body */ | |
2522 | return; | |
7a2f978b | 2523 | } |
62e76326 | 2524 | |
c8be6d7b | 2525 | debug(33, 2) ("clientReadBody: start fd=%d body_size=%lu in.notYetUsed=%ld cb=%p req=%p\n", |
62e76326 | 2526 | conn->fd, (unsigned long int) conn->body.size_left, |
2527 | (unsigned long int) conn->in.notYetUsed, callback, request); | |
94439e4e | 2528 | conn->body.callback = callback; |
7ec0ff5c | 2529 | conn->body.cbdata = cbdataReference(cbdata); |
94439e4e | 2530 | conn->body.buf = buf; |
2531 | conn->body.bufsize = size; | |
2532 | conn->body.request = requestLink(request); | |
50c09fc4 | 2533 | ClientBody body (conn); |
2534 | body.process(); | |
94439e4e | 2535 | } |
2536 | ||
a2ac85d9 | 2537 | ClientBody::ClientBody(ConnStateData::Pointer &aConn) : conn(aConn), buf (NULL), callback(NULL), request(NULL) |
50c09fc4 | 2538 | {} |
2539 | ||
2540 | void | |
2541 | ClientBody::preProcessing() | |
94439e4e | 2542 | { |
50c09fc4 | 2543 | callback = conn->body.callback; |
2544 | request = conn->body.request; | |
94439e4e | 2545 | /* Note: request is null while eating "aborted" transfers */ |
50c09fc4 | 2546 | debug(33, 2) ("clientBody::process: start fd=%d body_size=%lu in.notYetUsed=%lu cb=%p req=%p\n", |
62e76326 | 2547 | conn->fd, (unsigned long int) conn->body.size_left, |
2548 | (unsigned long int) conn->in.notYetUsed, callback, request); | |
50c09fc4 | 2549 | } |
2550 | ||
2551 | /* Called by clientReadRequest to process body content */ | |
2552 | void | |
2553 | ClientBody::process() | |
2554 | { | |
2555 | preProcessing(); | |
62e76326 | 2556 | |
50c09fc4 | 2557 | if (conn->in.notYetUsed) |
2558 | processBuffer(); | |
2559 | else | |
2560 | conn->readSomeData(); | |
2561 | } | |
62e76326 | 2562 | |
50c09fc4 | 2563 | void |
2564 | ClientBody::processBuffer() | |
2565 | { | |
2566 | /* Some sanity checks... */ | |
2567 | assert(conn->body.size_left > 0); | |
2568 | assert(conn->in.notYetUsed > 0); | |
2569 | assert(callback != NULL); | |
2570 | buf = conn->body.buf; | |
2571 | assert(buf != NULL); | |
2572 | /* How much do we have to process? */ | |
2573 | size_t size = conn->in.notYetUsed; | |
62e76326 | 2574 | |
50c09fc4 | 2575 | if (size > conn->body.size_left) /* only process the body part */ |
2576 | size = conn->body.size_left; | |
62e76326 | 2577 | |
50c09fc4 | 2578 | if (size > conn->body.bufsize) /* don't copy more than requested */ |
2579 | size = conn->body.bufsize; | |
62e76326 | 2580 | |
50c09fc4 | 2581 | xmemcpy(buf, conn->in.buf, size); |
62e76326 | 2582 | |
50c09fc4 | 2583 | conn->body.size_left -= size; |
62e76326 | 2584 | |
50c09fc4 | 2585 | /* Move any remaining data */ |
2586 | conn->in.notYetUsed -= size; | |
62e76326 | 2587 | |
50c09fc4 | 2588 | if (conn->in.notYetUsed > 0) |
2589 | xmemmove(conn->in.buf, conn->in.buf + size, conn->in.notYetUsed); | |
62e76326 | 2590 | |
50c09fc4 | 2591 | /* Remove request link if this is the last part of the body, as |
2592 | * clientReadRequest automatically continues to process next request */ | |
2593 | if (conn->body.size_left <= 0 && request != NULL) | |
2594 | request->body_connection = NULL; | |
62e76326 | 2595 | |
50c09fc4 | 2596 | /* Remove clientReadBody arguments (the call is completed) */ |
2597 | conn->body.request = NULL; | |
62e76326 | 2598 | |
50c09fc4 | 2599 | conn->body.callback = NULL; |
62e76326 | 2600 | |
50c09fc4 | 2601 | conn->body.buf = NULL; |
62e76326 | 2602 | |
50c09fc4 | 2603 | conn->body.bufsize = 0; |
2604 | ||
2605 | /* Remember that we have touched the body, not restartable */ | |
2606 | if (request != NULL) { | |
2607 | request->flags.body_sent = 1; | |
2608 | conn->body.request = NULL; | |
2609 | } | |
62e76326 | 2610 | |
50c09fc4 | 2611 | /* Invoke callback function */ |
7ec0ff5c | 2612 | void *cbdata; |
62e76326 | 2613 | |
7ec0ff5c | 2614 | if (cbdataReferenceValidDone(conn->body.cbdata, &cbdata)) |
2615 | callback(buf, size, cbdata); | |
62e76326 | 2616 | |
50c09fc4 | 2617 | if (request != NULL) { |
2618 | requestUnlink(request); /* Linked in clientReadBody */ | |
28ee8ce5 | 2619 | } |
50c09fc4 | 2620 | |
2621 | debug(33, 2) ("ClientBody::process: end fd=%d size=%lu body_size=%lu in.notYetUsed=%lu cb=%p req=%p\n", | |
2622 | conn->fd, (unsigned long int)size, (unsigned long int) conn->body.size_left, | |
2623 | (unsigned long) conn->in.notYetUsed, callback, request); | |
94439e4e | 2624 | } |
2625 | ||
2626 | /* A dummy handler that throws away a request-body */ | |
2d72d4fd | 2627 | static void |
e6ccf245 | 2628 | clientReadBodyAbortHandler(char *buf, ssize_t size, void *data) |
94439e4e | 2629 | { |
c8be6d7b | 2630 | static char bodyAbortBuf[SQUID_TCP_SO_RCVBUF]; |
94439e4e | 2631 | ConnStateData *conn = (ConnStateData *) data; |
e6ccf245 | 2632 | debug(33, 2) ("clientReadBodyAbortHandler: fd=%d body_size=%lu in.notYetUsed=%lu\n", |
62e76326 | 2633 | conn->fd, (unsigned long int) conn->body.size_left, |
2634 | (unsigned long) conn->in.notYetUsed); | |
2635 | ||
94439e4e | 2636 | if (size != 0 && conn->body.size_left != 0) { |
62e76326 | 2637 | debug(33, 3) ("clientReadBodyAbortHandler: fd=%d shedule next read\n", |
2638 | conn->fd); | |
2639 | conn->body.callback = clientReadBodyAbortHandler; | |
2640 | conn->body.buf = bodyAbortBuf; | |
2641 | conn->body.bufsize = sizeof(bodyAbortBuf); | |
7ec0ff5c | 2642 | conn->body.cbdata = cbdataReference(data); |
94439e4e | 2643 | } |
55e44db9 | 2644 | |
2645 | if (conn->closing()) | |
2646 | comm_close(conn->fd); | |
94439e4e | 2647 | } |
2648 | ||
2649 | /* Abort a body request */ | |
2650 | int | |
190154cf | 2651 | clientAbortBody(HttpRequest * request) |
94439e4e | 2652 | { |
a2ac85d9 | 2653 | ConnStateData::Pointer conn = request->body_connection; |
94439e4e | 2654 | char *buf; |
2655 | CBCB *callback; | |
94439e4e | 2656 | request->body_connection = NULL; |
62e76326 | 2657 | |
a2ac85d9 | 2658 | if (conn.getRaw() == NULL || conn->body.size_left <= 0) |
62e76326 | 2659 | return 0; /* No body to abort */ |
2660 | ||
94439e4e | 2661 | if (conn->body.callback != NULL) { |
62e76326 | 2662 | buf = conn->body.buf; |
2663 | callback = conn->body.callback; | |
62e76326 | 2664 | assert(request == conn->body.request); |
2665 | conn->body.buf = NULL; | |
2666 | conn->body.callback = NULL; | |
62e76326 | 2667 | conn->body.request = NULL; |
7ec0ff5c | 2668 | void *cbdata; |
2669 | ||
2670 | if (cbdataReferenceValidDone(conn->body.cbdata, &cbdata)) | |
2671 | callback(buf, -1, cbdata); /* Signal abort to clientReadBody caller */ | |
2672 | ||
1bb6a805 | 2673 | conn->body.cbdata = NULL; |
2674 | ||
62e76326 | 2675 | requestUnlink(request); |
94439e4e | 2676 | } |
62e76326 | 2677 | |
a2ac85d9 | 2678 | clientReadBodyAbortHandler(NULL, -1, conn.getRaw()); /* Install abort handler */ |
50c09fc4 | 2679 | /* ClientBody::process() */ |
94439e4e | 2680 | return 1; /* Aborted */ |
7a2f978b | 2681 | } |
2682 | ||
2683 | /* general lifetime handler for HTTP requests */ | |
2684 | static void | |
2685 | requestTimeout(int fd, void *data) | |
2686 | { | |
ad63ceea | 2687 | #if THIS_CONFUSES_PERSISTENT_CONNECTION_AWARE_BROWSERS_AND_USERS |
7a2f978b | 2688 | ConnStateData *conn = data; |
badd8ff0 | 2689 | debug(33, 3) ("requestTimeout: FD %d: lifetime is expired.\n", fd); |
62e76326 | 2690 | |
dec5db5d | 2691 | if (fd_table[fd].wstate) { |
2692 | /* FIXME: If this code is reinstated, check the conn counters, | |
2693 | * not the fd table state | |
2694 | */ | |
62e76326 | 2695 | /* |
2696 | * Some data has been sent to the client, just close the FD | |
2697 | */ | |
2698 | comm_close(fd); | |
7a2f978b | 2699 | } else if (conn->nrequests) { |
62e76326 | 2700 | /* |
2701 | * assume its a persistent connection; just close it | |
2702 | */ | |
2703 | comm_close(fd); | |
7a2f978b | 2704 | } else { |
62e76326 | 2705 | /* |
2706 | * Generate an error | |
2707 | */ | |
2708 | clientHttpRequest **H; | |
2709 | clientStreamNode *node; | |
2710 | clientHttpRequest *http = | |
2711 | parseHttpRequestAbort(conn, "error:Connection%20lifetime%20expired"); | |
2712 | node = http->client_stream.tail->prev->data; | |
0655fa4d | 2713 | clientReplyContext *repContext = dynamic_cast<clientReplyContext *>(node->data.getRaw()); |
2714 | assert (repContext); | |
2715 | repContext->setReplyToError(ERR_LIFETIME_EXP, | |
2716 | HTTP_REQUEST_TIMEOUT, METHOD_NONE, "N/A", &conn->peer.sin_addr, | |
2717 | NULL, NULL, NULL); | |
62e76326 | 2718 | /* No requests can be outstanded */ |
2719 | assert(conn->chr == NULL); | |
2720 | /* add to the client request queue */ | |
2721 | ||
2722 | for (H = &conn->chr; *H; H = &(*H)->next) | |
2723 | ||
2724 | ; | |
2725 | *H = http; | |
2726 | ||
2727 | clientStreamRead(http->client_stream.tail->data, http, 0, | |
2728 | HTTP_REQBUF_SZ, context->reqbuf); | |
2729 | ||
2730 | /* | |
2731 | * if we don't close() here, we still need a timeout handler! | |
2732 | */ | |
2733 | commSetTimeout(fd, 30, requestTimeout, conn); | |
2734 | ||
2735 | /* | |
2736 | * Aha, but we don't want a read handler! | |
2737 | */ | |
2738 | commSetSelect(fd, COMM_SELECT_READ, NULL, NULL, 0); | |
7a2f978b | 2739 | } |
62e76326 | 2740 | |
af57a2e3 | 2741 | #else |
2742 | /* | |
62e76326 | 2743 | * Just close the connection to not confuse browsers |
2744 | * using persistent connections. Some browsers opens | |
2745 | * an connection and then does not use it until much | |
2746 | * later (presumeably because the request triggering | |
2747 | * the open has already been completed on another | |
2748 | * connection) | |
2749 | */ | |
ad63ceea | 2750 | debug(33, 3) ("requestTimeout: FD %d: lifetime is expired.\n", fd); |
62e76326 | 2751 | |
af57a2e3 | 2752 | comm_close(fd); |
62e76326 | 2753 | |
af57a2e3 | 2754 | #endif |
7a2f978b | 2755 | } |
2756 | ||
b5c39993 | 2757 | static void |
2758 | clientLifetimeTimeout(int fd, void *data) | |
2759 | { | |
e6ccf245 | 2760 | clientHttpRequest *http = (clientHttpRequest *)data; |
edce4d98 | 2761 | debug(33, |
62e76326 | 2762 | 1) ("WARNING: Closing client %s connection due to lifetime timeout\n", |
a2ac85d9 | 2763 | inet_ntoa(http->getConn()->peer.sin_addr)); |
b5c39993 | 2764 | debug(33, 1) ("\t%s\n", http->uri); |
2765 | comm_close(fd); | |
2766 | } | |
2767 | ||
a46d2c0e | 2768 | static bool |
2769 | okToAccept() | |
7a2f978b | 2770 | { |
3d6629c6 | 2771 | static time_t last_warn = 0; |
62e76326 | 2772 | |
3d6629c6 | 2773 | if (fdNFree() >= RESERVED_FD) |
a46d2c0e | 2774 | return true; |
62e76326 | 2775 | |
3d6629c6 | 2776 | if (last_warn + 15 < squid_curtime) { |
62e76326 | 2777 | debug(33, 0) ("WARNING! Your cache is running out of filedescriptors\n"); |
2778 | last_warn = squid_curtime; | |
3d6629c6 | 2779 | } |
62e76326 | 2780 | |
a46d2c0e | 2781 | return false; |
7a2f978b | 2782 | } |
2783 | ||
c8be6d7b | 2784 | ConnStateData * |
62e76326 | 2785 | |
3f38a55e | 2786 | connStateCreate(struct sockaddr_in *peer, struct sockaddr_in *me, int fd, http_port_list *port) |
c8be6d7b | 2787 | { |
a46d2c0e | 2788 | ConnStateData *result = new ConnStateData; |
c4b7a5a9 | 2789 | result->peer = *peer; |
2790 | result->log_addr = peer->sin_addr; | |
c8be6d7b | 2791 | result->log_addr.s_addr &= Config.Addrs.client_netmask.s_addr; |
c4b7a5a9 | 2792 | result->me = *me; |
c8be6d7b | 2793 | result->fd = fd; |
e6ccf245 | 2794 | result->in.buf = (char *)memAllocBuf(CLIENT_REQ_BUF_SZ, &result->in.allocatedSize); |
3f38a55e | 2795 | result->port = cbdataReference(port); |
62e76326 | 2796 | |
2797 | if (port->transparent) | |
2798 | { | |
2799 | ||
2800 | struct sockaddr_in dst; | |
2801 | ||
2802 | if (clientNatLookup(fd, *me, *peer, &dst) == 0) { | |
2803 | result->me = dst; /* XXX This should be moved to another field */ | |
a46d2c0e | 2804 | result->transparent(true); |
62e76326 | 2805 | } |
3f38a55e | 2806 | } |
62e76326 | 2807 | |
c4b7a5a9 | 2808 | result->flags.readMoreRequests = 1; |
c8be6d7b | 2809 | return result; |
2810 | } | |
2811 | ||
bf8e5903 | 2812 | /* Handle a new connection on HTTP socket. */ |
7a2f978b | 2813 | void |
ee0989f2 | 2814 | httpAccept(int sock, int newfd, ConnectionDetail *details, |
62e76326 | 2815 | comm_err_t flag, int xerrno, void *data) |
7a2f978b | 2816 | { |
3f38a55e | 2817 | http_port_list *s = (http_port_list *)data; |
7a2f978b | 2818 | ConnStateData *connState = NULL; |
02d1422b | 2819 | |
2820 | if (flag == COMM_ERR_CLOSING) { | |
2821 | return; | |
2822 | } | |
2823 | ||
a46d2c0e | 2824 | if (!okToAccept()) |
2825 | AcceptLimiter::Instance().defer (sock, httpAccept, data); | |
2826 | else | |
2827 | /* kick off another one for later */ | |
2828 | comm_accept(sock, httpAccept, data); | |
c4b7a5a9 | 2829 | |
62e76326 | 2830 | if (flag != COMM_OK) { |
62e76326 | 2831 | debug(50, 1) ("httpAccept: FD %d: accept failure: %s\n", |
f3400a93 | 2832 | sock, xstrerr(xerrno)); |
62e76326 | 2833 | return; |
2834 | } | |
2835 | ||
2836 | debug(33, 4) ("httpAccept: FD %d: accepted\n", newfd); | |
2837 | fd_note(newfd, "client http connect"); | |
2838 | connState = connStateCreate(&details->peer, &details->me, newfd, s); | |
62e76326 | 2839 | comm_add_close_handler(newfd, connStateFree, connState); |
2840 | ||
2841 | if (Config.onoff.log_fqdn) | |
2842 | fqdncache_gethostbyaddr(details->peer.sin_addr, FQDN_LOOKUP_IF_MISS); | |
2843 | ||
2844 | commSetTimeout(newfd, Config.Timeout.request, requestTimeout, connState); | |
2845 | ||
3898f57f | 2846 | #if USE_IDENT |
62e76326 | 2847 | |
2848 | ACLChecklist identChecklist; | |
2849 | ||
2850 | identChecklist.src_addr = details->peer.sin_addr; | |
2851 | ||
2852 | identChecklist.my_addr = details->me.sin_addr; | |
2853 | ||
2854 | identChecklist.my_port = ntohs(details->me.sin_port); | |
2855 | ||
b448c119 | 2856 | identChecklist.accessList = Config.accessList.identLookup; |
2857 | ||
2858 | if (identChecklist.fastCheck()) | |
62e76326 | 2859 | identStart(&details->me, &details->peer, clientIdentDone, connState); |
2860 | ||
b448c119 | 2861 | identChecklist.accessList = NULL; |
2862 | ||
3898f57f | 2863 | #endif |
62e76326 | 2864 | |
a46d2c0e | 2865 | connState->readSomeData(); |
62e76326 | 2866 | |
2867 | clientdbEstablished(details->peer.sin_addr, 1); | |
2868 | ||
2869 | incoming_sockets_accepted++; | |
7a2f978b | 2870 | } |
2871 | ||
1f7c9178 | 2872 | #if USE_SSL |
2873 | ||
2874 | /* negotiate an SSL connection */ | |
2875 | static void | |
2876 | clientNegotiateSSL(int fd, void *data) | |
2877 | { | |
e6ccf245 | 2878 | ConnStateData *conn = (ConnStateData *)data; |
1f7c9178 | 2879 | X509 *client_cert; |
a7ad6e4e | 2880 | SSL *ssl = fd_table[fd].ssl; |
1f7c9178 | 2881 | int ret; |
2882 | ||
a7ad6e4e | 2883 | if ((ret = SSL_accept(ssl)) <= 0) { |
62e76326 | 2884 | int ssl_error = SSL_get_error(ssl, ret); |
2885 | ||
2886 | switch (ssl_error) { | |
2887 | ||
2888 | case SSL_ERROR_WANT_READ: | |
2889 | commSetSelect(fd, COMM_SELECT_READ, clientNegotiateSSL, conn, 0); | |
2890 | return; | |
2891 | ||
2892 | case SSL_ERROR_WANT_WRITE: | |
2893 | commSetSelect(fd, COMM_SELECT_WRITE, clientNegotiateSSL, conn, 0); | |
2894 | return; | |
2895 | ||
2896 | default: | |
2897 | debug(81, 1) ("clientNegotiateSSL: Error negotiating SSL connection on FD %d: %s (%d/%d)\n", | |
2898 | fd, ERR_error_string(ERR_get_error(), NULL), ssl_error, ret); | |
2899 | comm_close(fd); | |
2900 | return; | |
2901 | } | |
2902 | ||
2903 | /* NOTREACHED */ | |
1f7c9178 | 2904 | } |
62e76326 | 2905 | |
27d8545c | 2906 | debug(83, 5) ("clientNegotiateSSL: FD %d negotiated cipher %s\n", fd, |
62e76326 | 2907 | SSL_get_cipher(fd_table[fd].ssl)); |
1f7c9178 | 2908 | |
2909 | client_cert = SSL_get_peer_certificate(fd_table[fd].ssl); | |
62e76326 | 2910 | |
1f7c9178 | 2911 | if (client_cert != NULL) { |
62e76326 | 2912 | debug(83, 5) ("clientNegotiateSSL: FD %d client certificate: subject: %s\n", |
2913 | fd, X509_NAME_oneline(X509_get_subject_name(client_cert), 0, 0)); | |
1f7c9178 | 2914 | |
62e76326 | 2915 | debug(83, 5) ("clientNegotiateSSL: FD %d client certificate: issuer: %s\n", |
2916 | fd, X509_NAME_oneline(X509_get_issuer_name(client_cert), 0, 0)); | |
1f7c9178 | 2917 | |
62e76326 | 2918 | X509_free(client_cert); |
1f7c9178 | 2919 | } else { |
62e76326 | 2920 | debug(83, 5) ("clientNegotiateSSL: FD %d has no certificate.\n", fd); |
1f7c9178 | 2921 | } |
2922 | ||
a46d2c0e | 2923 | conn->readSomeData(); |
1f7c9178 | 2924 | } |
2925 | ||
2926 | /* handle a new HTTPS connection */ | |
2927 | static void | |
15595aab | 2928 | httpsAccept(int sock, int newfd, ConnectionDetail *details, |
62e76326 | 2929 | comm_err_t flag, int xerrno, void *data) |
1f7c9178 | 2930 | { |
3f38a55e | 2931 | https_port_list *s = (https_port_list *)data; |
2932 | SSL_CTX *sslContext = s->sslContext; | |
1f7c9178 | 2933 | ConnStateData *connState = NULL; |
1f7c9178 | 2934 | SSL *ssl; |
2935 | int ssl_error; | |
c4b7a5a9 | 2936 | |
02d1422b | 2937 | if (flag == COMM_ERR_CLOSING) { |
2938 | return; | |
2939 | } | |
2940 | ||
a46d2c0e | 2941 | if (!okToAccept()) |
2942 | AcceptLimiter::Instance().defer (sock, httpsAccept, data); | |
2943 | else | |
2944 | /* kick off another one for later */ | |
2945 | comm_accept(sock, httpsAccept, data); | |
2946 | ||
c4b7a5a9 | 2947 | if (flag != COMM_OK) { |
62e76326 | 2948 | errno = xerrno; |
2949 | debug(50, 1) ("httpsAccept: FD %d: accept failure: %s\n", | |
f3400a93 | 2950 | sock, xstrerr(xerrno)); |
62e76326 | 2951 | return; |
c4b7a5a9 | 2952 | } |
62e76326 | 2953 | |
c4b7a5a9 | 2954 | if ((ssl = SSL_new(sslContext)) == NULL) { |
62e76326 | 2955 | ssl_error = ERR_get_error(); |
2956 | debug(83, 1) ("httpsAccept: Error allocating handle: %s\n", | |
2957 | ERR_error_string(ssl_error, NULL)); | |
2958 | return; | |
c4b7a5a9 | 2959 | } |
2960 | ||
2961 | SSL_set_fd(ssl, newfd); | |
2962 | fd_table[newfd].ssl = ssl; | |
2963 | fd_table[newfd].read_method = &ssl_read_method; | |
2964 | fd_table[newfd].write_method = &ssl_write_method; | |
2965 | debug(50, 5) ("httpsAccept: FD %d accepted, starting SSL negotiation.\n", newfd); | |
3f38a55e | 2966 | fd_note(newfd, "client https connect"); |
62e76326 | 2967 | |
3f38a55e | 2968 | connState = connStateCreate(&details->peer, &details->me, newfd, (http_port_list *)s); |
2969 | connState->port = (http_port_list *)cbdataReference(s); | |
c4b7a5a9 | 2970 | comm_add_close_handler(newfd, connStateFree, connState); |
62e76326 | 2971 | |
c4b7a5a9 | 2972 | if (Config.onoff.log_fqdn) |
62e76326 | 2973 | fqdncache_gethostbyaddr(details->peer.sin_addr, FQDN_LOOKUP_IF_MISS); |
2974 | ||
c4b7a5a9 | 2975 | commSetTimeout(newfd, Config.Timeout.request, requestTimeout, connState); |
62e76326 | 2976 | |
1f7c9178 | 2977 | #if USE_IDENT |
62e76326 | 2978 | |
8000a965 | 2979 | ACLChecklist identChecklist; |
62e76326 | 2980 | |
15595aab | 2981 | identChecklist.src_addr = details->peer.sin_addr; |
62e76326 | 2982 | |
15595aab | 2983 | identChecklist.my_addr = details->me.sin_addr; |
62e76326 | 2984 | |
15595aab | 2985 | identChecklist.my_port = ntohs(details->me.sin_port); |
62e76326 | 2986 | |
010af6e6 | 2987 | identChecklist.accessList = Config.accessList.identLookup; |
2988 | ||
2989 | if (identChecklist.fastCheck()) | |
62e76326 | 2990 | identStart(&details->me, &details->peer, clientIdentDone, connState); |
2991 | ||
010af6e6 | 2992 | identChecklist.accessList = NULL; |
2993 | ||
1f7c9178 | 2994 | #endif |
62e76326 | 2995 | |
c4b7a5a9 | 2996 | commSetSelect(newfd, COMM_SELECT_READ, clientNegotiateSSL, connState, 0); |
62e76326 | 2997 | |
15595aab | 2998 | clientdbEstablished(details->peer.sin_addr, 1); |
62e76326 | 2999 | |
3f38a55e | 3000 | incoming_sockets_accepted++; |
1f7c9178 | 3001 | } |
3002 | ||
3003 | #endif /* USE_SSL */ | |
3004 | ||
15df8349 | 3005 | |
d193a436 | 3006 | static void |
15df8349 | 3007 | clientHttpConnectionsOpen(void) |
3008 | { | |
3f38a55e | 3009 | http_port_list *s; |
15df8349 | 3010 | int fd; |
62e76326 | 3011 | |
7e3ce7b9 | 3012 | for (s = Config.Sockaddr.http; s; s = s->next) { |
62e76326 | 3013 | if (MAXHTTPPORTS == NHttpSockets) { |
3014 | debug(1, 1) ("WARNING: You have too many 'http_port' lines.\n"); | |
3015 | debug(1, 1) (" The limit is %d\n", MAXHTTPPORTS); | |
3016 | continue; | |
3017 | } | |
3018 | ||
3019 | enter_suid(); | |
3020 | fd = comm_open(SOCK_STREAM, | |
bdb741f4 | 3021 | IPPROTO_TCP, |
62e76326 | 3022 | s->s.sin_addr, |
3023 | ntohs(s->s.sin_port), COMM_NONBLOCKING, "HTTP Socket"); | |
3024 | leave_suid(); | |
3025 | ||
3026 | if (fd < 0) | |
3027 | continue; | |
3028 | ||
3029 | comm_listen(fd); | |
3030 | ||
3031 | comm_accept(fd, httpAccept, s); | |
3032 | ||
2315dd40 | 3033 | debug(1, 1) ("Accepting %s HTTP connections at %s, port %d, FD %d.\n", |
3034 | s->transparent ? "transparently proxied" : | |
3035 | s->accel ? "accelerated" : | |
24450a8f | 3036 | "", |
62e76326 | 3037 | inet_ntoa(s->s.sin_addr), (int) ntohs(s->s.sin_port), fd); |
3038 | ||
3039 | HttpSockets[NHttpSockets++] = fd; | |
15df8349 | 3040 | } |
d193a436 | 3041 | } |
3042 | ||
3043 | #if USE_SSL | |
3044 | static void | |
3045 | clientHttpsConnectionsOpen(void) | |
3046 | { | |
3047 | https_port_list *s; | |
d193a436 | 3048 | int fd; |
62e76326 | 3049 | |
3f38a55e | 3050 | for (s = Config.Sockaddr.https; s; s = (https_port_list *)s->http.next) { |
62e76326 | 3051 | if (MAXHTTPPORTS == NHttpSockets) { |
3052 | debug(1, 1) ("WARNING: You have too many 'https_port' lines.\n"); | |
3053 | debug(1, 1) (" The limit is %d\n", MAXHTTPPORTS); | |
3054 | continue; | |
3055 | } | |
3056 | ||
f9ad0106 | 3057 | if (s->sslContext == NULL) { |
3058 | debug(1, 1) ("Can not accept HTTPS connections at %s, port %d\n", | |
3059 | inet_ntoa(s->http.s.sin_addr), (int) ntohs(s->http.s.sin_port)); | |
3060 | } | |
3061 | ||
62e76326 | 3062 | enter_suid(); |
3063 | fd = comm_open(SOCK_STREAM, | |
bdb741f4 | 3064 | IPPROTO_TCP, |
62e76326 | 3065 | s->http.s.sin_addr, |
3066 | ntohs(s->http.s.sin_port), COMM_NONBLOCKING, "HTTPS Socket"); | |
3067 | leave_suid(); | |
3068 | ||
3069 | if (fd < 0) | |
3070 | continue; | |
3071 | ||
3072 | comm_listen(fd); | |
3073 | ||
3074 | comm_accept(fd, httpsAccept, s); | |
3075 | ||
62e76326 | 3076 | debug(1, 1) ("Accepting HTTPS connections at %s, port %d, FD %d.\n", |
3077 | inet_ntoa(s->http.s.sin_addr), (int) ntohs(s->http.s.sin_port), fd); | |
3078 | ||
3079 | HttpSockets[NHttpSockets++] = fd; | |
1f7c9178 | 3080 | } |
d193a436 | 3081 | } |
3082 | ||
3083 | #endif | |
3084 | ||
3085 | void | |
3086 | clientOpenListenSockets(void) | |
3087 | { | |
3088 | clientHttpConnectionsOpen(); | |
3089 | #if USE_SSL | |
62e76326 | 3090 | |
d193a436 | 3091 | clientHttpsConnectionsOpen(); |
1f7c9178 | 3092 | #endif |
62e76326 | 3093 | |
15df8349 | 3094 | if (NHttpSockets < 1) |
62e76326 | 3095 | fatal("Cannot open HTTP Port"); |
15df8349 | 3096 | } |
edce4d98 | 3097 | |
c0fbae16 | 3098 | void |
3099 | clientHttpConnectionsClose(void) | |
3100 | { | |
3101 | int i; | |
62e76326 | 3102 | |
c0fbae16 | 3103 | for (i = 0; i < NHttpSockets; i++) { |
62e76326 | 3104 | if (HttpSockets[i] >= 0) { |
3105 | debug(1, 1) ("FD %d Closing HTTP connection\n", HttpSockets[i]); | |
3106 | comm_close(HttpSockets[i]); | |
3107 | HttpSockets[i] = -1; | |
3108 | } | |
c0fbae16 | 3109 | } |
62e76326 | 3110 | |
c0fbae16 | 3111 | NHttpSockets = 0; |
3112 | } | |
f66a9ef4 | 3113 | |
3114 | int | |
190154cf | 3115 | varyEvaluateMatch(StoreEntry * entry, HttpRequest * request) |
f66a9ef4 | 3116 | { |
3117 | const char *vary = request->vary_headers; | |
528b2c61 | 3118 | int has_vary = httpHeaderHas(&entry->getReply()->header, HDR_VARY); |
f66a9ef4 | 3119 | #if X_ACCELERATOR_VARY |
62e76326 | 3120 | |
edce4d98 | 3121 | has_vary |= |
62e76326 | 3122 | httpHeaderHas(&entry->getReply()->header, HDR_X_ACCELERATOR_VARY); |
f66a9ef4 | 3123 | #endif |
62e76326 | 3124 | |
f66a9ef4 | 3125 | if (!has_vary || !entry->mem_obj->vary_headers) { |
62e76326 | 3126 | if (vary) { |
3127 | /* Oops... something odd is going on here.. */ | |
3128 | debug(33, | |
3129 | 1) | |
3130 | ("varyEvaluateMatch: Oops. Not a Vary object on second attempt, '%s' '%s'\n", | |
3131 | entry->mem_obj->url, vary); | |
3132 | safe_free(request->vary_headers); | |
3133 | return VARY_CANCEL; | |
3134 | } | |
3135 | ||
3136 | if (!has_vary) { | |
3137 | /* This is not a varying object */ | |
3138 | return VARY_NONE; | |
3139 | } | |
3140 | ||
3141 | /* virtual "vary" object found. Calculate the vary key and | |
3142 | * continue the search | |
3143 | */ | |
3144 | vary = httpMakeVaryMark(request, entry->getReply()); | |
3145 | ||
3146 | if (vary) { | |
3147 | request->vary_headers = xstrdup(vary); | |
3148 | return VARY_OTHER; | |
3149 | } else { | |
3150 | /* Ouch.. we cannot handle this kind of variance */ | |
3151 | /* XXX This cannot really happen, but just to be complete */ | |
3152 | return VARY_CANCEL; | |
3153 | } | |
f66a9ef4 | 3154 | } else { |
62e76326 | 3155 | if (!vary) { |
3156 | vary = httpMakeVaryMark(request, entry->getReply()); | |
3157 | ||
3158 | if (vary) | |
3159 | request->vary_headers = xstrdup(vary); | |
3160 | } | |
3161 | ||
3162 | if (!vary) { | |
3163 | /* Ouch.. we cannot handle this kind of variance */ | |
3164 | /* XXX This cannot really happen, but just to be complete */ | |
3165 | return VARY_CANCEL; | |
3166 | } else if (strcmp(vary, entry->mem_obj->vary_headers) == 0) { | |
3167 | return VARY_MATCH; | |
3168 | } else { | |
3169 | /* Oops.. we have already been here and still haven't | |
3170 | * found the requested variant. Bail out | |
3171 | */ | |
3172 | debug(33, 1) ("varyEvaluateMatch: Oops. Not a Vary match on second attempt, '%s' '%s'\n", | |
3173 | entry->mem_obj->url, vary); | |
3174 | return VARY_CANCEL; | |
3175 | } | |
f66a9ef4 | 3176 | } |
3177 | } | |
28d4805a | 3178 | |
4fb35c3c | 3179 | ACLChecklist * |
98242069 | 3180 | clientAclChecklistCreate(const acl_access * acl, clientHttpRequest * http) |
28d4805a | 3181 | { |
4fb35c3c | 3182 | ACLChecklist *ch; |
a2ac85d9 | 3183 | ConnStateData::Pointer conn = http->getConn(); |
3184 | ch = aclChecklistCreate(acl, http->request, conn.getRaw() != NULL ? conn->rfc931 : dash_str); | |
28d4805a | 3185 | |
3186 | /* | |
3187 | * hack for ident ACL. It needs to get full addresses, and a place to store | |
3188 | * the ident result on persistent connections... | |
3189 | */ | |
3190 | /* connection oriented auth also needs these two lines for it's operation. */ | |
3191 | /* | |
3192 | * Internal requests do not have a connection reference, because: A) their | |
3193 | * byte count may be transformed before being applied to an outbound | |
3194 | * connection B) they are internal - any limiting on them should be done on | |
3195 | * the server end. | |
3196 | */ | |
62e76326 | 3197 | |
a2ac85d9 | 3198 | if (conn.getRaw() != NULL) |
3199 | ch->conn(conn); /* unreferenced in acl.cc */ | |
28d4805a | 3200 | |
3201 | return ch; | |
3202 | } | |
a46d2c0e | 3203 | |
3204 | CBDATA_CLASS_INIT(ConnStateData); | |
3205 | ||
3206 | void * | |
3207 | ConnStateData::operator new (size_t) | |
3208 | { | |
3209 | CBDATA_INIT_TYPE(ConnStateData); | |
3210 | ConnStateData *result = cbdataAlloc(ConnStateData); | |
3211 | return result; | |
3212 | } | |
3213 | ||
3214 | void | |
3215 | ConnStateData::operator delete (void *address) | |
3216 | { | |
3217 | ConnStateData *t = static_cast<ConnStateData *>(address); | |
3218 | cbdataFree(t); | |
3219 | } | |
3220 | ||
55e44db9 | 3221 | ConnStateData::ConnStateData() : transparent_ (false), reading_ (false), closing_ (false) |
b65351fa | 3222 | { |
3223 | openReference = this; | |
3224 | } | |
a46d2c0e | 3225 | |
3226 | bool | |
3227 | ConnStateData::transparent() const | |
3228 | { | |
3229 | return transparent_; | |
3230 | } | |
3231 | ||
3232 | void | |
3233 | ConnStateData::transparent(bool const anInt) | |
3234 | { | |
3235 | transparent_ = anInt; | |
3236 | } | |
3237 | ||
3238 | bool | |
3239 | ConnStateData::reading() const | |
3240 | { | |
3241 | return reading_; | |
3242 | } | |
3243 | ||
3244 | void | |
3245 | ConnStateData::reading(bool const newBool) | |
3246 | { | |
3247 | assert (reading() != newBool); | |
3248 | reading_ = newBool; | |
3249 | } | |
3250 | ||
55e44db9 | 3251 | bool |
3252 | ConnStateData::closing() const | |
3253 | { | |
3254 | return closing_; | |
3255 | } | |
3256 | ||
3257 | void | |
3258 | ConnStateData::closing(bool const newBool) | |
3259 | { | |
3260 | assert (closing() != newBool); | |
3261 | closing_ = newBool; | |
3262 | } | |
3263 | ||
a46d2c0e | 3264 | char * |
3265 | ConnStateData::In::addressToReadInto() const | |
3266 | { | |
3267 | return buf + notYetUsed; | |
3268 | } | |
3269 | ||
3270 | ConnStateData::In::In() : buf (NULL), notYetUsed (0), allocatedSize (0) | |
3271 | {} | |
3272 | ||
3273 | ConnStateData::In::~In() | |
3274 | { | |
3275 | if (allocatedSize) | |
3276 | memFreeBuf(allocatedSize, buf); | |
3277 | } |