| OLD | NEW |
| 1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. Use of this | 1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. Use of this |
| 2 // source code is governed by a BSD-style license that can be found in the | 2 // source code is governed by a BSD-style license that can be found in the |
| 3 // LICENSE file. | 3 // LICENSE file. |
| 4 | 4 |
| 5 #include "net/proxy/proxy_script_fetcher.h" | 5 #include "net/proxy/proxy_script_fetcher.h" |
| 6 | 6 |
| 7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "base/ref_counted.h" | 10 #include "base/ref_counted.h" |
| (...skipping 25 matching lines...) Expand all Loading... |
| 36 "application/x-ns-proxy-autoconfig", | 36 "application/x-ns-proxy-autoconfig", |
| 37 "application/x-javascript-config", | 37 "application/x-javascript-config", |
| 38 }; | 38 }; |
| 39 for (size_t i = 0; i < arraysize(kSupportedPacMimeTypes); ++i) { | 39 for (size_t i = 0; i < arraysize(kSupportedPacMimeTypes); ++i) { |
| 40 if (LowerCaseEqualsASCII(mime_type, kSupportedPacMimeTypes[i])) | 40 if (LowerCaseEqualsASCII(mime_type, kSupportedPacMimeTypes[i])) |
| 41 return true; | 41 return true; |
| 42 } | 42 } |
| 43 return false; | 43 return false; |
| 44 } | 44 } |
| 45 | 45 |
| 46 // Convert |bytes| (which is encoded by |charset|) in place to UTF8. |
| 47 // If |charset| is empty, then we don't know what it was and guess. |
| 48 void ConvertResponseToUTF8(const std::string& charset, std::string* bytes) { |
| 49 const char* codepage; |
| 50 |
| 51 if (charset.empty()) { |
| 52 // Assume ISO-8859-1 if no charset was specified. |
| 53 codepage = "ISO-8859-1"; |
| 54 } else { |
| 55 // Otherwise trust the charset that was provided. |
| 56 codepage = charset.c_str(); |
| 57 } |
| 58 |
| 59 // We will be generous in the conversion -- if any characters lie |
| 60 // outside of |charset| (i.e. invalid), then substitute them with |
| 61 // U+FFFD rather than failing. |
| 62 std::wstring tmp_wide; |
| 63 CodepageToWide(*bytes, codepage, |
| 64 OnStringUtilConversionError::SUBSTITUTE, |
| 65 &tmp_wide); |
| 66 // TODO(eroman): would be nice to have a CodepageToUTF8() function. |
| 67 *bytes = WideToUTF8(tmp_wide); |
| 68 } |
| 69 |
| 46 } // namespace | 70 } // namespace |
| 47 | 71 |
| 48 class ProxyScriptFetcherImpl : public ProxyScriptFetcher, | 72 class ProxyScriptFetcherImpl : public ProxyScriptFetcher, |
| 49 public URLRequest::Delegate { | 73 public URLRequest::Delegate { |
| 50 public: | 74 public: |
| 51 // Creates a ProxyScriptFetcher that issues requests through | 75 // Creates a ProxyScriptFetcher that issues requests through |
| 52 // |url_request_context|. |url_request_context| must remain valid for the | 76 // |url_request_context|. |url_request_context| must remain valid for the |
| 53 // lifetime of ProxyScriptFetcherImpl. | 77 // lifetime of ProxyScriptFetcherImpl. |
| 54 explicit ProxyScriptFetcherImpl(URLRequestContext* url_request_context); | 78 explicit ProxyScriptFetcherImpl(URLRequestContext* url_request_context); |
| 55 | 79 |
| (...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 266 int num_bytes; | 290 int num_bytes; |
| 267 if (request->Read(buf_, kBufSize, &num_bytes)) { | 291 if (request->Read(buf_, kBufSize, &num_bytes)) { |
| 268 OnReadCompleted(request, num_bytes); | 292 OnReadCompleted(request, num_bytes); |
| 269 } else if (!request->status().is_io_pending()) { | 293 } else if (!request->status().is_io_pending()) { |
| 270 // Read failed synchronously. | 294 // Read failed synchronously. |
| 271 OnResponseCompleted(request); | 295 OnResponseCompleted(request); |
| 272 } | 296 } |
| 273 } | 297 } |
| 274 | 298 |
| 275 void ProxyScriptFetcherImpl::FetchCompleted() { | 299 void ProxyScriptFetcherImpl::FetchCompleted() { |
| 276 // On error, the caller expects empty string for bytes. | 300 if (result_code_ == OK) { |
| 277 if (result_code_ != OK) | 301 // The caller expects the response to be encoded as UTF8. |
| 302 std::string charset; |
| 303 cur_request_->GetCharset(&charset); |
| 304 ConvertResponseToUTF8(charset, result_bytes_); |
| 305 } else { |
| 306 // On error, the caller expects empty string for bytes. |
| 278 result_bytes_->clear(); | 307 result_bytes_->clear(); |
| 308 } |
| 279 | 309 |
| 280 int result_code = result_code_; | 310 int result_code = result_code_; |
| 281 CompletionCallback* callback = callback_; | 311 CompletionCallback* callback = callback_; |
| 282 | 312 |
| 283 ResetCurRequestState(); | 313 ResetCurRequestState(); |
| 284 | 314 |
| 285 callback->Run(result_code); | 315 callback->Run(result_code); |
| 286 } | 316 } |
| 287 | 317 |
| 288 void ProxyScriptFetcherImpl::ResetCurRequestState() { | 318 void ProxyScriptFetcherImpl::ResetCurRequestState() { |
| (...skipping 30 matching lines...) Expand all Loading... |
| 319 } | 349 } |
| 320 | 350 |
| 321 // static | 351 // static |
| 322 size_t ProxyScriptFetcher::SetSizeConstraintForUnittest(size_t size_bytes) { | 352 size_t ProxyScriptFetcher::SetSizeConstraintForUnittest(size_t size_bytes) { |
| 323 size_t prev = max_response_bytes; | 353 size_t prev = max_response_bytes; |
| 324 max_response_bytes = size_bytes; | 354 max_response_bytes = size_bytes; |
| 325 return prev; | 355 return prev; |
| 326 } | 356 } |
| 327 | 357 |
| 328 } // namespace net | 358 } // namespace net |
| OLD | NEW |