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