blob: 9fc61d61815025a7e27d1f937112ca305e910864 [file] [log] [blame]
Daniel Erat35f65872015-08-17 20:59:29 -06001// 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 Vakulenko039da312015-02-03 08:58:55 -080014
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 Vakulenko330a10f2015-09-23 16:51:02 -070024#include <chromeos/streams/memory_stream.h>
Alex Vakulenko039da312015-02-03 08:58:55 -080025#include <chromeos/strings/string_utils.h>
Alex Vakulenko31a63792015-02-03 12:44:57 -080026#include <libwebserv/protocol_handler.h>
Alex Vakulenko039da312015-02-03 08:58:55 -080027
28namespace libwebserv {
29
Alex Vakulenko31a63792015-02-03 12:44:57 -080030Response::Response(ProtocolHandler* handler, const std::string& request_id)
31 : handler_{handler}, request_id_{request_id} {
Alex Vakulenko039da312015-02-03 08:58:55 -080032}
33
34Response::~Response() {
35 if (!reply_sent_) {
36 ReplyWithError(chromeos::http::status_code::InternalServerError,
37 "Internal server error");
38 }
39}
40
Alex Vakulenko039da312015-02-03 08:58:55 -080041void Response::AddHeader(const std::string& header_name,
42 const std::string& value) {
43 headers_.emplace(header_name, value);
44}
45
46void Response::AddHeaders(
47 const std::vector<std::pair<std::string, std::string>>& headers) {
48 headers_.insert(headers.begin(), headers.end());
49}
50
51void Response::Reply(int status_code,
Alex Vakulenko330a10f2015-09-23 16:51:02 -070052 chromeos::StreamPtr data_stream,
Alex Vakulenko039da312015-02-03 08:58:55 -080053 const std::string& mime_type) {
Alex Vakulenko330a10f2015-09-23 16:51:02 -070054 CHECK(data_stream);
Alex Vakulenko039da312015-02-03 08:58:55 -080055 status_code_ = status_code;
Alex Vakulenko330a10f2015-09-23 16:51:02 -070056 data_stream_ = std::move(data_stream);
Alex Vakulenko039da312015-02-03 08:58:55 -080057 AddHeader(chromeos::http::response_header::kContentType, mime_type);
58 SendResponse();
59}
60
61void Response::ReplyWithText(int status_code,
62 const std::string& text,
63 const std::string& mime_type) {
Alex Vakulenko330a10f2015-09-23 16:51:02 -070064 Reply(status_code, chromeos::MemoryStream::OpenCopyOf(text, nullptr),
65 mime_type);
Alex Vakulenko039da312015-02-03 08:58:55 -080066}
67
68void Response::ReplyWithJson(int status_code, const base::Value* json) {
69 std::string text;
Alex Vakulenkoe88b8062015-06-15 12:53:22 -070070 base::JSONWriter::WriteWithOptions(
71 *json, base::JSONWriter::OPTIONS_PRETTY_PRINT, &text);
Alex Vakulenko039da312015-02-03 08:58:55 -080072 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
79void 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
88void 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
93void Response::ReplyWithError(int status_code, const std::string& error_text) {
94 status_code_ = status_code;
Alex Vakulenko330a10f2015-09-23 16:51:02 -070095 data_stream_ = chromeos::MemoryStream::OpenCopyOf(error_text, nullptr);
Alex Vakulenko039da312015-02-03 08:58:55 -080096 SendResponse();
97}
98
99void Response::ReplyWithErrorNotFound() {
100 ReplyWithError(chromeos::http::status_code::NotFound, "Not Found");
101}
102
103void Response::SendResponse() {
104 CHECK(!reply_sent_) << "Response already sent";
105 reply_sent_ = true;
Alex Vakulenko330a10f2015-09-23 16:51:02 -0700106 handler_->CompleteRequest(request_id_, status_code_, headers_,
107 std::move(data_stream_));
Alex Vakulenko039da312015-02-03 08:58:55 -0800108}
109
110} // namespace libwebserv