]> git.ipfire.org Git - thirdparty/squid.git/blob - src/mgr/Inquirer.cc
Upgrade comm layer Connection handling
[thirdparty/squid.git] / src / mgr / Inquirer.cc
1 /*
2 * $Id$
3 *
4 * DEBUG: section 16 Cache Manager API
5 *
6 */
7
8 #include "config.h"
9 #include "base/TextException.h"
10 #include "comm/Connection.h"
11 #include "comm/Write.h"
12 #include "CommCalls.h"
13 #include "HttpReply.h"
14 #include "HttpRequest.h"
15 #include "ipc/UdsOp.h"
16 #include "mgr/ActionWriter.h"
17 #include "mgr/IntParam.h"
18 #include "mgr/Inquirer.h"
19 #include "mgr/Command.h"
20 #include "mgr/Request.h"
21 #include "mgr/Response.h"
22 #include "SquidTime.h"
23 #include "errorpage.h"
24 #include <memory>
25 #include <algorithm>
26
27
28 CBDATA_NAMESPACED_CLASS_INIT(Mgr, Inquirer);
29
30
31 Mgr::Inquirer::Inquirer(Action::Pointer anAction,
32 const Request &aCause, const Ipc::StrandCoords &coords):
33 Ipc::Inquirer(aCause.clone(), applyQueryParams(coords, aCause.params.queryParams), anAction->atomic() ? 10 : 100),
34 aggrAction(anAction)
35 {
36 conn = aCause.conn;
37 Ipc::ImportFdIntoComm(conn, SOCK_STREAM, IPPROTO_TCP, Ipc::fdnHttpSocket);
38
39 debugs(16, 5, HERE << conn << " action: " << aggrAction);
40
41 closer = asyncCall(16, 5, "Mgr::Inquirer::noteCommClosed",
42 CommCbMemFunT<Inquirer, CommCloseCbParams>(this, &Inquirer::noteCommClosed));
43 comm_add_close_handler(conn->fd, closer);
44 }
45
46 /// closes our copy of the client HTTP connection socket
47 void
48 Mgr::Inquirer::cleanup()
49 {
50 if (Comm::IsConnOpen(conn)) {
51 removeCloseHandler();
52 conn->close();
53 }
54 }
55
56 void
57 Mgr::Inquirer::removeCloseHandler()
58 {
59 if (closer != NULL) {
60 comm_remove_close_handler(conn->fd, closer);
61 closer = NULL;
62 }
63 }
64
65 void
66 Mgr::Inquirer::start()
67 {
68 debugs(16, 5, HERE);
69 Ipc::Inquirer::start();
70 Must(Comm::IsConnOpen(conn));
71 Must(aggrAction != NULL);
72
73 std::auto_ptr<MemBuf> replyBuf;
74 if (strands.empty()) {
75 LOCAL_ARRAY(char, url, MAX_URL);
76 snprintf(url, MAX_URL, "%s", aggrAction->command().params.httpUri.termedBuf());
77 HttpRequest *req = HttpRequest::CreateFromUrl(url);
78 ErrorState *err = errorCon(ERR_INVALID_URL, HTTP_NOT_FOUND, req);
79 std::auto_ptr<HttpReply> reply(err->BuildHttpReply());
80 replyBuf.reset(reply->pack());
81 errorStateFree(err);
82 } else {
83 std::auto_ptr<HttpReply> reply(new HttpReply);
84 reply->setHeaders(HTTP_OK, NULL, "text/plain", -1, squid_curtime, squid_curtime);
85 reply->header.putStr(HDR_CONNECTION, "close"); // until we chunk response
86 replyBuf.reset(reply->pack());
87 }
88 writer = asyncCall(16, 5, "Mgr::Inquirer::noteWroteHeader",
89 CommCbMemFunT<Inquirer, CommIoCbParams>(this, &Inquirer::noteWroteHeader));
90 Comm::Write(conn, replyBuf.get(), writer);
91 }
92
93 /// called when we wrote the response header
94 void
95 Mgr::Inquirer::noteWroteHeader(const CommIoCbParams& params)
96 {
97 debugs(16, 5, HERE);
98 writer = NULL;
99 Must(params.flag == COMM_OK);
100 Must(params.conn.getRaw() == conn.getRaw());
101 Must(params.size != 0);
102 // start inquiries at the initial pos
103 inquire();
104 }
105
106 /// called when the HTTP client or some external force closed our socket
107 void
108 Mgr::Inquirer::noteCommClosed(const CommCloseCbParams& params)
109 {
110 debugs(16, 5, HERE);
111 Must(!Comm::IsConnOpen(conn) && params.conn.getRaw() == conn.getRaw());
112 conn = NULL;
113 mustStop("commClosed");
114 }
115
116 bool
117 Mgr::Inquirer::aggregate(Ipc::Response::Pointer aResponse)
118 {
119 Mgr::Response& response = static_cast<Response&>(*aResponse);
120 if (response.hasAction())
121 aggrAction->add(response.getAction());
122 return true;
123 }
124
125 void
126 Mgr::Inquirer::sendResponse()
127 {
128 if (!strands.empty() && aggrAction->aggregatable()) {
129 removeCloseHandler();
130 AsyncJob::Start(new ActionWriter(aggrAction, conn));
131 conn = NULL; // should not close because we passed it to ActionWriter
132 }
133 }
134
135 bool
136 Mgr::Inquirer::doneAll() const
137 {
138 return !writer && Ipc::Inquirer::doneAll();
139 }
140
141 Ipc::StrandCoords
142 Mgr::Inquirer::applyQueryParams(const Ipc::StrandCoords& aStrands, const QueryParams& aParams)
143 {
144 Ipc::StrandCoords sc;
145
146 QueryParam::Pointer processesParam = aParams.get("processes");
147 QueryParam::Pointer workersParam = aParams.get("workers");
148
149 if (processesParam == NULL || workersParam == NULL) {
150 if (processesParam != NULL) {
151 IntParam* param = dynamic_cast<IntParam*>(processesParam.getRaw());
152 if (param != NULL && param->type == QueryParam::ptInt) {
153 const std::vector<int>& processes = param->value();
154 for (Ipc::StrandCoords::const_iterator iter = aStrands.begin();
155 iter != aStrands.end(); ++iter) {
156 if (std::find(processes.begin(), processes.end(), iter->kidId) != processes.end())
157 sc.push_back(*iter);
158 }
159 }
160 } else if (workersParam != NULL) {
161 IntParam* param = dynamic_cast<IntParam*>(workersParam.getRaw());
162 if (param != NULL && param->type == QueryParam::ptInt) {
163 const std::vector<int>& workers = param->value();
164 for (int i = 0; i < (int)aStrands.size(); ++i) {
165 if (std::find(workers.begin(), workers.end(), i + 1) != workers.end())
166 sc.push_back(aStrands[i]);
167 }
168 }
169 } else {
170 sc = aStrands;
171 }
172 }
173
174 debugs(0, 0, HERE << "strands kid IDs = ");
175 for (Ipc::StrandCoords::const_iterator iter = sc.begin(); iter != sc.end(); ++iter) {
176 debugs(0, 0, HERE << iter->kidId);
177 }
178
179 return sc;
180 }