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 
26 Mgr::Action::Action(const Command::Pointer &aCmd): cmd(aCmd)
27 {
28  Must(cmd != nullptr);
29  Must(cmd->profile != nullptr);
30 }
31 
33 {
34 }
35 
36 const Mgr::Command &
38 {
39  Must(cmd != nullptr);
40  return *cmd;
41 }
42 
43 bool
45 {
46  return command().profile->isAtomic;
47 }
48 
51 {
52  return command().profile->format;
53 }
54 
55 const char*
57 {
58  return command().profile->name;
59 }
60 
61 const char *
63 {
64  switch (format()) {
65  case Format::yaml:
66  return "application/yaml;charset=utf-8";
67  case Format::informal:
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 
82 void
84 {
85 }
86 
87 void
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 
101 void
103 {
104  Response response(requestId, this);
105  Ipc::TypedMsgHdr message;
106  response.pack(message);
108 }
109 
110 void
111 Mgr::Action::run(StoreEntry* entry, bool writeHttpHeader)
112 {
113  debugs(16, 5, MYNAME);
114  collect();
115  fillEntry(entry, writeHttpHeader);
116 }
117 
118 void
119 Mgr::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;
130  CacheManager::PutCommonResponseHeaders(*rep, originOrNil);
131 
132  entry->replaceHttpReply(rep);
133  }
134 
135  dump(entry);
136 
137  entry->flush();
138 
139  if (atomic())
140  entry->complete();
141 }
142 
143 void
144 Mgr::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 
155 void
156 Mgr::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 }
static void PutCommonResponseHeaders(HttpReply &, const char *httpOrigin)
void OpenKidSection(StoreEntry *, Format)
Definition: Action.cc:144
RequestId requestId
matches the request[or] with the response
Definition: Request.h:38
~Action() override
Definition: Action.cc:32
String httpUri
HTTP request URI.
Definition: ActionParams.h:33
combined hard-coded action profile with user-supplied action parameters
Definition: Command.h:21
int KidIdentifier
const char * contentType() const
HTTP Content-Type header value for this Action report.
Definition: Action.cc:62
void storeAppendPrintf(StoreEntry *e, const char *fmt,...)
Definition: store.cc:855
const CommandPointer cmd
the command that caused this action
Definition: Action.h:90
const char * name() const
label as seen in the cache manager menu
Definition: Action.cc:56
void CloseKidSection(StoreEntry *, Format)
Definition: Action.cc:156
void replaceHttpReply(const HttpReplyPointer &, const bool andStartWriting=true)
Definition: store.cc:1705
Action(const CommandPointer &aCmd)
Definition: Action.cc:26
void pack(Ipc::TypedMsgHdr &msg) const override
prepare for sendmsg()
Definition: Response.cc:43
static String CoordinatorAddr()
get the IPC message address for coordinator process
Definition: Port.cc:65
HttpRequestMethod httpMethod
HTTP request method.
Definition: ActionParams.h:34
RequestFlags httpFlags
HTTP request flags.
Definition: ActionParams.h:35
const Command & command() const
the cause of this action
Definition: Action.cc:37
bool atomic() const
dump() call writes everything before returning
Definition: Action.cc:44
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
void sendResponse(Ipc::RequestId)
notify Coordinator that this action is done with local processing
Definition: Action.cc:102
cache manager request
Definition: Request.h:23
void SendMessage(const String &toAddress, const TypedMsgHdr &message)
Definition: UdsOp.cc:189
#define assert(EX)
Definition: assert.h:17
void buffer() override
Definition: store.cc:1601
Format
whether Action report uses valid YAML or unspecified/legacy formatting
void flush() override
Definition: store.cc:1612
time_t squid_curtime
Definition: stub_libtime.cc:20
StoreEntry * storeCreateEntry(const char *url, const char *logUrl, const RequestFlags &flags, const HttpRequestMethod &method)
Definition: store.cc:759
void complete()
Definition: store.cc:1031
virtual Format format() const
action report syntax
Definition: Action.cc:50
const char * termedBuf() const
Definition: SquidString.h:93
void run(StoreEntry *entry, bool writeHttpHeader)
collect + fillEntry: collect local information and fill the store entry
Definition: Action.cc:111
Cache Manager Action parameters extracted from the user request.
Definition: ActionParams.h:23
#define Must(condition)
Definition: TextException.h:75
void fillEntry(StoreEntry *entry, bool writeHttpHeader)
prepare store entry, dump info, close store entry (if possible)
Definition: Action.cc:119
struct msghdr with a known type, fixed-size I/O and control buffers
Definition: TypedMsgHdr.h:34
#define MYNAME
Definition: Stream.h:219
int xclose(int fd)
POSIX close(2) equivalent.
Definition: unistd.h:43
void setHeaders(Http::StatusCode status, const char *reason, const char *ctype, int64_t clen, time_t lmt, time_t expires)
Definition: HttpReply.cc:170
@ scOkay
Definition: StatusCode.h:27
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Stream.h:192
Comm::ConnectionPointer conn
HTTP client connection descriptor.
Definition: Request.h:35
virtual void add(const Action &action)
incrementally merge in remote information (of the same action type)
Definition: Action.cc:83

 

Introduction

Documentation

Support

Miscellaneous