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