Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(58)

Side by Side Diff: net/server/http_server_response_info.cc

Issue 19637005: Allow HttpServer response to include custom headers. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: address comments Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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.AddHeader(HttpRequestHeaders::kContentLength, "0");
mmenke 2013/07/19 16:17:44 Maybe SetBody("");?
24 return response;
25 }
26
27 // static
28 HttpServerResponseInfo HttpServerResponseInfo::CreateFor500(
29 const std::string& message) {
30 HttpServerResponseInfo response(HTTP_INTERNAL_SERVER_ERROR);
31 response.SetBody(message, "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) {
mmenke 2013/07/19 16:17:44 Maybe DCHECK(body_.empty());?
42 body_ = body;
43 AddHeader(HttpRequestHeaders::kContentLength,
44 base::StringPrintf("%" PRIuS, body.length()));
45 AddHeader(HttpRequestHeaders::kContentType, content_type);
46 }
47
48 std::string HttpServerResponseInfo::Serialize() const {
49 std::string response = base::StringPrintf(
50 "HTTP/1.1 %d %s\r\n", status_code_, GetHttpReasonPhrase(status_code_));
51 Headers::const_iterator header;
52 for (header = headers_.begin(); header != headers_.end(); ++header)
53 response += header->first + ":" + header->second + "\r\n";
54
55 return response + "\r\n" + body_;
56 }
57
58 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698