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 #include "net/http/http_request_headers.h" |
| 10 |
| 11 namespace net { |
| 12 |
| 13 HttpServerResponseInfo::HttpServerResponseInfo() : status_code_(HTTP_OK) {} |
| 14 |
| 15 HttpServerResponseInfo::HttpServerResponseInfo(HttpStatusCode status_code) |
| 16 : status_code_(status_code) {} |
| 17 |
| 18 HttpServerResponseInfo::~HttpServerResponseInfo() {} |
| 19 |
| 20 // static |
| 21 HttpServerResponseInfo HttpServerResponseInfo::CreateFor404() { |
| 22 HttpServerResponseInfo response(HTTP_NOT_FOUND); |
| 23 response.SetBody(std::string(), "text/html"); |
| 24 return response; |
| 25 } |
| 26 |
| 27 // static |
| 28 HttpServerResponseInfo HttpServerResponseInfo::CreateFor500( |
| 29 const std::string& body) { |
| 30 HttpServerResponseInfo response(HTTP_INTERNAL_SERVER_ERROR); |
| 31 response.SetBody(body, "text/html"); |
| 32 return response; |
| 33 } |
| 34 |
| 35 void HttpServerResponseInfo::AddHeader(const std::string& name, |
| 36 const std::string& value) { |
| 37 headers_.push_back(std::make_pair(name, value)); |
| 38 } |
| 39 |
| 40 void HttpServerResponseInfo::SetBody(const std::string& body, |
| 41 const std::string& content_type) { |
| 42 DCHECK(body_.empty()); |
| 43 body_ = body; |
| 44 AddHeader(HttpRequestHeaders::kContentLength, |
| 45 base::StringPrintf("%" PRIuS, body.length())); |
| 46 AddHeader(HttpRequestHeaders::kContentType, content_type); |
| 47 } |
| 48 |
| 49 std::string HttpServerResponseInfo::Serialize() const { |
| 50 std::string response = base::StringPrintf( |
| 51 "HTTP/1.1 %d %s\r\n", status_code_, GetHttpReasonPhrase(status_code_)); |
| 52 Headers::const_iterator header; |
| 53 for (header = headers_.begin(); header != headers_.end(); ++header) |
| 54 response += header->first + ":" + header->second + "\r\n"; |
| 55 |
| 56 return response + "\r\n" + body_; |
| 57 } |
| 58 |
| 59 HttpStatusCode HttpServerResponseInfo::status_code() const { |
| 60 return status_code_; |
| 61 } |
| 62 |
| 63 const std::string& HttpServerResponseInfo::body() const { |
| 64 return body_; |
| 65 } |
| 66 |
| 67 } // namespace net |
OLD | NEW |