| 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/response.h> |
| 16 | |
| 17 | #include <algorithm> |
| 18 | |
| 19 | #include <base/json/json_writer.h> |
| 20 | #include <base/logging.h> |
| 21 | #include <base/values.h> |
| 22 | #include <chromeos/http/http_request.h> |
| 23 | #include <chromeos/mime_utils.h> |
| Alex Vakulenko | 330a10f | 2015-09-23 16:51:02 -0700 | [diff] [blame] | 24 | #include <chromeos/streams/memory_stream.h> |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 25 | #include <chromeos/strings/string_utils.h> |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 26 | #include <libwebserv/protocol_handler.h> |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 27 | |
| 28 | namespace libwebserv { |
| 29 | |
| Alex Vakulenko | 31a6379 | 2015-02-03 12:44:57 -0800 | [diff] [blame] | 30 | Response::Response(ProtocolHandler* handler, const std::string& request_id) |
| 31 | : handler_{handler}, request_id_{request_id} { |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | Response::~Response() { |
| 35 | if (!reply_sent_) { |
| 36 | ReplyWithError(chromeos::http::status_code::InternalServerError, |
| 37 | "Internal server error"); |
| 38 | } |
| 39 | } |
| 40 | |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 41 | void Response::AddHeader(const std::string& header_name, |
| 42 | const std::string& value) { |
| 43 | headers_.emplace(header_name, value); |
| 44 | } |
| 45 | |
| 46 | void Response::AddHeaders( |
| 47 | const std::vector<std::pair<std::string, std::string>>& headers) { |
| 48 | headers_.insert(headers.begin(), headers.end()); |
| 49 | } |
| 50 | |
| 51 | void Response::Reply(int status_code, |
| Alex Vakulenko | 330a10f | 2015-09-23 16:51:02 -0700 | [diff] [blame] | 52 | chromeos::StreamPtr data_stream, |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 53 | const std::string& mime_type) { |
| Alex Vakulenko | 330a10f | 2015-09-23 16:51:02 -0700 | [diff] [blame] | 54 | CHECK(data_stream); |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 55 | status_code_ = status_code; |
| Alex Vakulenko | 330a10f | 2015-09-23 16:51:02 -0700 | [diff] [blame] | 56 | data_stream_ = std::move(data_stream); |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 57 | AddHeader(chromeos::http::response_header::kContentType, mime_type); |
| 58 | SendResponse(); |
| 59 | } |
| 60 | |
| 61 | void Response::ReplyWithText(int status_code, |
| 62 | const std::string& text, |
| 63 | const std::string& mime_type) { |
| Alex Vakulenko | 330a10f | 2015-09-23 16:51:02 -0700 | [diff] [blame] | 64 | Reply(status_code, chromeos::MemoryStream::OpenCopyOf(text, nullptr), |
| 65 | mime_type); |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | void Response::ReplyWithJson(int status_code, const base::Value* json) { |
| 69 | std::string text; |
| Alex Vakulenko | e88b806 | 2015-06-15 12:53:22 -0700 | [diff] [blame] | 70 | base::JSONWriter::WriteWithOptions( |
| 71 | *json, base::JSONWriter::OPTIONS_PRETTY_PRINT, &text); |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 72 | std::string mime_type = chromeos::mime::AppendParameter( |
| 73 | chromeos::mime::application::kJson, |
| 74 | chromeos::mime::parameters::kCharset, |
| 75 | "utf-8"); |
| 76 | ReplyWithText(status_code, text, mime_type); |
| 77 | } |
| 78 | |
| 79 | void Response::ReplyWithJson(int status_code, |
| 80 | const std::map<std::string, std::string>& json) { |
| 81 | base::DictionaryValue json_value; |
| 82 | for (const auto& pair : json) { |
| 83 | json_value.SetString(pair.first, pair.second); |
| 84 | } |
| 85 | ReplyWithJson(status_code, &json_value); |
| 86 | } |
| 87 | |
| 88 | void Response::Redirect(int status_code, const std::string& redirect_url) { |
| 89 | AddHeader(chromeos::http::response_header::kLocation, redirect_url); |
| 90 | ReplyWithError(status_code, ""); |
| 91 | } |
| 92 | |
| 93 | void Response::ReplyWithError(int status_code, const std::string& error_text) { |
| 94 | status_code_ = status_code; |
| Alex Vakulenko | 330a10f | 2015-09-23 16:51:02 -0700 | [diff] [blame] | 95 | data_stream_ = chromeos::MemoryStream::OpenCopyOf(error_text, nullptr); |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 96 | SendResponse(); |
| 97 | } |
| 98 | |
| 99 | void Response::ReplyWithErrorNotFound() { |
| 100 | ReplyWithError(chromeos::http::status_code::NotFound, "Not Found"); |
| 101 | } |
| 102 | |
| 103 | void Response::SendResponse() { |
| 104 | CHECK(!reply_sent_) << "Response already sent"; |
| 105 | reply_sent_ = true; |
| Alex Vakulenko | 330a10f | 2015-09-23 16:51:02 -0700 | [diff] [blame] | 106 | handler_->CompleteRequest(request_id_, status_code_, headers_, |
| 107 | std::move(data_stream_)); |
| Alex Vakulenko | 039da31 | 2015-02-03 08:58:55 -0800 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | } // namespace libwebserv |