Forwarder.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2023 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 16 Cache Manager API */
10 
11 #include "squid.h"
12 #include "AccessLogEntry.h"
13 #include "base/AsyncJobCalls.h"
14 #include "base/TextException.h"
15 #include "comm.h"
16 #include "comm/Connection.h"
17 #include "CommCalls.h"
18 #include "errorpage.h"
19 #include "globals.h"
20 #include "HttpReply.h"
21 #include "HttpRequest.h"
22 #include "ipc/Port.h"
23 #include "mgr/Forwarder.h"
24 #include "mgr/Request.h"
25 #include "Store.h"
26 
28 
30  HttpRequest* aRequest, StoreEntry* anEntry, const AccessLogEntryPointer &anAle):
31  // TODO: Add virtual Forwarder::makeRequest() to avoid prematurely creating
32  // this dummy request with a dummy ID that are finalized by Ipc::Forwarder.
33  // Same for Snmp::Forwarder.
34  Ipc::Forwarder(new Request(KidIdentifier, Ipc::RequestId(/*XXX*/), aConn, aParams), 10),
35  httpRequest(aRequest), entry(anEntry), conn(aConn), ale(anAle)
36 {
37  debugs(16, 5, conn);
39  Must(httpRequest != nullptr);
40  Must(entry != nullptr);
41 
43  entry->lock("Mgr::Forwarder");
44 
45  closer = asyncCall(16, 5, "Mgr::Forwarder::noteCommClosed",
48 }
49 
51 {
53  Must(entry);
54  entry->unlock("Mgr::Forwarder");
55  Must(httpRequest);
56  HTTPMSGUNLOCK(httpRequest);
57  });
58 }
59 
61 void
63 {
64  if (Comm::IsConnOpen(conn)) {
65  if (closer != nullptr) {
66  comm_remove_close_handler(conn->fd, closer);
67  closer = nullptr;
68  }
69  conn->close();
70  }
71  conn = nullptr;
73 }
74 
75 void
77 {
78  debugs(16, DBG_CRITICAL, "ERROR: uri " << entry->url() << " exceeds buffer size");
79  sendError(new ErrorState(ERR_INVALID_URL, Http::scUriTooLong, httpRequest, ale));
80  mustStop("long URI");
81 }
82 
83 void
85 {
86  sendError(new ErrorState(ERR_LIFETIME_EXP, Http::scRequestTimeout, httpRequest, ale));
88 }
89 
90 void
91 Mgr::Forwarder::handleException(const std::exception &e)
92 {
93  if (entry != nullptr && httpRequest != nullptr && Comm::IsConnOpen(conn))
94  sendError(new ErrorState(ERR_INVALID_RESP, Http::scInternalServerError, httpRequest, ale));
96 }
97 
99 void
101 {
102  debugs(16, 5, MYNAME);
103  closer = nullptr;
104  if (conn) {
105  conn->noteClosure();
106  conn = nullptr;
107  }
108  mustStop("commClosed");
109 }
110 
112 void
114 {
115  debugs(16, 3, MYNAME);
116  Must(error != nullptr);
117  Must(entry != nullptr);
118  Must(httpRequest != nullptr);
119 
120  entry->buffer();
121  entry->replaceHttpReply(error->BuildHttpReply());
122  entry->expires = squid_curtime;
123  delete error;
124  entry->flush();
125  entry->complete();
126 }
127 
AsyncCall::Pointer comm_add_close_handler(int fd, CLCB *handler, void *data)
Definition: comm.cc:952
void noteCommClosed(const CommCloseCbParams &params)
called when the client socket gets closed by some external force
Definition: Forwarder.cc:100
@ ERR_INVALID_URL
Definition: forward.h:45
#define DBG_CRITICAL
Definition: Stream.h:37
void swanSong() override
closes our copy of the client HTTP connection socket
Definition: Forwarder.cc:62
int KidIdentifier
void lock(const char *context)
Definition: store.cc:445
virtual void handleException(const std::exception &e)
terminate with an error
Definition: Forwarder.cc:132
void error(char *format,...)
@ ERR_INVALID_RESP
Definition: forward.h:41
bool IsConnOpen(const Comm::ConnectionPointer &conn)
Definition: Connection.cc:27
void swanSong() override
Definition: Forwarder.cc:67
void HTTPMSGUNLOCK(M *&a)
Definition: Message.h:150
void handleError() override
Definition: Forwarder.cc:76
Forwarder(const Comm::ConnectionPointer &aConn, const ActionParams &aParams, HttpRequest *aRequest, StoreEntry *anEntry, const AccessLogEntryPointer &anAle)
Definition: Forwarder.cc:29
void handleTimeout() override
Definition: Forwarder.cc:84
RefCount< AsyncCallT< Dialer > > asyncCall(int aDebugSection, int aDebugLevel, const char *aName, const Dialer &aDialer)
Definition: AsyncCall.h:156
void sendError(ErrorState *error)
send error page
Definition: Forwarder.cc:113
virtual void handleTimeout()
Definition: Forwarder.cc:125
Comm::ConnectionPointer conn
HTTP client connection descriptor.
Definition: Forwarder.h:54
cache manager request
Definition: Request.h:23
HttpRequest * httpRequest
HTTP client request for detailing errors.
Definition: Forwarder.h:52
@ scUriTooLong
Definition: StatusCode.h:59
StoreEntry * entry
Store entry expecting the response.
Definition: Forwarder.h:53
void HTTPMSGLOCK(Http::Message *a)
Definition: Message.h:161
@ scInternalServerError
Definition: StatusCode.h:73
time_t squid_curtime
Definition: stub_libtime.cc:20
AsyncCall::Pointer closer
comm_close handler for the HTTP connection
Definition: Forwarder.h:55
@ scRequestTimeout
Definition: StatusCode.h:53
@ ERR_LIFETIME_EXP
Definition: forward.h:27
~Forwarder() override
Definition: Forwarder.cc:50
Cache Manager Action parameters extracted from the user request.
Definition: ActionParams.h:23
#define Must(condition)
Definition: TextException.h:75
#define MYNAME
Definition: Stream.h:219
CBDATA_NAMESPACED_CLASS_INIT(Mgr, Forwarder)
#define SWALLOW_EXCEPTIONS(code)
Definition: TextException.h:79
void handleException(const std::exception &e) override
terminate with an error
Definition: Forwarder.cc:91
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Stream.h:192
void comm_remove_close_handler(int fd, CLCB *handler, void *data)
Definition: comm.cc:981
Definition: IpcIoFile.h:23
Cache Manager API.
Definition: Action.h:20

 

Introduction

Documentation

Support

Miscellaneous