summaryrefslogtreecommitdiffstats
path: root/net/test/embedded_test_server/http_response.cc
blob: 759c0567bcc5c9ff989b3d0370a338b1b30a9ead (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "net/test/embedded_test_server/http_response.h"

#include "base/format_macros.h"
#include "base/logging.h"
#include "base/strings/stringprintf.h"

namespace net {
namespace test_server {

HttpResponse::~HttpResponse() {
}

BasicHttpResponse::BasicHttpResponse() : code_(SUCCESS) {
}

BasicHttpResponse::~BasicHttpResponse() {
}

std::string BasicHttpResponse::ToResponseString() const {
  // Response line with headers.
  std::string response_builder;

  // TODO(mtomasz): For http/1.0 requests, send http/1.0.
  // TODO(mtomasz): For different codes, send a corrent string instead of OK.
  base::StringAppendF(&response_builder, "HTTP/1.1 %d OK\r\n", code_);
  base::StringAppendF(&response_builder, "Connection: closed\r\n");
  base::StringAppendF(&response_builder,
                      "Content-Length: %"PRIuS"\r\n",
                      content_.size());
  base::StringAppendF(&response_builder,
                      "Content-Type: %s\r\n",
                      content_type_.c_str());
  for (std::map<std::string, std::string>::const_iterator it =
           custom_headers_.begin();
       it != custom_headers_.end();
       ++it) {
    // Multi-line header value support.
    const std::string& header_name = it->first;
    const std::string& header_value = it->second;
    DCHECK(header_value.find_first_of("\n\r") == std::string::npos) <<
        "Malformed header value.";
    base::StringAppendF(&response_builder,
                        "%s: %s\r\n",
                        header_name.c_str(),
                        header_value.c_str());
  }
  base::StringAppendF(&response_builder, "\r\n");

  return response_builder + content_;
}

}  // namespace test_server
}  // namespace net