Home | History | Annotate | Download | only in libwebserv
      1 // Copyright 2015 The Android Open Source Project
      2 //
      3 // Licensed under the Apache License, Version 2.0 (the "License");
      4 // you may not use this file except in compliance with the License.
      5 // You may obtain a copy of the License at
      6 //
      7 //      http://www.apache.org/licenses/LICENSE-2.0
      8 //
      9 // Unless required by applicable law or agreed to in writing, software
     10 // distributed under the License is distributed on an "AS IS" BASIS,
     11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 // See the License for the specific language governing permissions and
     13 // limitations under the License.
     14 
     15 #include <libwebserv/dbus_server.h>
     16 
     17 #include <tuple>
     18 #include <vector>
     19 
     20 #include <libwebserv/dbus_protocol_handler.h>
     21 #include <libwebserv/request_impl.h>
     22 
     23 #include "dbus_bindings/org.chromium.WebServer.RequestHandler.h"
     24 #include "webservd/dbus-proxies.h"
     25 
     26 namespace libwebserv {
     27 
     28 class DBusServer::RequestHandler final
     29     : public org::chromium::WebServer::RequestHandlerInterface {
     30  public:
     31   explicit RequestHandler(DBusServer* server) : server_{server} {}
     32   bool ProcessRequest(
     33       brillo::ErrorPtr* error,
     34       const std::tuple<std::string, std::string, std::string, std::string,
     35                        std::string>& in_request_info,
     36       const std::vector<std::tuple<std::string, std::string>>& in_headers,
     37       const std::vector<std::tuple<bool, std::string, std::string>>& in_params,
     38       const std::vector<std::tuple<int32_t, std::string, std::string,
     39                                    std::string, std::string>>& in_files,
     40       const dbus::FileDescriptor& in_body) override;
     41 
     42  private:
     43   DBusServer* server_{nullptr};
     44   DISALLOW_COPY_AND_ASSIGN(RequestHandler);
     45 };
     46 
     47 bool DBusServer::RequestHandler::ProcessRequest(
     48     brillo::ErrorPtr* error,
     49     const std::tuple<std::string, std::string, std::string, std::string,
     50                      std::string>& in_request_info,
     51     const std::vector<std::tuple<std::string, std::string>>& in_headers,
     52     const std::vector<std::tuple<bool, std::string, std::string>>& in_params,
     53     const std::vector<std::tuple<int32_t, std::string, std::string, std::string,
     54                                  std::string>>& in_files,
     55     const dbus::FileDescriptor& in_body) {
     56   std::string protocol_handler_id = std::get<0>(in_request_info);
     57   std::string request_handler_id = std::get<1>(in_request_info);
     58   std::string request_id = std::get<2>(in_request_info);
     59   std::string url = std::get<3>(in_request_info);
     60   std::string method = std::get<4>(in_request_info);
     61   DBusProtocolHandler* protocol_handler =
     62       server_->GetProtocolHandlerByID(protocol_handler_id);
     63   if (!protocol_handler) {
     64     brillo::Error::AddToPrintf(error, FROM_HERE,
     65                                brillo::errors::dbus::kDomain,
     66                                DBUS_ERROR_FAILED,
     67                                "Unknown protocol handler '%s'",
     68                                protocol_handler_id.c_str());
     69     return false;
     70   }
     71   std::unique_ptr<RequestImpl> request{
     72     new RequestImpl{protocol_handler, url, method}};
     73   // Convert request data into format required by the Request object.
     74   for (const auto& tuple : in_params) {
     75     if (std::get<0>(tuple))
     76       request->post_data_.emplace(std::get<1>(tuple), std::get<2>(tuple));
     77     else
     78       request->get_data_.emplace(std::get<1>(tuple), std::get<2>(tuple));
     79   }
     80 
     81   for (const auto& tuple : in_headers)
     82     request->headers_.emplace(std::get<0>(tuple), std::get<1>(tuple));
     83 
     84   for (const auto& tuple : in_files) {
     85     request->file_info_.emplace(
     86         std::get<1>(tuple),  // field_name
     87         std::unique_ptr<FileInfo>{new FileInfo{
     88             protocol_handler,
     89             std::get<0>(tuple),     // file_id
     90             request_id,
     91             std::get<2>(tuple),     // file_name
     92             std::get<3>(tuple),     // content_type
     93             std::get<4>(tuple)}});  // transfer_encoding
     94   }
     95 
     96   request->raw_data_fd_ = base::File(dup(in_body.value()));
     97   CHECK(request->raw_data_fd_.IsValid());
     98 
     99   return protocol_handler->ProcessRequest(protocol_handler_id,
    100                                           request_handler_id,
    101                                           request_id,
    102                                           std::move(request),
    103                                           error);
    104 }
    105 
    106 DBusServer::DBusServer()
    107     : request_handler_{new RequestHandler{this}},
    108       dbus_adaptor_{new org::chromium::WebServer::RequestHandlerAdaptor{
    109           request_handler_.get()}} {}
    110 
    111 void DBusServer::Connect(
    112     const scoped_refptr<dbus::Bus>& bus,
    113     const std::string& service_name,
    114     const brillo::dbus_utils::AsyncEventSequencer::CompletionAction& cb,
    115     const base::Closure& on_server_online,
    116     const base::Closure& on_server_offline) {
    117   service_name_ = service_name;
    118   dbus_object_.reset(new brillo::dbus_utils::DBusObject{
    119       nullptr, bus, dbus_adaptor_->GetObjectPath()});
    120   dbus_adaptor_->RegisterWithDBusObject(dbus_object_.get());
    121   dbus_object_->RegisterAsync(cb);
    122   on_server_online_ = on_server_online;
    123   on_server_offline_ = on_server_offline;
    124   object_manager_.reset(new org::chromium::WebServer::ObjectManagerProxy{bus});
    125   object_manager_->SetServerAddedCallback(
    126       base::Bind(&DBusServer::Online, base::Unretained(this)));
    127   object_manager_->SetServerRemovedCallback(
    128       base::Bind(&DBusServer::Offline, base::Unretained(this)));
    129   object_manager_->SetProtocolHandlerAddedCallback(
    130       base::Bind(&DBusServer::ProtocolHandlerAdded, base::Unretained(this)));
    131   object_manager_->SetProtocolHandlerRemovedCallback(
    132       base::Bind(&DBusServer::ProtocolHandlerRemoved, base::Unretained(this)));
    133 }
    134 
    135 void DBusServer::Online(
    136     org::chromium::WebServer::ServerProxyInterface* server) {
    137   VLOG(1) << "Web server is on-line.";
    138   proxy_ = server;
    139   if (!on_server_online_.is_null())
    140     on_server_online_.Run();
    141 }
    142 
    143 bool DBusServer::IsConnected() const {
    144   return proxy_ != nullptr;
    145 }
    146 
    147 void DBusServer::Offline(const dbus::ObjectPath& /* object_path */) {
    148   if (!on_server_offline_.is_null())
    149     on_server_offline_.Run();
    150   proxy_ = nullptr;
    151   VLOG(1) << "Web server is off-line.";
    152 }
    153 
    154 void DBusServer::ProtocolHandlerAdded(
    155     org::chromium::WebServer::ProtocolHandlerProxyInterface* handler) {
    156   VLOG(1) << "Server-side protocol handler with ID '" << handler->id()
    157           << "' is on-line (" << handler->name() << ")";
    158 
    159   protocol_handler_id_map_.emplace(handler->GetObjectPath(), handler->id());
    160   DBusProtocolHandler* registered_handler =
    161       GetProtocolHandlerImpl(handler->name());
    162   if (registered_handler) {
    163     protocol_handlers_ids_.emplace(handler->id(), registered_handler);
    164     registered_handler->Connect(handler);
    165     if (!on_protocol_handler_connected_.is_null())
    166       on_protocol_handler_connected_.Run(registered_handler);
    167   }
    168 }
    169 
    170 void DBusServer::ProtocolHandlerRemoved(const dbus::ObjectPath& object_path) {
    171   auto p = protocol_handler_id_map_.find(object_path);
    172   if (p == protocol_handler_id_map_.end())
    173     return;
    174 
    175   VLOG(1) << "Server-side protocol handler with ID '" << p->second
    176           << "' is off-line.";
    177 
    178   DBusProtocolHandler* registered_handler = GetProtocolHandlerByID(p->second);
    179   if (registered_handler) {
    180     if (!on_protocol_handler_disconnected_.is_null())
    181       on_protocol_handler_disconnected_.Run(registered_handler);
    182     registered_handler->Disconnect(object_path);
    183     protocol_handlers_ids_.erase(p->second);
    184   }
    185 
    186   protocol_handler_id_map_.erase(p);
    187 }
    188 
    189 ProtocolHandler* DBusServer::GetProtocolHandler(const std::string& name) {
    190   return GetProtocolHandlerImpl(name);
    191 }
    192 
    193 DBusProtocolHandler* DBusServer::GetProtocolHandlerImpl(
    194     const std::string& name) {
    195   auto p = protocol_handlers_names_.find(name);
    196   if (p == protocol_handlers_names_.end()) {
    197     VLOG(1) << "Creating a client-side instance of web server's protocol "
    198             << "handler with name '" << name << "'";
    199     p = protocol_handlers_names_.emplace(
    200         name,
    201         std::unique_ptr<DBusProtocolHandler>{
    202             new DBusProtocolHandler{name, this}}).first;
    203   }
    204   return p->second.get();
    205 }
    206 
    207 ProtocolHandler* DBusServer::GetDefaultHttpHandler() {
    208   return GetProtocolHandler(ProtocolHandler::kHttp);
    209 }
    210 
    211 ProtocolHandler* DBusServer::GetDefaultHttpsHandler() {
    212   return GetProtocolHandler(ProtocolHandler::kHttps);
    213 }
    214 
    215 DBusProtocolHandler* DBusServer::GetProtocolHandlerByID(
    216     const std::string& id) const {
    217   auto p = protocol_handlers_ids_.find(id);
    218   if (p == protocol_handlers_ids_.end()) {
    219     LOG(ERROR) << "Unable to locate protocol handler with ID '" << id << "'";
    220     return nullptr;
    221   }
    222   return p->second;
    223 }
    224 
    225 void DBusServer::OnProtocolHandlerConnected(
    226     const base::Callback<void(ProtocolHandler*)>& callback) {
    227   on_protocol_handler_connected_ = callback;
    228 }
    229 
    230 void DBusServer::OnProtocolHandlerDisconnected(
    231     const base::Callback<void(ProtocolHandler*)>& callback) {
    232   on_protocol_handler_disconnected_ = callback;
    233 }
    234 
    235 base::TimeDelta DBusServer::GetDefaultRequestTimeout() const {
    236   int timeout_seconds = proxy_ ? proxy_->default_request_timeout() : -1;
    237   if (timeout_seconds <= 0)
    238     return base::TimeDelta::Max();
    239   return base::TimeDelta::FromSeconds(timeout_seconds);
    240 }
    241 
    242 }  // namespace libwebserv
    243