OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/url_request/mime_sniffer_proxy.h" | 5 #include "net/url_request/mime_sniffer_proxy.h" |
6 | 6 |
7 #include "net/base/mime_sniffer.h" | 7 #include "net/base/mime_sniffer.h" |
8 | 8 |
| 9 static const int kBufferSize = 1024; |
| 10 |
9 MimeSnifferProxy::MimeSnifferProxy(URLRequest* request, | 11 MimeSnifferProxy::MimeSnifferProxy(URLRequest* request, |
10 URLRequest::Delegate* delegate) | 12 URLRequest::Delegate* delegate) |
11 : request_(request), delegate_(delegate), | 13 : request_(request), delegate_(delegate), |
12 sniff_content_(false), error_(false) { | 14 sniff_content_(false), error_(false), |
| 15 buf_(new net::IOBuffer(kBufferSize)) { |
13 request->set_delegate(this); | 16 request->set_delegate(this); |
14 } | 17 } |
15 | 18 |
16 void MimeSnifferProxy::OnResponseStarted(URLRequest* request) { | 19 void MimeSnifferProxy::OnResponseStarted(URLRequest* request) { |
17 if (request->status().is_success()) { | 20 if (request->status().is_success()) { |
18 request->GetMimeType(&mime_type_); | 21 request->GetMimeType(&mime_type_); |
19 if (net::ShouldSniffMimeType(request->url(), mime_type_)) { | 22 if (net::ShouldSniffMimeType(request->url(), mime_type_)) { |
20 // We need to read content before we know the mime type, | 23 // We need to read content before we know the mime type, |
21 // so we don't call OnResponseStarted. | 24 // so we don't call OnResponseStarted. |
22 sniff_content_ = true; | 25 sniff_content_ = true; |
23 if (request_->Read(buf_, sizeof(buf_), &bytes_read_) && bytes_read_) { | 26 if (request_->Read(buf_, kBufferSize, &bytes_read_) && bytes_read_) { |
24 OnReadCompleted(request, bytes_read_); | 27 OnReadCompleted(request, bytes_read_); |
25 } else if (!request_->status().is_io_pending()) { | 28 } else if (!request_->status().is_io_pending()) { |
26 error_ = true; | 29 error_ = true; |
27 delegate_->OnResponseStarted(request); | 30 delegate_->OnResponseStarted(request); |
28 } // Otherwise, IO pending. Wait for OnReadCompleted. | 31 } // Otherwise, IO pending. Wait for OnReadCompleted. |
29 return; | 32 return; |
30 } | 33 } |
31 } | 34 } |
32 delegate_->OnResponseStarted(request); | 35 delegate_->OnResponseStarted(request); |
33 } | 36 } |
34 | 37 |
35 bool MimeSnifferProxy::Read(char* buf, int max_bytes, int *bytes_read) { | 38 bool MimeSnifferProxy::Read(net::IOBuffer* buf, int max_bytes, |
| 39 int *bytes_read) { |
36 if (sniff_content_) { | 40 if (sniff_content_) { |
37 // This is the first call to Read() after we've sniffed content. | 41 // This is the first call to Read() after we've sniffed content. |
38 // Return our local buffer or the error we ran into. | 42 // Return our local buffer or the error we ran into. |
39 sniff_content_ = false; // We're done with sniffing for this request. | 43 sniff_content_ = false; // We're done with sniffing for this request. |
40 | 44 |
41 if (error_) { | 45 if (error_) { |
42 *bytes_read = 0; | 46 *bytes_read = 0; |
43 return false; | 47 return false; |
44 } | 48 } |
45 | 49 |
46 memcpy(buf, buf_, bytes_read_); | 50 memcpy(buf->data(), buf_->data(), bytes_read_); |
47 *bytes_read = bytes_read_; | 51 *bytes_read = bytes_read_; |
48 return true; | 52 return true; |
49 } | 53 } |
50 return request_->Read(buf, max_bytes, bytes_read); | 54 return request_->Read(buf, max_bytes, bytes_read); |
51 } | 55 } |
52 | 56 |
53 void MimeSnifferProxy::OnReadCompleted(URLRequest* request, int bytes_read) { | 57 void MimeSnifferProxy::OnReadCompleted(URLRequest* request, int bytes_read) { |
54 if (sniff_content_) { | 58 if (sniff_content_) { |
55 // Our initial content-sniffing Read() has completed. | 59 // Our initial content-sniffing Read() has completed. |
56 if (request->status().is_success() && bytes_read) { | 60 if (request->status().is_success() && bytes_read) { |
57 std::string type_hint; | 61 std::string type_hint; |
58 request_->GetMimeType(&type_hint); | 62 request_->GetMimeType(&type_hint); |
59 bytes_read_ = bytes_read; | 63 bytes_read_ = bytes_read; |
60 net::SniffMimeType( | 64 net::SniffMimeType(buf_->data(), bytes_read_, request_->url(), |
61 buf_, bytes_read_, request_->url(), type_hint, &mime_type_); | 65 type_hint, &mime_type_); |
62 } else { | 66 } else { |
63 error_ = true; | 67 error_ = true; |
64 } | 68 } |
65 delegate_->OnResponseStarted(request_); | 69 delegate_->OnResponseStarted(request_); |
66 return; | 70 return; |
67 } | 71 } |
68 delegate_->OnReadCompleted(request, bytes_read); | 72 delegate_->OnReadCompleted(request, bytes_read); |
69 } | 73 } |
70 | 74 |
OLD | NEW |