forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow HttpServer response to include custom headers.
This is done by introducing a simple HttpServerResponseInfo class, and changing the root HttpServer::Send to accept it instead of a status, data, and mime type. BUG=none Review URL: https://chromiumcodereview.appspot.com/19637005 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@213227 0039d316-1c4b-4281-b951-d872f2087c98
- Loading branch information
kkania@chromium.org
committed
Jul 23, 2013
1 parent
f23a449
commit c700fd1
Showing
12 changed files
with
206 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright 2013 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/server/http_server_response_info.h" | ||
|
||
#include "base/format_macros.h" | ||
#include "base/strings/stringprintf.h" | ||
#include "net/http/http_request_headers.h" | ||
|
||
namespace net { | ||
|
||
HttpServerResponseInfo::HttpServerResponseInfo() : status_code_(HTTP_OK) {} | ||
|
||
HttpServerResponseInfo::HttpServerResponseInfo(HttpStatusCode status_code) | ||
: status_code_(status_code) {} | ||
|
||
HttpServerResponseInfo::~HttpServerResponseInfo() {} | ||
|
||
// static | ||
HttpServerResponseInfo HttpServerResponseInfo::CreateFor404() { | ||
HttpServerResponseInfo response(HTTP_NOT_FOUND); | ||
response.SetBody(std::string(), "text/html"); | ||
return response; | ||
} | ||
|
||
// static | ||
HttpServerResponseInfo HttpServerResponseInfo::CreateFor500( | ||
const std::string& body) { | ||
HttpServerResponseInfo response(HTTP_INTERNAL_SERVER_ERROR); | ||
response.SetBody(body, "text/html"); | ||
return response; | ||
} | ||
|
||
void HttpServerResponseInfo::AddHeader(const std::string& name, | ||
const std::string& value) { | ||
headers_.push_back(std::make_pair(name, value)); | ||
} | ||
|
||
void HttpServerResponseInfo::SetBody(const std::string& body, | ||
const std::string& content_type) { | ||
DCHECK(body_.empty()); | ||
body_ = body; | ||
AddHeader(HttpRequestHeaders::kContentLength, | ||
base::StringPrintf("%" PRIuS, body.length())); | ||
AddHeader(HttpRequestHeaders::kContentType, content_type); | ||
} | ||
|
||
std::string HttpServerResponseInfo::Serialize() const { | ||
std::string response = base::StringPrintf( | ||
"HTTP/1.1 %d %s\r\n", status_code_, GetHttpReasonPhrase(status_code_)); | ||
Headers::const_iterator header; | ||
for (header = headers_.begin(); header != headers_.end(); ++header) | ||
response += header->first + ":" + header->second + "\r\n"; | ||
|
||
return response + "\r\n" + body_; | ||
} | ||
|
||
HttpStatusCode HttpServerResponseInfo::status_code() const { | ||
return status_code_; | ||
} | ||
|
||
const std::string& HttpServerResponseInfo::body() const { | ||
return body_; | ||
} | ||
|
||
} // namespace net |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright 2013 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. | ||
|
||
#ifndef NET_SERVER_HTTP_SERVER_RESPONSE_INFO_H_ | ||
#define NET_SERVER_HTTP_SERVER_RESPONSE_INFO_H_ | ||
|
||
#include <string> | ||
#include <utility> | ||
#include <vector> | ||
|
||
#include "net/http/http_status_code.h" | ||
|
||
namespace net { | ||
|
||
class HttpServerResponseInfo { | ||
public: | ||
// Creates a 200 OK HttpServerResponseInfo. | ||
HttpServerResponseInfo(); | ||
explicit HttpServerResponseInfo(HttpStatusCode status_code); | ||
~HttpServerResponseInfo(); | ||
|
||
static HttpServerResponseInfo CreateFor404(); | ||
static HttpServerResponseInfo CreateFor500(const std::string& body); | ||
|
||
void AddHeader(const std::string& name, const std::string& value); | ||
|
||
// This also adds an appropriate Content-Length header. | ||
void SetBody(const std::string& body, const std::string& content_type); | ||
|
||
std::string Serialize() const; | ||
|
||
HttpStatusCode status_code() const; | ||
const std::string& body() const; | ||
|
||
private: | ||
typedef std::vector<std::pair<std::string, std::string> > Headers; | ||
|
||
HttpStatusCode status_code_; | ||
Headers headers_; | ||
std::string body_; | ||
}; | ||
|
||
} // namespace net | ||
|
||
#endif // NET_SERVER_HTTP_SERVER_RESPONSE_INFO_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright 2013 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/http/http_status_code.h" | ||
#include "net/server/http_server_response_info.h" | ||
#include "testing/gtest/include/gtest/gtest.h" | ||
|
||
namespace net { | ||
|
||
TEST(HttpServerResponseInfoTest, StatusLine) { | ||
HttpServerResponseInfo response; | ||
ASSERT_EQ(HTTP_OK, response.status_code()); | ||
ASSERT_EQ("HTTP/1.1 200 OK\r\n\r\n", response.Serialize()); | ||
} | ||
|
||
TEST(HttpServerResponseInfoTest, Headers) { | ||
HttpServerResponseInfo response; | ||
response.AddHeader("A", "1"); | ||
response.AddHeader("A", "2"); | ||
ASSERT_EQ("HTTP/1.1 200 OK\r\nA:1\r\nA:2\r\n\r\n", response.Serialize()); | ||
} | ||
|
||
TEST(HttpServerResponseInfoTest, Body) { | ||
HttpServerResponseInfo response; | ||
ASSERT_EQ(std::string(), response.body()); | ||
response.SetBody("body", "type"); | ||
ASSERT_EQ("body", response.body()); | ||
ASSERT_EQ( | ||
"HTTP/1.1 200 OK\r\nContent-Length:4\r\nContent-Type:type\r\n\r\nbody", | ||
response.Serialize()); | ||
} | ||
|
||
TEST(HttpServerResponseInfoTest, CreateFor404) { | ||
HttpServerResponseInfo response = HttpServerResponseInfo::CreateFor404(); | ||
ASSERT_EQ( | ||
"HTTP/1.1 404 Not Found\r\n" | ||
"Content-Length:0\r\nContent-Type:text/html\r\n\r\n", | ||
response.Serialize()); | ||
} | ||
|
||
TEST(HttpServerResponseInfoTest, CreateFor500) { | ||
HttpServerResponseInfo response = | ||
HttpServerResponseInfo::CreateFor500("mess"); | ||
ASSERT_EQ( | ||
"HTTP/1.1 500 Internal Server Error\r\n" | ||
"Content-Length:4\r\nContent-Type:text/html\r\n\r\nmess", | ||
response.Serialize()); | ||
} | ||
|
||
} // namespace net |
Oops, something went wrong.