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/request_impl.h> 16 17 #include <base/callback.h> 18 #include <brillo/http/http_utils.h> 19 #include <brillo/streams/file_stream.h> 20 21 #include <libwebserv/dbus_protocol_handler.h> 22 23 namespace libwebserv { 24 25 FileInfo::FileInfo(DBusProtocolHandler* handler, 26 int file_id, 27 const std::string& request_id, 28 const std::string& file_name, 29 const std::string& content_type, 30 const std::string& transfer_encoding) 31 : handler_{handler}, 32 file_id_{file_id}, 33 request_id_{request_id}, 34 file_name_(file_name), 35 content_type_(content_type), 36 transfer_encoding_(transfer_encoding) { 37 } 38 39 void FileInfo::GetData( 40 const base::Callback<void(brillo::StreamPtr)>& success_callback, 41 const base::Callback<void(brillo::Error*)>& error_callback) const { 42 handler_->GetFileData(request_id_, 43 file_id_, 44 success_callback, 45 error_callback); 46 } 47 48 RequestImpl::RequestImpl(DBusProtocolHandler* handler, 49 const std::string& url, 50 const std::string& method) 51 : Request{url, method}, handler_{handler} { 52 } 53 54 brillo::StreamPtr RequestImpl::GetDataStream() { 55 return brillo::FileStream::FromFileDescriptor( 56 raw_data_fd_.GetPlatformFile(), false, nullptr); 57 } 58 59 std::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 66 std::vector<PairOfStrings> Request::GetFormDataGet() const { 67 return std::vector<PairOfStrings>{get_data_.begin(), get_data_.end()}; 68 } 69 70 std::vector<PairOfStrings> Request::GetFormDataPost() const { 71 return std::vector<PairOfStrings>{post_data_.begin(), post_data_.end()}; 72 } 73 74 std::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 83 std::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 98 std::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 109 std::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 120 std::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 131 std::vector<PairOfStrings> Request::GetHeaders() const { 132 return std::vector<PairOfStrings>{headers_.begin(), headers_.end()}; 133 } 134 135 std::vector<std::string> Request::GetHeader(const std::string& name) const { 136 std::vector<std::string> data; 137 auto range = 138 headers_.equal_range(brillo::http::GetCanonicalHeaderName(name)); 139 while (range.first != range.second) { 140 data.push_back(range.first->second); 141 ++range.first; 142 } 143 return data; 144 } 145 146 std::string Request::GetFirstHeader(const std::string& name) const { 147 auto p = headers_.find(brillo::http::GetCanonicalHeaderName(name)); 148 return (p != headers_.end()) ? p->second : std::string{}; 149 } 150 151 152 } // namespace libwebserv 153