| Daniel Erat | 35f6587 | 2015-08-17 20:59:29 -0600 | [diff] [blame] | 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. |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 14 | |
| Christopher Book | 692fdfd | 2015-11-24 08:53:31 -0500 | [diff] [blame] | 15 | #include <libwebserv/request_impl.h> |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 16 | |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 17 | #include <base/callback.h> |
| Alex Vakulenko | 75d6da2 | 2015-10-13 10:01:34 -0700 | [diff] [blame] | 18 | #include <brillo/http/http_utils.h> |
| 19 | #include <brillo/streams/file_stream.h> |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 20 | |
| Christopher Wiley | 3320726 | 2015-12-15 18:33:50 -0800 | [diff] [blame] | 21 | #include <libwebserv/dbus_protocol_handler.h> |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 22 | |
| 23 | namespace libwebserv { |
| 24 | |
| Christopher Wiley | 3320726 | 2015-12-15 18:33:50 -0800 | [diff] [blame] | 25 | FileInfo::FileInfo(DBusProtocolHandler* handler, |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 26 | int file_id, |
| 27 | const std::string& request_id, |
| 28 | const std::string& file_name, |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 29 | const std::string& content_type, |
| 30 | const std::string& transfer_encoding) |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 31 | : handler_{handler}, |
| 32 | file_id_{file_id}, |
| 33 | request_id_{request_id}, |
| 34 | file_name_(file_name), |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 35 | content_type_(content_type), |
| 36 | transfer_encoding_(transfer_encoding) { |
| 37 | } |
| 38 | |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 39 | void FileInfo::GetData( |
| Alex Vakulenko | 75d6da2 | 2015-10-13 10:01:34 -0700 | [diff] [blame] | 40 | const base::Callback<void(brillo::StreamPtr)>& success_callback, |
| 41 | const base::Callback<void(brillo::Error*)>& error_callback) const { |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 42 | handler_->GetFileData(request_id_, |
| 43 | file_id_, |
| 44 | success_callback, |
| 45 | error_callback); |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 46 | } |
| 47 | |
| Christopher Wiley | 3320726 | 2015-12-15 18:33:50 -0800 | [diff] [blame] | 48 | RequestImpl::RequestImpl(DBusProtocolHandler* handler, |
| Christopher Book | 692fdfd | 2015-11-24 08:53:31 -0500 | [diff] [blame] | 49 | const std::string& url, |
| 50 | const std::string& method) |
| 51 | : Request{url, method}, handler_{handler} { |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 52 | } |
| 53 | |
| Christopher Book | 692fdfd | 2015-11-24 08:53:31 -0500 | [diff] [blame] | 54 | brillo::StreamPtr RequestImpl::GetDataStream() { |
| Alex Vakulenko | 75d6da2 | 2015-10-13 10:01:34 -0700 | [diff] [blame] | 55 | return brillo::FileStream::FromFileDescriptor( |
| Alex Vakulenko | 0f6413a | 2015-09-21 11:06:58 -0700 | [diff] [blame] | 56 | raw_data_fd_.GetPlatformFile(), false, nullptr); |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 57 | } |
| 58 | |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 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; |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 137 | auto range = |
| Alex Vakulenko | 75d6da2 | 2015-10-13 10:01:34 -0700 | [diff] [blame] | 138 | headers_.equal_range(brillo::http::GetCanonicalHeaderName(name)); |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 139 | while (range.first != range.second) { |
| 140 | data.push_back(range.first->second); |
| 141 | ++range.first; |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 142 | } |
| 143 | return data; |
| 144 | } |
| 145 | |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 146 | std::string Request::GetFirstHeader(const std::string& name) const { |
| Alex Vakulenko | 75d6da2 | 2015-10-13 10:01:34 -0700 | [diff] [blame] | 147 | auto p = headers_.find(brillo::http::GetCanonicalHeaderName(name)); |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 148 | return (p != headers_.end()) ? p->second : std::string{}; |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 149 | } |
| 150 | |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 151 | |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 152 | } // namespace libwebserv |