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

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

Powered by Google App Engine
This is Rietveld 408576698