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