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