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