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