blob: f0bdd794df8b52e26b8415aad8b9dfab760ee60f [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
15#include <libwebserv/request.h>
16
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
21#include <libwebserv/protocol_handler.h>
Alex Vakulenko039da312015-02-03 08:58:55 -080022
23namespace libwebserv {
24
Alex Vakulenko31a63792015-02-03 12:44:57 -080025FileInfo::FileInfo(ProtocolHandler* handler,
26 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
Alex Vakulenko31a63792015-02-03 12:44:57 -080048Request::Request(ProtocolHandler* handler,
49 const std::string& url,
50 const std::string& method)
51 : handler_{handler}, url_{url}, method_{method} {
Alex Vakulenko039da312015-02-03 08:58:55 -080052}
53
54Request::~Request() {
55}
56
Alex Vakulenko75d6da22015-10-13 10:01:34 -070057brillo::StreamPtr Request::GetDataStream() {
58 return brillo::FileStream::FromFileDescriptor(
Alex Vakulenko0f6413a2015-09-21 11:06:58 -070059 raw_data_fd_.GetPlatformFile(), false, nullptr);
Alex Vakulenko039da312015-02-03 08:58:55 -080060}
61
Alex Vakulenko039da312015-02-03 08:58:55 -080062std::vector<PairOfStrings> Request::GetFormData() const {
63 auto data = GetFormDataGet();
64 auto post_data = GetFormDataPost();
65 data.insert(data.end(), post_data.begin(), post_data.end());
66 return data;
67}
68
69std::vector<PairOfStrings> Request::GetFormDataGet() const {
70 return std::vector<PairOfStrings>{get_data_.begin(), get_data_.end()};
71}
72
73std::vector<PairOfStrings> Request::GetFormDataPost() const {
74 return std::vector<PairOfStrings>{post_data_.begin(), post_data_.end()};
75}
76
77std::vector<std::pair<std::string, const FileInfo*>> Request::GetFiles() const {
78 std::vector<std::pair<std::string, const FileInfo*>> data;
79 data.reserve(file_info_.size());
80 for (const auto& pair : file_info_) {
81 data.emplace_back(pair.first, pair.second.get());
82 }
83 return data;
84}
85
86std::vector<std::string> Request::GetFormField(const std::string& name) const {
87 std::vector<std::string> data;
88 auto pair = get_data_.equal_range(name);
89 while (pair.first != pair.second) {
90 data.push_back(pair.first->second);
91 ++pair.first;
92 }
93 pair = post_data_.equal_range(name);
94 while (pair.first != pair.second) {
95 data.push_back(pair.first->second);
96 ++pair.first;
97 }
98 return data;
99}
100
101std::vector<std::string> Request::GetFormFieldPost(
102 const std::string& name) const {
103 std::vector<std::string> data;
104 auto pair = post_data_.equal_range(name);
105 while (pair.first != pair.second) {
106 data.push_back(pair.first->second);
107 ++pair.first;
108 }
109 return data;
110}
111
112std::vector<std::string> Request::GetFormFieldGet(
113 const std::string& name) const {
114 std::vector<std::string> data;
115 auto pair = get_data_.equal_range(name);
116 while (pair.first != pair.second) {
117 data.push_back(pair.first->second);
118 ++pair.first;
119 }
120 return data;
121}
122
123std::vector<const FileInfo*> Request::GetFileInfo(
124 const std::string& name) const {
125 std::vector<const FileInfo*> data;
126 auto pair = file_info_.equal_range(name);
127 while (pair.first != pair.second) {
128 data.push_back(pair.first->second.get());
129 ++pair.first;
130 }
131 return data;
132}
133
134std::vector<PairOfStrings> Request::GetHeaders() const {
135 return std::vector<PairOfStrings>{headers_.begin(), headers_.end()};
136}
137
138std::vector<std::string> Request::GetHeader(const std::string& name) const {
139 std::vector<std::string> data;
Alex Vakulenko31a63792015-02-03 12:44:57 -0800140 auto range =
Alex Vakulenko75d6da22015-10-13 10:01:34 -0700141 headers_.equal_range(brillo::http::GetCanonicalHeaderName(name));
Alex Vakulenko31a63792015-02-03 12:44:57 -0800142 while (range.first != range.second) {
143 data.push_back(range.first->second);
144 ++range.first;
Alex Vakulenko039da312015-02-03 08:58:55 -0800145 }
146 return data;
147}
148
Alex Vakulenko31a63792015-02-03 12:44:57 -0800149std::string Request::GetFirstHeader(const std::string& name) const {
Alex Vakulenko75d6da22015-10-13 10:01:34 -0700150 auto p = headers_.find(brillo::http::GetCanonicalHeaderName(name));
Alex Vakulenko31a63792015-02-03 12:44:57 -0800151 return (p != headers_.end()) ? p->second : std::string{};
Alex Vakulenko039da312015-02-03 08:58:55 -0800152}
153
Alex Vakulenko31a63792015-02-03 12:44:57 -0800154
Alex Vakulenko039da312015-02-03 08:58:55 -0800155} // namespace libwebserv