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