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