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

Side by Side Diff: content/browser/streams/stream_url_request_job.cc

Issue 12212031: Add support for redirecting ResourceHandlers to a blob: URL (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Rebase Created 7 years, 10 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
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 "content/browser/streams/stream_url_request_job.h"
6
7 #include "base/string_number_conversions.h"
8 #include "net/base/io_buffer.h"
9 #include "net/base/net_errors.h"
10 #include "net/http/http_response_headers.h"
11 #include "net/http/http_response_info.h"
12 #include "net/url_request/url_request.h"
13
14 namespace content {
15
16 namespace {
17
18 const int kHTTPOk = 200;
19 const int kHTTPNotAllowed = 403;
20 const int kHTTPNotFound = 404;
21 const int kHTTPMethodNotAllow = 405;
22 const int kHTTPInternalError = 500;
23
24 const char kHTTPOKText[] = "OK";
25 const char kHTTPNotAllowedText[] = "Not Allowed";
26 const char kHTTPNotFoundText[] = "Not Found";
27 const char kHTTPMethodNotAllowText[] = "Method Not Allowed";
28 const char kHTTPInternalErrorText[] = "Internal Server Error";
29
30 } // namespace
31
32 StreamURLRequestJob::StreamURLRequestJob(
33 net::URLRequest* request,
34 net::NetworkDelegate* network_delegate,
35 scoped_refptr<ChromeStream> stream)
36 : net::URLRequestJob(request, network_delegate),
37 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)),
38 stream_(stream),
39 headers_set_(false),
40 pending_buffer_size_(0) {
41 stream_->AddObserver(this);
42 }
43
44 StreamURLRequestJob::~StreamURLRequestJob() {
45 stream_->RemoveObserver(this);
46 }
47
48 void StreamURLRequestJob::OnDataAvailable(ChromeStream* stream) {
49 // Clear the IO_PENDING status
50 SetStatus(net::URLRequestStatus());
51 if (pending_buffer_) {
52 int bytes_read;
53 stream_->ReadRawData(pending_buffer_,
54 pending_buffer_size_,
55 &bytes_read);
56
57 pending_buffer_ = NULL;
58 pending_buffer_size_ = 0;
59
60 NotifyReadComplete(bytes_read);
61 }
62 }
63
64 void StreamURLRequestJob::OnStreamComplete(ChromeStream* stream) {
65 if (pending_buffer_) {
66 // Clear the IO_PENDING status
67 SetStatus(net::URLRequestStatus());
68 pending_buffer_ = NULL;
69 pending_buffer_size_ = 0;
70 NotifyReadComplete(0);
71 }
72 }
73
74 void StreamURLRequestJob::OnBufferAvailable(ChromeStream* stream) {
75 }
76
77 // net::URLRequestJob methods.
78 void StreamURLRequestJob::Start() {
79 // Continue asynchronously.
80 MessageLoop::current()->PostTask(
81 FROM_HERE,
82 base::Bind(&StreamURLRequestJob::DidStart, weak_factory_.GetWeakPtr()));
83 }
84
85 void StreamURLRequestJob::Kill() {
86 net::URLRequestJob::Kill();
87 weak_factory_.InvalidateWeakPtrs();
88 }
89
90 bool StreamURLRequestJob::ReadRawData(net::IOBuffer* buf,
91 int buf_size,
92 int* bytes_read) {
93 if (stream_->ReadRawData(buf, buf_size, bytes_read)) {
94 return true;
95 } else {
96 pending_buffer_ = buf;
97 pending_buffer_size_ = buf_size;
98 SetStatus(net::URLRequestStatus(net::URLRequestStatus::IO_PENDING, 0));
99 return false;
100 }
101 }
102
103 bool StreamURLRequestJob::GetMimeType(std::string* mime_type) const {
104 if (!response_info_.get())
105 return false;
106
107 return response_info_->headers->GetMimeType(mime_type);
108 }
109
110 void StreamURLRequestJob::GetResponseInfo(net::HttpResponseInfo* info) {
111 if (response_info_.get())
112 *info = *response_info_;
113 }
114
115 int StreamURLRequestJob::GetResponseCode() const {
116 if (!response_info_.get())
117 return -1;
118
119 return response_info_->headers->response_code();
120 }
121
122 void StreamURLRequestJob::SetExtraRequestHeaders(
123 const net::HttpRequestHeaders& headers) {
124 }
125
126 void StreamURLRequestJob::DidStart() {
127 // We only support GET request.
128 if (request()->method() != "GET") {
129 NotifyFailure(net::ERR_METHOD_NOT_SUPPORTED);
130 return;
131 }
132
133 if (!stream_) {
134 NotifyFailure(net::ERR_FILE_NOT_FOUND);
135 return;
136 }
137
138 HeadersCompleted(kHTTPOk, kHTTPOKText);
139 }
140
141 void StreamURLRequestJob::NotifyFailure(int error_code) {
142 // If we already return the headers on success, we can't change the headers
143 // now. Instead, we just error out.
144 if (headers_set_) {
145 NotifyDone(net::URLRequestStatus(net::URLRequestStatus::FAILED,
146 error_code));
147 return;
148 }
149
150 int status_code = 0;
151 std::string status_txt;
152 switch (error_code) {
153 case net::ERR_ACCESS_DENIED:
154 status_code = kHTTPNotAllowed;
155 status_txt = kHTTPNotAllowedText;
156 break;
157 case net::ERR_FILE_NOT_FOUND:
158 status_code = kHTTPNotFound;
159 status_txt = kHTTPNotFoundText;
160 break;
161 case net::ERR_METHOD_NOT_SUPPORTED:
162 status_code = kHTTPMethodNotAllow;
163 status_txt = kHTTPMethodNotAllowText;
164 break;
165 case net::ERR_FAILED:
166 status_code = kHTTPInternalError;
167 status_txt = kHTTPInternalErrorText;
168 break;
169 default:
170 DCHECK(false);
171 status_code = kHTTPInternalError;
172 status_txt = kHTTPInternalErrorText;
173 break;
174 }
175 HeadersCompleted(status_code, status_txt);
176 }
177
178 void StreamURLRequestJob::HeadersCompleted(int status_code,
179 const std::string& status_text) {
180 std::string status("HTTP/1.1 ");
181 status.append(base::IntToString(status_code));
182 status.append(" ");
183 status.append(status_text);
184 status.append("\0\0", 2);
185 net::HttpResponseHeaders* headers = new net::HttpResponseHeaders(status);
186
187 if (status_code == kHTTPOk) {
188 std::string content_type_header(net::HttpRequestHeaders::kContentType);
189 content_type_header.append(": ");
190 content_type_header.append("plain/text");
191 headers->AddHeader(content_type_header);
192 }
193
194 response_info_.reset(new net::HttpResponseInfo());
195 response_info_->headers = headers;
196
197 headers_set_ = true;
198
199 NotifyHeadersComplete();
200 }
201
202 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698