Action.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2023 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 "HttpReply.h"
15 #include "ipc/Port.h"
16 #include "mgr/Action.h"
17 #include "mgr/ActionCreator.h"
18 #include "mgr/ActionParams.h"
19 #include "mgr/ActionProfile.h"
20 #include "mgr/Command.h"
21 #include "mgr/Request.h"
22 #include "mgr/Response.h"
23 #include "Store.h"
24 
25 Mgr::Action::Action(const Command::Pointer &aCmd): cmd(aCmd)
26 {
27  Must(cmd != nullptr);
28  Must(cmd->profile != nullptr);
29 }
30 
32 {
33 }
34 
35 const Mgr::Command &
37 {
38  Must(cmd != nullptr);
39  return *cmd;
40 }
41 
42 bool
44 {
45  return command().profile->isAtomic;
46 }
47 
50 {
51  return command().profile->format;
52 }
53 
54 const char*
56 {
57  return command().profile->name;
58 }
59 
60 const char *
62 {
63  switch (format()) {
64  case Format::yaml:
65  return "application/yaml;charset=utf-8";
66  case Format::informal:
67  return "text/plain;charset=utf-8";
68  }
69  assert(!"unreachable code");
70  return "";
71 }
72 
75 {
76  const ActionParams &params = command().params;
77  const char *uri = params.httpUri.termedBuf();
78  return storeCreateEntry(uri, uri, params.httpFlags, params.httpMethod);
79 }
80 
81 void
83 {
84 }
85 
86 void
88 {
89  debugs(16, 5, MYNAME);
90 
91  // Assume most kid classes are fully aggregatable (i.e., they do not dump
92  // local info at all). Do not import the remote HTTP fd into our Comm
93  // space; collect and send an IPC msg with collected info to Coordinator.
94  ::close(request.conn->fd);
95  request.conn->fd = -1;
96  collect();
97  sendResponse(request.requestId);
98 }
99 
100 void
102 {
103  Response response(requestId, this);
104  Ipc::TypedMsgHdr message;
105  response.pack(message);
107 }
108 
109 void
110 Mgr::Action::run(StoreEntry* entry, bool writeHttpHeader)
111 {
112  debugs(16, 5, MYNAME);
113  collect();
114  fillEntry(entry, writeHttpHeader);
115 }
116 
117 void
118 Mgr::Action::fillEntry(StoreEntry* entry, bool writeHttpHeader)
119 {
120  debugs(16, 5, MYNAME);
121  entry->buffer();
122 
123  if (writeHttpHeader) {
124  HttpReply *rep = new HttpReply;
125  rep->setHeaders(Http::scOkay, nullptr, contentType(), -1, squid_curtime, squid_curtime);
126 
127  const auto &origin = command().params.httpOrigin;
128  const auto originOrNil = origin.size() ? origin.termedBuf() : nullptr;
129  CacheManager::PutCommonResponseHeaders(*rep, originOrNil);
130 
131  entry->replaceHttpReply(rep);
132  }
133 
134  dump(entry);
135 
136  entry->flush();
137 
138  if (atomic())
139  entry->complete();
140 }
141 
142 void
143 Mgr::OpenKidSection(StoreEntry * const entry, const Format format)
144 {
145  switch (format) {
146  case Format::yaml:
147  return storeAppendPrintf(entry, "---\nkid: %d\n", KidIdentifier);
148  case Format::informal:
149  return storeAppendPrintf(entry, "by kid%d {\n", KidIdentifier);
150  }
151  // unreachable code
152 }
153 
154 void
155 Mgr::CloseKidSection(StoreEntry * const entry, const Format format)
156 {
157  switch (format) {
158  case Format::yaml:
159  return storeAppendPrintf(entry, "...\n");
160  case Format::informal:
161  return storeAppendPrintf(entry, "} by kid%d\n\n", KidIdentifier);
162  }
163  // unreachable code
164 }
static void PutCommonResponseHeaders(HttpReply &, const char *httpOrigin)
void OpenKidSection(StoreEntry *, Format)
Definition: Action.cc:143
RequestId requestId
matches the request[or] with the response
Definition: Request.h:38
~Action() override
Definition: Action.cc:31
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:61
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:55
void CloseKidSection(StoreEntry *, Format)
Definition: Action.cc:155
void replaceHttpReply(const HttpReplyPointer &, const bool andStartWriting=true)
Definition: store.cc:1705
Action(const CommandPointer &aCmd)
Definition: Action.cc:25
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:36
bool atomic() const
dump() call writes everything before returning
Definition: Action.cc:43
virtual void respond(const Request &request)
respond to Coordinator request; default is to collect and sendResponse
Definition: Action.cc:87
StoreEntry * createStoreEntry() const
creates store entry from params
Definition: Action.cc:74
void sendResponse(Ipc::RequestId)
notify Coordinator that this action is done with local processing
Definition: Action.cc:101
cache manager request
Definition: Request.h:23
void SendMessage(const String &toAddress, const TypedMsgHdr &message)
Definition: UdsOp.cc:188
#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:49
const char * termedBuf() const
Definition: SquidString.h:92
void run(StoreEntry *entry, bool writeHttpHeader)
collect + fillEntry: collect local information and fill the store entry
Definition: Action.cc:110
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:118
struct msghdr with a known type, fixed-size I/O and control buffers
Definition: TypedMsgHdr.h:34
#define MYNAME
Definition: Stream.h:219
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:82

 

Introduction

Documentation

Support

Miscellaneous