Squid Web Cache master
Loading...
Searching...
No Matches
Action.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2025 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 "CacheManager.h"
13#include "comm/Connection.h"
14#include "compat/unistd.h"
15#include "HttpReply.h"
16#include "ipc/Port.h"
17#include "mgr/Action.h"
18#include "mgr/ActionCreator.h"
19#include "mgr/ActionParams.h"
20#include "mgr/ActionProfile.h"
21#include "mgr/Command.h"
22#include "mgr/Request.h"
23#include "mgr/Response.h"
24#include "Store.h"
25
27{
28 Must(cmd != nullptr);
29 Must(cmd->profile != nullptr);
30}
31
35
36const Mgr::Command &
38{
39 Must(cmd != nullptr);
40 return *cmd;
41}
42
43bool
45{
46 return command().profile->isAtomic;
47}
48
51{
52 return command().profile->format;
53}
54
55const char*
57{
58 return command().profile->name;
59}
60
61const char *
63{
64 switch (format()) {
65 case Format::yaml:
66 return "application/yaml;charset=utf-8";
68 return "text/plain;charset=utf-8";
69 }
70 assert(!"unreachable code");
71 return "";
72}
73
76{
77 const ActionParams &params = command().params;
78 const char *uri = params.httpUri.termedBuf();
79 return storeCreateEntry(uri, uri, params.httpFlags, params.httpMethod);
80}
81
82void
84{
85}
86
87void
89{
90 debugs(16, 5, MYNAME);
91
92 // Assume most kid classes are fully aggregatable (i.e., they do not dump
93 // local info at all). Do not import the remote HTTP fd into our Comm
94 // space; collect and send an IPC msg with collected info to Coordinator.
95 xclose(request.conn->fd);
96 request.conn->fd = -1;
97 collect();
98 sendResponse(request.requestId);
99}
100
101void
103{
104 Response response(requestId, this);
105 Ipc::TypedMsgHdr message;
106 response.pack(message);
108}
109
110void
111Mgr::Action::run(StoreEntry* entry, bool writeHttpHeader)
112{
113 debugs(16, 5, MYNAME);
114 collect();
115 fillEntry(entry, writeHttpHeader);
116}
117
118void
119Mgr::Action::fillEntry(StoreEntry* entry, bool writeHttpHeader)
120{
121 debugs(16, 5, MYNAME);
122 entry->buffer();
123
124 if (writeHttpHeader) {
125 HttpReply *rep = new HttpReply;
126 rep->setHeaders(Http::scOkay, nullptr, contentType(), -1, squid_curtime, squid_curtime);
127
128 const auto &origin = command().params.httpOrigin;
129 const auto originOrNil = origin.size() ? origin.termedBuf() : nullptr;
131
132 entry->replaceHttpReply(rep);
133 }
134
135 dump(entry);
136
137 entry->flush();
138
139 if (atomic())
140 entry->complete();
141}
142
143void
144Mgr::OpenKidSection(StoreEntry * const entry, const Format format)
145{
146 switch (format) {
147 case Format::yaml:
148 return storeAppendPrintf(entry, "---\nkid: %d\n", KidIdentifier);
149 case Format::informal:
150 return storeAppendPrintf(entry, "by kid%d {\n", KidIdentifier);
151 }
152 // unreachable code
153}
154
155void
156Mgr::CloseKidSection(StoreEntry * const entry, const Format format)
157{
158 switch (format) {
159 case Format::yaml:
160 return storeAppendPrintf(entry, "...\n");
161 case Format::informal:
162 return storeAppendPrintf(entry, "} by kid%d\n\n", KidIdentifier);
163 }
164 // unreachable code
165}
time_t squid_curtime
#define Must(condition)
#define assert(EX)
Definition assert.h:17
static void PutCommonResponseHeaders(HttpReply &, const char *httpOrigin)
void setHeaders(Http::StatusCode status, const char *reason, const char *ctype, int64_t clen, time_t lmt, time_t expires)
Definition HttpReply.cc:170
static String CoordinatorAddr()
get the IPC message address for coordinator process
Definition Port.cc:65
RequestId requestId
matches the request[or] with the response
Definition Request.h:38
struct msghdr with a known type, fixed-size I/O and control buffers
Definition TypedMsgHdr.h:35
Cache Manager Action parameters extracted from the user request.
String httpUri
HTTP request URI.
RequestFlags httpFlags
HTTP request flags.
HttpRequestMethod httpMethod
HTTP request method.
Action(const CommandPointer &aCmd)
Definition Action.cc:26
virtual Format format() const
action report syntax
Definition Action.cc:50
void fillEntry(StoreEntry *entry, bool writeHttpHeader)
prepare store entry, dump info, close store entry (if possible)
Definition Action.cc:119
const Command & command() const
the cause of this action
Definition Action.cc:37
void sendResponse(Ipc::RequestId)
notify Coordinator that this action is done with local processing
Definition Action.cc:102
virtual void respond(const Request &request)
respond to Coordinator request; default is to collect and sendResponse
Definition Action.cc:88
StoreEntry * createStoreEntry() const
creates store entry from params
Definition Action.cc:75
bool atomic() const
dump() call writes everything before returning
Definition Action.cc:44
~Action() override
Definition Action.cc:32
const char * contentType() const
HTTP Content-Type header value for this Action report.
Definition Action.cc:62
const char * name() const
label as seen in the cache manager menu
Definition Action.cc:56
const CommandPointer cmd
the command that caused this action
Definition Action.h:90
virtual void add(const Action &action)
incrementally merge in remote information (of the same action type)
Definition Action.cc:83
void run(StoreEntry *entry, bool writeHttpHeader)
collect + fillEntry: collect local information and fill the store entry
Definition Action.cc:111
combined hard-coded action profile with user-supplied action parameters
Definition Command.h:22
cache manager request
Definition Request.h:24
Comm::ConnectionPointer conn
HTTP client connection descriptor.
Definition Request.h:35
void pack(Ipc::TypedMsgHdr &msg) const override
prepare for sendmsg()
Definition Response.cc:43
void complete()
Definition store.cc:1031
void flush() override
Definition store.cc:1612
void replaceHttpReply(const HttpReplyPointer &, const bool andStartWriting=true)
Definition store.cc:1705
void buffer() override
Definition store.cc:1601
char const * termedBuf() const
Definition SquidString.h:93
#define MYNAME
Definition Stream.h:219
#define debugs(SECTION, LEVEL, CONTENT)
Definition Stream.h:192
int KidIdentifier
@ scOkay
Definition StatusCode.h:27
void SendMessage(const String &toAddress, const TypedMsgHdr &message)
Definition UdsOp.cc:189
void OpenKidSection(StoreEntry *, Format)
Definition Action.cc:144
Format
whether Action report uses valid YAML or unspecified/legacy formatting
void CloseKidSection(StoreEntry *, Format)
Definition Action.cc:156
void storeAppendPrintf(StoreEntry *e, const char *fmt,...)
Definition store.cc:855
StoreEntry * storeCreateEntry(const char *url, const char *logUrl, const RequestFlags &flags, const HttpRequestMethod &method)
Definition store.cc:759
int xclose(int fd)
POSIX close(2) equivalent.
Definition unistd.h:43