blob: fe4d567ef9ec2c0816ae92dda100f977f9bc4625 [file] [log] [blame]
Alex Vakulenko31a63792015-02-03 12:44:57 -08001// Copyright 2014 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "webserver/libwebserv/protocol_handler.h"
6
7#include <tuple>
8
9#include <base/logging.h>
10#include <chromeos/map_utils.h>
11
12#include "libwebserv/org.chromium.WebServer.RequestHandler.h"
13#include "webservd/dbus-proxies.h"
14#include "webserver/libwebserv/request.h"
15#include "webserver/libwebserv/request_handler_callback.h"
16#include "webserver/libwebserv/response.h"
17#include "webserver/libwebserv/server.h"
18
19namespace libwebserv {
20
21namespace {
22
23// A dummy callback for async D-Bus errors.
24void IgnoreError(chromeos::Error* error) {}
25
26} // anonymous namespace
27
28const char ProtocolHandler::kHttp[] = "http";
29const char ProtocolHandler::kHttps[] = "https";
30
Alex Vakulenko3d2d5632015-03-02 15:58:32 -080031ProtocolHandler::ProtocolHandler(const std::string& name, Server* server)
32 : name_{name}, server_{server} {
33}
Alex Vakulenko31a63792015-02-03 12:44:57 -080034
35ProtocolHandler::~ProtocolHandler() {
36 // Remove any existing handlers, so the web server knows that we don't
37 // need them anymore.
38
39 // We need to get a copy of the map keys since removing the handlers will
40 // modify the map in the middle of the loop and that's not a good thing.
41 auto handler_ids = chromeos::GetMapKeys(request_handlers_);
42 for (int handler_id : handler_ids) {
43 RemoveHandler(handler_id);
44 }
45}
46
Alex Vakulenko3d2d5632015-03-02 15:58:32 -080047std::string ProtocolHandler::GetName() const {
48 return name_;
Alex Vakulenko31a63792015-02-03 12:44:57 -080049}
50
Alex Vakulenko3d2d5632015-03-02 15:58:32 -080051std::set<uint16_t> ProtocolHandler::GetPorts() const {
52 std::set<uint16_t> ports;
53 for (const auto& pair : proxies_)
54 ports.insert(pair.second->port());
55 return ports;
Alex Vakulenko31a63792015-02-03 12:44:57 -080056}
57
Alex Vakulenko3d2d5632015-03-02 15:58:32 -080058std::set<std::string> ProtocolHandler::GetProtocols() const {
59 std::set<std::string> protocols;
60 for (const auto& pair : proxies_)
61 protocols.insert(pair.second->protocol());
62 return protocols;
Alex Vakulenko31a63792015-02-03 12:44:57 -080063}
64
65chromeos::Blob ProtocolHandler::GetCertificateFingerprint() const {
Alex Vakulenko3d2d5632015-03-02 15:58:32 -080066 chromeos::Blob fingerprint;
67 for (const auto& pair : proxies_) {
68 fingerprint = pair.second->certificate_fingerprint();
69 if (!fingerprint.empty())
70 break;
71 }
72 return fingerprint;
Alex Vakulenko31a63792015-02-03 12:44:57 -080073}
74
75int ProtocolHandler::AddHandler(
76 const std::string& url,
77 const std::string& method,
78 std::unique_ptr<RequestHandlerInterface> handler) {
79 request_handlers_.emplace(++last_handler_id_,
80 HandlerMapEntry{url, method, std::string{},
81 std::move(handler)});
Alex Vakulenko3d2d5632015-03-02 15:58:32 -080082 for (const auto& pair : proxies_) {
83 pair.second->AddRequestHandlerAsync(
Alex Vakulenko31a63792015-02-03 12:44:57 -080084 url,
85 method,
86 server_->service_name_,
87 base::Bind(&ProtocolHandler::AddHandlerSuccess,
88 weak_ptr_factory_.GetWeakPtr(),
89 last_handler_id_),
90 base::Bind(&ProtocolHandler::AddHandlerError,
91 weak_ptr_factory_.GetWeakPtr(),
92 last_handler_id_));
93 }
94 return last_handler_id_;
95}
96
97int ProtocolHandler::AddHandlerCallback(
98 const std::string& url,
99 const std::string& method,
100 const base::Callback<RequestHandlerInterface::HandlerSignature>&
101 handler_callback) {
102 std::unique_ptr<RequestHandlerInterface> handler{
103 new RequestHandlerCallback{handler_callback}};
104 return AddHandler(url, method, std::move(handler));
105}
106
107bool ProtocolHandler::RemoveHandler(int handler_id) {
108 auto p = request_handlers_.find(handler_id);
109 if (p == request_handlers_.end())
110 return false;
111
Alex Vakulenko3d2d5632015-03-02 15:58:32 -0800112 for (const auto& pair : proxies_) {
113 pair.second->RemoveRequestHandlerAsync(
Alex Vakulenko31a63792015-02-03 12:44:57 -0800114 p->second.remote_handler_id,
115 base::Bind(&base::DoNothing),
116 base::Bind(&IgnoreError));
117 }
118
119 request_handlers_.erase(p);
120 return true;
121}
122
Alex Vakulenko3d2d5632015-03-02 15:58:32 -0800123void ProtocolHandler::Connect(ProtocolHandlerProxy* proxy) {
124 proxies_.emplace(proxy->GetObjectPath(), proxy);
Alex Vakulenko31a63792015-02-03 12:44:57 -0800125 for (const auto& pair : request_handlers_) {
Alex Vakulenko3d2d5632015-03-02 15:58:32 -0800126 proxy->AddRequestHandlerAsync(
Alex Vakulenko31a63792015-02-03 12:44:57 -0800127 pair.second.url,
128 pair.second.method,
129 server_->service_name_,
130 base::Bind(&ProtocolHandler::AddHandlerSuccess,
131 weak_ptr_factory_.GetWeakPtr(),
132 pair.first),
133 base::Bind(&ProtocolHandler::AddHandlerError,
134 weak_ptr_factory_.GetWeakPtr(),
135 pair.first));
136 }
137}
138
Alex Vakulenko3d2d5632015-03-02 15:58:32 -0800139void ProtocolHandler::Disconnect(const dbus::ObjectPath& object_path) {
140 proxies_.erase(object_path);
141 if (proxies_.empty())
142 remote_handler_id_map_.clear();
Alex Vakulenko31a63792015-02-03 12:44:57 -0800143}
144
145void ProtocolHandler::AddHandlerSuccess(int handler_id,
146 const std::string& remote_handler_id) {
147 auto p = request_handlers_.find(handler_id);
148 CHECK(p != request_handlers_.end());
149 p->second.remote_handler_id = remote_handler_id;
150
151 remote_handler_id_map_.emplace(remote_handler_id, handler_id);
152}
153
154void ProtocolHandler::AddHandlerError(int handler_id, chromeos::Error* error) {
155 // Nothing to do at the moment.
156}
157
Alex Vakulenko3d2d5632015-03-02 15:58:32 -0800158bool ProtocolHandler::ProcessRequest(const std::string& protocol_handler_id,
159 const std::string& remote_handler_id,
Alex Vakulenko31a63792015-02-03 12:44:57 -0800160 const std::string& request_id,
161 std::unique_ptr<Request> request,
162 chromeos::ErrorPtr* error) {
Alex Vakulenko3d2d5632015-03-02 15:58:32 -0800163 request_id_map_.emplace(request_id, protocol_handler_id);
Alex Vakulenko31a63792015-02-03 12:44:57 -0800164 auto id_iter = remote_handler_id_map_.find(remote_handler_id);
165 if (id_iter == remote_handler_id_map_.end()) {
166 chromeos::Error::AddToPrintf(error, FROM_HERE,
167 chromeos::errors::dbus::kDomain,
168 DBUS_ERROR_FAILED,
169 "Unknown request handler '%s'",
170 remote_handler_id.c_str());
171 return false;
172 }
173 auto handler_iter = request_handlers_.find(id_iter->second);
174 if (handler_iter == request_handlers_.end()) {
175 chromeos::Error::AddToPrintf(error, FROM_HERE,
176 chromeos::errors::dbus::kDomain,
177 DBUS_ERROR_FAILED,
178 "Handler # %d is no longer available",
179 id_iter->second);
180 return false;
181 }
182 handler_iter->second.handler->HandleRequest(
183 scoped_ptr<Request>(request.release()),
184 scoped_ptr<Response>(new Response{this, request_id}));
185 return true;
186}
187
188void ProtocolHandler::CompleteRequest(
189 const std::string& request_id,
190 int status_code,
191 const std::multimap<std::string, std::string>& headers,
192 const std::vector<uint8_t>& data) {
Alex Vakulenko3d2d5632015-03-02 15:58:32 -0800193 ProtocolHandlerProxy* proxy = GetRequestProtocolHandlerProxy(request_id);
194 if (!proxy)
Alex Vakulenko31a63792015-02-03 12:44:57 -0800195 return;
Alex Vakulenko31a63792015-02-03 12:44:57 -0800196
197 std::vector<std::tuple<std::string, std::string>> header_list;
198 header_list.reserve(headers.size());
199 for (const auto& pair : headers)
200 header_list.emplace_back(pair.first, pair.second);
201
Alex Vakulenko3d2d5632015-03-02 15:58:32 -0800202 proxy->CompleteRequestAsync(request_id, status_code, header_list, data,
203 base::Bind(&base::DoNothing),
204 base::Bind([](chromeos::Error*) {}));
Alex Vakulenko31a63792015-02-03 12:44:57 -0800205}
206
207void ProtocolHandler::GetFileData(
208 const std::string& request_id,
209 int file_id,
210 const base::Callback<void(const std::vector<uint8_t>&)>& success_callback,
211 const base::Callback<void(chromeos::Error*)>& error_callback) {
Alex Vakulenko3d2d5632015-03-02 15:58:32 -0800212 ProtocolHandlerProxy* proxy = GetRequestProtocolHandlerProxy(request_id);
213 CHECK(proxy);
214
215 proxy->GetRequestFileDataAsync(
Alex Vakulenko31a63792015-02-03 12:44:57 -0800216 request_id, file_id, success_callback, error_callback);
217}
218
Alex Vakulenko3d2d5632015-03-02 15:58:32 -0800219ProtocolHandler::ProtocolHandlerProxy*
220ProtocolHandler::GetRequestProtocolHandlerProxy(
221 const std::string& request_id) const {
222 auto iter = request_id_map_.find(request_id);
223 if (iter == request_id_map_.end()) {
224 LOG(ERROR) << "Can't find pending request with ID " << request_id;
225 return nullptr;
226 }
227 std::string handler_id = iter->second;
228 auto find_proxy_by_id = [handler_id](decltype(*proxies_.begin()) pair) {
229 return pair.second->id() == handler_id;
230 };
231 auto proxy_iter = std::find_if(proxies_.begin(), proxies_.end(),
232 find_proxy_by_id);
233 if (proxy_iter == proxies_.end()) {
234 LOG(WARNING) << "Completing a request after the handler proxy is removed";
235 return nullptr;
236 }
237 return proxy_iter->second;
238}
239
Alex Vakulenko31a63792015-02-03 12:44:57 -0800240
241} // namespace libwebserv