OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013 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/server/http_server_response_info.h" | |
6 | |
7 #include "base/format_macros.h" | |
8 #include "base/strings/stringprintf.h" | |
9 | |
10 namespace net { | |
11 | |
12 HttpServerResponseInfo::HttpServerResponseInfo() : status_code_(HTTP_OK) {} | |
13 | |
14 HttpServerResponseInfo::HttpServerResponseInfo(HttpStatusCode status_code) | |
15 : status_code_(status_code) {} | |
16 | |
17 // static | |
18 HttpServerResponseInfo HttpServerResponseInfo::For404() { | |
19 HttpServerResponseInfo response(HTTP_NOT_FOUND); | |
20 response.AddHeader("Content-Length", "0"); | |
pfeldman
2013/07/19 12:47:57
Lets use constants from net::HttpRequestHeaders (n
kkania
2013/07/19 15:28:36
Done.
| |
21 return response; | |
22 } | |
23 | |
24 // static | |
25 HttpServerResponseInfo HttpServerResponseInfo::For500( | |
26 const std::string& message) { | |
27 HttpServerResponseInfo response(HTTP_INTERNAL_SERVER_ERROR); | |
28 response.SetBody(message, "text/html"); | |
29 return response; | |
30 } | |
31 | |
32 void HttpServerResponseInfo::AddHeader(const std::string& name, | |
33 const std::string& value) { | |
34 headers_.push_back(std::make_pair(name, value)); | |
35 } | |
36 | |
37 void HttpServerResponseInfo::SetBody(const std::string& body, | |
38 const std::string& content_type) { | |
39 body_ = body; | |
40 AddHeader("Content-Length", base::StringPrintf("%"PRIuS, body.length())); | |
41 AddHeader("Content-Type", content_type); | |
42 } | |
43 | |
44 std::string HttpServerResponseInfo::Serialize() const { | |
45 std::string response = base::StringPrintf( | |
46 "HTTP/1.1 %d %s\r\n", status_code_, GetHttpReasonPhrase(status_code_)); | |
pfeldman
2013/07/19 12:47:57
Extract const
| |
47 for (Headers::const_iterator header = headers_.begin(); | |
48 header != headers_.end(); | |
49 ++header) { | |
50 response += header->first + ":" + header->second + "\r\n"; | |
51 } | |
52 return response + "\r\n" + body_; | |
53 } | |
54 | |
55 } // namespace net | |
OLD | NEW |