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 "webservd/dbus_request_handler.h"
16
17#include <tuple>
18#include <vector>
19
20#include <base/bind.h>
21#include <brillo/http/http_request.h>
22#include <brillo/mime_utils.h>
23
24#include "libwebserv/dbus-proxies.h"
25#include "webservd/request.h"
26#include "webservd/server.h"
27
28namespace webservd {
29
30namespace {
31
32void OnError(Request* request,
33             bool debug,
34             brillo::Error* error) {
35  std::string error_msg{"Internal Server Error"};
36  if (debug) {
37    error_msg += "\r\n" + error->GetMessage();
38  }
39  request->Complete(brillo::http::status_code::InternalServerError, {},
40                    brillo::mime::text::kPlain, error_msg);
41}
42
43}  // anonymous namespace
44
45DBusRequestHandler::DBusRequestHandler(Server* server,
46                                       RequestHandlerProxy* handler_proxy)
47    : server_{server},
48      handler_proxy_{handler_proxy} {
49}
50
51void DBusRequestHandler::HandleRequest(Request* request) {
52  std::vector<std::tuple<std::string, std::string>> headers;
53  for (const auto& pair : request->GetHeaders())
54    headers.emplace_back(pair.first, pair.second);
55
56  std::vector<std::tuple<int32_t, std::string, std::string, std::string,
57                         std::string>> files;
58  int32_t index = 0;
59  for (const auto& file : request->GetFileInfo()) {
60    files.emplace_back(index++, file->field_name, file->file_name,
61                        file->content_type, file->transfer_encoding);
62  }
63
64  std::vector<std::tuple<bool, std::string, std::string>> params;
65  for (const auto& pair : request->GetDataGet())
66    params.emplace_back(false, pair.first, pair.second);
67
68  for (const auto& pair : request->GetDataPost())
69    params.emplace_back(true, pair.first, pair.second);
70
71  auto error_callback = base::Bind(&OnError,
72                                   base::Unretained(request),
73                                   server_->GetConfig().use_debug);
74
75  auto request_id = std::make_tuple(request->GetProtocolHandlerID(),
76                                    request->GetRequestHandlerID(),
77                                    request->GetID(),
78                                    request->GetURL(),
79                                    request->GetMethod());
80
81  dbus::FileDescriptor body_data_pipe;
82  body_data_pipe.PutValue(request->GetBodyDataFileDescriptor());
83  body_data_pipe.CheckValidity();
84  handler_proxy_->ProcessRequestAsync(
85      request_id, headers, params, files, body_data_pipe,
86      base::Bind(&base::DoNothing), error_callback);
87}
88
89}  // namespace webservd
90