diff options
author | tyoshino@chromium.org <tyoshino@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-06-07 12:51:38 +0000 |
---|---|---|
committer | tyoshino@chromium.org <tyoshino@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-06-07 12:51:38 +0000 |
commit | ff51404a38c4ff7e9ff98135facfd2701e3e9f09 (patch) | |
tree | 6364f224182701ffb0ef85d6f4cfa83340a1cf47 | |
parent | a765cc1a419696309423c3e6389bc6572530d617 (diff) | |
download | chromium_src-ff51404a38c4ff7e9ff98135facfd2701e3e9f09.zip chromium_src-ff51404a38c4ff7e9ff98135facfd2701e3e9f09.tar.gz chromium_src-ff51404a38c4ff7e9ff98135facfd2701e3e9f09.tar.bz2 |
Factor out HTTP status code and reason phrase in Blob and Stream infrastructure into net/.
TEST=
out/Debug/content_unittests --gtest_filter='*Stream*'
out/Debug/content_unittests --gtest_filter='*Blob*'
out/Debug/net_unittests
Review URL: https://chromiumcodereview.appspot.com/16120005
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@204797 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | content/browser/streams/stream_url_request_job.cc | 42 | ||||
-rw-r--r-- | content/browser/streams/stream_url_request_job.h | 3 | ||||
-rw-r--r-- | net/http/http_status_code.cc | 36 | ||||
-rw-r--r-- | net/http/http_status_code.h | 12 | ||||
-rw-r--r-- | net/net.gyp | 1 | ||||
-rw-r--r-- | webkit/browser/blob/blob_url_request_job.cc | 59 | ||||
-rw-r--r-- | webkit/browser/blob/blob_url_request_job.h | 3 |
7 files changed, 75 insertions, 81 deletions
diff --git a/content/browser/streams/stream_url_request_job.cc b/content/browser/streams/stream_url_request_job.cc index a497667..4bcc511 100644 --- a/content/browser/streams/stream_url_request_job.cc +++ b/content/browser/streams/stream_url_request_job.cc @@ -16,22 +16,6 @@ namespace content { -namespace { - -const int kHTTPOk = 200; -const int kHTTPNotAllowed = 403; -const int kHTTPNotFound = 404; -const int kHTTPMethodNotAllow = 405; -const int kHTTPInternalError = 500; - -const char kHTTPOKText[] = "OK"; -const char kHTTPNotAllowedText[] = "Not Allowed"; -const char kHTTPNotFoundText[] = "Not Found"; -const char kHTTPMethodNotAllowText[] = "Method Not Allowed"; -const char kHTTPInternalErrorText[] = "Internal Server Error"; - -} // namespace - StreamURLRequestJob::StreamURLRequestJob( net::URLRequest* request, net::NetworkDelegate* network_delegate, @@ -167,7 +151,7 @@ void StreamURLRequestJob::DidStart() { return; } - HeadersCompleted(kHTTPOk, kHTTPOKText); + HeadersCompleted(net::HTTP_OK); } void StreamURLRequestJob::NotifyFailure(int error_code) { @@ -182,44 +166,36 @@ void StreamURLRequestJob::NotifyFailure(int error_code) { } // TODO(zork): Share these with BlobURLRequestJob. - int status_code = 0; + net::HttpStatusCode status_code = net::HTTP_INTERNAL_SERVER_ERROR; std::string status_txt; switch (error_code) { case net::ERR_ACCESS_DENIED: - status_code = kHTTPNotAllowed; - status_txt = kHTTPNotAllowedText; + status_code = net::HTTP_FORBIDDEN; break; case net::ERR_FILE_NOT_FOUND: - status_code = kHTTPNotFound; - status_txt = kHTTPNotFoundText; + status_code = net::HTTP_NOT_FOUND; break; case net::ERR_METHOD_NOT_SUPPORTED: - status_code = kHTTPMethodNotAllow; - status_txt = kHTTPMethodNotAllowText; + status_code = net::HTTP_METHOD_NOT_ALLOWED; break; case net::ERR_FAILED: - status_code = kHTTPInternalError; - status_txt = kHTTPInternalErrorText; break; default: DCHECK(false); - status_code = kHTTPInternalError; - status_txt = kHTTPInternalErrorText; break; } - HeadersCompleted(status_code, status_txt); + HeadersCompleted(status_code); } -void StreamURLRequestJob::HeadersCompleted(int status_code, - const std::string& status_text) { +void StreamURLRequestJob::HeadersCompleted(net::HttpStatusCode status_code) { std::string status("HTTP/1.1 "); status.append(base::IntToString(status_code)); status.append(" "); - status.append(status_text); + status.append(net::GetHttpReasonPhrase(status_code)); status.append("\0\0", 2); net::HttpResponseHeaders* headers = new net::HttpResponseHeaders(status); - if (status_code == kHTTPOk) { + if (status_code == net::HTTP_OK) { std::string content_type_header(net::HttpRequestHeaders::kContentType); content_type_header.append(": "); content_type_header.append("plain/text"); diff --git a/content/browser/streams/stream_url_request_job.h b/content/browser/streams/stream_url_request_job.h index d087198..03187bf 100644 --- a/content/browser/streams/stream_url_request_job.h +++ b/content/browser/streams/stream_url_request_job.h @@ -5,6 +5,7 @@ #ifndef CONTENT_BROWSER_STREAMS_STREAM_URL_REQUEST_JOB_H_ #define CONTENT_BROWSER_STREAMS_STREAM_URL_REQUEST_JOB_H_ +#include "net/http/http_status_code.h" #include "net/url_request/url_request_job.h" #include "content/browser/streams/stream_read_observer.h" #include "content/common/content_export.h" @@ -43,7 +44,7 @@ class CONTENT_EXPORT StreamURLRequestJob private: void DidStart(); void NotifyFailure(int); - void HeadersCompleted(int status_code, const std::string& status_txt); + void HeadersCompleted(net::HttpStatusCode status_code); void ClearStream(); base::WeakPtrFactory<StreamURLRequestJob> weak_factory_; diff --git a/net/http/http_status_code.cc b/net/http/http_status_code.cc new file mode 100644 index 0000000..462202b --- /dev/null +++ b/net/http/http_status_code.cc @@ -0,0 +1,36 @@ +// 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 "base/logging.h" + +namespace net { + +const char* GetHttpReasonPhrase(HttpStatusCode code) { + switch (code) { + case HTTP_CONTINUE: + return "Continue"; + case HTTP_OK: + return "OK"; + case HTTP_PARTIAL_CONTENT: + return "Partial Content"; + case HTTP_FORBIDDEN: + return "Forbidden"; + case HTTP_NOT_FOUND: + return "Not Found"; + case HTTP_METHOD_NOT_ALLOWED: + return "Method Not Allowed"; + case HTTP_REQUESTED_RANGE_NOT_SATISFIABLE: + return "Range Not Satisfiable"; + case HTTP_INTERNAL_SERVER_ERROR: + return "Internal Server Error"; + default: + NOTREACHED(); + } + + return ""; +} + +} // namespace net diff --git a/net/http/http_status_code.h b/net/http/http_status_code.h index 0ae47d6..8780c71 100644 --- a/net/http/http_status_code.h +++ b/net/http/http_status_code.h @@ -5,6 +5,8 @@ #ifndef NET_HTTP_HTTP_STATUS_CODE_H_ #define NET_HTTP_HTTP_STATUS_CODE_H_ +#include "net/base/net_export.h" + namespace net { // HTTP status codes. @@ -62,6 +64,16 @@ enum HttpStatusCode { HTTP_VERSION_NOT_SUPPORTED = 505, }; +// Returns the corresponding HTTP status description to use in the Reason-Phrase +// field in an HTTP response for given |code|. It's based on the IANA HTTP +// Status Code Registry. +// http://www.iana.org/assignments/http-status-codes/http-status-codes.xml +// +// This function doesn't cover all codes defined above. It returns an empty +// string (or crash in debug build) for status codes which are not yet covered +// or just invalid. Please extend it when needed. +NET_EXPORT const char* GetHttpReasonPhrase(HttpStatusCode code); + } // namespace net #endif // NET_HTTP_HTTP_STATUS_CODE_H_ diff --git a/net/net.gyp b/net/net.gyp index 73e171d..f8c3d30 100644 --- a/net/net.gyp +++ b/net/net.gyp @@ -570,6 +570,7 @@ 'http/http_server_properties.h', 'http/http_server_properties_impl.cc', 'http/http_server_properties_impl.h', + 'http/http_status_code.cc', 'http/http_status_code.h', 'http/http_stream.h', 'http/http_stream_base.h', diff --git a/webkit/browser/blob/blob_url_request_job.cc b/webkit/browser/blob/blob_url_request_job.cc index e41b976..aaf38e7 100644 --- a/webkit/browser/blob/blob_url_request_job.cc +++ b/webkit/browser/blob/blob_url_request_job.cc @@ -32,23 +32,6 @@ namespace webkit_blob { namespace { -const int kHTTPOk = 200; -const int kHTTPPartialContent = 206; -const int kHTTPNotAllowed = 403; -const int kHTTPNotFound = 404; -const int kHTTPMethodNotAllow = 405; -const int kHTTPRequestedRangeNotSatisfiable = 416; -const int kHTTPInternalError = 500; - -const char kHTTPOKText[] = "OK"; -const char kHTTPPartialContentText[] = "Partial Content"; -const char kHTTPNotAllowedText[] = "Not Allowed"; -const char kHTTPNotFoundText[] = "Not Found"; -const char kHTTPMethodNotAllowText[] = "Method Not Allowed"; -const char kHTTPRequestedRangeNotSatisfiableText[] = - "Requested Range Not Satisfiable"; -const char kHTTPInternalErrorText[] = "Internal Server Error"; - bool IsFileType(BlobData::Item::Type type) { switch (type) { case BlobData::Item::TYPE_FILE: @@ -473,16 +456,10 @@ bool BlobURLRequestJob::ReadLoop(int* bytes_read) { } void BlobURLRequestJob::NotifySuccess() { - int status_code = 0; - std::string status_text; - if (byte_range_set_ && byte_range_.IsValid()) { - status_code = kHTTPPartialContent; - status_text += kHTTPPartialContentText; - } else { - status_code = kHTTPOk; - status_text = kHTTPOKText; - } - HeadersCompleted(status_code, status_text); + net::HttpStatusCode status_code = net::HTTP_OK; + if (byte_range_set_ && byte_range_.IsValid()) + status_code = net::HTTP_PARTIAL_CONTENT; + HeadersCompleted(status_code); } void BlobURLRequestJob::NotifyFailure(int error_code) { @@ -496,48 +473,38 @@ void BlobURLRequestJob::NotifyFailure(int error_code) { return; } - int status_code = 0; - std::string status_txt; + net::HttpStatusCode status_code = net::HTTP_INTERNAL_SERVER_ERROR; switch (error_code) { case net::ERR_ACCESS_DENIED: - status_code = kHTTPNotAllowed; - status_txt = kHTTPNotAllowedText; + status_code = net::HTTP_FORBIDDEN; break; case net::ERR_FILE_NOT_FOUND: - status_code = kHTTPNotFound; - status_txt = kHTTPNotFoundText; + status_code = net::HTTP_NOT_FOUND; break; case net::ERR_METHOD_NOT_SUPPORTED: - status_code = kHTTPMethodNotAllow; - status_txt = kHTTPMethodNotAllowText; + status_code = net::HTTP_METHOD_NOT_ALLOWED; break; case net::ERR_REQUEST_RANGE_NOT_SATISFIABLE: - status_code = kHTTPRequestedRangeNotSatisfiable; - status_txt = kHTTPRequestedRangeNotSatisfiableText; + status_code = net::HTTP_REQUESTED_RANGE_NOT_SATISFIABLE; break; case net::ERR_FAILED: - status_code = kHTTPInternalError; - status_txt = kHTTPInternalErrorText; break; default: DCHECK(false); - status_code = kHTTPInternalError; - status_txt = kHTTPInternalErrorText; break; } - HeadersCompleted(status_code, status_txt); + HeadersCompleted(status_code); } -void BlobURLRequestJob::HeadersCompleted(int status_code, - const std::string& status_text) { +void BlobURLRequestJob::HeadersCompleted(net::HttpStatusCode status_code) { std::string status("HTTP/1.1 "); status.append(base::IntToString(status_code)); status.append(" "); - status.append(status_text); + status.append(net::GetHttpReasonPhrase(status_code)); status.append("\0\0", 2); net::HttpResponseHeaders* headers = new net::HttpResponseHeaders(status); - if (status_code == kHTTPOk || status_code == kHTTPPartialContent) { + if (status_code == net::HTTP_OK || status_code == net::HTTP_PARTIAL_CONTENT) { std::string content_length_header(net::HttpRequestHeaders::kContentLength); content_length_header.append(": "); content_length_header.append(base::Int64ToString(remaining_bytes_)); diff --git a/webkit/browser/blob/blob_url_request_job.h b/webkit/browser/blob/blob_url_request_job.h index e59a067..fd39706 100644 --- a/webkit/browser/blob/blob_url_request_job.h +++ b/webkit/browser/blob/blob_url_request_job.h @@ -11,6 +11,7 @@ #include "base/memory/weak_ptr.h" #include "base/platform_file.h" #include "net/http/http_byte_range.h" +#include "net/http/http_status_code.h" #include "net/url_request/url_request_job.h" #include "webkit/common/blob/blob_data.h" #include "webkit/storage/webkit_storage_export.h" @@ -86,7 +87,7 @@ class WEBKIT_STORAGE_EXPORT BlobURLRequestJob : public net::URLRequestJob { // and pass it to URLRequestJob's NotifyDone() or NotifyHeadersComplete(). void NotifySuccess(); void NotifyFailure(int); - void HeadersCompleted(int status_code, const std::string& status_txt); + void HeadersCompleted(net::HttpStatusCode status_code); // Returns a FileStreamReader for a blob item at |index|. // If the item at |index| is not of file this returns NULL. |