OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2017 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 "base/strings/string_util.h" | |
6 #include "base/strings/stringprintf.h" | |
7 #include "chrome/browser/extensions/api/dial/device_description_fetcher.h" | |
8 #include "chrome/browser/extensions/api/dial/dial_device_data.h" | |
9 #include "chrome/browser/profiles/profile.h" | |
10 #include "content/public/browser/browser_thread.h" | |
11 #include "net/base/load_flags.h" | |
12 #include "net/http/http_response_headers.h" | |
13 #include "net/http/http_status_code.h" | |
14 #include "net/http/http_util.h" | |
15 #include "net/url_request/url_fetcher.h" | |
16 | |
17 using content::BrowserThread; | |
18 | |
19 constexpr char kApplicationUrlHeaderName[] = "Application-URL"; | |
20 constexpr int kMaxRetries = 3; | |
21 // DIAL devices are unlikely to expose uPnP functions other than DIAL, so 256kb | |
22 // should be more than sufficient. | |
23 constexpr int kMaxDescriptionSizeBytes = 262144; | |
24 | |
25 namespace extensions { | |
26 namespace api { | |
27 namespace dial { | |
28 | |
29 DeviceDescriptionFetcher::DeviceDescriptionFetcher( | |
30 const GURL& device_description_url, | |
31 Profile* profile, | |
32 base::OnceCallback<void(const DialDeviceDescriptionData&)> success_cb, | |
33 base::OnceCallback<void(const std::string&)> error_cb) | |
34 : device_description_url_(device_description_url), | |
35 profile_(profile), | |
36 success_cb_(std::move(success_cb)), | |
37 error_cb_(std::move(error_cb)) { | |
38 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
39 DCHECK(profile_); | |
40 DCHECK(device_description_url_.is_valid()); | |
41 } | |
42 | |
43 DeviceDescriptionFetcher::~DeviceDescriptionFetcher() { | |
44 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
45 } | |
46 | |
47 void DeviceDescriptionFetcher::Start() { | |
48 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
49 DCHECK(!fetcher_); | |
50 | |
51 // DIAL returns device descriptions via GET request. | |
52 fetcher_ = | |
53 net::URLFetcher::Create(kURLFetcherIDForTest, device_description_url_, | |
54 net::URLFetcher::GET, this); | |
55 | |
56 // net::LOAD_BYPASS_PROXY: Proxies almost certainly hurt more cases than they | |
57 // help. | |
58 // net::LOAD_DISABLE_CACHE: The request should not touch the cache. | |
59 // net::LOAD_DO_NOT_{SAVE,SEND}_COOKIES: The request should not touch cookies. | |
60 // net::LOAD_DO_NOT_SEND_AUTH_DATA: The request shoud not send auth data. | |
Wez
2017/01/09 22:12:58
typo: should
mark a. foltz
2017/01/10 00:36:14
Done.
| |
61 fetcher_->SetLoadFlags(net::LOAD_BYPASS_PROXY | net::LOAD_DISABLE_CACHE | | |
62 net::LOAD_DO_NOT_SAVE_COOKIES | | |
63 net::LOAD_DO_NOT_SEND_COOKIES | | |
64 net::LOAD_DO_NOT_SEND_AUTH_DATA); | |
65 | |
66 // Section 5.4 of the DIAL spec prohibits redirects. | |
67 fetcher_->SetStopOnRedirect(true); | |
68 | |
69 // Allow the fetcher to retry on 5XX responses and ERR_NETWORK_CHANGED. | |
70 fetcher_->SetMaxRetriesOn5xx(kMaxRetries); | |
71 fetcher_->SetAutomaticallyRetryOnNetworkChanges(kMaxRetries); | |
72 | |
73 fetcher_->SetRequestContext(profile_->GetRequestContext()); | |
74 fetcher_->Start(); | |
75 } | |
76 | |
77 void DeviceDescriptionFetcher::OnURLFetchComplete( | |
78 const net::URLFetcher* source) { | |
79 DCHECK(source); | |
80 | |
81 if (source->GetResponseCode() != net::HTTP_OK) { | |
82 ReportError( | |
83 base::StringPrintf("HTTP %d: Unable to fetch device description", | |
84 source->GetResponseCode())); | |
85 return; | |
86 } | |
87 | |
88 const net::HttpResponseHeaders* headers = source->GetResponseHeaders(); | |
89 | |
90 // NOTE: The uPnP spec requires devices to set a Content-Type: header of | |
91 // text/xml; charset="utf-8" (sec 2.11). However Chromecast (and possibly | |
92 // other devices) do not comply, so specifically not checking this header. | |
93 std::string app_url_header; | |
94 if (!headers->GetNormalizedHeader(kApplicationUrlHeaderName, | |
95 &app_url_header) || | |
96 app_url_header.empty()) { | |
97 ReportError("Missing or empty Application-URL:"); | |
98 return; | |
99 } | |
100 | |
101 // Section 5.4 of the DIAL spec implies that the Application URL should not | |
102 // have path, query or fragment...unsure if that can be enforced. | |
103 GURL app_url(app_url_header); | |
104 if (!app_url.is_valid() || !app_url.SchemeIs("http") || | |
105 !app_url.HostIsIPAddress() || | |
106 app_url.host() != device_description_url_.host()) { | |
107 ReportError(base::StringPrintf("Invalid Application-URL: %s", | |
108 app_url_header.c_str())); | |
109 return; | |
110 } | |
111 | |
112 if (source->GetReceivedResponseContentLength() > kMaxDescriptionSizeBytes) { | |
113 ReportError("Response too large"); | |
114 return; | |
115 } | |
116 | |
117 std::string device_description; | |
118 if (!source->GetResponseAsString(&device_description) || | |
119 device_description.empty()) { | |
120 ReportError("Missing or empty response"); | |
121 return; | |
122 } | |
123 | |
124 if (!base::IsStringUTF8(device_description)) { | |
125 ReportError("Invalid response encoding"); | |
126 return; | |
127 } | |
128 | |
129 std::move(success_cb_) | |
130 .Run(DialDeviceDescriptionData(std::move(device_description), app_url)); | |
131 } | |
132 | |
133 void DeviceDescriptionFetcher::OnURLFetchDownloadProgress( | |
134 const net::URLFetcher* source, | |
135 int64_t current, | |
136 int64_t total, | |
137 int64_t current_network_bytes) {} | |
138 | |
139 void DeviceDescriptionFetcher::OnURLFetchUploadProgress( | |
140 const net::URLFetcher* source, | |
141 int64_t current, | |
142 int64_t total) {} | |
143 | |
144 void DeviceDescriptionFetcher::ReportError(const std::string& message) { | |
145 std::move(error_cb_).Run(message); | |
146 } | |
147 | |
148 } // namespace dial | |
149 } // namespace api | |
150 } // namespace extensions | |
OLD | NEW |