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 // MimeSnifferProxy wraps an URLRequest to use mime_util's MIME | 5 // MimeSnifferProxy wraps an URLRequest to use mime_util's MIME |
6 // sniffer to better report the content's MIME type. | 6 // sniffer to better report the content's MIME type. |
7 // It only supports a subset of the URLRequest API, and must be used together | 7 // It only supports a subset of the URLRequest API, and must be used together |
8 // with an URLRequest. Their lifetimes should be the same. | 8 // with an URLRequest. Their lifetimes should be the same. |
9 // | 9 // |
10 // To use it, create a normal URLRequest and initialize it appropriately, | 10 // To use it, create a normal URLRequest and initialize it appropriately, |
11 // then insert a MimeSnifferProxy between your object and the URLRequest: | 11 // then insert a MimeSnifferProxy between your object and the URLRequest: |
12 // ms_.reset(new MimeSnifferProxy(url_request, this)); | 12 // ms_.reset(new MimeSnifferProxy(url_request, this)); |
13 // It then proxies URLRequest delegate callbacks (from URLRequest back into | 13 // It then proxies URLRequest delegate callbacks (from URLRequest back into |
14 // your object) appropriately. | 14 // your object) appropriately. |
15 // | 15 // |
16 // For the other direction of calls (from your object to URLRequest), be sure | 16 // For the other direction of calls (from your object to URLRequest), be sure |
17 // to use two MimeSniffed functions in place of the URLRequest functions: | 17 // to use two MimeSniffed functions in place of the URLRequest functions: |
18 // 1) ms_->Read() -- just like URLRequest::Read() | 18 // 1) ms_->Read() -- just like URLRequest::Read() |
19 // 2) ms_->mime_type() -- returns the sniffed mime type of the data; | 19 // 2) ms_->mime_type() -- returns the sniffed mime type of the data; |
20 // valid after OnResponseStarted() is called. | 20 // valid after OnResponseStarted() is called. |
21 | 21 |
| 22 #ifndef NET_URL_REQUEST_MIME_SNIFFER_PROXY_H_ |
| 23 #define NET_URL_REQUEST_MIME_SNIFFER_PROXY_H_ |
| 24 |
| 25 #include "net/base/io_buffer.h" |
22 #include "net/url_request/url_request.h" | 26 #include "net/url_request/url_request.h" |
23 | 27 |
24 class MimeSnifferProxy : public URLRequest::Delegate { | 28 class MimeSnifferProxy : public URLRequest::Delegate { |
25 public: | 29 public: |
26 // The constructor inserts this MimeSnifferProxy in between the URLRequest | 30 // The constructor inserts this MimeSnifferProxy in between the URLRequest |
27 // and the URLRequest::Delegate, so that the URLRequest's delegate callbacks | 31 // and the URLRequest::Delegate, so that the URLRequest's delegate callbacks |
28 // first go through the MimeSnifferProxy. | 32 // first go through the MimeSnifferProxy. |
29 MimeSnifferProxy(URLRequest* request, URLRequest::Delegate* delegate); | 33 MimeSnifferProxy(URLRequest* request, URLRequest::Delegate* delegate); |
30 | 34 |
31 // URLRequest::Delegate implementation. | 35 // URLRequest::Delegate implementation. |
32 // These first two functions are handled specially: | 36 // These first two functions are handled specially: |
33 virtual void OnResponseStarted(URLRequest* request); | 37 virtual void OnResponseStarted(URLRequest* request); |
34 virtual void OnReadCompleted(URLRequest* request, int bytes_read); | 38 virtual void OnReadCompleted(URLRequest* request, int bytes_read); |
35 // The remaining three just proxy directly to the delegate: | 39 // The remaining three just proxy directly to the delegate: |
36 virtual void OnReceivedRedirect(URLRequest* request, | 40 virtual void OnReceivedRedirect(URLRequest* request, |
37 const GURL& new_url) { | 41 const GURL& new_url) { |
38 delegate_->OnReceivedRedirect(request, new_url); | 42 delegate_->OnReceivedRedirect(request, new_url); |
39 } | 43 } |
40 virtual void OnAuthRequired(URLRequest* request, | 44 virtual void OnAuthRequired(URLRequest* request, |
41 net::AuthChallengeInfo* auth_info) { | 45 net::AuthChallengeInfo* auth_info) { |
42 delegate_->OnAuthRequired(request, auth_info); | 46 delegate_->OnAuthRequired(request, auth_info); |
43 } | 47 } |
44 virtual void OnSSLCertificateError(URLRequest* request, | 48 virtual void OnSSLCertificateError(URLRequest* request, |
45 int cert_error, | 49 int cert_error, |
46 net::X509Certificate* cert) { | 50 net::X509Certificate* cert) { |
47 delegate_->OnSSLCertificateError(request, cert_error, cert); | 51 delegate_->OnSSLCertificateError(request, cert_error, cert); |
48 } | 52 } |
49 | 53 |
50 // Wrapper around URLRequest::Read. | 54 // Wrapper around URLRequest::Read. |
51 bool Read(char* buf, int max_bytes, int *bytes_read); | 55 bool Read(net::IOBuffer* buf, int max_bytes, int *bytes_read); |
52 | 56 |
53 // Return the sniffed mime type of the request. Valid after | 57 // Return the sniffed mime type of the request. Valid after |
54 // OnResponseStarted() has been called on the delegate. | 58 // OnResponseStarted() has been called on the delegate. |
55 const std::string& mime_type() const { return mime_type_; } | 59 const std::string& mime_type() const { return mime_type_; } |
56 | 60 |
57 private: | 61 private: |
58 // The request underneath us. | 62 // The request underneath us. |
59 URLRequest* request_; | 63 URLRequest* request_; |
60 // The delegate above us, that we're proxying the request to. | 64 // The delegate above us, that we're proxying the request to. |
61 URLRequest::Delegate* delegate_; | 65 URLRequest::Delegate* delegate_; |
62 | 66 |
63 // The (sniffed, if necessary) request mime type. | 67 // The (sniffed, if necessary) request mime type. |
64 std::string mime_type_; | 68 std::string mime_type_; |
65 | 69 |
66 // Whether we're sniffing this request. | 70 // Whether we're sniffing this request. |
67 bool sniff_content_; | 71 bool sniff_content_; |
68 // Whether we've encountered an error on our initial Read(). | 72 // Whether we've encountered an error on our initial Read(). |
69 bool error_; | 73 bool error_; |
70 | 74 |
71 // A buffer for the first bit of the request. | 75 // A buffer for the first bit of the request. |
72 char buf_[1024]; | 76 scoped_refptr<net::IOBuffer> buf_; |
73 // The number of bytes we've read into the buffer. | 77 // The number of bytes we've read into the buffer. |
74 int bytes_read_; | 78 int bytes_read_; |
75 }; | 79 }; |
76 | 80 |
| 81 #endif // NET_URL_REQUEST_MIME_SNIFFER_PROXY_H_ |
OLD | NEW |