blob: fd5521197ba93aab14a4bbc921eea01050b608a9 [file] [log] [blame]
[email protected]d8a5e9f92012-11-15 08:20:211// Copyright (c) 2012 The Chromium 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
[email protected]b6e5b6c62013-05-07 17:50:265#include "net/test/embedded_test_server/http_response.h"
[email protected]d8a5e9f92012-11-15 08:20:216
[email protected]d8a5e9f92012-11-15 08:20:217#include "base/format_macros.h"
[email protected]c0adcfd2013-01-24 04:49:298#include "base/logging.h"
jam29f1aed42017-06-06 21:37:439#include "base/strings/string_util.h"
[email protected]4dc3ad4f2013-06-11 07:15:5010#include "base/strings/stringprintf.h"
[email protected]c1dffe82013-06-26 20:59:0511#include "net/http/http_status_code.h"
[email protected]d8a5e9f92012-11-15 08:20:2112
[email protected]eb7388f2013-05-09 17:00:2613namespace net {
[email protected]d8a5e9f92012-11-15 08:20:2114namespace test_server {
15
[email protected]d8a5e9f92012-11-15 08:20:2116HttpResponse::~HttpResponse() {
17}
18
svaldez6e7e82a22015-10-28 19:39:5319RawHttpResponse::RawHttpResponse(const std::string& headers,
20 const std::string& contents)
21 : headers_(headers), contents_(contents) {}
22
23RawHttpResponse::~RawHttpResponse() {}
24
25void RawHttpResponse::SendResponse(const SendBytesCallback& send,
26 const SendCompleteCallback& done) {
27 std::string response;
jam29f1aed42017-06-06 21:37:4328 if (!headers_.empty()) {
29 response = headers_;
Min Qin32c60fd2017-10-02 23:38:2330 // LocateEndOfHeadersHelper() searches for the first "\n\n" and "\n\r\n" as
31 // the end of the header.
32 std::size_t index = response.find_last_not_of("\r\n");
33 if (index != std::string::npos)
34 response.erase(index + 1);
35 response += "\n\n";
36 response += contents_;
jam29f1aed42017-06-06 21:37:4337 } else {
svaldez6e7e82a22015-10-28 19:39:5338 response = contents_;
jam29f1aed42017-06-06 21:37:4339 }
svaldez6e7e82a22015-10-28 19:39:5340 send.Run(response, done);
41}
42
43void RawHttpResponse::AddHeader(const std::string& key_value_pair) {
44 headers_.append(base::StringPrintf("%s\r\n", key_value_pair.c_str()));
45}
46
[email protected]c1dffe82013-06-26 20:59:0547BasicHttpResponse::BasicHttpResponse() : code_(HTTP_OK) {
[email protected]0d31fbc2013-05-28 17:00:3748}
49
50BasicHttpResponse::~BasicHttpResponse() {
51}
52
53std::string BasicHttpResponse::ToResponseString() const {
[email protected]d8a5e9f92012-11-15 08:20:2154 // Response line with headers.
55 std::string response_builder;
56
[email protected]c1dffe82013-06-26 20:59:0557 std::string http_reason_phrase(GetHttpReasonPhrase(code_));
58
[email protected]d8a5e9f92012-11-15 08:20:2159 // TODO(mtomasz): For http/1.0 requests, send http/1.0.
[email protected]c1dffe82013-06-26 20:59:0560 base::StringAppendF(&response_builder,
61 "HTTP/1.1 %d %s\r\n",
62 code_,
63 http_reason_phrase.c_str());
64 base::StringAppendF(&response_builder, "Connection: close\r\n");
svaldez6e7e82a22015-10-28 19:39:5365
66 base::StringAppendF(&response_builder, "Content-Length: %" PRIuS "\r\n",
[email protected]d8a5e9f92012-11-15 08:20:2167 content_.size());
svaldez6e7e82a22015-10-28 19:39:5368 base::StringAppendF(&response_builder, "Content-Type: %s\r\n",
[email protected]d8a5e9f92012-11-15 08:20:2169 content_type_.c_str());
[email protected]c1dffe82013-06-26 20:59:0570 for (size_t i = 0; i < custom_headers_.size(); ++i) {
71 const std::string& header_name = custom_headers_[i].first;
72 const std::string& header_value = custom_headers_[i].second;
[email protected]d8a5e9f92012-11-15 08:20:2173 DCHECK(header_value.find_first_of("\n\r") == std::string::npos) <<
74 "Malformed header value.";
75 base::StringAppendF(&response_builder,
76 "%s: %s\r\n",
77 header_name.c_str(),
78 header_value.c_str());
79 }
80 base::StringAppendF(&response_builder, "\r\n");
81
82 return response_builder + content_;
83}
84
svaldez6e7e82a22015-10-28 19:39:5385void BasicHttpResponse::SendResponse(const SendBytesCallback& send,
86 const SendCompleteCallback& done) {
87 send.Run(ToResponseString(), done);
88}
89
mmenke173a6aa2016-10-17 19:14:0990void HungResponse::SendResponse(const SendBytesCallback& send,
91 const SendCompleteCallback& done) {}
92
[email protected]d8a5e9f92012-11-15 08:20:2193} // namespace test_server
[email protected]eb7388f2013-05-09 17:00:2694} // namespace net