blob: 5d7abf77bd690d8f2a27617fdce988c428ed465f [file] [log] [blame]
Daniel Erat35f65872015-08-17 20:59:29 -06001// 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.
Alex Vakulenko039da312015-02-03 08:58:55 -080014
Christopher Book692fdfd2015-11-24 08:53:31 -050015#include <libwebserv/request_impl.h>
Alex Vakulenko039da312015-02-03 08:58:55 -080016
Alex Vakulenko31a63792015-02-03 12:44:57 -080017#include <base/callback.h>
Alex Vakulenko75d6da22015-10-13 10:01:34 -070018#include <brillo/http/http_utils.h>
19#include <brillo/streams/file_stream.h>
Alex Vakulenko31a63792015-02-03 12:44:57 -080020
Christopher Wiley33207262015-12-15 18:33:50 -080021#include <libwebserv/dbus_protocol_handler.h>
Alex Vakulenko039da312015-02-03 08:58:55 -080022
23namespace libwebserv {
24
Christopher Wiley33207262015-12-15 18:33:50 -080025FileInfo::FileInfo(DBusProtocolHandler* handler,
Alex Vakulenko31a63792015-02-03 12:44:57 -080026 int file_id,
27 const std::string& request_id,
28 const std::string& file_name,
Alex Vakulenko039da312015-02-03 08:58:55 -080029 const std::string& content_type,
30 const std::string& transfer_encoding)
Alex Vakulenko31a63792015-02-03 12:44:57 -080031 : handler_{handler},
32 file_id_{file_id},
33 request_id_{request_id},
34 file_name_(file_name),
Alex Vakulenko039da312015-02-03 08:58:55 -080035 content_type_(content_type),
36 transfer_encoding_(transfer_encoding) {
37}
38
Alex Vakulenko31a63792015-02-03 12:44:57 -080039void FileInfo::GetData(
Alex Vakulenko75d6da22015-10-13 10:01:34 -070040 const base::Callback<void(brillo::StreamPtr)>& success_callback,
41 const base::Callback<void(brillo::Error*)>& error_callback) const {
Alex Vakulenko31a63792015-02-03 12:44:57 -080042 handler_->GetFileData(request_id_,
43 file_id_,
44 success_callback,
45 error_callback);
Alex Vakulenko039da312015-02-03 08:58:55 -080046}
47
Christopher Wiley33207262015-12-15 18:33:50 -080048RequestImpl::RequestImpl(DBusProtocolHandler* handler,
Christopher Book692fdfd2015-11-24 08:53:31 -050049 const std::string& url,
50 const std::string& method)
51 : Request{url, method}, handler_{handler} {
Alex Vakulenko039da312015-02-03 08:58:55 -080052}
53
Christopher Book692fdfd2015-11-24 08:53:31 -050054brillo::StreamPtr RequestImpl::GetDataStream() {
Alex Vakulenko75d6da22015-10-13 10:01:34 -070055 return brillo::FileStream::FromFileDescriptor(
Alex Vakulenko0f6413a2015-09-21 11:06:58 -070056 raw_data_fd_.GetPlatformFile(), false, nullptr);
Alex Vakulenko039da312015-02-03 08:58:55 -080057}
58
Alex Vakulenko039da312015-02-03 08:58:55 -080059std::vector<PairOfStrings> Request::GetFormData() const {
60 auto data = GetFormDataGet();
61 auto post_data = GetFormDataPost();
62 data.insert(data.end(), post_data.begin(), post_data.end());
63 return data;
64}
65
66std::vector<PairOfStrings> Request::GetFormDataGet() const {
67 return std::vector<PairOfStrings>{get_data_.begin(), get_data_.end()};
68}
69
70std::vector<PairOfStrings> Request::GetFormDataPost() const {
71 return std::vector<PairOfStrings>{post_data_.begin(), post_data_.end()};
72}
73
74std::vector<std::pair<std::string, const FileInfo*>> Request::GetFiles() const {
75 std::vector<std::pair<std::string, const FileInfo*>> data;
76 data.reserve(file_info_.size());
77 for (const auto& pair : file_info_) {
78 data.emplace_back(pair.first, pair.second.get());
79 }
80 return data;
81}
82
83std::vector<std::string> Request::GetFormField(const std::string& name) const {
84 std::vector<std::string> data;
85 auto pair = get_data_.equal_range(name);
86 while (pair.first != pair.second) {
87 data.push_back(pair.first->second);
88 ++pair.first;
89 }
90 pair = post_data_.equal_range(name);
91 while (pair.first != pair.second) {
92 data.push_back(pair.first->second);
93 ++pair.first;
94 }
95 return data;
96}
97
98std::vector<std::string> Request::GetFormFieldPost(
99 const std::string& name) const {
100 std::vector<std::string> data;
101 auto pair = post_data_.equal_range(name);
102 while (pair.first != pair.second) {
103 data.push_back(pair.first->second);
104 ++pair.first;
105 }
106 return data;
107}
108
109std::vector<std::string> Request::GetFormFieldGet(
110 const std::string& name) const {
111 std::vector<std::string> data;
112 auto pair = get_data_.equal_range(name);
113 while (pair.first != pair.second) {
114 data.push_back(pair.first->second);
115 ++pair.first;
116 }
117 return data;
118}
119
120std::vector<const FileInfo*> Request::GetFileInfo(
121 const std::string& name) const {
122 std::vector<const FileInfo*> data;
123 auto pair = file_info_.equal_range(name);
124 while (pair.first != pair.second) {
125 data.push_back(pair.first->second.get());
126 ++pair.first;
127 }
128 return data;
129}
130
131std::vector<PairOfStrings> Request::GetHeaders() const {
132 return std::vector<PairOfStrings>{headers_.begin(), headers_.end()};
133}
134
135std::vector<std::string> Request::GetHeader(const std::string& name) const {
136 std::vector<std::string> data;
Alex Vakulenko31a63792015-02-03 12:44:57 -0800137 auto range =
Alex Vakulenko75d6da22015-10-13 10:01:34 -0700138 headers_.equal_range(brillo::http::GetCanonicalHeaderName(name));
Alex Vakulenko31a63792015-02-03 12:44:57 -0800139 while (range.first != range.second) {
140 data.push_back(range.first->second);
141 ++range.first;
Alex Vakulenko039da312015-02-03 08:58:55 -0800142 }
143 return data;
144}
145
Alex Vakulenko31a63792015-02-03 12:44:57 -0800146std::string Request::GetFirstHeader(const std::string& name) const {
Alex Vakulenko75d6da22015-10-13 10:01:34 -0700147 auto p = headers_.find(brillo::http::GetCanonicalHeaderName(name));
Alex Vakulenko31a63792015-02-03 12:44:57 -0800148 return (p != headers_.end()) ? p->second : std::string{};
Alex Vakulenko039da312015-02-03 08:58:55 -0800149}
150
Alex Vakulenko31a63792015-02-03 12:44:57 -0800151
Alex Vakulenko039da312015-02-03 08:58:55 -0800152} // namespace libwebserv