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