source: squid-ssl/trunk/fuentes/.pc/CVE-2016-10002.patch/src/client_side.cc @ 5502

Last change on this file since 5502 was 5502, checked in by Juanma, 4 years ago

Initial release

File size: 181.2 KB
Line 
1/*
2 * Copyright (C) 1996-2015 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9/* DEBUG: section 33    Client-side Routines */
10
11/**
12 \defgroup ClientSide Client-Side Logics
13 *
14 \section cserrors Errors and client side
15 *
16 \par Problem the first:
17 * the store entry is no longer authoritative on the
18 * reply status. EBITTEST (E_ABORT) is no longer a valid test outside
19 * of client_side_reply.c.
20 * Problem the second: resources are wasted if we delay in cleaning up.
21 * Problem the third we can't depend on a connection close to clean up.
22 *
23 \par Nice thing the first:
24 * Any step in the stream can callback with data
25 * representing an error.
26 * Nice thing the second: once you stop requesting reads from upstream,
27 * upstream can be stopped too.
28 *
29 \par Solution #1:
30 * Error has a callback mechanism to hand over a membuf
31 * with the error content. The failing node pushes that back as the
32 * reply. Can this be generalised to reduce duplicate efforts?
33 * A: Possibly. For now, only one location uses this.
34 * How to deal with pre-stream errors?
35 * Tell client_side_reply that we *want* an error page before any
36 * stream calls occur. Then we simply read as normal.
37 *
38 *
39 \section pconn_logic Persistent connection logic:
40 *
41 \par
42 * requests (httpClientRequest structs) get added to the connection
43 * list, with the current one being chr
44 *
45 \par
46 * The request is *immediately* kicked off, and data flows through
47 * to clientSocketRecipient.
48 *
49 \par
50 * If the data that arrives at clientSocketRecipient is not for the current
51 * request, clientSocketRecipient simply returns, without requesting more
52 * data, or sending it.
53 *
54 \par
55 * ClientKeepAliveNextRequest will then detect the presence of data in
56 * the next ClientHttpRequest, and will send it, restablishing the
57 * data flow.
58 */
59
60#include "squid.h"
61#include "acl/FilledChecklist.h"
62#include "anyp/PortCfg.h"
63#include "base/Subscription.h"
64#include "base/TextException.h"
65#include "CachePeer.h"
66#include "ChunkedCodingParser.h"
67#include "client_db.h"
68#include "client_side.h"
69#include "client_side_reply.h"
70#include "client_side_request.h"
71#include "ClientRequestContext.h"
72#include "clientStream.h"
73#include "comm.h"
74#include "comm/Connection.h"
75#include "comm/Loops.h"
76#include "comm/Read.h"
77#include "comm/TcpAcceptor.h"
78#include "comm/Write.h"
79#include "CommCalls.h"
80#include "errorpage.h"
81#include "fd.h"
82#include "fde.h"
83#include "fqdncache.h"
84#include "FwdState.h"
85#include "globals.h"
86#include "helper.h"
87#include "helper/Reply.h"
88#include "http.h"
89#include "HttpHdrContRange.h"
90#include "HttpHeaderTools.h"
91#include "HttpReply.h"
92#include "HttpRequest.h"
93#include "ident/Config.h"
94#include "ident/Ident.h"
95#include "internal.h"
96#include "ipc/FdNotes.h"
97#include "ipc/StartListening.h"
98#include "log/access_log.h"
99#include "Mem.h"
100#include "MemBuf.h"
101#include "MemObject.h"
102#include "mime_header.h"
103#include "parser/Tokenizer.h"
104#include "profiler/Profiler.h"
105#include "rfc1738.h"
106#include "servers/forward.h"
107#include "SquidConfig.h"
108#include "SquidTime.h"
109#include "StatCounters.h"
110#include "StatHist.h"
111#include "Store.h"
112#include "TimeOrTag.h"
113#include "tools.h"
114#include "URL.h"
115
116#if USE_AUTH
117#include "auth/UserRequest.h"
118#endif
119#if USE_DELAY_POOLS
120#include "ClientInfo.h"
121#endif
122#if USE_OPENSSL
123#include "ssl/bio.h"
124#include "ssl/context_storage.h"
125#include "ssl/gadgets.h"
126#include "ssl/helper.h"
127#include "ssl/ProxyCerts.h"
128#include "ssl/ServerBump.h"
129#include "ssl/support.h"
130#endif
131#if USE_SSL_CRTD
132#include "ssl/certificate_db.h"
133#include "ssl/crtd_message.h"
134#endif
135
136#include <climits>
137#include <cmath>
138#include <limits>
139
140#if LINGERING_CLOSE
141#define comm_close comm_lingering_close
142#endif
143
144/// dials clientListenerConnectionOpened call
145class ListeningStartedDialer: public CallDialer, public Ipc::StartListeningCb
146{
147public:
148    typedef void (*Handler)(AnyP::PortCfgPointer &portCfg, const Ipc::FdNoteId note, const Subscription::Pointer &sub);
149    ListeningStartedDialer(Handler aHandler, AnyP::PortCfgPointer &aPortCfg, const Ipc::FdNoteId note, const Subscription::Pointer &aSub):
150        handler(aHandler), portCfg(aPortCfg), portTypeNote(note), sub(aSub) {}
151
152    virtual void print(std::ostream &os) const {
153        startPrint(os) <<
154                       ", " << FdNote(portTypeNote) << " port=" << (void*)&portCfg << ')';
155    }
156
157    virtual bool canDial(AsyncCall &) const { return true; }
158    virtual void dial(AsyncCall &) { (handler)(portCfg, portTypeNote, sub); }
159
160public:
161    Handler handler;
162
163private:
164    AnyP::PortCfgPointer portCfg;   ///< from HttpPortList
165    Ipc::FdNoteId portTypeNote;    ///< Type of IPC socket being opened
166    Subscription::Pointer sub; ///< The handler to be subscribed for this connetion listener
167};
168
169static void clientListenerConnectionOpened(AnyP::PortCfgPointer &s, const Ipc::FdNoteId portTypeNote, const Subscription::Pointer &sub);
170
171/* our socket-related context */
172
173CBDATA_CLASS_INIT(ClientSocketContext);
174
175/* Local functions */
176static IOCB clientWriteComplete;
177static IOCB clientWriteBodyComplete;
178static IOACB httpAccept;
179#if USE_OPENSSL
180static IOACB httpsAccept;
181#endif
182static CTCB clientLifetimeTimeout;
183#if USE_IDENT
184static IDCB clientIdentDone;
185#endif
186static int clientIsContentLengthValid(HttpRequest * r);
187static int clientIsRequestBodyTooLargeForPolicy(int64_t bodyLength);
188
189static void clientUpdateStatHistCounters(LogTags logType, int svc_time);
190static void clientUpdateStatCounters(LogTags logType);
191static void clientUpdateHierCounters(HierarchyLogEntry *);
192static bool clientPingHasFinished(ping_data const *aPing);
193void prepareLogWithRequestDetails(HttpRequest *, AccessLogEntry::Pointer &);
194static void ClientSocketContextPushDeferredIfNeeded(ClientSocketContext::Pointer deferredRequest, ConnStateData * conn);
195static void clientUpdateSocketStats(LogTags logType, size_t size);
196
197char *skipLeadingSpace(char *aString);
198static void connNoteUseOfBuffer(ConnStateData* conn, size_t byteCount);
199
200clientStreamNode *
201ClientSocketContext::getTail() const
202{
203    if (http->client_stream.tail)
204        return (clientStreamNode *)http->client_stream.tail->data;
205
206    return NULL;
207}
208
209clientStreamNode *
210ClientSocketContext::getClientReplyContext() const
211{
212    return (clientStreamNode *)http->client_stream.tail->prev->data;
213}
214
215ConnStateData *
216ClientSocketContext::getConn() const
217{
218    return http->getConn();
219}
220
221/**
222 * This routine should be called to grow the in.buf and then
223 * call Comm::Read().
224 */
225void
226ConnStateData::readSomeData()
227{
228    if (reading())
229        return;
230
231    debugs(33, 4, HERE << clientConnection << ": reading request...");
232
233    // we can only read if there is more than 1 byte of space free
234    if (Config.maxRequestBufferSize - in.buf.length() < 2)
235        return;
236
237    typedef CommCbMemFunT<ConnStateData, CommIoCbParams> Dialer;
238    reader = JobCallback(33, 5, Dialer, this, ConnStateData::clientReadRequest);
239    Comm::Read(clientConnection, reader);
240}
241
242void
243ClientSocketContext::removeFromConnectionList(ConnStateData * conn)
244{
245    ClientSocketContext::Pointer *tempContextPointer;
246    assert(conn != NULL && cbdataReferenceValid(conn));
247    assert(conn->getCurrentContext() != NULL);
248    /* Unlink us from the connection request list */
249    tempContextPointer = & conn->currentobject;
250
251    while (tempContextPointer->getRaw()) {
252        if (*tempContextPointer == this)
253            break;
254
255        tempContextPointer = &(*tempContextPointer)->next;
256    }
257
258    assert(tempContextPointer->getRaw() != NULL);
259    *tempContextPointer = next;
260    next = NULL;
261}
262
263ClientSocketContext::~ClientSocketContext()
264{
265    clientStreamNode *node = getTail();
266
267    if (node) {
268        ClientSocketContext *streamContext = dynamic_cast<ClientSocketContext *> (node->data.getRaw());
269
270        if (streamContext) {
271            /* We are *always* the tail - prevent recursive free */
272            assert(this == streamContext);
273            node->data = NULL;
274        }
275    }
276
277    if (connRegistered_)
278        deRegisterWithConn();
279
280    httpRequestFree(http);
281
282    /* clean up connection links to us */
283    assert(this != next.getRaw());
284}
285
286void
287ClientSocketContext::registerWithConn()
288{
289    assert (!connRegistered_);
290    assert (http);
291    assert (http->getConn() != NULL);
292    connRegistered_ = true;
293    http->getConn()->addContextToQueue(this);
294}
295
296void
297ClientSocketContext::deRegisterWithConn()
298{
299    assert (connRegistered_);
300    removeFromConnectionList(http->getConn());
301    connRegistered_ = false;
302}
303
304void
305ClientSocketContext::connIsFinished()
306{
307    assert (http);
308    assert (http->getConn() != NULL);
309    deRegisterWithConn();
310    /* we can't handle any more stream data - detach */
311    clientStreamDetach(getTail(), http);
312}
313
314ClientSocketContext::ClientSocketContext(const Comm::ConnectionPointer &aConn, ClientHttpRequest *aReq) :
315    clientConnection(aConn),
316    http(aReq),
317    reply(NULL),
318    next(NULL),
319    writtenToSocket(0),
320    mayUseConnection_ (false),
321    connRegistered_ (false)
322{
323    assert(http != NULL);
324    memset (reqbuf, '\0', sizeof (reqbuf));
325    flags.deferred = 0;
326    flags.parsed_ok = 0;
327    deferredparams.node = NULL;
328    deferredparams.rep = NULL;
329}
330
331void
332ClientSocketContext::writeControlMsg(HttpControlMsg &msg)
333{
334    HttpReply::Pointer rep(msg.reply);
335    Must(rep != NULL);
336
337    // remember the callback
338    cbControlMsgSent = msg.cbSuccess;
339
340    AsyncCall::Pointer call = commCbCall(33, 5, "ClientSocketContext::wroteControlMsg",
341                                         CommIoCbPtrFun(&WroteControlMsg, this));
342
343    getConn()->writeControlMsgAndCall(this, rep.getRaw(), call);
344}
345
346/// called when we wrote the 1xx response
347void
348ClientSocketContext::wroteControlMsg(const Comm::ConnectionPointer &conn, char *, size_t, Comm::Flag errflag, int xerrno)
349{
350    if (errflag == Comm::ERR_CLOSING)
351        return;
352
353    if (errflag == Comm::OK) {
354        ScheduleCallHere(cbControlMsgSent);
355        return;
356    }
357
358    debugs(33, 3, HERE << "1xx writing failed: " << xstrerr(xerrno));
359    // no error notification: see HttpControlMsg.h for rationale and
360    // note that some errors are detected elsewhere (e.g., close handler)
361
362    // close on 1xx errors to be conservative and to simplify the code
363    // (if we do not close, we must notify the source of a failure!)
364    conn->close();
365
366    // XXX: writeControlMsgAndCall() should handle writer-specific writing
367    // results, including errors and then call us with success/failure outcome.
368}
369
370/// wroteControlMsg() wrapper: ClientSocketContext is not an AsyncJob
371void
372ClientSocketContext::WroteControlMsg(const Comm::ConnectionPointer &conn, char *bufnotused, size_t size, Comm::Flag errflag, int xerrno, void *data)
373{
374    ClientSocketContext *context = static_cast<ClientSocketContext*>(data);
375    context->wroteControlMsg(conn, bufnotused, size, errflag, xerrno);
376}
377
378#if USE_IDENT
379static void
380clientIdentDone(const char *ident, void *data)
381{
382    ConnStateData *conn = (ConnStateData *)data;
383    xstrncpy(conn->clientConnection->rfc931, ident ? ident : dash_str, USER_IDENT_SZ);
384}
385#endif
386
387void
388clientUpdateStatCounters(LogTags logType)
389{
390    ++statCounter.client_http.requests;
391
392    if (logTypeIsATcpHit(logType))
393        ++statCounter.client_http.hits;
394
395    if (logType == LOG_TCP_HIT)
396        ++statCounter.client_http.disk_hits;
397    else if (logType == LOG_TCP_MEM_HIT)
398        ++statCounter.client_http.mem_hits;
399}
400
401void
402clientUpdateStatHistCounters(LogTags logType, int svc_time)
403{
404    statCounter.client_http.allSvcTime.count(svc_time);
405    /**
406     * The idea here is not to be complete, but to get service times
407     * for only well-defined types.  For example, we don't include
408     * LOG_TCP_REFRESH_FAIL because its not really a cache hit
409     * (we *tried* to validate it, but failed).
410     */
411
412    switch (logType) {
413
414    case LOG_TCP_REFRESH_UNMODIFIED:
415        statCounter.client_http.nearHitSvcTime.count(svc_time);
416        break;
417
418    case LOG_TCP_IMS_HIT:
419        statCounter.client_http.nearMissSvcTime.count(svc_time);
420        break;
421
422    case LOG_TCP_HIT:
423
424    case LOG_TCP_MEM_HIT:
425
426    case LOG_TCP_OFFLINE_HIT:
427        statCounter.client_http.hitSvcTime.count(svc_time);
428        break;
429
430    case LOG_TCP_MISS:
431
432    case LOG_TCP_CLIENT_REFRESH_MISS:
433        statCounter.client_http.missSvcTime.count(svc_time);
434        break;
435
436    default:
437        /* make compiler warnings go away */
438        break;
439    }
440}
441
442bool
443clientPingHasFinished(ping_data const *aPing)
444{
445    if (0 != aPing->stop.tv_sec && 0 != aPing->start.tv_sec)
446        return true;
447
448    return false;
449}
450
451void
452clientUpdateHierCounters(HierarchyLogEntry * someEntry)
453{
454    ping_data *i;
455
456    switch (someEntry->code) {
457#if USE_CACHE_DIGESTS
458
459    case CD_PARENT_HIT:
460
461    case CD_SIBLING_HIT:
462        ++ statCounter.cd.times_used;
463        break;
464#endif
465
466    case SIBLING_HIT:
467
468    case PARENT_HIT:
469
470    case FIRST_PARENT_MISS:
471
472    case CLOSEST_PARENT_MISS:
473        ++ statCounter.icp.times_used;
474        i = &someEntry->ping;
475
476        if (clientPingHasFinished(i))
477            statCounter.icp.querySvcTime.count(tvSubUsec(i->start, i->stop));
478
479        if (i->timeout)
480            ++ statCounter.icp.query_timeouts;
481
482        break;
483
484    case CLOSEST_PARENT:
485
486    case CLOSEST_DIRECT:
487        ++ statCounter.netdb.times_used;
488
489        break;
490
491    default:
492        break;
493    }
494}
495
496void
497ClientHttpRequest::updateCounters()
498{
499    clientUpdateStatCounters(logType);
500
501    if (request->errType != ERR_NONE)
502        ++ statCounter.client_http.errors;
503
504    clientUpdateStatHistCounters(logType,
505                                 tvSubMsec(al->cache.start_time, current_time));
506
507    clientUpdateHierCounters(&request->hier);
508}
509
510void
511prepareLogWithRequestDetails(HttpRequest * request, AccessLogEntry::Pointer &aLogEntry)
512{
513    assert(request);
514    assert(aLogEntry != NULL);
515
516    if (Config.onoff.log_mime_hdrs) {
517        Packer p;
518        MemBuf mb;
519        mb.init();
520        packerToMemInit(&p, &mb);
521        request->header.packInto(&p);
522        //This is the request after adaptation or redirection
523        aLogEntry->headers.adapted_request = xstrdup(mb.buf);
524
525        // the virgin request is saved to aLogEntry->request
526        if (aLogEntry->request) {
527            packerClean(&p);
528            mb.reset();
529            packerToMemInit(&p, &mb);
530            aLogEntry->request->header.packInto(&p);
531            aLogEntry->headers.request = xstrdup(mb.buf);
532        }
533
534#if USE_ADAPTATION
535        const Adaptation::History::Pointer ah = request->adaptLogHistory();
536        if (ah != NULL) {
537            packerClean(&p);
538            mb.reset();
539            packerToMemInit(&p, &mb);
540            ah->lastMeta.packInto(&p);
541            aLogEntry->adapt.last_meta = xstrdup(mb.buf);
542        }
543#endif
544
545        packerClean(&p);
546        mb.clean();
547    }
548
549#if ICAP_CLIENT
550    const Adaptation::Icap::History::Pointer ih = request->icapHistory();
551    if (ih != NULL)
552        aLogEntry->icap.processingTime = ih->processingTime();
553#endif
554
555    aLogEntry->http.method = request->method;
556    aLogEntry->http.version = request->http_ver;
557    aLogEntry->hier = request->hier;
558    if (request->content_length > 0) // negative when no body or unknown length
559        aLogEntry->http.clientRequestSz.payloadData += request->content_length; // XXX: actually adaptedRequest payload size ??
560    aLogEntry->cache.extuser = request->extacl_user.termedBuf();
561
562    // Adapted request, if any, inherits and then collects all the stats, but
563    // the virgin request gets logged instead; copy the stats to log them.
564    // TODO: avoid losses by keeping these stats in a shared history object?
565    if (aLogEntry->request) {
566        aLogEntry->request->dnsWait = request->dnsWait;
567        aLogEntry->request->errType = request->errType;
568        aLogEntry->request->errDetail = request->errDetail;
569    }
570}
571
572void
573ClientHttpRequest::logRequest()
574{
575    if (!out.size && !logType)
576        debugs(33, 5, HERE << "logging half-baked transaction: " << log_uri);
577
578    al->icp.opcode = ICP_INVALID;
579    al->url = log_uri;
580    debugs(33, 9, "clientLogRequest: al.url='" << al->url << "'");
581
582    if (al->reply) {
583        al->http.code = al->reply->sline.status();
584        al->http.content_type = al->reply->content_type.termedBuf();
585    } else if (loggingEntry() && loggingEntry()->mem_obj) {
586        al->http.code = loggingEntry()->mem_obj->getReply()->sline.status();
587        al->http.content_type = loggingEntry()->mem_obj->getReply()->content_type.termedBuf();
588    }
589
590    debugs(33, 9, "clientLogRequest: http.code='" << al->http.code << "'");
591
592    if (loggingEntry() && loggingEntry()->mem_obj && loggingEntry()->objectLen() >= 0)
593        al->cache.objectSize = loggingEntry()->contentLen(); // payload duplicate ?? with or without TE ?
594
595    al->http.clientRequestSz.header = req_sz;
596    al->http.clientReplySz.header = out.headers_sz;
597    // XXX: calculate without payload encoding or headers !!
598    al->http.clientReplySz.payloadData = out.size - out.headers_sz; // pretend its all un-encoded data for now.
599
600    al->cache.highOffset = out.offset;
601
602    al->cache.code = logType;
603
604    al->cache.msec = tvSubMsec(al->cache.start_time, current_time);
605
606    if (request)
607        prepareLogWithRequestDetails(request, al);
608
609    if (getConn() != NULL && getConn()->clientConnection != NULL && getConn()->clientConnection->rfc931[0])
610        al->cache.rfc931 = getConn()->clientConnection->rfc931;
611
612#if USE_OPENSSL && 0
613
614    /* This is broken. Fails if the connection has been closed. Needs
615     * to snarf the ssl details some place earlier..
616     */
617    if (getConn() != NULL)
618        al->cache.ssluser = sslGetUserEmail(fd_table[getConn()->fd].ssl);
619
620#endif
621
622    /* Add notes (if we have a request to annotate) */
623    if (request) {
624        // The al->notes and request->notes must point to the same object.
625        (void)SyncNotes(*al, *request);
626        for (Notes::iterator i = Config.notes.begin(); i != Config.notes.end(); ++i) {
627            if (const char *value = (*i)->match(request, al->reply, NULL)) {
628                NotePairs &notes = SyncNotes(*al, *request);
629                notes.add((*i)->key.termedBuf(), value);
630                debugs(33, 3, (*i)->key.termedBuf() << " " << value);
631            }
632        }
633    }
634
635    ACLFilledChecklist checklist(NULL, request, NULL);
636    if (al->reply) {
637        checklist.reply = al->reply;
638        HTTPMSGLOCK(checklist.reply);
639    }
640
641    if (request) {
642        al->adapted_request = request;
643        HTTPMSGLOCK(al->adapted_request);
644    }
645    accessLogLog(al, &checklist);
646
647    bool updatePerformanceCounters = true;
648    if (Config.accessList.stats_collection) {
649        ACLFilledChecklist statsCheck(Config.accessList.stats_collection, request, NULL);
650        if (al->reply) {
651            statsCheck.reply = al->reply;
652            HTTPMSGLOCK(statsCheck.reply);
653        }
654        updatePerformanceCounters = (statsCheck.fastCheck() == ACCESS_ALLOWED);
655    }
656
657    if (updatePerformanceCounters) {
658        if (request)
659            updateCounters();
660
661        if (getConn() != NULL && getConn()->clientConnection != NULL)
662            clientdbUpdate(getConn()->clientConnection->remote, logType, AnyP::PROTO_HTTP, out.size);
663    }
664}
665
666void
667ClientHttpRequest::freeResources()
668{
669    safe_free(uri);
670    safe_free(log_uri);
671    safe_free(redirect.location);
672    range_iter.boundary.clean();
673    HTTPMSGUNLOCK(request);
674
675    if (client_stream.tail)
676        clientStreamAbort((clientStreamNode *)client_stream.tail->data, this);
677}
678
679void
680httpRequestFree(void *data)
681{
682    ClientHttpRequest *http = (ClientHttpRequest *)data;
683    assert(http != NULL);
684    delete http;
685}
686
687bool
688ConnStateData::areAllContextsForThisConnection() const
689{
690    ClientSocketContext::Pointer context = getCurrentContext();
691
692    while (context.getRaw()) {
693        if (context->http->getConn() != this)
694            return false;
695
696        context = context->next;
697    }
698
699    return true;
700}
701
702void
703ConnStateData::freeAllContexts()
704{
705    ClientSocketContext::Pointer context;
706
707    while ((context = getCurrentContext()).getRaw() != NULL) {
708        assert(getCurrentContext() !=
709               getCurrentContext()->next);
710        context->connIsFinished();
711        assert (context != currentobject);
712    }
713}
714
715/// propagates abort event to all contexts
716void
717ConnStateData::notifyAllContexts(int xerrno)
718{
719    typedef ClientSocketContext::Pointer CSCP;
720    for (CSCP c = getCurrentContext(); c.getRaw(); c = c->next)
721        c->noteIoError(xerrno);
722}
723
724/* This is a handler normally called by comm_close() */
725void ConnStateData::connStateClosed(const CommCloseCbParams &io)
726{
727    deleteThis("ConnStateData::connStateClosed");
728}
729
730#if USE_AUTH
731void
732ConnStateData::setAuth(const Auth::UserRequest::Pointer &aur, const char *by)
733{
734    if (auth_ == NULL) {
735        if (aur != NULL) {
736            debugs(33, 2, "Adding connection-auth to " << clientConnection << " from " << by);
737            auth_ = aur;
738        }
739        return;
740    }
741
742    // clobered with self-pointer
743    // NP: something nasty is going on in Squid, but harmless.
744    if (aur == auth_) {
745        debugs(33, 2, "WARNING: Ignoring duplicate connection-auth for " << clientConnection << " from " << by);
746        return;
747    }
748
749    /*
750     * Connection-auth relies on a single set of credentials being preserved
751     * for all requests on a connection once they have been setup.
752     * There are several things which need to happen to preserve security
753     * when connection-auth credentials change unexpectedly or are unset.
754     *
755     * 1) auth helper released from any active state
756     *
757     * They can only be reserved by a handshake process which this
758     * connection can now never complete.
759     * This prevents helpers hanging when their connections close.
760     *
761     * 2) pinning is expected to be removed and server conn closed
762     *
763     * The upstream link is authenticated with the same credentials.
764     * Expecting the same level of consistency we should have received.
765     * This prevents upstream being faced with multiple or missing
766     * credentials after authentication.
767     * NP: un-pin is left to the cleanup in ConnStateData::swanSong()
768     *     we just trigger that cleanup here via comm_reset_close() or
769     *     ConnStateData::stopReceiving()
770     *
771     * 3) the connection needs to close.
772     *
773     * This prevents attackers injecting requests into a connection,
774     * or gateways wrongly multiplexing users into a single connection.
775     *
776     * When credentials are missing closure needs to follow an auth
777     * challenge for best recovery by the client.
778     *
779     * When credentials change there is nothing we can do but abort as
780     * fast as possible. Sending TCP RST instead of an HTTP response
781     * is the best-case action.
782     */
783
784    // clobbered with nul-pointer
785    if (aur == NULL) {
786        debugs(33, 2, "WARNING: Graceful closure on " << clientConnection << " due to connection-auth erase from " << by);
787        auth_->releaseAuthServer();
788        auth_ = NULL;
789        // XXX: need to test whether the connection re-auth challenge is sent. If not, how to trigger it from here.
790        // NP: the current situation seems to fix challenge loops in Safari without visible issues in others.
791        // we stop receiving more traffic but can leave the Job running to terminate after the error or challenge is delivered.
792        stopReceiving("connection-auth removed");
793        return;
794    }
795
796    // clobbered with alternative credentials
797    if (aur != auth_) {
798        debugs(33, 2, "ERROR: Closing " << clientConnection << " due to change of connection-auth from " << by);
799        auth_->releaseAuthServer();
800        auth_ = NULL;
801        // this is a fatal type of problem.
802        // Close the connection immediately with TCP RST to abort all traffic flow
803        comm_reset_close(clientConnection);
804        return;
805    }
806
807    /* NOT REACHABLE */
808}
809#endif
810
811// cleans up before destructor is called
812void
813ConnStateData::swanSong()
814{
815    debugs(33, 2, HERE << clientConnection);
816    flags.readMore = false;
817    DeregisterRunner(this);
818    clientdbEstablished(clientConnection->remote, -1);  /* decrement */
819    assert(areAllContextsForThisConnection());
820    freeAllContexts();
821
822    unpinConnection(true);
823
824    if (Comm::IsConnOpen(clientConnection))
825        clientConnection->close();
826
827#if USE_AUTH
828    // NP: do this bit after closing the connections to avoid side effects from unwanted TCP RST
829    setAuth(NULL, "ConnStateData::SwanSong cleanup");
830#endif
831
832    BodyProducer::swanSong();
833    flags.swanSang = true;
834}
835
836bool
837ConnStateData::isOpen() const
838{
839    return cbdataReferenceValid(this) && // XXX: checking "this" in a method
840           Comm::IsConnOpen(clientConnection) &&
841           !fd_table[clientConnection->fd].closing();
842}
843
844ConnStateData::~ConnStateData()
845{
846    debugs(33, 3, HERE << clientConnection);
847
848    if (isOpen())
849        debugs(33, DBG_IMPORTANT, "BUG: ConnStateData did not close " << clientConnection);
850
851    if (!flags.swanSang)
852        debugs(33, DBG_IMPORTANT, "BUG: ConnStateData was not destroyed properly; " << clientConnection);
853
854    if (bodyPipe != NULL)
855        stopProducingFor(bodyPipe, false);
856
857#if USE_OPENSSL
858    delete sslServerBump;
859#endif
860}
861
862/**
863 * clientSetKeepaliveFlag() sets request->flags.proxyKeepalive.
864 * This is the client-side persistent connection flag.  We need
865 * to set this relatively early in the request processing
866 * to handle hacks for broken servers and clients.
867 */
868void
869clientSetKeepaliveFlag(ClientHttpRequest * http)
870{
871    HttpRequest *request = http->request;
872
873    debugs(33, 3, "http_ver = " << request->http_ver);
874    debugs(33, 3, "method = " << request->method);
875
876    // TODO: move to HttpRequest::hdrCacheInit, just like HttpReply.
877    request->flags.proxyKeepalive = request->persistent();
878}
879
880/// checks body length of non-chunked requests
881static int
882clientIsContentLengthValid(HttpRequest * r)
883{
884    // No Content-Length means this request just has no body, but conflicting
885    // Content-Lengths mean a message framing error (RFC 7230 Section 3.3.3 #4).
886    if (r->header.conflictingContentLength())
887        return 0;
888
889    switch (r->method.id()) {
890
891    case Http::METHOD_GET:
892
893    case Http::METHOD_HEAD:
894        /* We do not want to see a request entity on GET/HEAD requests */
895        return (r->content_length <= 0 || Config.onoff.request_entities);
896
897    default:
898        /* For other types of requests we don't care */
899        return 1;
900    }
901
902    /* NOT REACHED */
903}
904
905int
906clientIsRequestBodyTooLargeForPolicy(int64_t bodyLength)
907{
908    if (Config.maxRequestBodySize &&
909            bodyLength > Config.maxRequestBodySize)
910        return 1;       /* too large */
911
912    return 0;
913}
914
915// careful: the "current" context may be gone if we wrote an early response
916ClientSocketContext::Pointer
917ConnStateData::getCurrentContext() const
918{
919    return currentobject;
920}
921
922void
923ClientSocketContext::deferRecipientForLater(clientStreamNode * node, HttpReply * rep, StoreIOBuffer receivedData)
924{
925    debugs(33, 2, "clientSocketRecipient: Deferring request " << http->uri);
926    assert(flags.deferred == 0);
927    flags.deferred = 1;
928    deferredparams.node = node;
929    deferredparams.rep = rep;
930    deferredparams.queuedBuffer = receivedData;
931    return;
932}
933
934bool
935ClientSocketContext::startOfOutput() const
936{
937    return http->out.size == 0;
938}
939
940size_t
941ClientSocketContext::lengthToSend(Range<int64_t> const &available)
942{
943    /*the size of available range can always fit in a size_t type*/
944    size_t maximum = (size_t)available.size();
945
946    if (!http->request->range)
947        return maximum;
948
949    assert (canPackMoreRanges());
950
951    if (http->range_iter.debt() == -1)
952        return maximum;
953
954    assert (http->range_iter.debt() > 0);
955
956    /* TODO this + the last line could be a range intersection calculation */
957    if (available.start < http->range_iter.currentSpec()->offset)
958        return 0;
959
960    return min(http->range_iter.debt(), (int64_t)maximum);
961}
962
963void
964ClientSocketContext::noteSentBodyBytes(size_t bytes)
965{
966    debugs(33, 7, bytes << " body bytes");
967
968    http->out.offset += bytes;
969
970    if (!http->request->range)
971        return;
972
973    if (http->range_iter.debt() != -1) {
974        http->range_iter.debt(http->range_iter.debt() - bytes);
975        assert (http->range_iter.debt() >= 0);
976    }
977
978    /* debt() always stops at -1, below that is a bug */
979    assert (http->range_iter.debt() >= -1);
980}
981
982bool
983ClientHttpRequest::multipartRangeRequest() const
984{
985    return request->multipartRangeRequest();
986}
987
988bool
989ClientSocketContext::multipartRangeRequest() const
990{
991    return http->multipartRangeRequest();
992}
993
994void
995ClientSocketContext::sendBody(HttpReply * rep, StoreIOBuffer bodyData)
996{
997    assert(rep == NULL);
998
999    if (!multipartRangeRequest() && !http->request->flags.chunkedReply) {
1000        size_t length = lengthToSend(bodyData.range());
1001        noteSentBodyBytes (length);
1002        AsyncCall::Pointer call = commCbCall(33, 5, "clientWriteBodyComplete",
1003                                             CommIoCbPtrFun(clientWriteBodyComplete, this));
1004        Comm::Write(clientConnection, bodyData.data, length, call, NULL);
1005        return;
1006    }
1007
1008    MemBuf mb;
1009    mb.init();
1010    if (multipartRangeRequest())
1011        packRange(bodyData, &mb);
1012    else
1013        packChunk(bodyData, mb);
1014
1015    if (mb.contentSize()) {
1016        /* write */
1017        AsyncCall::Pointer call = commCbCall(33, 5, "clientWriteComplete",
1018                                             CommIoCbPtrFun(clientWriteComplete, this));
1019        Comm::Write(clientConnection, &mb, call);
1020    }  else
1021        writeComplete(clientConnection, NULL, 0, Comm::OK);
1022}
1023
1024/**
1025 * Packs bodyData into mb using chunked encoding. Packs the last-chunk
1026 * if bodyData is empty.
1027 */
1028void
1029ClientSocketContext::packChunk(const StoreIOBuffer &bodyData, MemBuf &mb)
1030{
1031    const uint64_t length =
1032        static_cast<uint64_t>(lengthToSend(bodyData.range()));
1033    noteSentBodyBytes(length);
1034
1035    mb.Printf("%" PRIX64 "\r\n", length);
1036    mb.append(bodyData.data, length);
1037    mb.Printf("\r\n");
1038}
1039
1040/** put terminating boundary for multiparts */
1041static void
1042clientPackTermBound(String boundary, MemBuf * mb)
1043{
1044    mb->Printf("\r\n--" SQUIDSTRINGPH "--\r\n", SQUIDSTRINGPRINT(boundary));
1045    debugs(33, 6, "clientPackTermBound: buf offset: " << mb->size);
1046}
1047
1048/** appends a "part" HTTP header (as in a multi-part/range reply) to the buffer */
1049static void
1050clientPackRangeHdr(const HttpReply * rep, const HttpHdrRangeSpec * spec, String boundary, MemBuf * mb)
1051{
1052    HttpHeader hdr(hoReply);
1053    Packer p;
1054    assert(rep);
1055    assert(spec);
1056
1057    /* put boundary */
1058    debugs(33, 5, "clientPackRangeHdr: appending boundary: " << boundary);
1059    /* rfc2046 requires to _prepend_ boundary with <crlf>! */
1060    mb->Printf("\r\n--" SQUIDSTRINGPH "\r\n", SQUIDSTRINGPRINT(boundary));
1061
1062    /* stuff the header with required entries and pack it */
1063
1064    if (rep->header.has(HDR_CONTENT_TYPE))
1065        hdr.putStr(HDR_CONTENT_TYPE, rep->header.getStr(HDR_CONTENT_TYPE));
1066
1067    httpHeaderAddContRange(&hdr, *spec, rep->content_length);
1068
1069    packerToMemInit(&p, mb);
1070
1071    hdr.packInto(&p);
1072
1073    packerClean(&p);
1074
1075    hdr.clean();
1076
1077    /* append <crlf> (we packed a header, not a reply) */
1078    mb->Printf("\r\n");
1079}
1080
1081/**
1082 * extracts a "range" from *buf and appends them to mb, updating
1083 * all offsets and such.
1084 */
1085void
1086ClientSocketContext::packRange(StoreIOBuffer const &source, MemBuf * mb)
1087{
1088    HttpHdrRangeIter * i = &http->range_iter;
1089    Range<int64_t> available (source.range());
1090    char const *buf = source.data;
1091
1092    while (i->currentSpec() && available.size()) {
1093        const size_t copy_sz = lengthToSend(available);
1094
1095        if (copy_sz) {
1096            /*
1097             * intersection of "have" and "need" ranges must not be empty
1098             */
1099            assert(http->out.offset < i->currentSpec()->offset + i->currentSpec()->length);
1100            assert(http->out.offset + (int64_t)available.size() > i->currentSpec()->offset);
1101
1102            /*
1103             * put boundary and headers at the beginning of a range in a
1104             * multi-range
1105             */
1106
1107            if (http->multipartRangeRequest() && i->debt() == i->currentSpec()->length) {
1108                assert(http->memObject());
1109                clientPackRangeHdr(
1110                    http->memObject()->getReply(),  /* original reply */
1111                    i->currentSpec(),       /* current range */
1112                    i->boundary,    /* boundary, the same for all */
1113                    mb);
1114            }
1115
1116            /*
1117             * append content
1118             */
1119            debugs(33, 3, "clientPackRange: appending " << copy_sz << " bytes");
1120
1121            noteSentBodyBytes (copy_sz);
1122
1123            mb->append(buf, copy_sz);
1124
1125            /*
1126             * update offsets
1127             */
1128            available.start += copy_sz;
1129
1130            buf += copy_sz;
1131
1132        }
1133
1134        if (!canPackMoreRanges()) {
1135            debugs(33, 3, "clientPackRange: Returning because !canPackMoreRanges.");
1136
1137            if (i->debt() == 0)
1138                /* put terminating boundary for multiparts */
1139                clientPackTermBound(i->boundary, mb);
1140
1141            return;
1142        }
1143
1144        int64_t nextOffset = getNextRangeOffset();
1145
1146        assert (nextOffset >= http->out.offset);
1147
1148        int64_t skip = nextOffset - http->out.offset;
1149
1150        /* adjust for not to be transmitted bytes */
1151        http->out.offset = nextOffset;
1152
1153        if (available.size() <= (uint64_t)skip)
1154            return;
1155
1156        available.start += skip;
1157
1158        buf += skip;
1159
1160        if (copy_sz == 0)
1161            return;
1162    }
1163}
1164
1165/** returns expected content length for multi-range replies
1166 * note: assumes that httpHdrRangeCanonize has already been called
1167 * warning: assumes that HTTP headers for individual ranges at the
1168 *          time of the actuall assembly will be exactly the same as
1169 *          the headers when clientMRangeCLen() is called */
1170int
1171ClientHttpRequest::mRangeCLen()
1172{
1173    int64_t clen = 0;
1174    MemBuf mb;
1175
1176    assert(memObject());
1177
1178    mb.init();
1179    HttpHdrRange::iterator pos = request->range->begin();
1180
1181    while (pos != request->range->end()) {
1182        /* account for headers for this range */
1183        mb.reset();
1184        clientPackRangeHdr(memObject()->getReply(),
1185                           *pos, range_iter.boundary, &mb);
1186        clen += mb.size;
1187
1188        /* account for range content */
1189        clen += (*pos)->length;
1190
1191        debugs(33, 6, "clientMRangeCLen: (clen += " << mb.size << " + " << (*pos)->length << ") == " << clen);
1192        ++pos;
1193    }
1194
1195    /* account for the terminating boundary */
1196    mb.reset();
1197
1198    clientPackTermBound(range_iter.boundary, &mb);
1199
1200    clen += mb.size;
1201
1202    mb.clean();
1203
1204    return clen;
1205}
1206
1207/**
1208 * returns true if If-Range specs match reply, false otherwise
1209 */
1210static int
1211clientIfRangeMatch(ClientHttpRequest * http, HttpReply * rep)
1212{
1213    const TimeOrTag spec = http->request->header.getTimeOrTag(HDR_IF_RANGE);
1214    /* check for parsing falure */
1215
1216    if (!spec.valid)
1217        return 0;
1218
1219    /* got an ETag? */
1220    if (spec.tag.str) {
1221        ETag rep_tag = rep->header.getETag(HDR_ETAG);
1222        debugs(33, 3, "clientIfRangeMatch: ETags: " << spec.tag.str << " and " <<
1223               (rep_tag.str ? rep_tag.str : "<none>"));
1224
1225        if (!rep_tag.str)
1226            return 0;       /* entity has no etag to compare with! */
1227
1228        if (spec.tag.weak || rep_tag.weak) {
1229            debugs(33, DBG_IMPORTANT, "clientIfRangeMatch: Weak ETags are not allowed in If-Range: " << spec.tag.str << " ? " << rep_tag.str);
1230            return 0;       /* must use strong validator for sub-range requests */
1231        }
1232
1233        return etagIsStrongEqual(rep_tag, spec.tag);
1234    }
1235
1236    /* got modification time? */
1237    if (spec.time >= 0) {
1238        return http->storeEntry()->lastmod <= spec.time;
1239    }
1240
1241    assert(0);          /* should not happen */
1242    return 0;
1243}
1244
1245/**
1246 * generates a "unique" boundary string for multipart responses
1247 * the caller is responsible for cleaning the string */
1248String
1249ClientHttpRequest::rangeBoundaryStr() const
1250{
1251    const char *key;
1252    String b(APP_FULLNAME);
1253    b.append(":",1);
1254    key = storeEntry()->getMD5Text();
1255    b.append(key, strlen(key));
1256    return b;
1257}
1258
1259/** adds appropriate Range headers if needed */
1260void
1261ClientSocketContext::buildRangeHeader(HttpReply * rep)
1262{
1263    HttpHeader *hdr = rep ? &rep->header : 0;
1264    const char *range_err = NULL;
1265    HttpRequest *request = http->request;
1266    assert(request->range);
1267    /* check if we still want to do ranges */
1268
1269    int64_t roffLimit = request->getRangeOffsetLimit();
1270
1271    if (!rep)
1272        range_err = "no [parse-able] reply";
1273    else if ((rep->sline.status() != Http::scOkay) && (rep->sline.status() != Http::scPartialContent))
1274        range_err = "wrong status code";
1275    else if (hdr->has(HDR_CONTENT_RANGE))
1276        range_err = "origin server does ranges";
1277    else if (rep->content_length < 0)
1278        range_err = "unknown length";
1279    else if (rep->content_length != http->memObject()->getReply()->content_length)
1280        range_err = "INCONSISTENT length";  /* a bug? */
1281
1282    /* hits only - upstream CachePeer determines correct behaviour on misses, and client_side_reply determines
1283     * hits candidates
1284     */
1285    else if (logTypeIsATcpHit(http->logType) && http->request->header.has(HDR_IF_RANGE) && !clientIfRangeMatch(http, rep))
1286        range_err = "If-Range match failed";
1287    else if (!http->request->range->canonize(rep))
1288        range_err = "canonization failed";
1289    else if (http->request->range->isComplex())
1290        range_err = "too complex range header";
1291    else if (!logTypeIsATcpHit(http->logType) && http->request->range->offsetLimitExceeded(roffLimit))
1292        range_err = "range outside range_offset_limit";
1293
1294    /* get rid of our range specs on error */
1295    if (range_err) {
1296        /* XXX We do this here because we need canonisation etc. However, this current
1297         * code will lead to incorrect store offset requests - the store will have the
1298         * offset data, but we won't be requesting it.
1299         * So, we can either re-request, or generate an error
1300         */
1301        http->request->ignoreRange(range_err);
1302    } else {
1303        /* XXX: TODO: Review, this unconditional set may be wrong. */
1304        rep->sline.set(rep->sline.version, Http::scPartialContent);
1305        // web server responded with a valid, but unexpected range.
1306        // will (try-to) forward as-is.
1307        //TODO: we should cope with multirange request/responses
1308        bool replyMatchRequest = rep->content_range != NULL ?
1309                                 request->range->contains(rep->content_range->spec) :
1310                                 true;
1311        const int spec_count = http->request->range->specs.size();
1312        int64_t actual_clen = -1;
1313
1314        debugs(33, 3, "clientBuildRangeHeader: range spec count: " <<
1315               spec_count << " virgin clen: " << rep->content_length);
1316        assert(spec_count > 0);
1317        /* append appropriate header(s) */
1318
1319        if (spec_count == 1) {
1320            if (!replyMatchRequest) {
1321                hdr->delById(HDR_CONTENT_RANGE);
1322                hdr->putContRange(rep->content_range);
1323                actual_clen = rep->content_length;
1324                //http->range_iter.pos = rep->content_range->spec.begin();
1325                (*http->range_iter.pos)->offset = rep->content_range->spec.offset;
1326                (*http->range_iter.pos)->length = rep->content_range->spec.length;
1327
1328            } else {
1329                HttpHdrRange::iterator pos = http->request->range->begin();
1330                assert(*pos);
1331                /* append Content-Range */
1332
1333                if (!hdr->has(HDR_CONTENT_RANGE)) {
1334                    /* No content range, so this was a full object we are
1335                     * sending parts of.
1336                     */
1337                    httpHeaderAddContRange(hdr, **pos, rep->content_length);
1338                }
1339
1340                /* set new Content-Length to the actual number of bytes
1341                 * transmitted in the message-body */
1342                actual_clen = (*pos)->length;
1343            }
1344        } else {
1345            /* multipart! */
1346            /* generate boundary string */
1347            http->range_iter.boundary = http->rangeBoundaryStr();
1348            /* delete old Content-Type, add ours */
1349            hdr->delById(HDR_CONTENT_TYPE);
1350            httpHeaderPutStrf(hdr, HDR_CONTENT_TYPE,
1351                              "multipart/byteranges; boundary=\"" SQUIDSTRINGPH "\"",
1352                              SQUIDSTRINGPRINT(http->range_iter.boundary));
1353            /* Content-Length is not required in multipart responses
1354             * but it is always nice to have one */
1355            actual_clen = http->mRangeCLen();
1356            /* http->out needs to start where we want data at */
1357            http->out.offset = http->range_iter.currentSpec()->offset;
1358        }
1359
1360        /* replace Content-Length header */
1361        assert(actual_clen >= 0);
1362
1363        hdr->delById(HDR_CONTENT_LENGTH);
1364
1365        hdr->putInt64(HDR_CONTENT_LENGTH, actual_clen);
1366
1367        debugs(33, 3, "clientBuildRangeHeader: actual content length: " << actual_clen);
1368
1369        /* And start the range iter off */
1370        http->range_iter.updateSpec();
1371    }
1372}
1373
1374void
1375ClientSocketContext::prepareReply(HttpReply * rep)
1376{
1377    reply = rep;
1378
1379    if (http->request->range)
1380        buildRangeHeader(rep);
1381}
1382
1383void
1384ClientSocketContext::sendStartOfMessage(HttpReply * rep, StoreIOBuffer bodyData)
1385{
1386    prepareReply(rep);
1387    assert (rep);
1388    MemBuf *mb = rep->pack();
1389
1390    // dump now, so we dont output any body.
1391    debugs(11, 2, "HTTP Client " << clientConnection);
1392    debugs(11, 2, "HTTP Client REPLY:\n---------\n" << mb->buf << "\n----------");
1393
1394    /* Save length of headers for persistent conn checks */
1395    http->out.headers_sz = mb->contentSize();
1396#if HEADERS_LOG
1397
1398    headersLog(0, 0, http->request->method, rep);
1399#endif
1400
1401    if (bodyData.data && bodyData.length) {
1402        if (multipartRangeRequest())
1403            packRange(bodyData, mb);
1404        else if (http->request->flags.chunkedReply) {
1405            packChunk(bodyData, *mb);
1406        } else {
1407            size_t length = lengthToSend(bodyData.range());
1408            noteSentBodyBytes (length);
1409
1410            mb->append(bodyData.data, length);
1411        }
1412    }
1413
1414    /* write */
1415    debugs(33,7, HERE << "sendStartOfMessage schedules clientWriteComplete");
1416    AsyncCall::Pointer call = commCbCall(33, 5, "clientWriteComplete",
1417                                         CommIoCbPtrFun(clientWriteComplete, this));
1418    Comm::Write(clientConnection, mb, call);
1419    delete mb;
1420}
1421
1422/**
1423 * Write a chunk of data to a client socket. If the reply is present,
1424 * send the reply headers down the wire too, and clean them up when
1425 * finished.
1426 * Pre-condition:
1427 *   The request is one backed by a connection, not an internal request.
1428 *   data context is not NULL
1429 *   There are no more entries in the stream chain.
1430 */
1431void
1432clientSocketRecipient(clientStreamNode * node, ClientHttpRequest * http,
1433                      HttpReply * rep, StoreIOBuffer receivedData)
1434{
1435    // dont tryt to deliver if client already ABORTED
1436    if (!http->getConn() || !cbdataReferenceValid(http->getConn()) || !Comm::IsConnOpen(http->getConn()->clientConnection))
1437        return;
1438
1439    /* Test preconditions */
1440    assert(node != NULL);
1441    PROF_start(clientSocketRecipient);
1442    /* TODO: handle this rather than asserting
1443     * - it should only ever happen if we cause an abort and
1444     * the callback chain loops back to here, so we can simply return.
1445     * However, that itself shouldn't happen, so it stays as an assert for now.
1446     */
1447    assert(cbdataReferenceValid(node));
1448    assert(node->node.next == NULL);
1449    ClientSocketContext::Pointer context = dynamic_cast<ClientSocketContext *>(node->data.getRaw());
1450    assert(context != NULL);
1451
1452    /* TODO: check offset is what we asked for */
1453
1454    if (context != http->getConn()->getCurrentContext())
1455        context->deferRecipientForLater(node, rep, receivedData);
1456    else
1457        http->getConn()->handleReply(rep, receivedData);
1458
1459    PROF_stop(clientSocketRecipient);
1460}
1461
1462/**
1463 * Called when a downstream node is no longer interested in
1464 * our data. As we are a terminal node, this means on aborts
1465 * only
1466 */
1467void
1468clientSocketDetach(clientStreamNode * node, ClientHttpRequest * http)
1469{
1470    /* Test preconditions */
1471    assert(node != NULL);
1472    /* TODO: handle this rather than asserting
1473     * - it should only ever happen if we cause an abort and
1474     * the callback chain loops back to here, so we can simply return.
1475     * However, that itself shouldn't happen, so it stays as an assert for now.
1476     */
1477    assert(cbdataReferenceValid(node));
1478    /* Set null by ContextFree */
1479    assert(node->node.next == NULL);
1480    /* this is the assert discussed above */
1481    assert(NULL == dynamic_cast<ClientSocketContext *>(node->data.getRaw()));
1482    /* We are only called when the client socket shutsdown.
1483     * Tell the prev pipeline member we're finished
1484     */
1485    clientStreamDetach(node, http);
1486}
1487
1488static void
1489clientWriteBodyComplete(const Comm::ConnectionPointer &conn, char *buf, size_t size, Comm::Flag errflag, int xerrno, void *data)
1490{
1491    debugs(33,7, HERE << "clientWriteBodyComplete schedules clientWriteComplete");
1492    clientWriteComplete(conn, NULL, size, errflag, xerrno, data);
1493}
1494
1495void
1496ConnStateData::readNextRequest()
1497{
1498    debugs(33, 5, HERE << clientConnection << " reading next req");
1499
1500    fd_note(clientConnection->fd, "Idle client: Waiting for next request");
1501    /**
1502     * Set the timeout BEFORE calling readSomeData().
1503     */
1504    typedef CommCbMemFunT<ConnStateData, CommTimeoutCbParams> TimeoutDialer;
1505    AsyncCall::Pointer timeoutCall = JobCallback(33, 5,
1506                                     TimeoutDialer, this, ConnStateData::requestTimeout);
1507    commSetConnTimeout(clientConnection, idleTimeout(), timeoutCall);
1508
1509    readSomeData();
1510    /** Please don't do anything with the FD past here! */
1511}
1512
1513static void
1514ClientSocketContextPushDeferredIfNeeded(ClientSocketContext::Pointer deferredRequest, ConnStateData * conn)
1515{
1516    debugs(33, 2, HERE << conn->clientConnection << " Sending next");
1517
1518    /** If the client stream is waiting on a socket write to occur, then */
1519
1520    if (deferredRequest->flags.deferred) {
1521        /** NO data is allowed to have been sent. */
1522        assert(deferredRequest->http->out.size == 0);
1523        /** defer now. */
1524        clientSocketRecipient(deferredRequest->deferredparams.node,
1525                              deferredRequest->http,
1526                              deferredRequest->deferredparams.rep,
1527                              deferredRequest->deferredparams.queuedBuffer);
1528    }
1529
1530    /** otherwise, the request is still active in a callbacksomewhere,
1531     * and we are done
1532     */
1533}
1534
1535/// called when we have successfully finished writing the response
1536void
1537ClientSocketContext::keepaliveNextRequest()
1538{
1539    ConnStateData * conn = http->getConn();
1540
1541    debugs(33, 3, HERE << "ConnnStateData(" << conn->clientConnection << "), Context(" << clientConnection << ")");
1542    connIsFinished();
1543
1544    if (conn->pinning.pinned && !Comm::IsConnOpen(conn->pinning.serverConnection)) {
1545        debugs(33, 2, HERE << conn->clientConnection << " Connection was pinned but server side gone. Terminating client connection");
1546        conn->clientConnection->close();
1547        return;
1548    }
1549
1550    /** \par
1551     * We are done with the response, and we are either still receiving request
1552     * body (early response!) or have already stopped receiving anything.
1553     *
1554     * If we are still receiving, then clientParseRequest() below will fail.
1555     * (XXX: but then we will call readNextRequest() which may succeed and
1556     * execute a smuggled request as we are not done with the current request).
1557     *
1558     * If we stopped because we got everything, then try the next request.
1559     *
1560     * If we stopped receiving because of an error, then close now to avoid
1561     * getting stuck and to prevent accidental request smuggling.
1562     */
1563
1564    if (const char *reason = conn->stoppedReceiving()) {
1565        debugs(33, 3, HERE << "closing for earlier request error: " << reason);
1566        conn->clientConnection->close();
1567        return;
1568    }
1569
1570    /** \par
1571     * Attempt to parse a request from the request buffer.
1572     * If we've been fed a pipelined request it may already
1573     * be in our read buffer.
1574     *
1575     \par
1576     * This needs to fall through - if we're unlucky and parse the _last_ request
1577     * from our read buffer we may never re-register for another client read.
1578     */
1579
1580    if (conn->clientParseRequests()) {
1581        debugs(33, 3, HERE << conn->clientConnection << ": parsed next request from buffer");
1582    }
1583
1584    /** \par
1585     * Either we need to kick-start another read or, if we have
1586     * a half-closed connection, kill it after the last request.
1587     * This saves waiting for half-closed connections to finished being
1588     * half-closed _AND_ then, sometimes, spending "Timeout" time in
1589     * the keepalive "Waiting for next request" state.
1590     */
1591    if (commIsHalfClosed(conn->clientConnection->fd) && (conn->getConcurrentRequestCount() == 0)) {
1592        debugs(33, 3, "ClientSocketContext::keepaliveNextRequest: half-closed client with no pending requests, closing");
1593        conn->clientConnection->close();
1594        return;
1595    }
1596
1597    ClientSocketContext::Pointer deferredRequest;
1598
1599    /** \par
1600     * At this point we either have a parsed request (which we've
1601     * kicked off the processing for) or not. If we have a deferred
1602     * request (parsed but deferred for pipeling processing reasons)
1603     * then look at processing it. If not, simply kickstart
1604     * another read.
1605     */
1606
1607    if ((deferredRequest = conn->getCurrentContext()).getRaw()) {
1608        debugs(33, 3, HERE << conn->clientConnection << ": calling PushDeferredIfNeeded");
1609        ClientSocketContextPushDeferredIfNeeded(deferredRequest, conn);
1610    } else if (conn->flags.readMore) {
1611        debugs(33, 3, HERE << conn->clientConnection << ": calling conn->readNextRequest()");
1612        conn->readNextRequest();
1613    } else {
1614        // XXX: Can this happen? CONNECT tunnels have deferredRequest set.
1615        debugs(33, DBG_IMPORTANT, HERE << "abandoning " << conn->clientConnection);
1616    }
1617}
1618
1619void
1620clientUpdateSocketStats(LogTags logType, size_t size)
1621{
1622    if (size == 0)
1623        return;
1624
1625    kb_incr(&statCounter.client_http.kbytes_out, size);
1626
1627    if (logTypeIsATcpHit(logType))
1628        kb_incr(&statCounter.client_http.hit_kbytes_out, size);
1629}
1630
1631/**
1632 * increments iterator "i"
1633 * used by clientPackMoreRanges
1634 *
1635 \retval true    there is still data available to pack more ranges
1636 \retval false
1637 */
1638bool
1639ClientSocketContext::canPackMoreRanges() const
1640{
1641    /** first update iterator "i" if needed */
1642
1643    if (!http->range_iter.debt()) {
1644        debugs(33, 5, HERE << "At end of current range spec for " << clientConnection);
1645
1646        if (http->range_iter.pos != http->range_iter.end)
1647            ++http->range_iter.pos;
1648
1649        http->range_iter.updateSpec();
1650    }
1651
1652    assert(!http->range_iter.debt() == !http->range_iter.currentSpec());
1653
1654    /* paranoid sync condition */
1655    /* continue condition: need_more_data */
1656    debugs(33, 5, "ClientSocketContext::canPackMoreRanges: returning " << (http->range_iter.currentSpec() ? true : false));
1657    return http->range_iter.currentSpec() ? true : false;
1658}
1659
1660int64_t
1661ClientSocketContext::getNextRangeOffset() const
1662{
1663    debugs (33, 5, "range: " << http->request->range <<
1664            "; http offset " << http->out.offset <<
1665            "; reply " << reply);
1666
1667    // XXX: This method is called from many places, including pullData() which
1668    // may be called before prepareReply() [on some Squid-generated errors].
1669    // Hence, we may not even know yet whether we should honor/do ranges.
1670
1671    if (http->request->range) {
1672        /* offset in range specs does not count the prefix of an http msg */
1673        /* check: reply was parsed and range iterator was initialized */
1674        assert(http->range_iter.valid);
1675        /* filter out data according to range specs */
1676        assert (canPackMoreRanges());
1677        {
1678            int64_t start;      /* offset of still missing data */
1679            assert(http->range_iter.currentSpec());
1680            start = http->range_iter.currentSpec()->offset + http->range_iter.currentSpec()->length - http->range_iter.debt();
1681            debugs(33, 3, "clientPackMoreRanges: in:  offset: " << http->out.offset);
1682            debugs(33, 3, "clientPackMoreRanges: out:"
1683                   " start: " << start <<
1684                   " spec[" << http->range_iter.pos - http->request->range->begin() << "]:" <<
1685                   " [" << http->range_iter.currentSpec()->offset <<
1686                   ", " << http->range_iter.currentSpec()->offset + http->range_iter.currentSpec()->length << "),"
1687                   " len: " << http->range_iter.currentSpec()->length <<
1688                   " debt: " << http->range_iter.debt());
1689            if (http->range_iter.currentSpec()->length != -1)
1690                assert(http->out.offset <= start);  /* we did not miss it */
1691
1692            return start;
1693        }
1694
1695    } else if (reply && reply->content_range) {
1696        /* request does not have ranges, but reply does */
1697        /** \todo FIXME: should use range_iter_pos on reply, as soon as reply->content_range
1698         *        becomes HttpHdrRange rather than HttpHdrRangeSpec.
1699         */
1700        return http->out.offset + reply->content_range->spec.offset;
1701    }
1702
1703    return http->out.offset;
1704}
1705
1706void
1707ClientSocketContext::pullData()
1708{
1709    debugs(33, 5, reply << " written " << http->out.size << " into " << clientConnection);
1710
1711    /* More data will be coming from the stream. */
1712    StoreIOBuffer readBuffer;
1713    /* XXX: Next requested byte in the range sequence */
1714    /* XXX: length = getmaximumrangelenfgth */
1715    readBuffer.offset = getNextRangeOffset();
1716    readBuffer.length = HTTP_REQBUF_SZ;
1717    readBuffer.data = reqbuf;
1718    /* we may note we have reached the end of the wanted ranges */
1719    clientStreamRead(getTail(), http, readBuffer);
1720}
1721
1722/** Adapt stream status to account for Range cases
1723 *
1724 */
1725clientStream_status_t
1726ClientSocketContext::socketState()
1727{
1728    switch (clientStreamStatus(getTail(), http)) {
1729
1730    case STREAM_NONE:
1731        /* check for range support ending */
1732
1733        if (http->request->range) {
1734            /* check: reply was parsed and range iterator was initialized */
1735            assert(http->range_iter.valid);
1736            /* filter out data according to range specs */
1737
1738            if (!canPackMoreRanges()) {
1739                debugs(33, 5, HERE << "Range request at end of returnable " <<
1740                       "range sequence on " << clientConnection);
1741                // we got everything we wanted from the store
1742                return STREAM_COMPLETE;
1743            }
1744        } else if (reply && reply->content_range) {
1745            /* reply has content-range, but Squid is not managing ranges */
1746            const int64_t &bytesSent = http->out.offset;
1747            const int64_t &bytesExpected = reply->content_range->spec.length;
1748
1749            debugs(33, 7, HERE << "body bytes sent vs. expected: " <<
1750                   bytesSent << " ? " << bytesExpected << " (+" <<
1751                   reply->content_range->spec.offset << ")");
1752
1753            // did we get at least what we expected, based on range specs?
1754
1755            if (bytesSent == bytesExpected) // got everything
1756                return STREAM_COMPLETE;
1757
1758            if (bytesSent > bytesExpected) // Error: Sent more than expected
1759                return STREAM_UNPLANNED_COMPLETE;
1760        }
1761
1762        return STREAM_NONE;
1763
1764    case STREAM_COMPLETE:
1765        return STREAM_COMPLETE;
1766
1767    case STREAM_UNPLANNED_COMPLETE:
1768        return STREAM_UNPLANNED_COMPLETE;
1769
1770    case STREAM_FAILED:
1771        return STREAM_FAILED;
1772    }
1773
1774    fatal ("unreachable code\n");
1775    return STREAM_NONE;
1776}
1777
1778/**
1779 * A write has just completed to the client, or we have just realised there is
1780 * no more data to send.
1781 */
1782void
1783clientWriteComplete(const Comm::ConnectionPointer &conn, char *bufnotused, size_t size, Comm::Flag errflag, int xerrno, void *data)
1784{
1785    ClientSocketContext *context = (ClientSocketContext *)data;
1786    context->writeComplete(conn, bufnotused, size, errflag);
1787}
1788
1789/// remembers the abnormal connection termination for logging purposes
1790void
1791ClientSocketContext::noteIoError(const int xerrno)
1792{
1793    if (http) {
1794        if (xerrno == ETIMEDOUT)
1795            http->al->http.timedout = true;
1796        else // even if xerrno is zero (which means read abort/eof)
1797            http->al->http.aborted = true;
1798    }
1799}
1800
1801void
1802ClientSocketContext::doClose()
1803{
1804    clientConnection->close();
1805}
1806
1807/// called when we encounter a response-related error
1808void
1809ClientSocketContext::initiateClose(const char *reason)
1810{
1811    http->getConn()->stopSending(reason); // closes ASAP
1812}
1813
1814void
1815ConnStateData::stopSending(const char *error)
1816{
1817    debugs(33, 4, HERE << "sending error (" << clientConnection << "): " << error <<
1818           "; old receiving error: " <<
1819           (stoppedReceiving() ? stoppedReceiving_ : "none"));
1820
1821    if (const char *oldError = stoppedSending()) {
1822        debugs(33, 3, HERE << "already stopped sending: " << oldError);
1823        return; // nothing has changed as far as this connection is concerned
1824    }
1825    stoppedSending_ = error;
1826
1827    if (!stoppedReceiving()) {
1828        if (const int64_t expecting = mayNeedToReadMoreBody()) {
1829            debugs(33, 5, HERE << "must still read " << expecting <<
1830                   " request body bytes with " << in.buf.length() << " unused");
1831            return; // wait for the request receiver to finish reading
1832        }
1833    }
1834
1835    clientConnection->close();
1836}
1837
1838void
1839ClientSocketContext::writeComplete(const Comm::ConnectionPointer &conn, char *bufnotused, size_t size, Comm::Flag errflag)
1840{
1841    const StoreEntry *entry = http->storeEntry();
1842    http->out.size += size;
1843    debugs(33, 5, HERE << conn << ", sz " << size <<
1844           ", err " << errflag << ", off " << http->out.size << ", len " <<
1845           (entry ? entry->objectLen() : 0));
1846    clientUpdateSocketStats(http->logType, size);
1847
1848    /* Bail out quickly on Comm::ERR_CLOSING - close handlers will tidy up */
1849
1850    if (errflag == Comm::ERR_CLOSING || !Comm::IsConnOpen(conn))
1851        return;
1852
1853    if (errflag || clientHttpRequestStatus(conn->fd, http)) {
1854        initiateClose("failure or true request status");
1855        /* Do we leak here ? */
1856        return;
1857    }
1858
1859    switch (socketState()) {
1860
1861    case STREAM_NONE:
1862        pullData();
1863        break;
1864
1865    case STREAM_COMPLETE:
1866        debugs(33, 5, conn << " Stream complete, keepalive is " << http->request->flags.proxyKeepalive);
1867        if (http->request->flags.proxyKeepalive)
1868            keepaliveNextRequest();
1869        else
1870            initiateClose("STREAM_COMPLETE NOKEEPALIVE");
1871        return;
1872
1873    case STREAM_UNPLANNED_COMPLETE:
1874        initiateClose("STREAM_UNPLANNED_COMPLETE");
1875        return;
1876
1877    case STREAM_FAILED:
1878        initiateClose("STREAM_FAILED");
1879        return;
1880
1881    default:
1882        fatal("Hit unreachable code in clientWriteComplete\n");
1883    }
1884}
1885
1886ClientSocketContext *
1887ConnStateData::abortRequestParsing(const char *const uri)
1888{
1889    ClientHttpRequest *http = new ClientHttpRequest(this);
1890    http->req_sz = in.buf.length();
1891    http->uri = xstrdup(uri);
1892    setLogUri (http, uri);
1893    ClientSocketContext *context = new ClientSocketContext(clientConnection, http);
1894    StoreIOBuffer tempBuffer;
1895    tempBuffer.data = context->reqbuf;
1896    tempBuffer.length = HTTP_REQBUF_SZ;
1897    clientStreamInit(&http->client_stream, clientGetMoreData, clientReplyDetach,
1898                     clientReplyStatus, new clientReplyContext(http), clientSocketRecipient,
1899                     clientSocketDetach, context, tempBuffer);
1900    return context;
1901}
1902
1903void
1904ConnStateData::startShutdown()
1905{
1906    // RegisteredRunner API callback - Squid has been shut down
1907
1908    // if connection is idle terminate it now,
1909    // otherwise wait for grace period to end
1910    if (getConcurrentRequestCount() == 0)
1911        endingShutdown();
1912}
1913
1914void
1915ConnStateData::endingShutdown()
1916{
1917    // RegisteredRunner API callback - Squid shutdown grace period is over
1918
1919    // force the client connection to close immediately
1920    // swanSong() in the close handler will cleanup.
1921    if (Comm::IsConnOpen(clientConnection))
1922        clientConnection->close();
1923
1924    // deregister now to ensure finalShutdown() does not kill us prematurely.
1925    // fd_table purge will cleanup if close handler was not fast enough.
1926    DeregisterRunner(this);
1927}
1928
1929char *
1930skipLeadingSpace(char *aString)
1931{
1932    char *result = aString;
1933
1934    while (xisspace(*aString))
1935        ++aString;
1936
1937    return result;
1938}
1939
1940/**
1941 * 'end' defaults to NULL for backwards compatibility
1942 * remove default value if we ever get rid of NULL-terminated
1943 * request buffers.
1944 */
1945const char *
1946findTrailingHTTPVersion(const char *uriAndHTTPVersion, const char *end)
1947{
1948    if (NULL == end) {
1949        end = uriAndHTTPVersion + strcspn(uriAndHTTPVersion, "\r\n");
1950        assert(end);
1951    }
1952
1953    for (; end > uriAndHTTPVersion; --end) {
1954        if (*end == '\n' || *end == '\r')
1955            continue;
1956
1957        if (xisspace(*end)) {
1958            if (strncasecmp(end + 1, "HTTP/", 5) == 0)
1959                return end + 1;
1960            else
1961                break;
1962        }
1963    }
1964
1965    return NULL;
1966}
1967
1968void
1969setLogUri(ClientHttpRequest * http, char const *uri, bool cleanUrl)
1970{
1971    safe_free(http->log_uri);
1972
1973    if (!cleanUrl)
1974        // The uri is already clean just dump it.
1975        http->log_uri = xstrndup(uri, MAX_URL);
1976    else {
1977        int flags = 0;
1978        switch (Config.uri_whitespace) {
1979        case URI_WHITESPACE_ALLOW:
1980            flags |= RFC1738_ESCAPE_NOSPACE;
1981
1982        case URI_WHITESPACE_ENCODE:
1983            flags |= RFC1738_ESCAPE_UNESCAPED;
1984            http->log_uri = xstrndup(rfc1738_do_escape(uri, flags), MAX_URL);
1985            break;
1986
1987        case URI_WHITESPACE_CHOP: {
1988            flags |= RFC1738_ESCAPE_NOSPACE;
1989            flags |= RFC1738_ESCAPE_UNESCAPED;
1990            http->log_uri = xstrndup(rfc1738_do_escape(uri, flags), MAX_URL);
1991            int pos = strcspn(http->log_uri, w_space);
1992            http->log_uri[pos] = '\0';
1993        }
1994        break;
1995
1996        case URI_WHITESPACE_DENY:
1997        case URI_WHITESPACE_STRIP:
1998        default: {
1999            const char *t;
2000            char *tmp_uri = static_cast<char*>(xmalloc(strlen(uri) + 1));
2001            char *q = tmp_uri;
2002            t = uri;
2003            while (*t) {
2004                if (!xisspace(*t)) {
2005                    *q = *t;
2006                    ++q;
2007                }
2008                ++t;
2009            }
2010            *q = '\0';
2011            http->log_uri = xstrndup(rfc1738_escape_unescaped(tmp_uri), MAX_URL);
2012            xfree(tmp_uri);
2013        }
2014        break;
2015        }
2016    }
2017}
2018
2019static void
2020prepareAcceleratedURL(ConnStateData * conn, ClientHttpRequest *http, char *url, const char *req_hdr)
2021{
2022    int vhost = conn->port->vhost;
2023    int vport = conn->port->vport;
2024    char *host;
2025    char ipbuf[MAX_IPSTRLEN];
2026
2027    http->flags.accel = true;
2028
2029    /* BUG: Squid cannot deal with '*' URLs (RFC2616 5.1.2) */
2030
2031    if (strncasecmp(url, "cache_object://", 15) == 0)
2032        return; /* already in good shape */
2033
2034    if (*url != '/') {
2035        if (conn->port->vhost)
2036            return; /* already in good shape */
2037
2038        /* else we need to ignore the host name */
2039        url = strstr(url, "//");
2040
2041#if SHOULD_REJECT_UNKNOWN_URLS
2042
2043        if (!url) {
2044            hp->request_parse_status = Http::scBadRequest;
2045            return conn->abortRequestParsing("error:invalid-request");
2046        }
2047#endif
2048
2049        if (url)
2050            url = strchr(url + 2, '/');
2051
2052        if (!url)
2053            url = (char *) "/";
2054    }
2055
2056    if (vport < 0)
2057        vport = http->getConn()->clientConnection->local.port();
2058
2059    const bool switchedToHttps = conn->switchedToHttps();
2060    const bool tryHostHeader = vhost || switchedToHttps;
2061    if (tryHostHeader && (host = mime_get_header(req_hdr, "Host")) != NULL) {
2062        debugs(33, 5, "ACCEL VHOST REWRITE: vhost=" << host << " + vport=" << vport);
2063        char thost[256];
2064        if (vport > 0) {
2065            thost[0] = '\0';
2066            char *t = NULL;
2067            if (host[strlen(host)] != ']' && (t = strrchr(host,':')) != NULL) {
2068                strncpy(thost, host, (t-host));
2069                snprintf(thost+(t-host), sizeof(thost)-(t-host), ":%d", vport);
2070                host = thost;
2071            } else if (!t) {
2072                snprintf(thost, sizeof(thost), "%s:%d",host, vport);
2073                host = thost;
2074            }
2075        } // else nothing to alter port-wise.
2076        int url_sz = strlen(url) + 32 + Config.appendDomainLen +
2077                     strlen(host);
2078        http->uri = (char *)xcalloc(url_sz, 1);
2079        const char *protocol = switchedToHttps ?
2080                               "https" : AnyP::UriScheme(conn->port->transport.protocol).c_str();
2081        snprintf(http->uri, url_sz, "%s://%s%s", protocol, host, url);
2082        debugs(33, 5, "ACCEL VHOST REWRITE: '" << http->uri << "'");
2083    } else if (conn->port->defaultsite /* && !vhost */) {
2084        debugs(33, 5, "ACCEL DEFAULTSITE REWRITE: defaultsite=" << conn->port->defaultsite << " + vport=" << vport);
2085        int url_sz = strlen(url) + 32 + Config.appendDomainLen +
2086                     strlen(conn->port->defaultsite);
2087        http->uri = (char *)xcalloc(url_sz, 1);
2088        char vportStr[32];
2089        vportStr[0] = '\0';
2090        if (vport > 0) {
2091            snprintf(vportStr, sizeof(vportStr),":%d",vport);
2092        }
2093        snprintf(http->uri, url_sz, "%s://%s%s%s",
2094                 AnyP::UriScheme(conn->port->transport.protocol).c_str(), conn->port->defaultsite, vportStr, url);
2095        debugs(33, 5, "ACCEL DEFAULTSITE REWRITE: '" << http->uri <<"'");
2096    } else if (vport > 0 /* && (!vhost || no Host:) */) {
2097        debugs(33, 5, "ACCEL VPORT REWRITE: *_port IP + vport=" << vport);
2098        /* Put the local socket IP address as the hostname, with whatever vport we found  */
2099        int url_sz = strlen(url) + 32 + Config.appendDomainLen;
2100        http->uri = (char *)xcalloc(url_sz, 1);
2101        http->getConn()->clientConnection->local.toHostStr(ipbuf,MAX_IPSTRLEN);
2102        snprintf(http->uri, url_sz, "%s://%s:%d%s",
2103                 AnyP::UriScheme(conn->port->transport.protocol).c_str(),
2104                 ipbuf, vport, url);
2105        debugs(33, 5, "ACCEL VPORT REWRITE: '" << http->uri << "'");
2106    }
2107}
2108
2109static void
2110prepareTransparentURL(ConnStateData * conn, ClientHttpRequest *http, char *url, const char *req_hdr)
2111{
2112    char *host;
2113    char ipbuf[MAX_IPSTRLEN];
2114
2115    if (*url != '/')
2116        return; /* already in good shape */
2117
2118    /* BUG: Squid cannot deal with '*' URLs (RFC2616 5.1.2) */
2119
2120    if ((host = mime_get_header(req_hdr, "Host")) != NULL) {
2121        int url_sz = strlen(url) + 32 + Config.appendDomainLen +
2122                     strlen(host);
2123        http->uri = (char *)xcalloc(url_sz, 1);
2124        snprintf(http->uri, url_sz, "%s://%s%s", AnyP::UriScheme(conn->port->transport.protocol).c_str(), host, url);
2125        debugs(33, 5, "TRANSPARENT HOST REWRITE: '" << http->uri <<"'");
2126    } else {
2127        /* Put the local socket IP address as the hostname.  */
2128        int url_sz = strlen(url) + 32 + Config.appendDomainLen;
2129        http->uri = (char *)xcalloc(url_sz, 1);
2130        http->getConn()->clientConnection->local.toHostStr(ipbuf,MAX_IPSTRLEN);
2131        snprintf(http->uri, url_sz, "%s://%s:%d%s",
2132                 AnyP::UriScheme(http->getConn()->port->transport.protocol).c_str(),
2133                 ipbuf, http->getConn()->clientConnection->local.port(), url);
2134        debugs(33, 5, "TRANSPARENT REWRITE: '" << http->uri << "'");
2135    }
2136}
2137
2138/** Parse an HTTP request
2139 *
2140 *  \note Sets result->flags.parsed_ok to 0 if failed to parse the request,
2141 *          to 1 if the request was correctly parsed.
2142 *  \param[in] csd a ConnStateData. The caller must make sure it is not null
2143 *  \param[in] hp an HttpParser
2144 *  \param[out] mehtod_p will be set as a side-effect of the parsing.
2145 *          Pointed-to value will be set to Http::METHOD_NONE in case of
2146 *          parsing failure
2147 *  \param[out] http_ver will be set as a side-effect of the parsing
2148 *  \return NULL on incomplete requests,
2149 *          a ClientSocketContext structure on success or failure.
2150 */
2151ClientSocketContext *
2152parseHttpRequest(ConnStateData *csd, HttpParser *hp, HttpRequestMethod * method_p, Http::ProtocolVersion *http_ver)
2153{
2154    char *req_hdr = NULL;
2155    char *end;
2156    size_t req_sz;
2157    ClientHttpRequest *http;
2158    ClientSocketContext *result;
2159    StoreIOBuffer tempBuffer;
2160    int r;
2161
2162    /* pre-set these values to make aborting simpler */
2163    *method_p = Http::METHOD_NONE;
2164
2165    /* NP: don't be tempted to move this down or remove again.
2166     * It's the only DDoS protection old-String has against long URL */
2167    if ( hp->bufsiz <= 0) {
2168        debugs(33, 5, "Incomplete request, waiting for end of request line");
2169        return NULL;
2170    } else if ( (size_t)hp->bufsiz >= Config.maxRequestHeaderSize && headersEnd(hp->buf, Config.maxRequestHeaderSize) == 0) {
2171        debugs(33, 5, "parseHttpRequest: Too large request");
2172        hp->request_parse_status = Http::scHeaderTooLarge;
2173        return csd->abortRequestParsing("error:request-too-large");
2174    }
2175
2176    /* Attempt to parse the first line; this'll define the method, url, version and header begin */
2177    r = HttpParserParseReqLine(hp);
2178
2179    if (r == 0) {
2180        debugs(33, 5, "Incomplete request, waiting for end of request line");
2181        return NULL;
2182    }
2183
2184    if (r == -1) {
2185        return csd->abortRequestParsing("error:invalid-request");
2186    }
2187
2188    /* Request line is valid here .. */
2189    *http_ver = Http::ProtocolVersion(hp->req.v_maj, hp->req.v_min);
2190
2191    /* This call scans the entire request, not just the headers */
2192    if (hp->req.v_maj > 0) {
2193        if ((req_sz = headersEnd(hp->buf, hp->bufsiz)) == 0) {
2194            debugs(33, 5, "Incomplete request, waiting for end of headers");
2195            return NULL;
2196        }
2197    } else {
2198        debugs(33, 3, "parseHttpRequest: Missing HTTP identifier");
2199        req_sz = HttpParserReqSz(hp);
2200    }
2201
2202    /* We know the whole request is in hp->buf now */
2203
2204    assert(req_sz <= (size_t) hp->bufsiz);
2205
2206    /* Will the following be true with HTTP/0.9 requests? probably not .. */
2207    /* So the rest of the code will need to deal with '0'-byte headers (ie, none, so don't try parsing em) */
2208    assert(req_sz > 0);
2209
2210    hp->hdr_end = req_sz - 1;
2211
2212    hp->hdr_start = hp->req.end + 1;
2213
2214    /* Enforce max_request_size */
2215    if (req_sz >= Config.maxRequestHeaderSize) {
2216        debugs(33, 5, "parseHttpRequest: Too large request");
2217        hp->request_parse_status = Http::scHeaderTooLarge;
2218        return csd->abortRequestParsing("error:request-too-large");
2219    }
2220
2221    /* Set method_p */
2222    *method_p = HttpRequestMethod(&hp->buf[hp->req.m_start], &hp->buf[hp->req.m_end]+1);
2223
2224    /* deny CONNECT via accelerated ports */
2225    if (*method_p == Http::METHOD_CONNECT && csd->port != NULL && csd->port->flags.accelSurrogate) {
2226        debugs(33, DBG_IMPORTANT, "WARNING: CONNECT method received on " << csd->port->transport.protocol << " Accelerator port " << csd->port->s.port());
2227        /* XXX need a way to say "this many character length string" */
2228        debugs(33, DBG_IMPORTANT, "WARNING: for request: " << hp->buf);
2229        hp->request_parse_status = Http::scMethodNotAllowed;
2230        return csd->abortRequestParsing("error:method-not-allowed");
2231    }
2232
2233    /* RFC 7540 section 11.6 registers the method PRI as HTTP/2 specific
2234     * Deny "PRI" method if used in HTTP/1.x or 0.9 versions.
2235     * If seen it signals a broken client or proxy has corrupted the traffic.
2236     */
2237    if (*method_p == Http::METHOD_PRI && *http_ver < Http::ProtocolVersion(2,0)) {
2238        debugs(33, DBG_IMPORTANT, "WARNING: PRI method received on " << csd->port->transport.protocol << "port " << csd->port->s.port());
2239        debugs(33, DBG_IMPORTANT, "WARNING: for request: " << hp->buf);
2240        hp->request_parse_status = Http::scMethodNotAllowed;
2241        return csd->abortRequestParsing("error:method-not-allowed");
2242    }
2243
2244    if (*method_p == Http::METHOD_NONE) {
2245        /* XXX need a way to say "this many character length string" */
2246        debugs(33, DBG_IMPORTANT, "clientParseRequestMethod: Unsupported method in request '" << hp->buf << "'");
2247        hp->request_parse_status = Http::scMethodNotAllowed;
2248        return csd->abortRequestParsing("error:unsupported-request-method");
2249    }
2250
2251    /*
2252     * Process headers after request line
2253     * TODO: Use httpRequestParse here.
2254     */
2255    /* XXX this code should be modified to take a const char * later! */
2256    req_hdr = (char *) hp->buf + hp->req.end + 1;
2257
2258    debugs(33, 3, "parseHttpRequest: req_hdr = {" << req_hdr << "}");
2259
2260    end = (char *) hp->buf + hp->hdr_end;
2261
2262    debugs(33, 3, "parseHttpRequest: end = {" << end << "}");
2263
2264    debugs(33, 3, "parseHttpRequest: prefix_sz = " <<
2265           (int) HttpParserRequestLen(hp) << ", req_line_sz = " <<
2266           HttpParserReqSz(hp));
2267
2268    /* Ok, all headers are received */
2269    http = new ClientHttpRequest(csd);
2270
2271    http->req_sz = HttpParserRequestLen(hp);
2272    result = new ClientSocketContext(csd->clientConnection, http);
2273    tempBuffer.data = result->reqbuf;
2274    tempBuffer.length = HTTP_REQBUF_SZ;
2275
2276    ClientStreamData newServer = new clientReplyContext(http);
2277    ClientStreamData newClient = result;
2278    clientStreamInit(&http->client_stream, clientGetMoreData, clientReplyDetach,
2279                     clientReplyStatus, newServer, clientSocketRecipient,
2280                     clientSocketDetach, newClient, tempBuffer);
2281
2282    debugs(33, 5, "parseHttpRequest: Request Header is\n" <<(hp->buf) + hp->hdr_start);
2283
2284    /* set url */
2285    /*
2286     * XXX this should eventually not use a malloc'ed buffer; the transformation code
2287     * below needs to be modified to not expect a mutable nul-terminated string.
2288     */
2289    char *url = (char *)xmalloc(hp->req.u_end - hp->req.u_start + 16);
2290
2291    memcpy(url, hp->buf + hp->req.u_start, hp->req.u_end - hp->req.u_start + 1);
2292
2293    url[hp->req.u_end - hp->req.u_start + 1] = '\0';
2294
2295#if THIS_VIOLATES_HTTP_SPECS_ON_URL_TRANSFORMATION
2296
2297    if ((t = strchr(url, '#'))) /* remove HTML anchors */
2298        *t = '\0';
2299
2300#endif
2301
2302    debugs(33,5, "Prepare absolute URL from " <<
2303           (csd->transparent()?"intercept":(csd->port->flags.accelSurrogate ? "accel":"")));
2304    /* Rewrite the URL in transparent or accelerator mode */
2305    /* NP: there are several cases to traverse here:
2306     *  - standard mode (forward proxy)
2307     *  - transparent mode (TPROXY)
2308     *  - transparent mode with failures
2309     *  - intercept mode (NAT)
2310     *  - intercept mode with failures
2311     *  - accelerator mode (reverse proxy)
2312     *  - internal URL
2313     *  - mixed combos of the above with internal URL
2314     *  - remote interception with PROXY protocol
2315     *  - remote reverse-proxy with PROXY protocol
2316     */
2317    if (csd->transparent()) {
2318        /* intercept or transparent mode, properly working with no failures */
2319        prepareTransparentURL(csd, http, url, req_hdr);
2320
2321    } else if (internalCheck(url)) {
2322        /* internal URL mode */
2323        /* prepend our name & port */
2324        http->uri = xstrdup(internalLocalUri(NULL, url));
2325        // We just re-wrote the URL. Must replace the Host: header.
2326        //  But have not parsed there yet!! flag for local-only handling.
2327        http->flags.internal = true;
2328
2329    } else if (csd->port->flags.accelSurrogate || csd->switchedToHttps()) {
2330        /* accelerator mode */
2331        prepareAcceleratedURL(csd, http, url, req_hdr);
2332    }
2333
2334    if (!http->uri) {
2335        /* No special rewrites have been applied above, use the
2336         * requested url. may be rewritten later, so make extra room */
2337        int url_sz = strlen(url) + Config.appendDomainLen + 5;
2338        http->uri = (char *)xcalloc(url_sz, 1);
2339        strcpy(http->uri, url);
2340    }
2341
2342    debugs(33, 5, "parseHttpRequest: Complete request received");
2343
2344    // XXX: crop this dump at the end of headers. No need for extras
2345    debugs(11, 2, "HTTP Client " << csd->clientConnection);
2346    debugs(11, 2, "HTTP Client REQUEST:\n---------\n" << (hp->buf) + hp->req.m_start << "\n----------");
2347
2348    result->flags.parsed_ok = 1;
2349    xfree(url);
2350    return result;
2351}
2352
2353bool
2354ConnStateData::In::maybeMakeSpaceAvailable()
2355{
2356    if (buf.spaceSize() < 2) {
2357        const SBuf::size_type haveCapacity = buf.length() + buf.spaceSize();
2358        if (haveCapacity >= Config.maxRequestBufferSize) {
2359            debugs(33, 4, "request buffer full: client_request_buffer_max_size=" << Config.maxRequestBufferSize);
2360            return false;
2361        }
2362        if (haveCapacity == 0) {
2363            // haveCapacity is based on the SBuf visible window of the MemBlob buffer, which may fill up.
2364            // at which point bump the buffer back to default. This allocates a new MemBlob with any un-parsed bytes.
2365            buf.reserveCapacity(CLIENT_REQ_BUF_SZ);
2366        } else {
2367            const SBuf::size_type wantCapacity = min(static_cast<SBuf::size_type>(Config.maxRequestBufferSize), haveCapacity*2);
2368            buf.reserveCapacity(wantCapacity);
2369        }
2370        debugs(33, 2, "growing request buffer: available=" << buf.spaceSize() << " used=" << buf.length());
2371    }
2372    return (buf.spaceSize() >= 2);
2373}
2374
2375void
2376ConnStateData::addContextToQueue(ClientSocketContext * context)
2377{
2378    ClientSocketContext::Pointer *S;
2379
2380    for (S = (ClientSocketContext::Pointer *) & currentobject; S->getRaw();
2381            S = &(*S)->next);
2382    *S = context;
2383
2384    ++nrequests;
2385}
2386
2387int
2388ConnStateData::getConcurrentRequestCount() const
2389{
2390    int result = 0;
2391    ClientSocketContext::Pointer *T;
2392
2393    for (T = (ClientSocketContext::Pointer *) &currentobject;
2394            T->getRaw(); T = &(*T)->next, ++result);
2395    return result;
2396}
2397
2398int
2399ConnStateData::connFinishedWithConn(int size)
2400{
2401    if (size == 0) {
2402        if (getConcurrentRequestCount() == 0 && in.buf.isEmpty()) {
2403            /* no current or pending requests */
2404            debugs(33, 4, HERE << clientConnection << " closed");
2405            return 1;
2406        } else if (!Config.onoff.half_closed_clients) {
2407            /* admin doesn't want to support half-closed client sockets */
2408            debugs(33, 3, HERE << clientConnection << " aborted (half_closed_clients disabled)");
2409            notifyAllContexts(0); // no specific error implies abort
2410            return 1;
2411        }
2412    }
2413
2414    return 0;
2415}
2416
2417void
2418ConnStateData::consumeInput(const size_t byteCount)
2419{
2420    assert(byteCount > 0 && byteCount <= in.buf.length());
2421    in.buf.consume(byteCount);
2422    debugs(33, 5, "in.buf has " << in.buf.length() << " unused bytes");
2423}
2424
2425// TODO: Remove when renaming ConnStateData
2426void
2427connNoteUseOfBuffer(ConnStateData* conn, size_t byteCount)
2428{
2429    conn->consumeInput(byteCount);
2430}
2431
2432void
2433ConnStateData::clientAfterReadingRequests()
2434{
2435    // Were we expecting to read more request body from half-closed connection?
2436    if (mayNeedToReadMoreBody() && commIsHalfClosed(clientConnection->fd)) {
2437        debugs(33, 3, HERE << "truncated body: closing half-closed " << clientConnection);
2438        clientConnection->close();
2439        return;
2440    }
2441
2442    if (flags.readMore)
2443        readSomeData();
2444}
2445
2446void
2447ConnStateData::quitAfterError(HttpRequest *request)
2448{
2449    // From HTTP p.o.v., we do not have to close after every error detected
2450    // at the client-side, but many such errors do require closure and the
2451    // client-side code is bad at handling errors so we play it safe.
2452    if (request)
2453        request->flags.proxyKeepalive = false;
2454    flags.readMore = false;
2455    debugs(33,4, HERE << "Will close after error: " << clientConnection);
2456}
2457
2458#if USE_OPENSSL
2459bool ConnStateData::serveDelayedError(ClientSocketContext *context)
2460{
2461    ClientHttpRequest *http = context->http;
2462
2463    if (!sslServerBump)
2464        return false;
2465
2466    assert(sslServerBump->entry);
2467    // Did we create an error entry while processing CONNECT?
2468    if (!sslServerBump->entry->isEmpty()) {
2469        quitAfterError(http->request);
2470
2471        // Get the saved error entry and send it to the client by replacing the
2472        // ClientHttpRequest store entry with it.
2473        clientStreamNode *node = context->getClientReplyContext();
2474        clientReplyContext *repContext = dynamic_cast<clientReplyContext *>(node->data.getRaw());
2475        assert(repContext);
2476        debugs(33, 5, "Responding with delated error for " << http->uri);
2477        repContext->setReplyToStoreEntry(sslServerBump->entry, "delayed SslBump error");
2478
2479        // save the original request for logging purposes
2480        if (!context->http->al->request) {
2481            context->http->al->request = http->request;
2482            HTTPMSGLOCK(context->http->al->request);
2483        }
2484
2485        // Get error details from the fake certificate-peeking request.
2486        http->request->detailError(sslServerBump->request->errType, sslServerBump->request->errDetail);
2487        context->pullData();
2488        return true;
2489    }
2490
2491    // In bump-server-first mode, we have not necessarily seen the intended
2492    // server name at certificate-peeking time. Check for domain mismatch now,
2493    // when we can extract the intended name from the bumped HTTP request.
2494    if (X509 *srvCert = sslServerBump->serverCert.get()) {
2495        HttpRequest *request = http->request;
2496        if (!Ssl::checkX509ServerValidity(srvCert, request->GetHost())) {
2497            debugs(33, 2, "SQUID_X509_V_ERR_DOMAIN_MISMATCH: Certificate " <<
2498                   "does not match domainname " << request->GetHost());
2499
2500            bool allowDomainMismatch = false;
2501            if (Config.ssl_client.cert_error) {
2502                ACLFilledChecklist check(Config.ssl_client.cert_error, request, dash_str);
2503                check.sslErrors = new Ssl::CertErrors(Ssl::CertError(SQUID_X509_V_ERR_DOMAIN_MISMATCH, srvCert));
2504                allowDomainMismatch = (check.fastCheck() == ACCESS_ALLOWED);
2505                delete check.sslErrors;
2506                check.sslErrors = NULL;
2507            }
2508
2509            if (!allowDomainMismatch) {
2510                quitAfterError(request);
2511
2512                clientStreamNode *node = context->getClientReplyContext();
2513                clientReplyContext *repContext = dynamic_cast<clientReplyContext *>(node->data.getRaw());
2514                assert (repContext);
2515
2516                // Fill the server IP and hostname for error page generation.
2517                HttpRequest::Pointer const & peekerRequest = sslServerBump->request;
2518                request->hier.note(peekerRequest->hier.tcpServer, request->GetHost());
2519
2520                // Create an error object and fill it
2521                ErrorState *err = new ErrorState(ERR_SECURE_CONNECT_FAIL, Http::scServiceUnavailable, request);
2522                err->src_addr = clientConnection->remote;
2523                Ssl::ErrorDetail *errDetail = new Ssl::ErrorDetail(
2524                    SQUID_X509_V_ERR_DOMAIN_MISMATCH,
2525                    srvCert, NULL);
2526                err->detail = errDetail;
2527                // Save the original request for logging purposes.
2528                if (!context->http->al->request) {
2529                    context->http->al->request = request;
2530                    HTTPMSGLOCK(context->http->al->request);
2531                }
2532                repContext->setReplyToError(request->method, err);
2533                assert(context->http->out.offset == 0);
2534                context->pullData();
2535                return true;
2536            }
2537        }
2538    }
2539
2540    return false;
2541}
2542#endif // USE_OPENSSL
2543
2544static void
2545clientProcessRequestFinished(ConnStateData *conn, const HttpRequest::Pointer &request)
2546{
2547    /*
2548     * DPW 2007-05-18
2549     * Moved the TCP_RESET feature from clientReplyContext::sendMoreData
2550     * to here because calling comm_reset_close() causes http to
2551     * be freed and the above connNoteUseOfBuffer() would hit an
2552     * assertion, not to mention that we were accessing freed memory.
2553     */
2554    if (request != NULL && request->flags.resetTcp && Comm::IsConnOpen(conn->clientConnection)) {
2555        debugs(33, 3, HERE << "Sending TCP RST on " << conn->clientConnection);
2556        conn->flags.readMore = false;
2557        comm_reset_close(conn->clientConnection);
2558    }
2559}
2560
2561void
2562clientProcessRequest(ConnStateData *conn, HttpParser *hp, ClientSocketContext *context, const HttpRequestMethod& method, Http::ProtocolVersion http_ver)
2563{
2564    ClientHttpRequest *http = context->http;
2565    HttpRequest::Pointer request;
2566    bool notedUseOfBuffer = false;
2567    bool chunked = false;
2568    bool mustReplyToOptions = false;
2569    bool unsupportedTe = false;
2570    bool expectBody = false;
2571
2572    // temporary hack to avoid splitting this huge function with sensitive code
2573    const bool isFtp = !hp;
2574    if (isFtp) {
2575        // In FTP, case, we already have the request parsed and checked, so we
2576        // only need to go through the final body/conn setup to doCallouts().
2577        assert(http->request);
2578        request = http->request;
2579        notedUseOfBuffer = true;
2580    } else {
2581
2582        if (context->flags.parsed_ok == 0) {
2583            clientStreamNode *node = context->getClientReplyContext();
2584            debugs(33, 2, "clientProcessRequest: Invalid Request");
2585            conn->quitAfterError(NULL);
2586            // setLogUri should called before repContext->setReplyToError
2587            setLogUri(http, http->uri,  true);
2588            clientReplyContext *repContext = dynamic_cast<clientReplyContext *>(node->data.getRaw());
2589            assert (repContext);
2590            switch (hp->request_parse_status) {
2591            case Http::scHeaderTooLarge:
2592                repContext->setReplyToError(ERR_TOO_BIG, Http::scBadRequest, method, http->uri, conn->clientConnection->remote, NULL, conn->in.buf.c_str(), NULL);
2593                break;
2594            case Http::scMethodNotAllowed:
2595                repContext->setReplyToError(ERR_UNSUP_REQ, Http::scMethodNotAllowed, method, http->uri,
2596                                            conn->clientConnection->remote, NULL, conn->in.buf.c_str(), NULL);
2597                break;
2598            default:
2599                repContext->setReplyToError(ERR_INVALID_REQ, hp->request_parse_status, method, http->uri,
2600                                            conn->clientConnection->remote, NULL, conn->in.buf.c_str(), NULL);
2601            }
2602            assert(context->http->out.offset == 0);
2603            context->pullData();
2604            connNoteUseOfBuffer(conn, http->req_sz);
2605            return;
2606        }
2607
2608        if ((request = HttpRequest::CreateFromUrlAndMethod(http->uri, method)) == NULL) {
2609            clientStreamNode *node = context->getClientReplyContext();
2610            debugs(33, 5, "Invalid URL: " << http->uri);
2611            conn->quitAfterError(request.getRaw());
2612            // setLogUri should called before repContext->setReplyToError
2613            setLogUri(http, http->uri,  true);
2614            clientReplyContext *repContext = dynamic_cast<clientReplyContext *>(node->data.getRaw());
2615            assert (repContext);
2616            repContext->setReplyToError(ERR_INVALID_URL, Http::scBadRequest, method, http->uri, conn->clientConnection->remote, NULL, NULL, NULL);
2617            assert(context->http->out.offset == 0);
2618            context->pullData();
2619            connNoteUseOfBuffer(conn, http->req_sz);
2620            return;
2621        }
2622
2623        /* RFC 2616 section 10.5.6 : handle unsupported HTTP major versions cleanly. */
2624        /* We currently only support 0.9, 1.0, 1.1 properly */
2625        /* TODO: move HTTP-specific processing into servers/HttpServer and such */
2626        if ( (http_ver.major == 0 && http_ver.minor != 9) ||
2627                (http_ver.major > 1) ) {
2628
2629            clientStreamNode *node = context->getClientReplyContext();
2630            debugs(33, 5, "Unsupported HTTP version discovered. :\n" << HttpParserHdrBuf(hp));
2631            conn->quitAfterError(request.getRaw());
2632            // setLogUri should called before repContext->setReplyToError
2633            setLogUri(http, http->uri,  true);
2634            clientReplyContext *repContext = dynamic_cast<clientReplyContext *>(node->data.getRaw());
2635            assert (repContext);
2636            repContext->setReplyToError(ERR_UNSUP_HTTPVERSION, Http::scHttpVersionNotSupported, method, http->uri,
2637                                        conn->clientConnection->remote, NULL, HttpParserHdrBuf(hp), NULL);
2638            assert(context->http->out.offset == 0);
2639            context->pullData();
2640            connNoteUseOfBuffer(conn, http->req_sz);
2641            clientProcessRequestFinished(conn, request);
2642            return;
2643        }
2644
2645        /* compile headers */
2646        /* we should skip request line! */
2647        /* XXX should actually know the damned buffer size here */
2648        if (http_ver.major >= 1 && !request->parseHeader(HttpParserHdrBuf(hp), HttpParserHdrSz(hp))) {
2649            clientStreamNode *node = context->getClientReplyContext();
2650            debugs(33, 5, "Failed to parse request headers:\n" << HttpParserHdrBuf(hp));
2651            conn->quitAfterError(request.getRaw());
2652            // setLogUri should called before repContext->setReplyToError
2653            setLogUri(http, http->uri,  true);
2654            clientReplyContext *repContext = dynamic_cast<clientReplyContext *>(node->data.getRaw());
2655            assert (repContext);
2656            repContext->setReplyToError(ERR_INVALID_REQ, Http::scBadRequest, method, http->uri, conn->clientConnection->remote, NULL, NULL, NULL);
2657            assert(context->http->out.offset == 0);
2658            context->pullData();
2659            connNoteUseOfBuffer(conn, http->req_sz);
2660            clientProcessRequestFinished(conn, request);
2661            return;
2662        }
2663
2664        // when absolute-URI is provided Host header should be ignored. However
2665        // some code still uses Host directly so normalize it.
2666        // For now preserve the case where Host is completely absent. That matters.
2667        if (request->header.has(HDR_HOST)) {
2668            const char *host = request->header.getStr(HDR_HOST);
2669            SBuf authority(request->GetHost());
2670            if (request->port != urlDefaultPort(request->url.getScheme()))
2671                authority.appendf(":%d", request->port);
2672            debugs(33, 5, "URL domain " << authority << " overrides header Host: " << host);
2673            // URL authority overrides Host header
2674            request->header.delById(HDR_HOST);
2675            request->header.putStr(HDR_HOST, authority.c_str());
2676        }
2677    }
2678
2679    // Some blobs below are still HTTP-specific, but we would have to rewrite
2680    // this entire function to remove them from the FTP code path. Connection
2681    // setup and body_pipe preparation blobs are needed for FTP.
2682
2683    request->clientConnectionManager = conn;
2684
2685    request->flags.accelerated = http->flags.accel;
2686    request->flags.sslBumped=conn->switchedToHttps();
2687    request->flags.ignoreCc = conn->port->ignore_cc;
2688    // TODO: decouple http->flags.accel from request->flags.sslBumped
2689    request->flags.noDirect = (request->flags.accelerated && !request->flags.sslBumped) ?
2690                              !conn->port->allow_direct : 0;
2691#if USE_AUTH
2692    if (request->flags.sslBumped) {
2693        if (conn->getAuth() != NULL)
2694            request->auth_user_request = conn->getAuth();
2695    }
2696#endif
2697
2698    /** \par
2699     * If transparent or interception mode is working clone the transparent and interception flags
2700     * from the port settings to the request.
2701     */
2702    if (http->clientConnection != NULL) {
2703        request->flags.intercepted = ((http->clientConnection->flags & COMM_INTERCEPTION) != 0);
2704        request->flags.interceptTproxy = ((http->clientConnection->flags & COMM_TRANSPARENT) != 0 ) ;
2705        static const bool proxyProtocolPort = (conn->port != NULL) ? conn->port->flags.proxySurrogate : false;
2706        if (request->flags.interceptTproxy && !proxyProtocolPort) {
2707            if (Config.accessList.spoof_client_ip) {
2708                ACLFilledChecklist *checklist = clientAclChecklistCreate(Config.accessList.spoof_client_ip, http);
2709                request->flags.spoofClientIp = (checklist->fastCheck() == ACCESS_ALLOWED);
2710                delete checklist;
2711            } else
2712                request->flags.spoofClientIp = true;
2713        } else
2714            request->flags.spoofClientIp = false;
2715    }
2716
2717    if (internalCheck(request->urlpath.termedBuf())) {
2718        if (internalHostnameIs(request->GetHost()) && request->port == getMyPort()) {
2719            debugs(33, 2, "internal URL found: " << request->url.getScheme() << "://" << request->GetHost() <<
2720                   ':' << request->port);
2721            http->flags.internal = true;
2722        } else if (Config.onoff.global_internal_static && internalStaticCheck(request->urlpath.termedBuf())) {
2723            debugs(33, 2, "internal URL found: " << request->url.getScheme() << "://" << request->GetHost() <<
2724                   ':' << request->port << " (global_internal_static on)");
2725            request->url.setScheme(AnyP::PROTO_HTTP);
2726            request->SetHost(internalHostname());
2727            request->port = getMyPort();
2728            http->flags.internal = true;
2729        } else
2730            debugs(33, 2, "internal URL found: " << request->url.getScheme() << "://" << request->GetHost() <<
2731                   ':' << request->port << " (not this proxy)");
2732    }
2733
2734    if (http->flags.internal)
2735        request->login[0] = '\0';
2736
2737    request->flags.internal = http->flags.internal;
2738    setLogUri (http, urlCanonicalClean(request.getRaw()));
2739    request->client_addr = conn->clientConnection->remote; // XXX: remove reuest->client_addr member.
2740#if FOLLOW_X_FORWARDED_FOR
2741    // indirect client gets stored here because it is an HTTP header result (from X-Forwarded-For:)
2742    // not a details about teh TCP connection itself
2743    request->indirect_client_addr = conn->clientConnection->remote;
2744#endif /* FOLLOW_X_FORWARDED_FOR */
2745    request->my_addr = conn->clientConnection->local;
2746    request->myportname = conn->port->name;
2747    request->http_ver = http_ver;
2748
2749    // Link this HttpRequest to ConnStateData relatively early so the following complex handling can use it
2750    // TODO: this effectively obsoletes a lot of conn->FOO copying. That needs cleaning up later.
2751    request->clientConnectionManager = conn;
2752
2753    if (request->header.chunked()) {
2754        chunked = true;
2755    } else if (request->header.has(HDR_TRANSFER_ENCODING)) {
2756        const String te = request->header.getList(HDR_TRANSFER_ENCODING);
2757        // HTTP/1.1 requires chunking to be the last encoding if there is one
2758        unsupportedTe = te.size() && te != "identity";
2759    } // else implied identity coding
2760
2761    mustReplyToOptions = (method == Http::METHOD_OPTIONS) &&
2762                         (request->header.getInt64(HDR_MAX_FORWARDS) == 0);
2763    if (!urlCheckRequest(request.getRaw()) || mustReplyToOptions || unsupportedTe) {
2764        clientStreamNode *node = context->getClientReplyContext();
2765        conn->quitAfterError(request.getRaw());
2766        clientReplyContext *repContext = dynamic_cast<clientReplyContext *>(node->data.getRaw());
2767        assert (repContext);
2768        repContext->setReplyToError(ERR_UNSUP_REQ, Http::scNotImplemented, request->method, NULL,
2769                                    conn->clientConnection->remote, request.getRaw(), NULL, NULL);
2770        assert(context->http->out.offset == 0);
2771        context->pullData();
2772        connNoteUseOfBuffer(conn, http->req_sz);
2773        clientProcessRequestFinished(conn, request);
2774        return;
2775    }
2776
2777    if (!chunked && !clientIsContentLengthValid(request.getRaw())) {
2778        clientStreamNode *node = context->getClientReplyContext();
2779        clientReplyContext *repContext = dynamic_cast<clientReplyContext *>(node->data.getRaw());
2780        assert (repContext);
2781        conn->quitAfterError(request.getRaw());
2782        repContext->setReplyToError(ERR_INVALID_REQ,
2783                                    Http::scLengthRequired, request->method, NULL,
2784                                    conn->clientConnection->remote, request.getRaw(), NULL, NULL);
2785        assert(context->http->out.offset == 0);
2786        context->pullData();
2787        connNoteUseOfBuffer(conn, http->req_sz);
2788        clientProcessRequestFinished(conn, request);
2789        return;
2790    }
2791
2792    if (request->header.has(HDR_EXPECT)) {
2793        const String expect = request->header.getList(HDR_EXPECT);
2794        const bool supportedExpect = (expect.caseCmp("100-continue") == 0);
2795        if (!supportedExpect) {
2796            clientStreamNode *node = context->getClientReplyContext();
2797            clientReplyContext *repContext = dynamic_cast<clientReplyContext *>(node->data.getRaw());
2798            assert (repContext);
2799            conn->quitAfterError(request.getRaw());
2800            repContext->setReplyToError(ERR_INVALID_REQ, Http::scExpectationFailed, request->method, http->uri,
2801                                        conn->clientConnection->remote, request.getRaw(), NULL, NULL);
2802            assert(context->http->out.offset == 0);
2803            context->pullData();
2804            connNoteUseOfBuffer(conn, http->req_sz);
2805            clientProcessRequestFinished(conn, request);
2806            return;
2807        }
2808    }
2809
2810    if (!isFtp) {
2811        http->request = request.getRaw();
2812        HTTPMSGLOCK(http->request);
2813    }
2814
2815    clientSetKeepaliveFlag(http);
2816    // Let tunneling code be fully responsible for CONNECT requests
2817    if (http->request->method == Http::METHOD_CONNECT) {
2818        context->mayUseConnection(true);
2819        conn->flags.readMore = false;
2820
2821        // consume header early so that tunnel gets just the body
2822        connNoteUseOfBuffer(conn, http->req_sz);
2823        notedUseOfBuffer = true;
2824    }
2825
2826#if USE_OPENSSL
2827    if (conn->switchedToHttps() && conn->serveDelayedError(context)) {
2828        if (!notedUseOfBuffer)
2829            connNoteUseOfBuffer(conn, http->req_sz);
2830        clientProcessRequestFinished(conn, request);
2831        return;
2832    }
2833#endif
2834
2835    /* Do we expect a request-body? */
2836    expectBody = chunked || request->content_length > 0;
2837    if (!context->mayUseConnection() && expectBody) {
2838        request->body_pipe = conn->expectRequestBody(
2839                                 chunked ? -1 : request->content_length);
2840
2841        if (!isFtp) {
2842            // consume header early so that body pipe gets just the body
2843            connNoteUseOfBuffer(conn, http->req_sz);
2844            notedUseOfBuffer = true;
2845        }
2846
2847        /* Is it too large? */
2848        if (!chunked && // if chunked, we will check as we accumulate
2849                clientIsRequestBodyTooLargeForPolicy(request->content_length)) {
2850            clientStreamNode *node = context->getClientReplyContext();
2851            clientReplyContext *repContext = dynamic_cast<clientReplyContext *>(node->data.getRaw());
2852            assert (repContext);
2853            conn->quitAfterError(request.getRaw());
2854            repContext->setReplyToError(ERR_TOO_BIG,
2855                                        Http::scPayloadTooLarge, Http::METHOD_NONE, NULL,
2856                                        conn->clientConnection->remote, http->request, NULL, NULL);
2857            assert(context->http->out.offset == 0);
2858            context->pullData();
2859            clientProcessRequestFinished(conn, request);
2860            return;
2861        }
2862
2863        if (!isFtp) {
2864            // We may stop producing, comm_close, and/or call setReplyToError()
2865            // below, so quit on errors to avoid http->doCallouts()
2866            if (!conn->handleRequestBodyData()) {
2867                clientProcessRequestFinished(conn, request);
2868                return;
2869            }
2870
2871            if (!request->body_pipe->productionEnded()) {
2872                debugs(33, 5, "need more request body");
2873                context->mayUseConnection(true);
2874                assert(conn->flags.readMore);
2875            }
2876        }
2877    }
2878
2879    http->calloutContext = new ClientRequestContext(http);
2880
2881    http->doCallouts();
2882
2883    if (!notedUseOfBuffer)
2884        connNoteUseOfBuffer(conn, http->req_sz);
2885
2886    clientProcessRequestFinished(conn, request);
2887}
2888
2889static void
2890connStripBufferWhitespace (ConnStateData * conn)
2891{
2892    // XXX: kill this whole function.
2893    while (!conn->in.buf.isEmpty() && xisspace(conn->in.buf.at(0))) {
2894        conn->in.buf.consume(1);
2895    }
2896}
2897
2898int
2899ConnStateData::pipelinePrefetchMax() const
2900{
2901    // TODO: Support pipelined requests through pinned connections.
2902    if (pinning.pinned)
2903        return 0;
2904    return Config.pipeline_max_prefetch;
2905}
2906
2907/**
2908 * Limit the number of concurrent requests.
2909 * \return true  when there are available position(s) in the pipeline queue for another request.
2910 * \return false when the pipeline queue is full or disabled.
2911 */
2912bool
2913ConnStateData::concurrentRequestQueueFilled() const
2914{
2915    const int existingRequestCount = getConcurrentRequestCount();
2916
2917    // default to the configured pipeline size.
2918    // add 1 because the head of pipeline is counted in concurrent requests and not prefetch queue
2919#if USE_OPENSSL
2920    const int internalRequest = (transparent() && sslBumpMode == Ssl::bumpSplice) ? 1 : 0;
2921#else
2922    const int internalRequest = 0;
2923#endif
2924    const int concurrentRequestLimit = pipelinePrefetchMax() + 1 + internalRequest;
2925
2926    // when queue filled already we cant add more.
2927    if (existingRequestCount >= concurrentRequestLimit) {
2928        debugs(33, 3, clientConnection << " max concurrent requests reached (" << concurrentRequestLimit << ")");
2929        debugs(33, 5, clientConnection << " deferring new request until one is done");
2930        return true;
2931    }
2932
2933    return false;
2934}
2935
2936/**
2937 * Perform proxy_protocol_access ACL tests on the client which
2938 * connected to PROXY protocol port to see if we trust the
2939 * sender enough to accept their PROXY header claim.
2940 */
2941bool
2942ConnStateData::proxyProtocolValidateClient()
2943{
2944    if (!Config.accessList.proxyProtocol)
2945        return proxyProtocolError("PROXY client not permitted by default ACL");
2946
2947    ACLFilledChecklist ch(Config.accessList.proxyProtocol, NULL, clientConnection->rfc931);
2948    ch.src_addr = clientConnection->remote;
2949    ch.my_addr = clientConnection->local;
2950    ch.conn(this);
2951
2952    if (ch.fastCheck() != ACCESS_ALLOWED)
2953        return proxyProtocolError("PROXY client not permitted by ACLs");
2954
2955    return true;
2956}
2957
2958/**
2959 * Perform cleanup on PROXY protocol errors.
2960 * If header parsing hits a fatal error terminate the connection,
2961 * otherwise wait for more data.
2962 */
2963bool
2964ConnStateData::proxyProtocolError(const char *msg)
2965{
2966    if (msg) {
2967        // This is important to know, but maybe not so much that flooding the log is okay.
2968#if QUIET_PROXY_PROTOCOL
2969        // display the first of every 32 occurances at level 1, the others at level 2.
2970        static uint8_t hide = 0;
2971        debugs(33, (hide++ % 32 == 0 ? DBG_IMPORTANT : 2), msg << " from " << clientConnection);
2972#else
2973        debugs(33, DBG_IMPORTANT, msg << " from " << clientConnection);
2974#endif
2975        mustStop(msg);
2976    }
2977    return false;
2978}
2979
2980/// magic octet prefix for PROXY protocol version 1
2981static const SBuf Proxy1p0magic("PROXY ", 6);
2982
2983/// magic octet prefix for PROXY protocol version 2
2984static const SBuf Proxy2p0magic("\x0D\x0A\x0D\x0A\x00\x0D\x0A\x51\x55\x49\x54\x0A", 12);
2985
2986/**
2987 * Test the connection read buffer for PROXY protocol header.
2988 * Version 1 and 2 header currently supported.
2989 */
2990bool
2991ConnStateData::parseProxyProtocolHeader()
2992{
2993    // http://www.haproxy.org/download/1.5/doc/proxy-protocol.txt
2994
2995    // detect and parse PROXY/2.0 protocol header
2996    if (in.buf.startsWith(Proxy2p0magic))
2997        return parseProxy2p0();
2998
2999    // detect and parse PROXY/1.0 protocol header
3000    if (in.buf.startsWith(Proxy1p0magic))
3001        return parseProxy1p0();
3002
3003    // detect and terminate other protocols
3004    if (in.buf.length() >= Proxy2p0magic.length()) {
3005        // PROXY/1.0 magic is shorter, so we know that
3006        // the input does not start with any PROXY magic
3007        return proxyProtocolError("PROXY protocol error: invalid header");
3008    }
3009
3010    // TODO: detect short non-magic prefixes earlier to avoid
3011    // waiting for more data which may never come
3012
3013    // not enough bytes to parse yet.
3014    return false;
3015}
3016
3017/// parse the PROXY/1.0 protocol header from the connection read buffer
3018bool
3019ConnStateData::parseProxy1p0()
3020{
3021    ::Parser::Tokenizer tok(in.buf);
3022    tok.skip(Proxy1p0magic);
3023
3024    // skip to first LF (assumes it is part of CRLF)
3025    static const CharacterSet lineContent = CharacterSet::LF.complement("non-LF");
3026    SBuf line;
3027    if (tok.prefix(line, lineContent, 107-Proxy1p0magic.length())) {
3028        if (tok.skip('\n')) {
3029            // found valid header
3030            in.buf = tok.remaining();
3031            needProxyProtocolHeader_ = false;
3032            // reset the tokenizer to work on found line only.
3033            tok.reset(line);
3034        } else
3035            return false; // no LF yet
3036
3037    } else // protocol error only if there are more than 107 bytes prefix header
3038        return proxyProtocolError(in.buf.length() > 107? "PROXY/1.0 error: missing CRLF" : NULL);
3039
3040    static const SBuf unknown("UNKNOWN"), tcpName("TCP");
3041    if (tok.skip(tcpName)) {
3042
3043        // skip TCP/IP version number
3044        static const CharacterSet tcpVersions("TCP-version","46");
3045        if (!tok.skipOne(tcpVersions))
3046            return proxyProtocolError("PROXY/1.0 error: missing TCP version");
3047
3048        // skip SP after protocol version
3049        if (!tok.skip(' '))
3050            return proxyProtocolError("PROXY/1.0 error: missing SP");
3051
3052        SBuf ipa, ipb;
3053        int64_t porta, portb;
3054        static const CharacterSet ipChars = CharacterSet("IP Address",".:") + CharacterSet::HEXDIG;
3055
3056        // parse:  src-IP SP dst-IP SP src-port SP dst-port CR
3057        // leave the LF until later.
3058        const bool correct = tok.prefix(ipa, ipChars) && tok.skip(' ') &&
3059                             tok.prefix(ipb, ipChars) && tok.skip(' ') &&
3060                             tok.int64(porta) && tok.skip(' ') &&
3061                             tok.int64(portb) &&
3062                             tok.skip('\r');
3063        if (!correct)
3064            return proxyProtocolError("PROXY/1.0 error: invalid syntax");
3065
3066        // parse IP and port strings
3067        Ip::Address originalClient, originalDest;
3068
3069        if (!originalClient.GetHostByName(ipa.c_str()))
3070            return proxyProtocolError("PROXY/1.0 error: invalid src-IP address");
3071
3072        if (!originalDest.GetHostByName(ipb.c_str()))
3073            return proxyProtocolError("PROXY/1.0 error: invalid dst-IP address");
3074
3075        if (porta > 0 && porta <= 0xFFFF) // max uint16_t
3076            originalClient.port(static_cast<uint16_t>(porta));
3077        else
3078            return proxyProtocolError("PROXY/1.0 error: invalid src port");
3079
3080        if (portb > 0 && portb <= 0xFFFF) // max uint16_t
3081            originalDest.port(static_cast<uint16_t>(portb));
3082        else
3083            return proxyProtocolError("PROXY/1.0 error: invalid dst port");
3084
3085        // we have original client and destination details now
3086        // replace the client connection values
3087        debugs(33, 5, "PROXY/1.0 protocol on connection " << clientConnection);
3088        clientConnection->local = originalDest;
3089        clientConnection->remote = originalClient;
3090        if ((clientConnection->flags & COMM_TRANSPARENT))
3091            clientConnection->flags ^= COMM_TRANSPARENT; // prevent TPROXY spoofing of this new IP.
3092        debugs(33, 5, "PROXY/1.0 upgrade: " << clientConnection);
3093
3094        // repeat fetch ensuring the new client FQDN can be logged
3095        if (Config.onoff.log_fqdn)
3096            fqdncache_gethostbyaddr(clientConnection->remote, FQDN_LOOKUP_IF_MISS);
3097
3098        return true;
3099
3100    } else if (tok.skip(unknown)) {
3101        // found valid but unusable header
3102        return true;
3103
3104    } else
3105        return proxyProtocolError("PROXY/1.0 error: invalid protocol family");
3106
3107    return false;
3108}
3109
3110/// parse the PROXY/2.0 protocol header from the connection read buffer
3111bool
3112ConnStateData::parseProxy2p0()
3113{
3114    static const SBuf::size_type prefixLen = Proxy2p0magic.length();
3115    if (in.buf.length() < prefixLen + 4)
3116        return false; // need more bytes
3117
3118    if ((in.buf[prefixLen] & 0xF0) != 0x20) // version == 2 is mandatory
3119        return proxyProtocolError("PROXY/2.0 error: invalid version");
3120
3121    const char command = (in.buf[prefixLen] & 0x0F);
3122    if ((command & 0xFE) != 0x00) // values other than 0x0-0x1 are invalid
3123        return proxyProtocolError("PROXY/2.0 error: invalid command");
3124
3125    const char family = (in.buf[prefixLen+1] & 0xF0) >>4;
3126    if (family > 0x3) // values other than 0x0-0x3 are invalid
3127        return proxyProtocolError("PROXY/2.0 error: invalid family");
3128
3129    const char proto = (in.buf[prefixLen+1] & 0x0F);
3130    if (proto > 0x2) // values other than 0x0-0x2 are invalid
3131        return proxyProtocolError("PROXY/2.0 error: invalid protocol type");
3132
3133    const char *clen = in.buf.rawContent() + prefixLen + 2;
3134    uint16_t len;
3135    memcpy(&len, clen, sizeof(len));
3136    len = ntohs(len);
3137
3138    if (in.buf.length() < prefixLen + 4 + len)
3139        return false; // need more bytes
3140
3141    in.buf.consume(prefixLen + 4); // 4 being the extra bytes
3142    const SBuf extra = in.buf.consume(len);
3143    needProxyProtocolHeader_ = false; // found successfully
3144
3145    // LOCAL connections do nothing with the extras
3146    if (command == 0x00/* LOCAL*/)
3147        return true;
3148
3149    union pax {
3150        struct {        /* for TCP/UDP over IPv4, len = 12 */
3151            struct in_addr src_addr;
3152            struct in_addr dst_addr;
3153            uint16_t src_port;
3154            uint16_t dst_port;
3155        } ipv4_addr;
3156        struct {        /* for TCP/UDP over IPv6, len = 36 */
3157            struct in6_addr src_addr;
3158            struct in6_addr dst_addr;
3159            uint16_t src_port;
3160            uint16_t dst_port;
3161        } ipv6_addr;
3162#if NOT_SUPPORTED
3163        struct {        /* for AF_UNIX sockets, len = 216 */
3164            uint8_t src_addr[108];
3165            uint8_t dst_addr[108];
3166        } unix_addr;
3167#endif
3168    };
3169
3170    pax ipu;
3171    memcpy(&ipu, extra.rawContent(), sizeof(pax));
3172
3173    // replace the client connection values
3174    debugs(33, 5, "PROXY/2.0 protocol on connection " << clientConnection);
3175    switch (family) {
3176    case 0x1: // IPv4
3177        clientConnection->local = ipu.ipv4_addr.dst_addr;
3178        clientConnection->local.port(ntohs(ipu.ipv4_addr.dst_port));
3179        clientConnection->remote = ipu.ipv4_addr.src_addr;
3180        clientConnection->remote.port(ntohs(ipu.ipv4_addr.src_port));
3181        if ((clientConnection->flags & COMM_TRANSPARENT))
3182            clientConnection->flags ^= COMM_TRANSPARENT; // prevent TPROXY spoofing of this new IP.
3183        break;
3184    case 0x2: // IPv6
3185        clientConnection->local = ipu.ipv6_addr.dst_addr;
3186        clientConnection->local.port(ntohs(ipu.ipv6_addr.dst_port));
3187        clientConnection->remote = ipu.ipv6_addr.src_addr;
3188        clientConnection->remote.port(ntohs(ipu.ipv6_addr.src_port));
3189        if ((clientConnection->flags & COMM_TRANSPARENT))
3190            clientConnection->flags ^= COMM_TRANSPARENT; // prevent TPROXY spoofing of this new IP.
3191        break;
3192    default: // do nothing
3193        break;
3194    }
3195    debugs(33, 5, "PROXY/2.0 upgrade: " << clientConnection);
3196
3197    // repeat fetch ensuring the new client FQDN can be logged
3198    if (Config.onoff.log_fqdn)
3199        fqdncache_gethostbyaddr(clientConnection->remote, FQDN_LOOKUP_IF_MISS);
3200
3201    return true;
3202}
3203
3204/**
3205 * Attempt to parse one or more requests from the input buffer.
3206 * Returns true after completing parsing of at least one request [header]. That
3207 * includes cases where parsing ended with an error (e.g., a huge request).
3208 */
3209bool
3210ConnStateData::clientParseRequests()
3211{
3212    bool parsed_req = false;
3213
3214    debugs(33, 5, HERE << clientConnection << ": attempting to parse");
3215
3216    // Loop while we have read bytes that are not needed for producing the body
3217    // On errors, bodyPipe may become nil, but readMore will be cleared
3218    while (!in.buf.isEmpty() && !bodyPipe && flags.readMore) {
3219        connStripBufferWhitespace(this);
3220
3221        /* Don't try to parse if the buffer is empty */
3222        if (in.buf.isEmpty())
3223            break;
3224
3225        /* Limit the number of concurrent requests */
3226        if (concurrentRequestQueueFilled())
3227            break;
3228
3229        // try to parse the PROXY protocol header magic bytes
3230        if (needProxyProtocolHeader_ && !parseProxyProtocolHeader())
3231            break;
3232
3233        Http::ProtocolVersion http_ver;
3234        if (ClientSocketContext *context = parseOneRequest(http_ver)) {
3235            debugs(33, 5, clientConnection << ": done parsing a request");
3236            AsyncCall::Pointer timeoutCall = commCbCall(5, 4, "clientLifetimeTimeout",
3237                                             CommTimeoutCbPtrFun(clientLifetimeTimeout, context->http));
3238            commSetConnTimeout(clientConnection, Config.Timeout.lifetime, timeoutCall);
3239
3240            context->registerWithConn();
3241
3242            processParsedRequest(context, http_ver);
3243
3244            parsed_req = true; // XXX: do we really need to parse everything right NOW ?
3245
3246            if (context->mayUseConnection()) {
3247                debugs(33, 3, HERE << "Not parsing new requests, as this request may need the connection");
3248                break;
3249            }
3250        } else {
3251            debugs(33, 5, clientConnection << ": not enough request data: " <<
3252                   in.buf.length() << " < " << Config.maxRequestHeaderSize);
3253            Must(in.buf.length() < Config.maxRequestHeaderSize);
3254            break;
3255        }
3256    }
3257
3258    /* XXX where to 'finish' the parsing pass? */
3259    return parsed_req;
3260}
3261
3262void
3263ConnStateData::clientReadRequest(const CommIoCbParams &io)
3264{
3265    debugs(33,5, io.conn);
3266    Must(reading());
3267    reader = NULL;
3268
3269    /* Bail out quickly on Comm::ERR_CLOSING - close handlers will tidy up */
3270    if (io.flag == Comm::ERR_CLOSING) {
3271        debugs(33,5, io.conn << " closing Bailout.");
3272        return;
3273    }
3274
3275    assert(Comm::IsConnOpen(clientConnection));
3276    assert(io.conn->fd == clientConnection->fd);
3277
3278    /*
3279     * Don't reset the timeout value here. The value should be
3280     * counting Config.Timeout.request and applies to the request
3281     * as a whole, not individual read() calls.
3282     * Plus, it breaks our lame *HalfClosed() detection
3283     */
3284
3285    in.maybeMakeSpaceAvailable();
3286    CommIoCbParams rd(this); // will be expanded with ReadNow results
3287    rd.conn = io.conn;
3288    switch (Comm::ReadNow(rd, in.buf)) {
3289    case Comm::INPROGRESS:
3290        if (in.buf.isEmpty())
3291            debugs(33, 2, io.conn << ": no data to process, " << xstrerr(rd.xerrno));
3292        readSomeData();
3293        return;
3294
3295    case Comm::OK:
3296        kb_incr(&(statCounter.client_http.kbytes_in), rd.size);
3297        // may comm_close or setReplyToError
3298        if (!handleReadData())
3299            return;
3300
3301        /* Continue to process previously read data */
3302        break;
3303
3304    case Comm::ENDFILE: // close detected by 0-byte read
3305        debugs(33, 5, io.conn << " closed?");
3306
3307        if (connFinishedWithConn(rd.size)) {
3308            clientConnection->close();
3309            return;
3310        }
3311
3312        /* It might be half-closed, we can't tell */
3313        fd_table[io.conn->fd].flags.socket_eof = true;
3314        commMarkHalfClosed(io.conn->fd);
3315        fd_note(io.conn->fd, "half-closed");
3316
3317        /* There is one more close check at the end, to detect aborted
3318         * (partial) requests. At this point we can't tell if the request
3319         * is partial.
3320         */
3321
3322        /* Continue to process previously read data */
3323        break;
3324
3325    // case Comm::COMM_ERROR:
3326    default: // no other flags should ever occur
3327        debugs(33, 2, io.conn << ": got flag " << rd.flag << "; " << xstrerr(rd.xerrno));
3328        notifyAllContexts(rd.xerrno);
3329        io.conn->close();
3330        return;
3331    }
3332
3333    /* Process next request */
3334    if (getConcurrentRequestCount() == 0)
3335        fd_note(io.fd, "Reading next request");
3336
3337    if (!clientParseRequests()) {
3338        if (!isOpen())
3339            return;
3340        /*
3341         * If the client here is half closed and we failed
3342         * to parse a request, close the connection.
3343         * The above check with connFinishedWithConn() only
3344         * succeeds _if_ the buffer is empty which it won't
3345         * be if we have an incomplete request.
3346         * XXX: This duplicates ClientSocketContext::keepaliveNextRequest
3347         */
3348        if (getConcurrentRequestCount() == 0 && commIsHalfClosed(io.fd)) {
3349            debugs(33, 5, HERE << io.conn << ": half-closed connection, no completed request parsed, connection closing.");
3350            clientConnection->close();
3351            return;
3352        }
3353    }
3354
3355    if (!isOpen())
3356        return;
3357
3358    clientAfterReadingRequests();
3359}
3360
3361/**
3362 * called when new request data has been read from the socket
3363 *
3364 * \retval false called comm_close or setReplyToError (the caller should bail)
3365 * \retval true  we did not call comm_close or setReplyToError
3366 */
3367bool
3368ConnStateData::handleReadData()
3369{
3370    // if we are reading a body, stuff data into the body pipe
3371    if (bodyPipe != NULL)
3372        return handleRequestBodyData();
3373    return true;
3374}
3375
3376/**
3377 * called when new request body data has been buffered in in.buf
3378 * may close the connection if we were closing and piped everything out
3379 *
3380 * \retval false called comm_close or setReplyToError (the caller should bail)
3381 * \retval true  we did not call comm_close or setReplyToError
3382 */
3383bool
3384ConnStateData::handleRequestBodyData()
3385{
3386    assert(bodyPipe != NULL);
3387
3388    size_t putSize = 0;
3389
3390    if (in.bodyParser) { // chunked encoding
3391        if (const err_type error = handleChunkedRequestBody(putSize)) {
3392            abortChunkedRequestBody(error);
3393            return false;
3394        }
3395    } else { // identity encoding
3396        debugs(33,5, HERE << "handling plain request body for " << clientConnection);
3397        putSize = bodyPipe->putMoreData(in.buf.c_str(), in.buf.length());
3398        if (!bodyPipe->mayNeedMoreData()) {
3399            // BodyPipe will clear us automagically when we produced everything
3400            bodyPipe = NULL;
3401        }
3402    }
3403
3404    if (putSize > 0)
3405        connNoteUseOfBuffer(this, putSize);
3406
3407    if (!bodyPipe) {
3408        debugs(33,5, HERE << "produced entire request body for " << clientConnection);
3409
3410        if (const char *reason = stoppedSending()) {
3411            /* we've finished reading like good clients,
3412             * now do the close that initiateClose initiated.
3413             */
3414            debugs(33, 3, HERE << "closing for earlier sending error: " << reason);
3415            clientConnection->close();
3416            return false;
3417        }
3418    }
3419
3420    return true;
3421}
3422
3423/// parses available chunked encoded body bytes, checks size, returns errors
3424err_type
3425ConnStateData::handleChunkedRequestBody(size_t &putSize)
3426{
3427    debugs(33, 7, "chunked from " << clientConnection << ": " << in.buf.length());
3428
3429    try { // the parser will throw on errors
3430
3431        if (in.buf.isEmpty()) // nothing to do
3432            return ERR_NONE;
3433
3434        MemBuf raw; // ChunkedCodingParser only works with MemBufs
3435        // add one because MemBuf will assert if it cannot 0-terminate
3436        raw.init(in.buf.length(), in.buf.length()+1);
3437        raw.append(in.buf.c_str(), in.buf.length());
3438
3439        const mb_size_t wasContentSize = raw.contentSize();
3440        BodyPipeCheckout bpc(*bodyPipe);
3441        const bool parsed = in.bodyParser->parse(&raw, &bpc.buf);
3442        bpc.checkIn();
3443        putSize = wasContentSize - raw.contentSize();
3444
3445        // dechunk then check: the size limit applies to _dechunked_ content
3446        if (clientIsRequestBodyTooLargeForPolicy(bodyPipe->producedSize()))
3447            return ERR_TOO_BIG;
3448
3449        if (parsed) {
3450            finishDechunkingRequest(true);
3451            Must(!bodyPipe);
3452            return ERR_NONE; // nil bodyPipe implies body end for the caller
3453        }
3454
3455        // if chunk parser needs data, then the body pipe must need it too
3456        Must(!in.bodyParser->needsMoreData() || bodyPipe->mayNeedMoreData());
3457
3458        // if parser needs more space and we can consume nothing, we will stall
3459        Must(!in.bodyParser->needsMoreSpace() || bodyPipe->buf().hasContent());
3460    } catch (...) { // TODO: be more specific
3461        debugs(33, 3, HERE << "malformed chunks" << bodyPipe->status());
3462        return ERR_INVALID_REQ;
3463    }
3464
3465    debugs(33, 7, HERE << "need more chunked data" << *bodyPipe->status());
3466    return ERR_NONE;
3467}
3468
3469/// quit on errors related to chunked request body handling
3470void
3471ConnStateData::abortChunkedRequestBody(const err_type error)
3472{
3473    finishDechunkingRequest(false);
3474
3475    // XXX: The code below works if we fail during initial request parsing,
3476    // but if we fail when the server connection is used already, the server may send
3477    // us its response too, causing various assertions. How to prevent that?
3478#if WE_KNOW_HOW_TO_SEND_ERRORS
3479    ClientSocketContext::Pointer context = getCurrentContext();
3480    if (context != NULL && !context->http->out.offset) { // output nothing yet
3481        clientStreamNode *node = context->getClientReplyContext();
3482        clientReplyContext *repContext = dynamic_cast<clientReplyContext*>(node->data.getRaw());
3483        assert(repContext);
3484        const Http::StatusCode scode = (error == ERR_TOO_BIG) ?
3485                                       Http::scPayloadTooLarge : HTTP_BAD_REQUEST;
3486        repContext->setReplyToError(error, scode,
3487                                    repContext->http->request->method,
3488                                    repContext->http->uri,
3489                                    CachePeer,
3490                                    repContext->http->request,
3491                                    in.buf, NULL);
3492        context->pullData();
3493    } else {
3494        // close or otherwise we may get stuck as nobody will notice the error?
3495        comm_reset_close(clientConnection);
3496    }
3497#else
3498    debugs(33, 3, HERE << "aborting chunked request without error " << error);
3499    comm_reset_close(clientConnection);
3500#endif
3501    flags.readMore = false;
3502}
3503
3504void
3505ConnStateData::noteBodyConsumerAborted(BodyPipe::Pointer )
3506{
3507    // request reader may get stuck waiting for space if nobody consumes body
3508    if (bodyPipe != NULL)
3509        bodyPipe->enableAutoConsumption();
3510
3511    // kids extend
3512}
3513
3514/** general lifetime handler for HTTP requests */
3515void
3516ConnStateData::requestTimeout(const CommTimeoutCbParams &io)
3517{
3518    /*
3519    * Just close the connection to not confuse browsers
3520    * using persistent connections. Some browsers open
3521    * a connection and then do not use it until much
3522    * later (presumeably because the request triggering
3523    * the open has already been completed on another
3524    * connection)
3525    */
3526    debugs(33, 3, "requestTimeout: FD " << io.fd << ": lifetime is expired.");
3527    io.conn->close();
3528}
3529
3530static void
3531clientLifetimeTimeout(const CommTimeoutCbParams &io)
3532{
3533    ClientHttpRequest *http = static_cast<ClientHttpRequest *>(io.data);
3534    debugs(33, DBG_IMPORTANT, "WARNING: Closing client connection due to lifetime timeout");
3535    debugs(33, DBG_IMPORTANT, "\t" << http->uri);
3536    http->al->http.timedout = true;
3537    if (Comm::IsConnOpen(io.conn))
3538        io.conn->close();
3539}
3540
3541ConnStateData::ConnStateData(const MasterXaction::Pointer &xact) :
3542    AsyncJob("ConnStateData"), // kids overwrite
3543    nrequests(0),
3544#if USE_OPENSSL
3545    sslBumpMode(Ssl::bumpEnd),
3546#endif
3547    needProxyProtocolHeader_(false),
3548#if USE_OPENSSL
3549    switchedToHttps_(false),
3550    sslServerBump(NULL),
3551    signAlgorithm(Ssl::algSignTrusted),
3552#endif
3553    stoppedSending_(NULL),
3554    stoppedReceiving_(NULL)
3555{
3556    flags.readMore = true; // kids may overwrite
3557    flags.swanSang = false;
3558
3559    pinning.host = NULL;
3560    pinning.port = -1;
3561    pinning.pinned = false;
3562    pinning.auth = false;
3563    pinning.zeroReply = false;
3564    pinning.peer = NULL;
3565
3566    // store the details required for creating more MasterXaction objects as new requests come in
3567    clientConnection = xact->tcpClient;
3568    port = xact->squidPort;
3569    log_addr = xact->tcpClient->remote;
3570    log_addr.applyMask(Config.Addrs.client_netmask);
3571
3572    // register to receive notice of Squid signal events
3573    // which may affect long persisting client connections
3574    RegisterRunner(this);
3575}
3576
3577void
3578ConnStateData::start()
3579{
3580    BodyProducer::start();
3581    HttpControlMsgSink::start();
3582
3583    if (port->disable_pmtu_discovery != DISABLE_PMTU_OFF &&
3584            (transparent() || port->disable_pmtu_discovery == DISABLE_PMTU_ALWAYS)) {
3585#if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)
3586        int i = IP_PMTUDISC_DONT;
3587        if (setsockopt(clientConnection->fd, SOL_IP, IP_MTU_DISCOVER, &i, sizeof(i)) < 0)
3588            debugs(33, 2, "WARNING: Path MTU discovery disabling failed on " << clientConnection << " : " << xstrerror());
3589#else
3590        static bool reported = false;
3591
3592        if (!reported) {
3593            debugs(33, DBG_IMPORTANT, "NOTICE: Path MTU discovery disabling is not supported on your platform.");
3594            reported = true;
3595        }
3596#endif
3597    }
3598
3599    typedef CommCbMemFunT<ConnStateData, CommCloseCbParams> Dialer;
3600    AsyncCall::Pointer call = JobCallback(33, 5, Dialer, this, ConnStateData::connStateClosed);
3601    comm_add_close_handler(clientConnection->fd, call);
3602
3603    if (Config.onoff.log_fqdn)
3604        fqdncache_gethostbyaddr(clientConnection->remote, FQDN_LOOKUP_IF_MISS);
3605
3606#if USE_IDENT
3607    if (Ident::TheConfig.identLookup) {
3608        ACLFilledChecklist identChecklist(Ident::TheConfig.identLookup, NULL, NULL);
3609        identChecklist.src_addr = clientConnection->remote;
3610        identChecklist.my_addr = clientConnection->local;
3611        if (identChecklist.fastCheck() == ACCESS_ALLOWED)
3612            Ident::Start(clientConnection, clientIdentDone, this);
3613    }
3614#endif
3615
3616    clientdbEstablished(clientConnection->remote, 1);
3617
3618    needProxyProtocolHeader_ = port->flags.proxySurrogate;
3619    if (needProxyProtocolHeader_) {
3620        if (!proxyProtocolValidateClient()) // will close the connection on failure
3621            return;
3622    }
3623
3624#if USE_DELAY_POOLS
3625    fd_table[clientConnection->fd].clientInfo = NULL;
3626
3627    if (Config.onoff.client_db) {
3628        /* it was said several times that client write limiter does not work if client_db is disabled */
3629
3630        ClientDelayPools& pools(Config.ClientDelay.pools);
3631        ACLFilledChecklist ch(NULL, NULL, NULL);
3632
3633        // TODO: we check early to limit error response bandwith but we
3634        // should recheck when we can honor delay_pool_uses_indirect
3635        // TODO: we should also pass the port details for myportname here.
3636        ch.src_addr = clientConnection->remote;
3637        ch.my_addr = clientConnection->local;
3638
3639        for (unsigned int pool = 0; pool < pools.size(); ++pool) {
3640
3641            /* pools require explicit 'allow' to assign a client into them */
3642            if (pools[pool].access) {
3643                cbdataReferenceDone(ch.accessList);
3644                ch.accessList = cbdataReference(pools[pool].access);
3645                allow_t answer = ch.fastCheck();
3646                if (answer == ACCESS_ALLOWED) {
3647
3648                    /*  request client information from db after we did all checks
3649                        this will save hash lookup if client failed checks */
3650                    ClientInfo * cli = clientdbGetInfo(clientConnection->remote);
3651                    assert(cli);
3652
3653                    /* put client info in FDE */
3654                    fd_table[clientConnection->fd].clientInfo = cli;
3655
3656                    /* setup write limiter for this request */
3657                    const double burst = floor(0.5 +
3658                                               (pools[pool].highwatermark * Config.ClientDelay.initial)/100.0);
3659                    cli->setWriteLimiter(pools[pool].rate, burst, pools[pool].highwatermark);
3660                    break;
3661                } else {
3662                    debugs(83, 4, HERE << "Delay pool " << pool << " skipped because ACL " << answer);
3663                }
3664            }
3665        }
3666    }
3667#endif
3668
3669    // kids must extend to actually start doing something (e.g., reading)
3670}
3671
3672/** Handle a new connection on an HTTP socket. */
3673void
3674httpAccept(const CommAcceptCbParams &params)
3675{
3676    MasterXaction::Pointer xact = params.xaction;
3677    AnyP::PortCfgPointer s = xact->squidPort;
3678
3679    // NP: it is possible the port was reconfigured when the call or accept() was queued.
3680
3681    if (params.flag != Comm::OK) {
3682        // Its possible the call was still queued when the client disconnected
3683        debugs(33, 2, s->listenConn << ": accept failure: " << xstrerr(params.xerrno));
3684        return;
3685    }
3686
3687    debugs(33, 4, params.conn << ": accepted");
3688    fd_note(params.conn->fd, "client http connect");
3689
3690    if (s->tcp_keepalive.enabled)
3691        commSetTcpKeepalive(params.conn->fd, s->tcp_keepalive.idle, s->tcp_keepalive.interval, s->tcp_keepalive.timeout);
3692
3693    ++incoming_sockets_accepted;
3694
3695    // Socket is ready, setup the connection manager to start using it
3696    ConnStateData *connState = Http::NewServer(xact);
3697    AsyncJob::Start(connState); // usually async-calls readSomeData()
3698}
3699
3700#if USE_OPENSSL
3701
3702/** Create SSL connection structure and update fd_table */
3703static SSL *
3704httpsCreate(const Comm::ConnectionPointer &conn, SSL_CTX *sslContext)
3705{
3706    if (SSL *ssl = Ssl::CreateServer(sslContext, conn->fd, "client https start")) {
3707        debugs(33, 5, "will negotate SSL on " << conn);
3708        return ssl;
3709    }
3710
3711    conn->close();
3712    return NULL;
3713}
3714
3715static bool
3716Squid_SSL_accept(ConnStateData *conn, PF *callback)
3717{
3718    int fd = conn->clientConnection->fd;
3719    SSL *ssl = fd_table[fd].ssl;
3720    int ret;
3721
3722    errno = 0;
3723    if ((ret = SSL_accept(ssl)) <= 0) {
3724        int xerrno = errno;
3725        int ssl_error = SSL_get_error(ssl, ret);
3726
3727        switch (ssl_error) {
3728
3729        case SSL_ERROR_WANT_READ:
3730            Comm::SetSelect(fd, COMM_SELECT_READ, callback, conn, 0);
3731            return false;
3732
3733        case SSL_ERROR_WANT_WRITE:
3734            Comm::SetSelect(fd, COMM_SELECT_WRITE, callback, conn, 0);
3735            return false;
3736
3737        case SSL_ERROR_SYSCALL:
3738            if (ret == 0) {
3739                debugs(83, 2, "Error negotiating SSL connection on FD " << fd << ": Aborted by client: " << ssl_error);
3740            } else {
3741                debugs(83, (xerrno == ECONNRESET) ? 1 : 2, "Error negotiating SSL connection on FD " << fd << ": " <<
3742                       (xerrno == 0 ? ERR_error_string(ssl_error, NULL) : xstrerr(xerrno)));
3743            }
3744            conn->clientConnection->close();
3745            return false;
3746
3747        case SSL_ERROR_ZERO_RETURN:
3748            debugs(83, DBG_IMPORTANT, "Error negotiating SSL connection on FD " << fd << ": Closed by client");
3749            conn->clientConnection->close();
3750            return false;
3751
3752        default:
3753            debugs(83, DBG_IMPORTANT, "Error negotiating SSL connection on FD " <<
3754                   fd << ": " << ERR_error_string(ERR_get_error(), NULL) <<
3755                   " (" << ssl_error << "/" << ret << ")");
3756            conn->clientConnection->close();
3757            return false;
3758        }
3759
3760        /* NOTREACHED */
3761    }
3762    return true;
3763}
3764
3765/** negotiate an SSL connection */
3766static void
3767clientNegotiateSSL(int fd, void *data)
3768{
3769    ConnStateData *conn = (ConnStateData *)data;
3770    X509 *client_cert;
3771    SSL *ssl = fd_table[fd].ssl;
3772
3773    if (!Squid_SSL_accept(conn, clientNegotiateSSL))
3774        return;
3775
3776    if (SSL_session_reused(ssl)) {
3777        debugs(83, 2, "clientNegotiateSSL: Session " << SSL_get_session(ssl) <<
3778               " reused on FD " << fd << " (" << fd_table[fd].ipaddr << ":" << (int)fd_table[fd].remote_port << ")");
3779    } else {
3780        if (do_debug(83, 4)) {
3781            /* Write out the SSL session details.. actually the call below, but
3782             * OpenSSL headers do strange typecasts confusing GCC.. */
3783            /* PEM_write_SSL_SESSION(debug_log, SSL_get_session(ssl)); */
3784#if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x00908000L
3785            PEM_ASN1_write((i2d_of_void *)i2d_SSL_SESSION, PEM_STRING_SSL_SESSION, debug_log, (char *)SSL_get_session(ssl), NULL,NULL,0,NULL,NULL);
3786
3787#elif (ALLOW_ALWAYS_SSL_SESSION_DETAIL == 1)
3788
3789            /* When using gcc 3.3.x and OpenSSL 0.9.7x sometimes a compile error can occur here.
3790            * This is caused by an unpredicatble gcc behaviour on a cast of the first argument
3791            * of PEM_ASN1_write(). For this reason this code section is disabled. To enable it,
3792            * define ALLOW_ALWAYS_SSL_SESSION_DETAIL=1.
3793            * Because there are two possible usable cast, if you get an error here, try the other
3794            * commented line. */
3795
3796            PEM_ASN1_write((int(*)())i2d_SSL_SESSION, PEM_STRING_SSL_SESSION, debug_log, (char *)SSL_get_session(ssl), NULL,NULL,0,NULL,NULL);
3797            /* PEM_ASN1_write((int(*)(...))i2d_SSL_SESSION, PEM_STRING_SSL_SESSION, debug_log, (char *)SSL_get_session(ssl), NULL,NULL,0,NULL,NULL); */
3798
3799#else
3800
3801            debugs(83, 4, "With " OPENSSL_VERSION_TEXT ", session details are available only defining ALLOW_ALWAYS_SSL_SESSION_DETAIL=1 in the source." );
3802
3803#endif
3804            /* Note: This does not automatically fflush the log file.. */
3805        }
3806
3807        debugs(83, 2, "clientNegotiateSSL: New session " <<
3808               SSL_get_session(ssl) << " on FD " << fd << " (" <<
3809               fd_table[fd].ipaddr << ":" << (int)fd_table[fd].remote_port <<
3810               ")");
3811    }
3812
3813    debugs(83, 3, "clientNegotiateSSL: FD " << fd << " negotiated cipher " <<
3814           SSL_get_cipher(ssl));
3815
3816    client_cert = SSL_get_peer_certificate(ssl);
3817
3818    if (client_cert != NULL) {
3819        debugs(83, 3, "clientNegotiateSSL: FD " << fd <<
3820               " client certificate: subject: " <<
3821               X509_NAME_oneline(X509_get_subject_name(client_cert), 0, 0));
3822
3823        debugs(83, 3, "clientNegotiateSSL: FD " << fd <<
3824               " client certificate: issuer: " <<
3825               X509_NAME_oneline(X509_get_issuer_name(client_cert), 0, 0));
3826
3827        X509_free(client_cert);
3828    } else {
3829        debugs(83, 5, "clientNegotiateSSL: FD " << fd <<
3830               " has no certificate.");
3831    }
3832
3833#if defined(TLSEXT_NAMETYPE_host_name)
3834    if (!conn->serverBump()) {
3835        // when in bumpClientFirst mode, get the server name from SNI
3836        if (const char *server = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name))
3837            conn->resetSslCommonName(server);
3838    }
3839#endif
3840
3841    conn->readSomeData();
3842}
3843
3844/**
3845 * If SSL_CTX is given, starts reading the SSL handshake.
3846 * Otherwise, calls switchToHttps to generate a dynamic SSL_CTX.
3847 */
3848static void
3849httpsEstablish(ConnStateData *connState,  SSL_CTX *sslContext, Ssl::BumpMode bumpMode)
3850{
3851    SSL *ssl = NULL;
3852    assert(connState);
3853    const Comm::ConnectionPointer &details = connState->clientConnection;
3854
3855    if (sslContext && !(ssl = httpsCreate(details, sslContext)))
3856        return;
3857
3858    typedef CommCbMemFunT<ConnStateData, CommTimeoutCbParams> TimeoutDialer;
3859    AsyncCall::Pointer timeoutCall = JobCallback(33, 5, TimeoutDialer,
3860                                     connState, ConnStateData::requestTimeout);
3861    commSetConnTimeout(details, Config.Timeout.request, timeoutCall);
3862
3863    if (ssl)
3864        Comm::SetSelect(details->fd, COMM_SELECT_READ, clientNegotiateSSL, connState, 0);
3865    else {
3866        char buf[MAX_IPSTRLEN];
3867        assert(bumpMode != Ssl::bumpNone && bumpMode != Ssl::bumpEnd);
3868        HttpRequest::Pointer fakeRequest(new HttpRequest);
3869        fakeRequest->SetHost(details->local.toStr(buf, sizeof(buf)));
3870        fakeRequest->port = details->local.port();
3871        fakeRequest->clientConnectionManager = connState;
3872        fakeRequest->client_addr = connState->clientConnection->remote;
3873#if FOLLOW_X_FORWARDED_FOR
3874        fakeRequest->indirect_client_addr = connState->clientConnection->remote;
3875#endif
3876        fakeRequest->my_addr = connState->clientConnection->local;
3877        fakeRequest->flags.interceptTproxy = ((connState->clientConnection->flags & COMM_TRANSPARENT) != 0 ) ;
3878        fakeRequest->flags.intercepted = ((connState->clientConnection->flags & COMM_INTERCEPTION) != 0);
3879        fakeRequest->myportname = connState->port->name;
3880        if (fakeRequest->flags.interceptTproxy) {
3881            if (Config.accessList.spoof_client_ip) {
3882                ACLFilledChecklist checklist(Config.accessList.spoof_client_ip, fakeRequest.getRaw(), NULL);
3883                fakeRequest->flags.spoofClientIp = (checklist.fastCheck() == ACCESS_ALLOWED);
3884            } else
3885                fakeRequest->flags.spoofClientIp = true;
3886        } else
3887            fakeRequest->flags.spoofClientIp = false;
3888        debugs(33, 4, HERE << details << " try to generate a Dynamic SSL CTX");
3889        connState->switchToHttps(fakeRequest.getRaw(), bumpMode);
3890    }
3891}
3892
3893/**
3894 * A callback function to use with the ACLFilledChecklist callback.
3895 * In the case of ACCESS_ALLOWED answer initializes a bumped SSL connection,
3896 * else reverts the connection to tunnel mode.
3897 */
3898static void
3899httpsSslBumpAccessCheckDone(allow_t answer, void *data)
3900{
3901    ConnStateData *connState = (ConnStateData *) data;
3902
3903    // if the connection is closed or closing, just return.
3904    if (!connState->isOpen())
3905        return;
3906
3907    // Require both a match and a positive bump mode to work around exceptional
3908    // cases where ACL code may return ACCESS_ALLOWED with zero answer.kind.
3909    if (answer == ACCESS_ALLOWED && (answer.kind != Ssl::bumpNone && answer.kind != Ssl::bumpSplice)) {
3910        debugs(33, 2, "sslBump needed for " << connState->clientConnection << " method " << answer.kind);
3911        connState->sslBumpMode = static_cast<Ssl::BumpMode>(answer.kind);
3912    } else {
3913        debugs(33, 2, HERE << "sslBump not needed for " << connState->clientConnection);
3914        connState->sslBumpMode = Ssl::bumpNone;
3915    }
3916    connState->fakeAConnectRequest("ssl-bump", connState->in.buf);
3917}
3918
3919/** handle a new HTTPS connection */
3920static void
3921httpsAccept(const CommAcceptCbParams &params)
3922{
3923    MasterXaction::Pointer xact = params.xaction;
3924    const AnyP::PortCfgPointer s = xact->squidPort;
3925
3926    // NP: it is possible the port was reconfigured when the call or accept() was queued.
3927
3928    if (params.flag != Comm::OK) {
3929        // Its possible the call was still queued when the client disconnected
3930        debugs(33, 2, "httpsAccept: " << s->listenConn << ": accept failure: " << xstrerr(params.xerrno));
3931        return;
3932    }
3933
3934    debugs(33, 4, HERE << params.conn << " accepted, starting SSL negotiation.");
3935    fd_note(params.conn->fd, "client https connect");
3936
3937    if (s->tcp_keepalive.enabled) {
3938        commSetTcpKeepalive(params.conn->fd, s->tcp_keepalive.idle, s->tcp_keepalive.interval, s->tcp_keepalive.timeout);
3939    }
3940
3941    ++incoming_sockets_accepted;
3942
3943    // Socket is ready, setup the connection manager to start using it
3944    ConnStateData *connState = Https::NewServer(xact);
3945    AsyncJob::Start(connState); // usually async-calls postHttpsAccept()
3946}
3947
3948void
3949ConnStateData::postHttpsAccept()
3950{
3951    if (port->flags.tunnelSslBumping) {
3952        debugs(33, 5, "accept transparent connection: " << clientConnection);
3953
3954        if (!Config.accessList.ssl_bump) {
3955            httpsSslBumpAccessCheckDone(ACCESS_DENIED, this);
3956            return;
3957        }
3958
3959        // Create a fake HTTP request for ssl_bump ACL check,
3960        // using tproxy/intercept provided destination IP and port.
3961        HttpRequest *request = new HttpRequest();
3962        static char ip[MAX_IPSTRLEN];
3963        assert(clientConnection->flags & (COMM_TRANSPARENT | COMM_INTERCEPTION));
3964        request->SetHost(clientConnection->local.toStr(ip, sizeof(ip)));
3965        request->port = clientConnection->local.port();
3966        request->myportname = port->name;
3967
3968        ACLFilledChecklist *acl_checklist = new ACLFilledChecklist(Config.accessList.ssl_bump, request, NULL);
3969        acl_checklist->src_addr = clientConnection->remote;
3970        acl_checklist->my_addr = port->s;
3971        acl_checklist->nonBlockingCheck(httpsSslBumpAccessCheckDone, this);
3972        return;
3973    } else {
3974        SSL_CTX *sslContext = port->staticSslContext.get();
3975        httpsEstablish(this, sslContext, Ssl::bumpNone);
3976    }
3977}
3978
3979void
3980ConnStateData::sslCrtdHandleReplyWrapper(void *data, const Helper::Reply &reply)
3981{
3982    ConnStateData * state_data = (ConnStateData *)(data);
3983    state_data->sslCrtdHandleReply(reply);
3984}
3985
3986void
3987ConnStateData::sslCrtdHandleReply(const Helper::Reply &reply)
3988{
3989    if (!isOpen()) {
3990        debugs(33, 3, "Connection gone while waiting for ssl_crtd helper reply; helper reply:" << reply);
3991        return;
3992    }
3993
3994    if (reply.result == Helper::BrokenHelper) {
3995        debugs(33, 5, HERE << "Certificate for " << sslConnectHostOrIp << " cannot be generated. ssl_crtd response: " << reply);
3996    } else if (!reply.other().hasContent()) {
3997        debugs(1, DBG_IMPORTANT, HERE << "\"ssl_crtd\" helper returned <NULL> reply.");
3998    } else {
3999        Ssl::CrtdMessage reply_message(Ssl::CrtdMessage::REPLY);
4000        if (reply_message.parse(reply.other().content(), reply.other().contentSize()) != Ssl::CrtdMessage::OK) {
4001            debugs(33, 5, HERE << "Reply from ssl_crtd for " << sslConnectHostOrIp << " is incorrect");
4002        } else {
4003            if (reply.result != Helper::Okay) {
4004                debugs(33, 5, HERE << "Certificate for " << sslConnectHostOrIp << " cannot be generated. ssl_crtd response: " << reply_message.getBody());
4005            } else {
4006                debugs(33, 5, HERE << "Certificate for " << sslConnectHostOrIp << " was successfully recieved from ssl_crtd");
4007                if (sslServerBump && (sslServerBump->act.step1 == Ssl::bumpPeek || sslServerBump->act.step1 == Ssl::bumpStare)) {
4008                    doPeekAndSpliceStep();
4009                    SSL *ssl = fd_table[clientConnection->fd].ssl;
4010                    bool ret = Ssl::configureSSLUsingPkeyAndCertFromMemory(ssl, reply_message.getBody().c_str(), *port);
4011                    if (!ret)
4012                        debugs(33, 5, "Failed to set certificates to ssl object for PeekAndSplice mode");
4013                } else {
4014                    SSL_CTX *ctx = Ssl::generateSslContextUsingPkeyAndCertFromMemory(reply_message.getBody().c_str(), *port);
4015                    getSslContextDone(ctx, true);
4016                }
4017                return;
4018            }
4019        }
4020    }
4021    getSslContextDone(NULL);
4022}
4023
4024void ConnStateData::buildSslCertGenerationParams(Ssl::CertificateProperties &certProperties)
4025{
4026    certProperties.commonName =  sslCommonName_.isEmpty() ? sslConnectHostOrIp.termedBuf() : sslCommonName_.c_str();
4027
4028    // fake certificate adaptation requires bump-server-first mode
4029    if (!sslServerBump) {
4030        assert(port->signingCert.get());
4031        certProperties.signWithX509.resetAndLock(port->signingCert.get());
4032        if (port->signPkey.get())
4033            certProperties.signWithPkey.resetAndLock(port->signPkey.get());
4034        certProperties.signAlgorithm = Ssl::algSignTrusted;
4035        return;
4036    }
4037
4038    // In case of an error while connecting to the secure server, use a fake
4039    // trusted certificate, with no mimicked fields and no adaptation
4040    // algorithms. There is nothing we can mimic so we want to minimize the
4041    // number of warnings the user will have to see to get to the error page.
4042    assert(sslServerBump->entry);
4043    if (sslServerBump->entry->isEmpty()) {
4044        if (X509 *mimicCert = sslServerBump->serverCert.get())
4045            certProperties.mimicCert.resetAndLock(mimicCert);
4046
4047        ACLFilledChecklist checklist(NULL, sslServerBump->request.getRaw(),
4048                                     clientConnection != NULL ? clientConnection->rfc931 : dash_str);
4049        checklist.sslErrors = cbdataReference(sslServerBump->sslErrors);
4050
4051        for (sslproxy_cert_adapt *ca = Config.ssl_client.cert_adapt; ca != NULL; ca = ca->next) {
4052            // If the algorithm already set, then ignore it.
4053            if ((ca->alg == Ssl::algSetCommonName && certProperties.setCommonName) ||
4054                    (ca->alg == Ssl::algSetValidAfter && certProperties.setValidAfter) ||
4055                    (ca->alg == Ssl::algSetValidBefore && certProperties.setValidBefore) )
4056                continue;
4057
4058            if (ca->aclList && checklist.fastCheck(ca->aclList) == ACCESS_ALLOWED) {
4059                const char *alg = Ssl::CertAdaptAlgorithmStr[ca->alg];
4060                const char *param = ca->param;
4061
4062                // For parameterless CN adaptation, use hostname from the
4063                // CONNECT request.
4064                if (ca->alg == Ssl::algSetCommonName) {
4065                    if (!param)
4066                        param = sslConnectHostOrIp.termedBuf();
4067                    certProperties.commonName = param;
4068                    certProperties.setCommonName = true;
4069                } else if (ca->alg == Ssl::algSetValidAfter)
4070                    certProperties.setValidAfter = true;
4071                else if (ca->alg == Ssl::algSetValidBefore)
4072                    certProperties.setValidBefore = true;
4073
4074                debugs(33, 5, HERE << "Matches certificate adaptation aglorithm: " <<
4075                       alg << " param: " << (param ? param : "-"));
4076            }
4077        }
4078
4079        certProperties.signAlgorithm = Ssl::algSignEnd;
4080        for (sslproxy_cert_sign *sg = Config.ssl_client.cert_sign; sg != NULL; sg = sg->next) {
4081            if (sg->aclList && checklist.fastCheck(sg->aclList) == ACCESS_ALLOWED) {
4082                certProperties.signAlgorithm = (Ssl::CertSignAlgorithm)sg->alg;
4083                break;
4084            }
4085        }
4086    } else {// if (!sslServerBump->entry->isEmpty())
4087        // Use trusted certificate for a Squid-generated error
4088        // or the user would have to add a security exception
4089        // just to see the error page. We will close the connection
4090        // so that the trust is not extended to non-Squid content.
4091        certProperties.signAlgorithm = Ssl::algSignTrusted;
4092    }
4093
4094    assert(certProperties.signAlgorithm != Ssl::algSignEnd);
4095
4096    if (certProperties.signAlgorithm == Ssl::algSignUntrusted) {
4097        assert(port->untrustedSigningCert.get());
4098        certProperties.signWithX509.resetAndLock(port->untrustedSigningCert.get());
4099        certProperties.signWithPkey.resetAndLock(port->untrustedSignPkey.get());
4100    } else {
4101        assert(port->signingCert.get());
4102        certProperties.signWithX509.resetAndLock(port->signingCert.get());
4103
4104        if (port->signPkey.get())
4105            certProperties.signWithPkey.resetAndLock(port->signPkey.get());
4106    }
4107    signAlgorithm = certProperties.signAlgorithm;
4108
4109    certProperties.signHash = Ssl::DefaultSignHash;
4110}
4111
4112void
4113ConnStateData::getSslContextStart()
4114{
4115    assert(areAllContextsForThisConnection());
4116    freeAllContexts();
4117    /* careful: freeAllContexts() above frees request, host, etc. */
4118
4119    if (port->generateHostCertificates) {
4120        Ssl::CertificateProperties certProperties;
4121        buildSslCertGenerationParams(certProperties);
4122        sslBumpCertKey = certProperties.dbKey().c_str();
4123        assert(sslBumpCertKey.size() > 0 && sslBumpCertKey[0] != '\0');
4124
4125        // Disable caching for bumpPeekAndSplice mode
4126        if (!(sslServerBump && (sslServerBump->act.step1 == Ssl::bumpPeek || sslServerBump->act.step1 == Ssl::bumpStare))) {
4127            debugs(33, 5, "Finding SSL certificate for " << sslBumpCertKey << " in cache");
4128            Ssl::LocalContextStorage * ssl_ctx_cache = Ssl::TheGlobalContextStorage.getLocalStorage(port->s);
4129            SSL_CTX * dynCtx = NULL;
4130            Ssl::SSL_CTX_Pointer *cachedCtx = ssl_ctx_cache ? ssl_ctx_cache->get(sslBumpCertKey.termedBuf()) : NULL;
4131            if (cachedCtx && (dynCtx = cachedCtx->get())) {
4132                debugs(33, 5, "SSL certificate for " << sslBumpCertKey << " found in cache");
4133                if (Ssl::verifySslCertificate(dynCtx, certProperties)) {
4134                    debugs(33, 5, "Cached SSL certificate for " << sslBumpCertKey << " is valid");
4135                    getSslContextDone(dynCtx);
4136                    return;
4137                } else {
4138                    debugs(33, 5, "Cached SSL certificate for " << sslBumpCertKey << " is out of date. Delete this certificate from cache");
4139                    if (ssl_ctx_cache)
4140                        ssl_ctx_cache->del(sslBumpCertKey.termedBuf());
4141                }
4142            } else {
4143                debugs(33, 5, "SSL certificate for " << sslBumpCertKey << " haven't found in cache");
4144            }
4145        }
4146
4147#if USE_SSL_CRTD
4148        try {
4149            debugs(33, 5, HERE << "Generating SSL certificate for " << certProperties.commonName << " using ssl_crtd.");
4150            Ssl::CrtdMessage request_message(Ssl::CrtdMessage::REQUEST);
4151            request_message.setCode(Ssl::CrtdMessage::code_new_certificate);
4152            request_message.composeRequest(certProperties);
4153            debugs(33, 5, HERE << "SSL crtd request: " << request_message.compose().c_str());
4154            Ssl::Helper::GetInstance()->sslSubmit(request_message, sslCrtdHandleReplyWrapper, this);
4155            return;
4156        } catch (const std::exception &e) {
4157            debugs(33, DBG_IMPORTANT, "ERROR: Failed to compose ssl_crtd " <<
4158                   "request for " << certProperties.commonName <<
4159                   " certificate: " << e.what() << "; will now block to " <<
4160                   "generate that certificate.");
4161            // fall through to do blocking in-process generation.
4162        }
4163#endif // USE_SSL_CRTD
4164
4165        debugs(33, 5, HERE << "Generating SSL certificate for " << certProperties.commonName);
4166        if (sslServerBump && (sslServerBump->act.step1 == Ssl::bumpPeek || sslServerBump->act.step1 == Ssl::bumpStare)) {
4167            doPeekAndSpliceStep();
4168            SSL *ssl = fd_table[clientConnection->fd].ssl;
4169            if (!Ssl::configureSSL(ssl, certProperties, *port))
4170                debugs(33, 5, "Failed to set certificates to ssl object for PeekAndSplice mode");
4171        } else {
4172            SSL_CTX *dynCtx = Ssl::generateSslContext(certProperties, *port);
4173            getSslContextDone(dynCtx, true);
4174        }
4175        return;
4176    }
4177    getSslContextDone(NULL);
4178}
4179
4180void
4181ConnStateData::getSslContextDone(SSL_CTX * sslContext, bool isNew)
4182{
4183    // Try to add generated ssl context to storage.
4184    if (port->generateHostCertificates && isNew) {
4185
4186        if (signAlgorithm == Ssl::algSignTrusted) {
4187            // Add signing certificate to the certificates chain
4188            X509 *cert = port->signingCert.get();
4189            if (SSL_CTX_add_extra_chain_cert(sslContext, cert)) {
4190                // increase the certificate lock
4191                CRYPTO_add(&(cert->references),1,CRYPTO_LOCK_X509);
4192            } else {
4193                const int ssl_error = ERR_get_error();
4194                debugs(33, DBG_IMPORTANT, "WARNING: can not add signing certificate to SSL context chain: " << ERR_error_string(ssl_error, NULL));
4195            }
4196            Ssl::addChainToSslContext(sslContext, port->certsToChain.get());
4197        }
4198        //else it is self-signed or untrusted do not attrach any certificate
4199
4200        Ssl::LocalContextStorage *ssl_ctx_cache = Ssl::TheGlobalContextStorage.getLocalStorage(port->s);
4201        assert(sslBumpCertKey.size() > 0 && sslBumpCertKey[0] != '\0');
4202        if (sslContext) {
4203            if (!ssl_ctx_cache || !ssl_ctx_cache->add(sslBumpCertKey.termedBuf(), new Ssl::SSL_CTX_Pointer(sslContext))) {
4204                // If it is not in storage delete after using. Else storage deleted it.
4205                fd_table[clientConnection->fd].dynamicSslContext = sslContext;
4206            }
4207        } else {
4208            debugs(33, 2, HERE << "Failed to generate SSL cert for " << sslConnectHostOrIp);
4209        }
4210    }
4211
4212    // If generated ssl context = NULL, try to use static ssl context.
4213    if (!sslContext) {
4214        if (!port->staticSslContext) {
4215            debugs(83, DBG_IMPORTANT, "Closing SSL " << clientConnection->remote << " as lacking SSL context");
4216            clientConnection->close();
4217            return;
4218        } else {
4219            debugs(33, 5, HERE << "Using static ssl context.");
4220            sslContext = port->staticSslContext.get();
4221        }
4222    }
4223
4224    if (!httpsCreate(clientConnection, sslContext))
4225        return;
4226
4227    // bumped intercepted conns should already have Config.Timeout.request set
4228    // but forwarded connections may only have Config.Timeout.lifetime. [Re]set
4229    // to make sure the connection does not get stuck on non-SSL clients.
4230    typedef CommCbMemFunT<ConnStateData, CommTimeoutCbParams> TimeoutDialer;
4231    AsyncCall::Pointer timeoutCall = JobCallback(33, 5, TimeoutDialer,
4232                                     this, ConnStateData::requestTimeout);
4233    commSetConnTimeout(clientConnection, Config.Timeout.request, timeoutCall);
4234
4235    // Disable the client read handler until CachePeer selection is complete
4236    Comm::SetSelect(clientConnection->fd, COMM_SELECT_READ, NULL, NULL, 0);
4237    Comm::SetSelect(clientConnection->fd, COMM_SELECT_READ, clientNegotiateSSL, this, 0);
4238    switchedToHttps_ = true;
4239}
4240
4241void
4242ConnStateData::switchToHttps(HttpRequest *request, Ssl::BumpMode bumpServerMode)
4243{
4244    assert(!switchedToHttps_);
4245
4246    sslConnectHostOrIp = request->GetHost();
4247    resetSslCommonName(request->GetHost());
4248
4249    // We are going to read new request
4250    flags.readMore = true;
4251    debugs(33, 5, HERE << "converting " << clientConnection << " to SSL");
4252
4253    // If sslServerBump is set, then we have decided to deny CONNECT
4254    // and now want to switch to SSL to send the error to the client
4255    // without even peeking at the origin server certificate.
4256    if (bumpServerMode == Ssl::bumpServerFirst && !sslServerBump) {
4257        request->flags.sslPeek = true;
4258        sslServerBump = new Ssl::ServerBump(request);
4259
4260        // will call httpsPeeked() with certificate and connection, eventually
4261        FwdState::fwdStart(clientConnection, sslServerBump->entry, sslServerBump->request.getRaw());
4262        return;
4263    } else if (bumpServerMode == Ssl::bumpPeek || bumpServerMode == Ssl::bumpStare) {
4264        request->flags.sslPeek = true;
4265        sslServerBump = new Ssl::ServerBump(request, NULL, bumpServerMode);
4266        startPeekAndSplice();
4267        return;
4268    }
4269
4270    // otherwise, use sslConnectHostOrIp
4271    getSslContextStart();
4272}
4273
4274/** negotiate an SSL connection */
4275static void
4276clientPeekAndSpliceSSL(int fd, void *data)
4277{
4278    ConnStateData *conn = (ConnStateData *)data;
4279    SSL *ssl = fd_table[fd].ssl;
4280
4281    debugs(83, 5, "Start peek and splice on FD " << fd);
4282
4283    if (!Squid_SSL_accept(conn, clientPeekAndSpliceSSL))
4284        debugs(83, 2, "SSL_accept failed.");
4285
4286    BIO *b = SSL_get_rbio(ssl);
4287    assert(b);
4288    Ssl::ClientBio *bio = static_cast<Ssl::ClientBio *>(b->ptr);
4289    if (bio->gotHello()) {
4290        if (conn->serverBump()) {
4291            Ssl::Bio::sslFeatures const &features = bio->getFeatures();
4292            if (!features.serverName.isEmpty()) {
4293                conn->serverBump()->clientSni = features.serverName;
4294                conn->resetSslCommonName(features.serverName.c_str());
4295            }
4296        }
4297
4298        debugs(83, 5, "I got hello. Start forwarding the request!!! ");
4299        Comm::SetSelect(fd, COMM_SELECT_READ, NULL, NULL, 0);
4300        Comm::SetSelect(fd, COMM_SELECT_WRITE, NULL, NULL, 0);
4301        conn->startPeekAndSpliceDone();
4302        return;
4303    }
4304}
4305
4306void ConnStateData::startPeekAndSplice()
4307{
4308    // will call httpsPeeked() with certificate and connection, eventually
4309    SSL_CTX *unConfiguredCTX = Ssl::createSSLContext(port->signingCert, port->signPkey, *port);
4310    fd_table[clientConnection->fd].dynamicSslContext = unConfiguredCTX;
4311
4312    if (!httpsCreate(clientConnection, unConfiguredCTX))
4313        return;
4314
4315    // commSetConnTimeout() was called for this request before we switched.
4316
4317    // Disable the client read handler until CachePeer selection is complete
4318    Comm::SetSelect(clientConnection->fd, COMM_SELECT_READ, NULL, NULL, 0);
4319    Comm::SetSelect(clientConnection->fd, COMM_SELECT_READ, clientPeekAndSpliceSSL, this, 0);
4320    switchedToHttps_ = true;
4321
4322    SSL *ssl = fd_table[clientConnection->fd].ssl;
4323    BIO *b = SSL_get_rbio(ssl);
4324    Ssl::ClientBio *bio = static_cast<Ssl::ClientBio *>(b->ptr);
4325    bio->hold(true);
4326}
4327
4328void httpsSslBumpStep2AccessCheckDone(allow_t answer, void *data)
4329{
4330    ConnStateData *connState = (ConnStateData *) data;
4331
4332    // if the connection is closed or closing, just return.
4333    if (!connState->isOpen())
4334        return;
4335
4336    debugs(33, 5, "Answer: " << answer << " kind:" << answer.kind);
4337    assert(connState->serverBump());
4338    Ssl::BumpMode bumpAction;
4339    if (answer == ACCESS_ALLOWED) {
4340        bumpAction = (Ssl::BumpMode)answer.kind;
4341    } else
4342        bumpAction = Ssl::bumpSplice;
4343
4344    connState->serverBump()->act.step2 = bumpAction;
4345    connState->sslBumpMode = bumpAction;
4346
4347    if (bumpAction == Ssl::bumpTerminate) {
4348        connState->clientConnection->close();
4349    } else if (bumpAction != Ssl::bumpSplice) {
4350        connState->startPeekAndSpliceDone();
4351    } else {
4352        //Normally we can splice here, because we just got client hello message
4353        SSL *ssl = fd_table[connState->clientConnection->fd].ssl;
4354        BIO *b = SSL_get_rbio(ssl);
4355        Ssl::ClientBio *bio = static_cast<Ssl::ClientBio *>(b->ptr);
4356        MemBuf const &rbuf = bio->rBufData();
4357        debugs(83,5, "Bio for  " << connState->clientConnection << " read " << rbuf.contentSize() << " helo bytes");
4358        // Do splice:
4359        fd_table[connState->clientConnection->fd].read_method = &default_read_method;
4360        fd_table[connState->clientConnection->fd].write_method = &default_write_method;
4361
4362        if (connState->transparent()) {
4363            // fake a CONNECT request to force connState to tunnel
4364            // XXX: copy from MemBuf reallocates, not a regression since old code did too
4365            SBuf temp;
4366            temp.append(rbuf.content(), rbuf.contentSize());
4367            connState->fakeAConnectRequest("intercepted TLS spliced", temp);
4368        } else {
4369            // in.buf still has the "CONNECT ..." request data, reset it to SSL hello message
4370            connState->in.buf.append(rbuf.content(), rbuf.contentSize());
4371            ClientSocketContext::Pointer context = connState->getCurrentContext();
4372            ClientHttpRequest *http = context->http;
4373            tunnelStart(http, &http->out.size, &http->al->http.code, http->al);
4374        }
4375    }
4376}
4377
4378void
4379ConnStateData::startPeekAndSpliceDone()
4380{
4381    // This is the Step2 of the SSL bumping
4382    assert(sslServerBump);
4383    if (sslServerBump->step == Ssl::bumpStep1) {
4384        sslServerBump->step = Ssl::bumpStep2;
4385        // Run a accessList check to check if want to splice or continue bumping
4386
4387        ACLFilledChecklist *acl_checklist = new ACLFilledChecklist(Config.accessList.ssl_bump, sslServerBump->request.getRaw(), NULL);
4388        //acl_checklist->src_addr = params.conn->remote;
4389        //acl_checklist->my_addr = s->s;
4390        acl_checklist->banAction(allow_t(ACCESS_ALLOWED, Ssl::bumpNone));
4391        acl_checklist->banAction(allow_t(ACCESS_ALLOWED, Ssl::bumpClientFirst));
4392        acl_checklist->banAction(allow_t(ACCESS_ALLOWED, Ssl::bumpServerFirst));
4393        acl_checklist->nonBlockingCheck(httpsSslBumpStep2AccessCheckDone, this);
4394        return;
4395    }
4396
4397    FwdState::fwdStart(clientConnection, sslServerBump->entry, sslServerBump->request.getRaw());
4398}
4399
4400void
4401ConnStateData::doPeekAndSpliceStep()
4402{
4403    SSL *ssl = fd_table[clientConnection->fd].ssl;
4404    BIO *b = SSL_get_rbio(ssl);
4405    assert(b);
4406    Ssl::ClientBio *bio = static_cast<Ssl::ClientBio *>(b->ptr);
4407
4408    debugs(33, 5, "PeekAndSplice mode, proceed with client negotiation. Currrent state:" << SSL_state_string_long(ssl));
4409    bio->hold(false);
4410
4411    Comm::SetSelect(clientConnection->fd, COMM_SELECT_WRITE, clientNegotiateSSL, this, 0);
4412    switchedToHttps_ = true;
4413}
4414
4415void
4416ConnStateData::httpsPeeked(Comm::ConnectionPointer serverConnection)
4417{
4418    Must(sslServerBump != NULL);
4419
4420    if (Comm::IsConnOpen(serverConnection)) {
4421        pinConnection(serverConnection, NULL, NULL, false);
4422
4423        debugs(33, 5, HERE << "bumped HTTPS server: " << sslConnectHostOrIp);
4424    } else {
4425        debugs(33, 5, HERE << "Error while bumping: " << sslConnectHostOrIp);
4426
4427        //  copy error detail from bump-server-first request to CONNECT request
4428        if (currentobject != NULL && currentobject->http != NULL && currentobject->http->request)
4429            currentobject->http->request->detailError(sslServerBump->request->errType, sslServerBump->request->errDetail);
4430    }
4431
4432    getSslContextStart();
4433}
4434
4435#endif /* USE_OPENSSL */
4436
4437void
4438ConnStateData::fakeAConnectRequest(const char *reason, const SBuf &payload)
4439{
4440    // fake a CONNECT request to force connState to tunnel
4441    SBuf connectHost;
4442#if USE_OPENSSL
4443    if (serverBump() && !serverBump()->clientSni.isEmpty()) {
4444        connectHost.assign(serverBump()->clientSni);
4445        if (clientConnection->local.port() > 0)
4446            connectHost.appendf(":%d",clientConnection->local.port());
4447    } else
4448#endif
4449    {
4450        static char ip[MAX_IPSTRLEN];
4451        connectHost.assign(clientConnection->local.toUrl(ip, sizeof(ip)));
4452    }
4453    // Pre-pend this fake request to the TLS bits already in the buffer
4454    SBuf retStr;
4455    retStr.append("CONNECT ");
4456    retStr.append(connectHost);
4457    retStr.append(" HTTP/1.1\r\nHost: ");
4458    retStr.append(connectHost);
4459    retStr.append("\r\n\r\n");
4460    retStr.append(payload);
4461    in.buf = retStr;
4462    bool ret = handleReadData();
4463    if (ret)
4464        ret = clientParseRequests();
4465
4466    if (!ret) {
4467        debugs(33, 2, "Failed to start fake CONNECT request for " << reason << " connection: " << clientConnection);
4468        clientConnection->close();
4469    }
4470}
4471
4472/// check FD after clientHttp[s]ConnectionOpened, adjust HttpSockets as needed
4473static bool
4474OpenedHttpSocket(const Comm::ConnectionPointer &c, const Ipc::FdNoteId portType)
4475{
4476    if (!Comm::IsConnOpen(c)) {
4477        Must(NHttpSockets > 0); // we tried to open some
4478        --NHttpSockets; // there will be fewer sockets than planned
4479        Must(HttpSockets[NHttpSockets] < 0); // no extra fds received
4480
4481        if (!NHttpSockets) // we could not open any listen sockets at all
4482            fatalf("Unable to open %s",FdNote(portType));
4483
4484        return false;
4485    }
4486    return true;
4487}
4488
4489/// find any unused HttpSockets[] slot and store fd there or return false
4490static bool
4491AddOpenedHttpSocket(const Comm::ConnectionPointer &conn)
4492{
4493    bool found = false;
4494    for (int i = 0; i < NHttpSockets && !found; ++i) {
4495        if ((found = HttpSockets[i] < 0))
4496            HttpSockets[i] = conn->fd;
4497    }
4498    return found;
4499}
4500
4501static void
4502clientHttpConnectionsOpen(void)
4503{
4504    for (AnyP::PortCfgPointer s = HttpPortList; s != NULL; s = s->next) {
4505        if (MAXTCPLISTENPORTS == NHttpSockets) {
4506            debugs(1, DBG_IMPORTANT, "WARNING: You have too many 'http_port' lines.");
4507            debugs(1, DBG_IMPORTANT, "         The limit is " << MAXTCPLISTENPORTS << " HTTP ports.");
4508            continue;
4509        }
4510
4511#if USE_OPENSSL
4512        if (s->flags.tunnelSslBumping && !Config.accessList.ssl_bump) {
4513            debugs(33, DBG_IMPORTANT, "WARNING: No ssl_bump configured. Disabling ssl-bump on " << AnyP::UriScheme(s->transport.protocol) << "_port " << s->s);
4514            s->flags.tunnelSslBumping = false;
4515        }
4516
4517        if (s->flags.tunnelSslBumping &&
4518                !s->staticSslContext &&
4519                !s->generateHostCertificates) {
4520            debugs(1, DBG_IMPORTANT, "Will not bump SSL at http_port " << s->s << " due to SSL initialization failure.");
4521            s->flags.tunnelSslBumping = false;
4522        }
4523        if (s->flags.tunnelSslBumping) {
4524            // Create ssl_ctx cache for this port.
4525            Ssl::TheGlobalContextStorage.addLocalStorage(s->s, s->dynamicCertMemCacheSize == std::numeric_limits<size_t>::max() ? 4194304 : s->dynamicCertMemCacheSize);
4526        }
4527#endif
4528
4529        // Fill out a Comm::Connection which IPC will open as a listener for us
4530        //  then pass back when active so we can start a TcpAcceptor subscription.
4531        s->listenConn = new Comm::Connection;
4532        s->listenConn->local = s->s;
4533        s->listenConn->flags = COMM_NONBLOCKING | (s->flags.tproxyIntercept ? COMM_TRANSPARENT : 0) | (s->flags.natIntercept ? COMM_INTERCEPTION : 0);
4534
4535        // setup the subscriptions such that new connections accepted by listenConn are handled by HTTP
4536        typedef CommCbFunPtrCallT<CommAcceptCbPtrFun> AcceptCall;
4537        RefCount<AcceptCall> subCall = commCbCall(5, 5, "httpAccept", CommAcceptCbPtrFun(httpAccept, CommAcceptCbParams(NULL)));
4538        Subscription::Pointer sub = new CallSubscription<AcceptCall>(subCall);
4539
4540        AsyncCall::Pointer listenCall = asyncCall(33,2, "clientListenerConnectionOpened",
4541                                        ListeningStartedDialer(&clientListenerConnectionOpened, s, Ipc::fdnHttpSocket, sub));
4542        Ipc::StartListening(SOCK_STREAM, IPPROTO_TCP, s->listenConn, Ipc::fdnHttpSocket, listenCall);
4543
4544        HttpSockets[NHttpSockets] = -1; // set in clientListenerConnectionOpened
4545        ++NHttpSockets;
4546    }
4547}
4548
4549#if USE_OPENSSL
4550static void
4551clientHttpsConnectionsOpen(void)
4552{
4553    for (AnyP::PortCfgPointer s = HttpsPortList; s != NULL; s = s->next) {
4554        if (MAXTCPLISTENPORTS == NHttpSockets) {
4555            debugs(1, DBG_IMPORTANT, "Ignoring 'https_port' lines exceeding the limit.");
4556            debugs(1, DBG_IMPORTANT, "The limit is " << MAXTCPLISTENPORTS << " HTTPS ports.");
4557            continue;
4558        }
4559
4560        if (!s->staticSslContext) {
4561            debugs(1, DBG_IMPORTANT, "Ignoring https_port " << s->s <<
4562                   " due to SSL initialization failure.");
4563            continue;
4564        }
4565
4566        // TODO: merge with similar code in clientHttpConnectionsOpen()
4567        if (s->flags.tunnelSslBumping && !Config.accessList.ssl_bump) {
4568            debugs(33, DBG_IMPORTANT, "WARNING: No ssl_bump configured. Disabling ssl-bump on " << AnyP::UriScheme(s->transport.protocol) << "_port " << s->s);
4569            s->flags.tunnelSslBumping = false;
4570        }
4571
4572        if (s->flags.tunnelSslBumping && !s->staticSslContext && !s->generateHostCertificates) {
4573            debugs(1, DBG_IMPORTANT, "Will not bump SSL at https_port " << s->s << " due to SSL initialization failure.");
4574            s->flags.tunnelSslBumping = false;
4575        }
4576
4577        if (s->flags.tunnelSslBumping) {
4578            // Create ssl_ctx cache for this port.
4579            Ssl::TheGlobalContextStorage.addLocalStorage(s->s, s->dynamicCertMemCacheSize == std::numeric_limits<size_t>::max() ? 4194304 : s->dynamicCertMemCacheSize);
4580        }
4581
4582        // Fill out a Comm::Connection which IPC will open as a listener for us
4583        s->listenConn = new Comm::Connection;
4584        s->listenConn->local = s->s;
4585        s->listenConn->flags = COMM_NONBLOCKING | (s->flags.tproxyIntercept ? COMM_TRANSPARENT : 0) |
4586                               (s->flags.natIntercept ? COMM_INTERCEPTION : 0);
4587
4588        // setup the subscriptions such that new connections accepted by listenConn are handled by HTTPS
4589        typedef CommCbFunPtrCallT<CommAcceptCbPtrFun> AcceptCall;
4590        RefCount<AcceptCall> subCall = commCbCall(5, 5, "httpsAccept", CommAcceptCbPtrFun(httpsAccept, CommAcceptCbParams(NULL)));
4591        Subscription::Pointer sub = new CallSubscription<AcceptCall>(subCall);
4592
4593        AsyncCall::Pointer listenCall = asyncCall(33, 2, "clientListenerConnectionOpened",
4594                                        ListeningStartedDialer(&clientListenerConnectionOpened,
4595                                                s, Ipc::fdnHttpsSocket, sub));
4596        Ipc::StartListening(SOCK_STREAM, IPPROTO_TCP, s->listenConn, Ipc::fdnHttpsSocket, listenCall);
4597        HttpSockets[NHttpSockets] = -1;
4598        ++NHttpSockets;
4599    }
4600}
4601#endif
4602
4603void
4604clientStartListeningOn(AnyP::PortCfgPointer &port, const RefCount< CommCbFunPtrCallT<CommAcceptCbPtrFun> > &subCall, const Ipc::FdNoteId fdNote)
4605{
4606    // Fill out a Comm::Connection which IPC will open as a listener for us
4607    port->listenConn = new Comm::Connection;
4608    port->listenConn->local = port->s;
4609    port->listenConn->flags =
4610        COMM_NONBLOCKING |
4611        (port->flags.tproxyIntercept ? COMM_TRANSPARENT : 0) |
4612        (port->flags.natIntercept ? COMM_INTERCEPTION : 0);
4613
4614    // route new connections to subCall
4615    typedef CommCbFunPtrCallT<CommAcceptCbPtrFun> AcceptCall;
4616    Subscription::Pointer sub = new CallSubscription<AcceptCall>(subCall);
4617    AsyncCall::Pointer listenCall =
4618        asyncCall(33, 2, "clientListenerConnectionOpened",
4619                  ListeningStartedDialer(&clientListenerConnectionOpened,
4620                                         port, fdNote, sub));
4621    Ipc::StartListening(SOCK_STREAM, IPPROTO_TCP, port->listenConn, fdNote, listenCall);
4622
4623    assert(NHttpSockets < MAXTCPLISTENPORTS);
4624    HttpSockets[NHttpSockets] = -1;
4625    ++NHttpSockets;
4626}
4627
4628/// process clientHttpConnectionsOpen result
4629static void
4630clientListenerConnectionOpened(AnyP::PortCfgPointer &s, const Ipc::FdNoteId portTypeNote, const Subscription::Pointer &sub)
4631{
4632    Must(s != NULL);
4633
4634    if (!OpenedHttpSocket(s->listenConn, portTypeNote))
4635        return;
4636
4637    Must(Comm::IsConnOpen(s->listenConn));
4638
4639    // TCP: setup a job to handle accept() with subscribed handler
4640    AsyncJob::Start(new Comm::TcpAcceptor(s, FdNote(portTypeNote), sub));
4641
4642    debugs(1, DBG_IMPORTANT, "Accepting " <<
4643           (s->flags.natIntercept ? "NAT intercepted " : "") <<
4644           (s->flags.tproxyIntercept ? "TPROXY intercepted " : "") <<
4645           (s->flags.tunnelSslBumping ? "SSL bumped " : "") <<
4646           (s->flags.accelSurrogate ? "reverse-proxy " : "")
4647           << FdNote(portTypeNote) << " connections at "
4648           << s->listenConn);
4649
4650    Must(AddOpenedHttpSocket(s->listenConn)); // otherwise, we have received a fd we did not ask for
4651}
4652
4653void
4654clientOpenListenSockets(void)
4655{
4656    clientHttpConnectionsOpen();
4657#if USE_OPENSSL
4658    clientHttpsConnectionsOpen();
4659#endif
4660    Ftp::StartListening();
4661
4662    if (NHttpSockets < 1)
4663        fatal("No HTTP, HTTPS, or FTP ports configured");
4664}
4665
4666void
4667clientConnectionsClose()
4668{
4669    for (AnyP::PortCfgPointer s = HttpPortList; s != NULL; s = s->next) {
4670        if (s->listenConn != NULL) {
4671            debugs(1, DBG_IMPORTANT, "Closing HTTP port " << s->listenConn->local);
4672            s->listenConn->close();
4673            s->listenConn = NULL;
4674        }
4675    }
4676
4677#if USE_OPENSSL
4678    for (AnyP::PortCfgPointer s = HttpsPortList; s != NULL; s = s->next) {
4679        if (s->listenConn != NULL) {
4680            debugs(1, DBG_IMPORTANT, "Closing HTTPS port " << s->listenConn->local);
4681            s->listenConn->close();
4682            s->listenConn = NULL;
4683        }
4684    }
4685#endif
4686
4687    Ftp::StopListening();
4688
4689    // TODO see if we can drop HttpSockets array entirely */
4690    for (int i = 0; i < NHttpSockets; ++i) {
4691        HttpSockets[i] = -1;
4692    }
4693
4694    NHttpSockets = 0;
4695}
4696
4697int
4698varyEvaluateMatch(StoreEntry * entry, HttpRequest * request)
4699{
4700    const char *vary = request->vary_headers;
4701    int has_vary = entry->getReply()->header.has(HDR_VARY);
4702#if X_ACCELERATOR_VARY
4703
4704    has_vary |=
4705        entry->getReply()->header.has(HDR_X_ACCELERATOR_VARY);
4706#endif
4707
4708    if (!has_vary || !entry->mem_obj->vary_headers) {
4709        if (vary) {
4710            /* Oops... something odd is going on here.. */
4711            debugs(33, DBG_IMPORTANT, "varyEvaluateMatch: Oops. Not a Vary object on second attempt, '" <<
4712                   entry->mem_obj->urlXXX() << "' '" << vary << "'");
4713            safe_free(request->vary_headers);
4714            return VARY_CANCEL;
4715        }
4716
4717        if (!has_vary) {
4718            /* This is not a varying object */
4719            return VARY_NONE;
4720        }
4721
4722        /* virtual "vary" object found. Calculate the vary key and
4723         * continue the search
4724         */
4725        vary = httpMakeVaryMark(request, entry->getReply());
4726
4727        if (vary) {
4728            request->vary_headers = xstrdup(vary);
4729            return VARY_OTHER;
4730        } else {
4731            /* Ouch.. we cannot handle this kind of variance */
4732            /* XXX This cannot really happen, but just to be complete */
4733            return VARY_CANCEL;
4734        }
4735    } else {
4736        if (!vary) {
4737            vary = httpMakeVaryMark(request, entry->getReply());
4738
4739            if (vary)
4740                request->vary_headers = xstrdup(vary);
4741        }
4742
4743        if (!vary) {
4744            /* Ouch.. we cannot handle this kind of variance */
4745            /* XXX This cannot really happen, but just to be complete */
4746            return VARY_CANCEL;
4747        } else if (strcmp(vary, entry->mem_obj->vary_headers) == 0) {
4748            return VARY_MATCH;
4749        } else {
4750            /* Oops.. we have already been here and still haven't
4751             * found the requested variant. Bail out
4752             */
4753            debugs(33, DBG_IMPORTANT, "varyEvaluateMatch: Oops. Not a Vary match on second attempt, '" <<
4754                   entry->mem_obj->urlXXX() << "' '" << vary << "'");
4755            return VARY_CANCEL;
4756        }
4757    }
4758}
4759
4760ACLFilledChecklist *
4761clientAclChecklistCreate(const acl_access * acl, ClientHttpRequest * http)
4762{
4763    ConnStateData * conn = http->getConn();
4764    ACLFilledChecklist *ch = new ACLFilledChecklist(acl, http->request,
4765            cbdataReferenceValid(conn) && conn != NULL && conn->clientConnection != NULL ? conn->clientConnection->rfc931 : dash_str);
4766    ch->al = http->al;
4767    /*
4768     * hack for ident ACL. It needs to get full addresses, and a place to store
4769     * the ident result on persistent connections...
4770     */
4771    /* connection oriented auth also needs these two lines for it's operation. */
4772    return ch;
4773}
4774
4775bool
4776ConnStateData::transparent() const
4777{
4778    return clientConnection != NULL && (clientConnection->flags & (COMM_TRANSPARENT|COMM_INTERCEPTION));
4779}
4780
4781bool
4782ConnStateData::reading() const
4783{
4784    return reader != NULL;
4785}
4786
4787void
4788ConnStateData::stopReading()
4789{
4790    if (reading()) {
4791        Comm::ReadCancel(clientConnection->fd, reader);
4792        reader = NULL;
4793    }
4794}
4795
4796BodyPipe::Pointer
4797ConnStateData::expectRequestBody(int64_t size)
4798{
4799    bodyPipe = new BodyPipe(this);
4800    if (size >= 0)
4801        bodyPipe->setBodySize(size);
4802    else
4803        startDechunkingRequest();
4804    return bodyPipe;
4805}
4806
4807int64_t
4808ConnStateData::mayNeedToReadMoreBody() const
4809{
4810    if (!bodyPipe)
4811        return 0; // request without a body or read/produced all body bytes
4812
4813    if (!bodyPipe->bodySizeKnown())
4814        return -1; // probably need to read more, but we cannot be sure
4815
4816    const int64_t needToProduce = bodyPipe->unproducedSize();
4817    const int64_t haveAvailable = static_cast<int64_t>(in.buf.length());
4818
4819    if (needToProduce <= haveAvailable)
4820        return 0; // we have read what we need (but are waiting for pipe space)
4821
4822    return needToProduce - haveAvailable;
4823}
4824
4825void
4826ConnStateData::stopReceiving(const char *error)
4827{
4828    debugs(33, 4, HERE << "receiving error (" << clientConnection << "): " << error <<
4829           "; old sending error: " <<
4830           (stoppedSending() ? stoppedSending_ : "none"));
4831
4832    if (const char *oldError = stoppedReceiving()) {
4833        debugs(33, 3, HERE << "already stopped receiving: " << oldError);
4834        return; // nothing has changed as far as this connection is concerned
4835    }
4836
4837    stoppedReceiving_ = error;
4838
4839    if (const char *sendError = stoppedSending()) {
4840        debugs(33, 3, HERE << "closing because also stopped sending: " << sendError);
4841        clientConnection->close();
4842    }
4843}
4844
4845void
4846ConnStateData::expectNoForwarding()
4847{
4848    if (bodyPipe != NULL) {
4849        debugs(33, 4, HERE << "no consumer for virgin body " << bodyPipe->status());
4850        bodyPipe->expectNoConsumption();
4851    }
4852}
4853
4854/// initialize dechunking state
4855void
4856ConnStateData::startDechunkingRequest()
4857{
4858    Must(bodyPipe != NULL);
4859    debugs(33, 5, HERE << "start dechunking" << bodyPipe->status());
4860    assert(!in.bodyParser);
4861    in.bodyParser = new ChunkedCodingParser;
4862}
4863
4864/// put parsed content into input buffer and clean up
4865void
4866ConnStateData::finishDechunkingRequest(bool withSuccess)
4867{
4868    debugs(33, 5, HERE << "finish dechunking: " << withSuccess);
4869
4870    if (bodyPipe != NULL) {
4871        debugs(33, 7, HERE << "dechunked tail: " << bodyPipe->status());
4872        BodyPipe::Pointer myPipe = bodyPipe;
4873        stopProducingFor(bodyPipe, withSuccess); // sets bodyPipe->bodySize()
4874        Must(!bodyPipe); // we rely on it being nil after we are done with body
4875        if (withSuccess) {
4876            Must(myPipe->bodySizeKnown());
4877            ClientSocketContext::Pointer context = getCurrentContext();
4878            if (context != NULL && context->http && context->http->request)
4879                context->http->request->setContentLength(myPipe->bodySize());
4880        }
4881    }
4882
4883    delete in.bodyParser;
4884    in.bodyParser = NULL;
4885}
4886
4887ConnStateData::In::In() :
4888    bodyParser(NULL),
4889    buf()
4890{}
4891
4892ConnStateData::In::~In()
4893{
4894    delete bodyParser; // TODO: pool
4895}
4896
4897void
4898ConnStateData::sendControlMsg(HttpControlMsg msg)
4899{
4900    if (!isOpen()) {
4901        debugs(33, 3, HERE << "ignoring 1xx due to earlier closure");
4902        return;
4903    }
4904
4905    ClientSocketContext::Pointer context = getCurrentContext();
4906    if (context != NULL) {
4907        context->writeControlMsg(msg); // will call msg.cbSuccess
4908        return;
4909    }
4910
4911    debugs(33, 3, HERE << " closing due to missing context for 1xx");
4912    clientConnection->close();
4913}
4914
4915/// Our close handler called by Comm when the pinned connection is closed
4916void
4917ConnStateData::clientPinnedConnectionClosed(const CommCloseCbParams &io)
4918{
4919    // FwdState might repin a failed connection sooner than this close
4920    // callback is called for the failed connection.
4921    assert(pinning.serverConnection == io.conn);
4922    pinning.closeHandler = NULL; // Comm unregisters handlers before calling
4923    const bool sawZeroReply = pinning.zeroReply; // reset when unpinning
4924    pinning.serverConnection->noteClosure();
4925    unpinConnection(false);
4926
4927    if (sawZeroReply && clientConnection != NULL) {
4928        debugs(33, 3, "Closing client connection on pinned zero reply.");
4929        clientConnection->close();
4930    }
4931
4932}
4933
4934void
4935ConnStateData::pinConnection(const Comm::ConnectionPointer &pinServer, HttpRequest *request, CachePeer *aPeer, bool auth, bool monitor)
4936{
4937    if (!Comm::IsConnOpen(pinning.serverConnection) ||
4938            pinning.serverConnection->fd != pinServer->fd)
4939        pinNewConnection(pinServer, request, aPeer, auth);
4940
4941    if (monitor)
4942        startPinnedConnectionMonitoring();
4943}
4944
4945void
4946ConnStateData::pinNewConnection(const Comm::ConnectionPointer &pinServer, HttpRequest *request, CachePeer *aPeer, bool auth)
4947{
4948    unpinConnection(true); // closes pinned connection, if any, and resets fields
4949
4950    pinning.serverConnection = pinServer;
4951
4952    debugs(33, 3, HERE << pinning.serverConnection);
4953
4954    Must(pinning.serverConnection != NULL);
4955
4956    // when pinning an SSL bumped connection, the request may be NULL
4957    const char *pinnedHost = "[unknown]";
4958    if (request) {
4959        pinning.host = xstrdup(request->GetHost());
4960        pinning.port = request->port;
4961        pinnedHost = pinning.host;
4962    } else {
4963        pinning.port = pinServer->remote.port();
4964    }
4965    pinning.pinned = true;
4966    if (aPeer)
4967        pinning.peer = cbdataReference(aPeer);
4968    pinning.auth = auth;
4969    char stmp[MAX_IPSTRLEN];
4970    char desc[FD_DESC_SZ];
4971    snprintf(desc, FD_DESC_SZ, "%s pinned connection for %s (%d)",
4972             (auth || !aPeer) ? pinnedHost : aPeer->name,
4973             clientConnection->remote.toUrl(stmp,MAX_IPSTRLEN),
4974             clientConnection->fd);
4975    fd_note(pinning.serverConnection->fd, desc);
4976
4977    typedef CommCbMemFunT<ConnStateData, CommCloseCbParams> Dialer;
4978    pinning.closeHandler = JobCallback(33, 5,
4979                                       Dialer, this, ConnStateData::clientPinnedConnectionClosed);
4980    // remember the pinned connection so that cb does not unpin a fresher one
4981    typedef CommCloseCbParams Params;
4982    Params &params = GetCommParams<Params>(pinning.closeHandler);
4983    params.conn = pinning.serverConnection;
4984    comm_add_close_handler(pinning.serverConnection->fd, pinning.closeHandler);
4985}
4986
4987/// [re]start monitoring pinned connection for peer closures so that we can
4988/// propagate them to an _idle_ client pinned to that peer
4989void
4990ConnStateData::startPinnedConnectionMonitoring()
4991{
4992    if (pinning.readHandler != NULL)
4993        return; // already monitoring
4994
4995    typedef CommCbMemFunT<ConnStateData, CommIoCbParams> Dialer;
4996    pinning.readHandler = JobCallback(33, 3,
4997                                      Dialer, this, ConnStateData::clientPinnedConnectionRead);
4998    Comm::Read(pinning.serverConnection, pinning.readHandler);
4999}
5000
5001void
5002ConnStateData::stopPinnedConnectionMonitoring()
5003{
5004    if (pinning.readHandler != NULL) {
5005        Comm::ReadCancel(pinning.serverConnection->fd, pinning.readHandler);
5006        pinning.readHandler = NULL;
5007    }
5008}
5009
5010#if USE_OPENSSL
5011bool
5012ConnStateData::handleIdleClientPinnedTlsRead()
5013{
5014    // A ready-for-reading connection means that the TLS server either closed
5015    // the connection, sent us some unexpected HTTP data, or started TLS
5016    // renegotiations. We should close the connection except for the last case.
5017
5018    Must(pinning.serverConnection != NULL);
5019    SSL *ssl = fd_table[pinning.serverConnection->fd].ssl;
5020    if (!ssl)
5021        return false;
5022
5023    char buf[1];
5024    const int readResult = SSL_read(ssl, buf, sizeof(buf));
5025
5026    if (readResult > 0 || SSL_pending(ssl) > 0) {
5027        debugs(83, 2, pinning.serverConnection << " TLS application data read");
5028        return false;
5029    }
5030
5031    switch(const int error = SSL_get_error(ssl, readResult)) {
5032    case SSL_ERROR_WANT_WRITE:
5033        debugs(83, DBG_IMPORTANT, pinning.serverConnection << " TLS SSL_ERROR_WANT_WRITE request for idle pinned connection");
5034    // fall through to restart monitoring, for now
5035    case SSL_ERROR_NONE:
5036    case SSL_ERROR_WANT_READ:
5037        startPinnedConnectionMonitoring();
5038        return true;
5039
5040    default:
5041        debugs(83, 2, pinning.serverConnection << " TLS error: " << error);
5042        return false;
5043    }
5044
5045    // not reached
5046    return true;
5047}
5048#endif
5049
5050/// Our read handler called by Comm when the server either closes an idle pinned connection or
5051/// perhaps unexpectedly sends something on that idle (from Squid p.o.v.) connection.
5052void
5053ConnStateData::clientPinnedConnectionRead(const CommIoCbParams &io)
5054{
5055    pinning.readHandler = NULL; // Comm unregisters handlers before calling
5056
5057    if (io.flag == Comm::ERR_CLOSING)
5058        return; // close handler will clean up
5059
5060    Must(pinning.serverConnection == io.conn);
5061
5062#if USE_OPENSSL
5063    if (handleIdleClientPinnedTlsRead())
5064        return;
5065#endif
5066
5067    // We could use getConcurrentRequestCount(), but this may be faster.
5068    const bool clientIsIdle = !getCurrentContext();
5069
5070    debugs(33, 3, "idle pinned " << pinning.serverConnection << " read " <<
5071           io.size << (clientIsIdle ? " with idle client" : ""));
5072
5073    pinning.serverConnection->close();
5074
5075    // If we are still sending data to the client, do not close now. When we are done sending,
5076    // ClientSocketContext::keepaliveNextRequest() checks pinning.serverConnection and will close.
5077    // However, if we are idle, then we must close to inform the idle client and minimize races.
5078    if (clientIsIdle && clientConnection != NULL)
5079        clientConnection->close();
5080}
5081
5082const Comm::ConnectionPointer
5083ConnStateData::validatePinnedConnection(HttpRequest *request, const CachePeer *aPeer)
5084{
5085    debugs(33, 7, HERE << pinning.serverConnection);
5086
5087    bool valid = true;
5088    if (!Comm::IsConnOpen(pinning.serverConnection))
5089        valid = false;
5090    else if (pinning.auth && pinning.host && request && strcasecmp(pinning.host, request->GetHost()) != 0)
5091        valid = false;
5092    else if (request && pinning.port != request->port)
5093        valid = false;
5094    else if (pinning.peer && !cbdataReferenceValid(pinning.peer))
5095        valid = false;
5096    else if (aPeer != pinning.peer)
5097        valid = false;
5098
5099    if (!valid) {
5100        /* The pinning info is not safe, remove any pinning info */
5101        unpinConnection(true);
5102    }
5103
5104    return pinning.serverConnection;
5105}
5106
5107Comm::ConnectionPointer
5108ConnStateData::borrowPinnedConnection(HttpRequest *request, const CachePeer *aPeer)
5109{
5110    debugs(33, 7, pinning.serverConnection);
5111    if (validatePinnedConnection(request, aPeer) != NULL)
5112        stopPinnedConnectionMonitoring();
5113
5114    return pinning.serverConnection; // closed if validation failed
5115}
5116
5117void
5118ConnStateData::unpinConnection(const bool andClose)
5119{
5120    debugs(33, 3, HERE << pinning.serverConnection);
5121
5122    if (pinning.peer)
5123        cbdataReferenceDone(pinning.peer);
5124
5125    if (Comm::IsConnOpen(pinning.serverConnection)) {
5126        if (pinning.closeHandler != NULL) {
5127            comm_remove_close_handler(pinning.serverConnection->fd, pinning.closeHandler);
5128            pinning.closeHandler = NULL;
5129        }
5130
5131        stopPinnedConnectionMonitoring();
5132
5133        // close the server side socket if requested
5134        if (andClose)
5135            pinning.serverConnection->close();
5136        pinning.serverConnection = NULL;
5137    }
5138
5139    safe_free(pinning.host);
5140
5141    pinning.zeroReply = false;
5142
5143    /* NOTE: pinning.pinned should be kept. This combined with fd == -1 at the end of a request indicates that the host
5144     * connection has gone away */
5145}
5146
Note: See TracBrowser for help on using the repository browser.