]> git.ipfire.org Git - thirdparty/squid.git/blob - src/mgr/Action.cc
SourceLayout: Move time related tools to time/libtime.la (#1001)
[thirdparty/squid.git] / src / mgr / Action.cc
1 /*
2 * Copyright (C) 1996-2022 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 "comm/Connection.h"
13 #include "HttpReply.h"
14 #include "ipc/Port.h"
15 #include "mgr/Action.h"
16 #include "mgr/ActionCreator.h"
17 #include "mgr/ActionParams.h"
18 #include "mgr/ActionProfile.h"
19 #include "mgr/Command.h"
20 #include "mgr/Request.h"
21 #include "mgr/Response.h"
22 #include "Store.h"
23
24 Mgr::Action::Action(const Command::Pointer &aCmd): cmd(aCmd)
25 {
26 Must(cmd != NULL);
27 Must(cmd->profile != NULL);
28 }
29
30 Mgr::Action::~Action()
31 {
32 }
33
34 const Mgr::Command &
35 Mgr::Action::command() const
36 {
37 Must(cmd != NULL);
38 return *cmd;
39 }
40
41 bool
42 Mgr::Action::atomic() const
43 {
44 return command().profile->isAtomic;
45 }
46
47 const char*
48 Mgr::Action::name() const
49 {
50 return command().profile->name;
51 }
52
53 StoreEntry*
54 Mgr::Action::createStoreEntry() const
55 {
56 const ActionParams &params = command().params;
57 const char *uri = params.httpUri.termedBuf();
58 return storeCreateEntry(uri, uri, params.httpFlags, params.httpMethod);
59 }
60
61 void
62 Mgr::Action::add(const Action &)
63 {
64 }
65
66 void
67 Mgr::Action::respond(const Request &request)
68 {
69 debugs(16, 5, MYNAME);
70
71 // Assume most kid classes are fully aggregatable (i.e., they do not dump
72 // local info at all). Do not import the remote HTTP fd into our Comm
73 // space; collect and send an IPC msg with collected info to Coordinator.
74 ::close(request.conn->fd);
75 request.conn->fd = -1;
76 collect();
77 sendResponse(request.requestId);
78 }
79
80 void
81 Mgr::Action::sendResponse(const Ipc::RequestId requestId)
82 {
83 Response response(requestId, this);
84 Ipc::TypedMsgHdr message;
85 response.pack(message);
86 Ipc::SendMessage(Ipc::Port::CoordinatorAddr(), message);
87 }
88
89 void
90 Mgr::Action::run(StoreEntry* entry, bool writeHttpHeader)
91 {
92 debugs(16, 5, MYNAME);
93 collect();
94 fillEntry(entry, writeHttpHeader);
95 }
96
97 void
98 Mgr::Action::fillEntry(StoreEntry* entry, bool writeHttpHeader)
99 {
100 debugs(16, 5, MYNAME);
101 entry->buffer();
102
103 if (writeHttpHeader) {
104 HttpReply *rep = new HttpReply;
105 rep->setHeaders(Http::scOkay, NULL, contentType(), -1, squid_curtime, squid_curtime);
106 // Allow cachemgr and other XHR scripts access to our version string
107 const ActionParams &params = command().params;
108 if (params.httpOrigin.size() > 0) {
109 rep->header.putExt("Access-Control-Allow-Origin", params.httpOrigin.termedBuf());
110 #if HAVE_AUTH_MODULE_BASIC
111 rep->header.putExt("Access-Control-Allow-Credentials","true");
112 #endif
113 rep->header.putExt("Access-Control-Expose-Headers","Server");
114 }
115 entry->replaceHttpReply(rep);
116 }
117
118 dump(entry);
119
120 entry->flush();
121
122 if (atomic())
123 entry->complete();
124 }
125