| // Copyright 2015 The Android Open Source Project |
| // |
| // Licensed under the Apache License, Version 2.0 (the "License"); |
| // you may not use this file except in compliance with the License. |
| // You may obtain a copy of the License at |
| // |
| // http://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, software |
| // distributed under the License is distributed on an "AS IS" BASIS, |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| // See the License for the specific language governing permissions and |
| // limitations under the License. |
| |
| #include <libwebserv/response.h> |
| |
| #include <algorithm> |
| |
| #include <base/json/json_writer.h> |
| #include <base/logging.h> |
| #include <base/values.h> |
| #include <chromeos/http/http_request.h> |
| #include <chromeos/mime_utils.h> |
| #include <chromeos/streams/memory_stream.h> |
| #include <chromeos/strings/string_utils.h> |
| #include <libwebserv/protocol_handler.h> |
| |
| namespace libwebserv { |
| |
| Response::Response(ProtocolHandler* handler, const std::string& request_id) |
| : handler_{handler}, request_id_{request_id} { |
| } |
| |
| Response::~Response() { |
| if (!reply_sent_) { |
| ReplyWithError(chromeos::http::status_code::InternalServerError, |
| "Internal server error"); |
| } |
| } |
| |
| void Response::AddHeader(const std::string& header_name, |
| const std::string& value) { |
| headers_.emplace(header_name, value); |
| } |
| |
| void Response::AddHeaders( |
| const std::vector<std::pair<std::string, std::string>>& headers) { |
| headers_.insert(headers.begin(), headers.end()); |
| } |
| |
| void Response::Reply(int status_code, |
| chromeos::StreamPtr data_stream, |
| const std::string& mime_type) { |
| CHECK(data_stream); |
| status_code_ = status_code; |
| data_stream_ = std::move(data_stream); |
| AddHeader(chromeos::http::response_header::kContentType, mime_type); |
| SendResponse(); |
| } |
| |
| void Response::ReplyWithText(int status_code, |
| const std::string& text, |
| const std::string& mime_type) { |
| Reply(status_code, chromeos::MemoryStream::OpenCopyOf(text, nullptr), |
| mime_type); |
| } |
| |
| void Response::ReplyWithJson(int status_code, const base::Value* json) { |
| std::string text; |
| base::JSONWriter::WriteWithOptions( |
| *json, base::JSONWriter::OPTIONS_PRETTY_PRINT, &text); |
| std::string mime_type = chromeos::mime::AppendParameter( |
| chromeos::mime::application::kJson, |
| chromeos::mime::parameters::kCharset, |
| "utf-8"); |
| ReplyWithText(status_code, text, mime_type); |
| } |
| |
| void Response::ReplyWithJson(int status_code, |
| const std::map<std::string, std::string>& json) { |
| base::DictionaryValue json_value; |
| for (const auto& pair : json) { |
| json_value.SetString(pair.first, pair.second); |
| } |
| ReplyWithJson(status_code, &json_value); |
| } |
| |
| void Response::Redirect(int status_code, const std::string& redirect_url) { |
| AddHeader(chromeos::http::response_header::kLocation, redirect_url); |
| ReplyWithError(status_code, ""); |
| } |
| |
| void Response::ReplyWithError(int status_code, const std::string& error_text) { |
| status_code_ = status_code; |
| data_stream_ = chromeos::MemoryStream::OpenCopyOf(error_text, nullptr); |
| SendResponse(); |
| } |
| |
| void Response::ReplyWithErrorNotFound() { |
| ReplyWithError(chromeos::http::status_code::NotFound, "Not Found"); |
| } |
| |
| void Response::SendResponse() { |
| CHECK(!reply_sent_) << "Response already sent"; |
| reply_sent_ = true; |
| handler_->CompleteRequest(request_id_, status_code_, headers_, |
| std::move(data_stream_)); |
| } |
| |
| } // namespace libwebserv |