| OLD | NEW | 
|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be | 
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. | 
| 4 | 4 | 
| 5 #include "net/test/embedded_test_server/http_response.h" | 5 #include "net/test/embedded_test_server/http_response.h" | 
| 6 | 6 | 
| 7 #include "base/format_macros.h" | 7 #include "base/format_macros.h" | 
| 8 #include "base/logging.h" | 8 #include "base/logging.h" | 
| 9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" | 
| 10 #include "net/http/http_status_code.h" | 10 #include "net/http/http_status_code.h" | 
| 11 | 11 | 
| 12 namespace net { | 12 namespace net { | 
| 13 namespace test_server { | 13 namespace test_server { | 
| 14 | 14 | 
| 15 HttpResponse::~HttpResponse() { | 15 HttpResponse::~HttpResponse() { | 
| 16 } | 16 } | 
| 17 | 17 | 
| 18 RawHttpResponse::RawHttpResponse(const std::string& headers, |  | 
| 19                                  const std::string& contents) |  | 
| 20     : headers_(headers), contents_(contents) {} |  | 
| 21 |  | 
| 22 RawHttpResponse::~RawHttpResponse() {} |  | 
| 23 |  | 
| 24 void RawHttpResponse::SendResponse(const SendBytesCallback& send, |  | 
| 25                                    const SendCompleteCallback& done) { |  | 
| 26   std::string response; |  | 
| 27   if (!headers_.empty()) |  | 
| 28     response = headers_ + "\r\n" + contents_; |  | 
| 29   else |  | 
| 30     response = contents_; |  | 
| 31   send.Run(response, done); |  | 
| 32 } |  | 
| 33 |  | 
| 34 void RawHttpResponse::AddHeader(const std::string& key_value_pair) { |  | 
| 35   headers_.append(base::StringPrintf("%s\r\n", key_value_pair.c_str())); |  | 
| 36 } |  | 
| 37 |  | 
| 38 BasicHttpResponse::BasicHttpResponse() : code_(HTTP_OK) { | 18 BasicHttpResponse::BasicHttpResponse() : code_(HTTP_OK) { | 
| 39 } | 19 } | 
| 40 | 20 | 
| 41 BasicHttpResponse::~BasicHttpResponse() { | 21 BasicHttpResponse::~BasicHttpResponse() { | 
| 42 } | 22 } | 
| 43 | 23 | 
| 44 std::string BasicHttpResponse::ToResponseString() const { | 24 std::string BasicHttpResponse::ToResponseString() const { | 
| 45   // Response line with headers. | 25   // Response line with headers. | 
| 46   std::string response_builder; | 26   std::string response_builder; | 
| 47 | 27 | 
| 48   std::string http_reason_phrase(GetHttpReasonPhrase(code_)); | 28   std::string http_reason_phrase(GetHttpReasonPhrase(code_)); | 
| 49 | 29 | 
| 50   // TODO(mtomasz): For http/1.0 requests, send http/1.0. | 30   // TODO(mtomasz): For http/1.0 requests, send http/1.0. | 
| 51   base::StringAppendF(&response_builder, | 31   base::StringAppendF(&response_builder, | 
| 52                       "HTTP/1.1 %d %s\r\n", | 32                       "HTTP/1.1 %d %s\r\n", | 
| 53                       code_, | 33                       code_, | 
| 54                       http_reason_phrase.c_str()); | 34                       http_reason_phrase.c_str()); | 
| 55   base::StringAppendF(&response_builder, "Connection: close\r\n"); | 35   base::StringAppendF(&response_builder, "Connection: close\r\n"); | 
| 56 | 36   base::StringAppendF(&response_builder, | 
| 57   base::StringAppendF(&response_builder, "Content-Length: %" PRIuS "\r\n", | 37                       "Content-Length: %" PRIuS "\r\n", | 
| 58                       content_.size()); | 38                       content_.size()); | 
| 59   base::StringAppendF(&response_builder, "Content-Type: %s\r\n", | 39   base::StringAppendF(&response_builder, | 
|  | 40                       "Content-Type: %s\r\n", | 
| 60                       content_type_.c_str()); | 41                       content_type_.c_str()); | 
| 61   for (size_t i = 0; i < custom_headers_.size(); ++i) { | 42   for (size_t i = 0; i < custom_headers_.size(); ++i) { | 
| 62     const std::string& header_name = custom_headers_[i].first; | 43     const std::string& header_name = custom_headers_[i].first; | 
| 63     const std::string& header_value = custom_headers_[i].second; | 44     const std::string& header_value = custom_headers_[i].second; | 
| 64     DCHECK(header_value.find_first_of("\n\r") == std::string::npos) << | 45     DCHECK(header_value.find_first_of("\n\r") == std::string::npos) << | 
| 65         "Malformed header value."; | 46         "Malformed header value."; | 
| 66     base::StringAppendF(&response_builder, | 47     base::StringAppendF(&response_builder, | 
| 67                         "%s: %s\r\n", | 48                         "%s: %s\r\n", | 
| 68                         header_name.c_str(), | 49                         header_name.c_str(), | 
| 69                         header_value.c_str()); | 50                         header_value.c_str()); | 
| 70   } | 51   } | 
| 71   base::StringAppendF(&response_builder, "\r\n"); | 52   base::StringAppendF(&response_builder, "\r\n"); | 
| 72 | 53 | 
| 73   return response_builder + content_; | 54   return response_builder + content_; | 
| 74 } | 55 } | 
| 75 | 56 | 
| 76 void BasicHttpResponse::SendResponse(const SendBytesCallback& send, |  | 
| 77                                      const SendCompleteCallback& done) { |  | 
| 78   send.Run(ToResponseString(), done); |  | 
| 79 } |  | 
| 80 |  | 
| 81 }  // namespace test_server | 57 }  // namespace test_server | 
| 82 }  // namespace net | 58 }  // namespace net | 
| OLD | NEW | 
|---|