| 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 | |
| 15 | #include <libwebserv/request.h> |
| 16 | |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 17 | #include <base/callback.h> |
| 18 | #include <chromeos/http/http_utils.h> |
| 19 | |
| 20 | #include <libwebserv/protocol_handler.h> |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 21 | |
| 22 | namespace libwebserv { |
| 23 | |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 24 | FileInfo::FileInfo(ProtocolHandler* handler, |
| 25 | int file_id, |
| 26 | const std::string& request_id, |
| 27 | const std::string& file_name, |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 28 | const std::string& content_type, |
| 29 | const std::string& transfer_encoding) |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 30 | : handler_{handler}, |
| 31 | file_id_{file_id}, |
| 32 | request_id_{request_id}, |
| 33 | file_name_(file_name), |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 34 | content_type_(content_type), |
| 35 | transfer_encoding_(transfer_encoding) { |
| 36 | } |
| 37 | |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 38 | void FileInfo::GetData( |
| 39 | const base::Callback<void(const std::vector<uint8_t>&)>& success_callback, |
| 40 | const base::Callback<void(chromeos::Error*)>& error_callback) { |
| 41 | handler_->GetFileData(request_id_, |
| 42 | file_id_, |
| 43 | success_callback, |
| 44 | error_callback); |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 45 | } |
| 46 | |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 47 | Request::Request(ProtocolHandler* handler, |
| 48 | const std::string& url, |
| 49 | const std::string& method) |
| 50 | : handler_{handler}, url_{url}, method_{method} { |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | Request::~Request() { |
| 54 | } |
| 55 | |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 56 | const std::vector<uint8_t>& Request::GetData() const { |
| 57 | return raw_data_; |
| 58 | } |
| 59 | |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 60 | std::vector<PairOfStrings> Request::GetFormData() const { |
| 61 | auto data = GetFormDataGet(); |
| 62 | auto post_data = GetFormDataPost(); |
| 63 | data.insert(data.end(), post_data.begin(), post_data.end()); |
| 64 | return data; |
| 65 | } |
| 66 | |
| 67 | std::vector<PairOfStrings> Request::GetFormDataGet() const { |
| 68 | return std::vector<PairOfStrings>{get_data_.begin(), get_data_.end()}; |
| 69 | } |
| 70 | |
| 71 | std::vector<PairOfStrings> Request::GetFormDataPost() const { |
| 72 | return std::vector<PairOfStrings>{post_data_.begin(), post_data_.end()}; |
| 73 | } |
| 74 | |
| 75 | std::vector<std::pair<std::string, const FileInfo*>> Request::GetFiles() const { |
| 76 | std::vector<std::pair<std::string, const FileInfo*>> data; |
| 77 | data.reserve(file_info_.size()); |
| 78 | for (const auto& pair : file_info_) { |
| 79 | data.emplace_back(pair.first, pair.second.get()); |
| 80 | } |
| 81 | return data; |
| 82 | } |
| 83 | |
| 84 | std::vector<std::string> Request::GetFormField(const std::string& name) const { |
| 85 | std::vector<std::string> data; |
| 86 | auto pair = get_data_.equal_range(name); |
| 87 | while (pair.first != pair.second) { |
| 88 | data.push_back(pair.first->second); |
| 89 | ++pair.first; |
| 90 | } |
| 91 | pair = post_data_.equal_range(name); |
| 92 | while (pair.first != pair.second) { |
| 93 | data.push_back(pair.first->second); |
| 94 | ++pair.first; |
| 95 | } |
| 96 | return data; |
| 97 | } |
| 98 | |
| 99 | std::vector<std::string> Request::GetFormFieldPost( |
| 100 | const std::string& name) const { |
| 101 | std::vector<std::string> data; |
| 102 | auto pair = post_data_.equal_range(name); |
| 103 | while (pair.first != pair.second) { |
| 104 | data.push_back(pair.first->second); |
| 105 | ++pair.first; |
| 106 | } |
| 107 | return data; |
| 108 | } |
| 109 | |
| 110 | std::vector<std::string> Request::GetFormFieldGet( |
| 111 | const std::string& name) const { |
| 112 | std::vector<std::string> data; |
| 113 | auto pair = get_data_.equal_range(name); |
| 114 | while (pair.first != pair.second) { |
| 115 | data.push_back(pair.first->second); |
| 116 | ++pair.first; |
| 117 | } |
| 118 | return data; |
| 119 | } |
| 120 | |
| 121 | std::vector<const FileInfo*> Request::GetFileInfo( |
| 122 | const std::string& name) const { |
| 123 | std::vector<const FileInfo*> data; |
| 124 | auto pair = file_info_.equal_range(name); |
| 125 | while (pair.first != pair.second) { |
| 126 | data.push_back(pair.first->second.get()); |
| 127 | ++pair.first; |
| 128 | } |
| 129 | return data; |
| 130 | } |
| 131 | |
| 132 | std::vector<PairOfStrings> Request::GetHeaders() const { |
| 133 | return std::vector<PairOfStrings>{headers_.begin(), headers_.end()}; |
| 134 | } |
| 135 | |
| 136 | std::vector<std::string> Request::GetHeader(const std::string& name) const { |
| 137 | std::vector<std::string> data; |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 138 | auto range = |
| 139 | headers_.equal_range(chromeos::http::GetCanonicalHeaderName(name)); |
| 140 | while (range.first != range.second) { |
| 141 | data.push_back(range.first->second); |
| 142 | ++range.first; |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 143 | } |
| 144 | return data; |
| 145 | } |
| 146 | |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 147 | std::string Request::GetFirstHeader(const std::string& name) const { |
| 148 | auto p = headers_.find(chromeos::http::GetCanonicalHeaderName(name)); |
| 149 | return (p != headers_.end()) ? p->second : std::string{}; |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 150 | } |
| 151 | |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 152 | |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 153 | } // namespace libwebserv |