[email protected] | d8a5e9f9 | 2012-11-15 08:20:21 | [diff] [blame] | 1 | // 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] | b6e5b6c6 | 2013-05-07 17:50:26 | [diff] [blame] | 5 | #include "net/test/embedded_test_server/http_response.h" |
[email protected] | d8a5e9f9 | 2012-11-15 08:20:21 | [diff] [blame] | 6 | |
[email protected] | d8a5e9f9 | 2012-11-15 08:20:21 | [diff] [blame] | 7 | #include "base/format_macros.h" |
[email protected] | c0adcfd | 2013-01-24 04:49:29 | [diff] [blame] | 8 | #include "base/logging.h" |
[email protected] | d8a5e9f9 | 2012-11-15 08:20:21 | [diff] [blame] | 9 | #include "base/stringprintf.h" |
| 10 | |
[email protected] | eb7388f | 2013-05-09 17:00:26 | [diff] [blame] | 11 | namespace net { |
[email protected] | d8a5e9f9 | 2012-11-15 08:20:21 | [diff] [blame] | 12 | namespace test_server { |
| 13 | |
[email protected] | d8a5e9f9 | 2012-11-15 08:20:21 | [diff] [blame] | 14 | HttpResponse::~HttpResponse() { |
| 15 | } |
| 16 | |
[email protected] | 0d31fbc | 2013-05-28 17:00:37 | [diff] [blame^] | 17 | BasicHttpResponse::BasicHttpResponse() : code_(SUCCESS) { |
| 18 | } |
| 19 | |
| 20 | BasicHttpResponse::~BasicHttpResponse() { |
| 21 | } |
| 22 | |
| 23 | std::string BasicHttpResponse::ToResponseString() const { |
[email protected] | d8a5e9f9 | 2012-11-15 08:20:21 | [diff] [blame] | 24 | // Response line with headers. |
| 25 | std::string response_builder; |
| 26 | |
| 27 | // TODO(mtomasz): For http/1.0 requests, send http/1.0. |
| 28 | // TODO(mtomasz): For different codes, send a corrent string instead of OK. |
| 29 | base::StringAppendF(&response_builder, "HTTP/1.1 %d OK\r\n", code_); |
| 30 | base::StringAppendF(&response_builder, "Connection: closed\r\n"); |
| 31 | base::StringAppendF(&response_builder, |
| 32 | "Content-Length: %"PRIuS"\r\n", |
| 33 | content_.size()); |
| 34 | base::StringAppendF(&response_builder, |
| 35 | "Content-Type: %s\r\n", |
| 36 | content_type_.c_str()); |
| 37 | for (std::map<std::string, std::string>::const_iterator it = |
| 38 | custom_headers_.begin(); |
| 39 | it != custom_headers_.end(); |
| 40 | ++it) { |
| 41 | // Multi-line header value support. |
| 42 | const std::string& header_name = it->first; |
| 43 | const std::string& header_value = it->second; |
| 44 | DCHECK(header_value.find_first_of("\n\r") == std::string::npos) << |
| 45 | "Malformed header value."; |
| 46 | base::StringAppendF(&response_builder, |
| 47 | "%s: %s\r\n", |
| 48 | header_name.c_str(), |
| 49 | header_value.c_str()); |
| 50 | } |
| 51 | base::StringAppendF(&response_builder, "\r\n"); |
| 52 | |
| 53 | return response_builder + content_; |
| 54 | } |
| 55 | |
| 56 | } // namespace test_server |
[email protected] | eb7388f | 2013-05-09 17:00:26 | [diff] [blame] | 57 | } // namespace net |