| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "content/child/ftp_directory_listing_response_delegate.h" | 5 #include "content/child/ftp_directory_listing_response_delegate.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/i18n/encoding_detection.h" | 12 #include "base/i18n/encoding_detection.h" |
| 13 #include "base/i18n/icu_string_conversions.h" | 13 #include "base/i18n/icu_string_conversions.h" |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
| 16 #include "base/strings/sys_string_conversions.h" | 16 #include "base/strings/sys_string_conversions.h" |
| 17 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
| 18 #include "base/time/time.h" | 18 #include "base/time/time.h" |
| 19 #include "content/child/weburlresponse_extradata_impl.h" | 19 #include "content/child/weburlresponse_extradata_impl.h" |
| 20 #include "net/base/directory_listing.h" | 20 #include "net/base/directory_listing.h" |
| 21 #include "net/base/escape.h" | 21 #include "net/base/escape.h" |
| 22 #include "net/base/net_errors.h" | 22 #include "net/base/net_errors.h" |
| 23 #include "net/ftp/ftp_directory_listing_parser.h" | 23 #include "net/ftp/ftp_directory_listing_parser.h" |
| 24 #include "net/net_features.h" | 24 #include "net/net_features.h" |
| 25 #include "third_party/WebKit/public/platform/WebURL.h" | 25 #include "third_party/WebKit/public/platform/WebURL.h" |
| 26 #include "third_party/WebKit/public/platform/WebURLLoader.h" |
| 26 #include "third_party/WebKit/public/platform/WebURLLoaderClient.h" | 27 #include "third_party/WebKit/public/platform/WebURLLoaderClient.h" |
| 27 | 28 |
| 28 using blink::WebURLLoader; | |
| 29 using blink::WebURLLoaderClient; | |
| 30 using blink::WebURLResponse; | |
| 31 using net::FtpDirectoryListingEntry; | 29 using net::FtpDirectoryListingEntry; |
| 32 | 30 |
| 33 namespace { | 31 namespace { |
| 34 | 32 |
| 35 base::string16 ConvertPathToUTF16(const std::string& path) { | 33 base::string16 ConvertPathToUTF16(const std::string& path) { |
| 36 // Per RFC 2640, FTP servers should use UTF-8 or its proper subset ASCII, | 34 // Per RFC 2640, FTP servers should use UTF-8 or its proper subset ASCII, |
| 37 // but many old FTP servers use legacy encodings. Try UTF-8 first. | 35 // but many old FTP servers use legacy encodings. Try UTF-8 first. |
| 38 if (base::IsStringUTF8(path)) | 36 if (base::IsStringUTF8(path)) |
| 39 return base::UTF8ToUTF16(path); | 37 return base::UTF8ToUTF16(path); |
| 40 | 38 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 52 | 50 |
| 53 // Use system native encoding as the last resort. | 51 // Use system native encoding as the last resort. |
| 54 return base::WideToUTF16(base::SysNativeMBToWide(path)); | 52 return base::WideToUTF16(base::SysNativeMBToWide(path)); |
| 55 } | 53 } |
| 56 | 54 |
| 57 } // namespace | 55 } // namespace |
| 58 | 56 |
| 59 namespace content { | 57 namespace content { |
| 60 | 58 |
| 61 FtpDirectoryListingResponseDelegate::FtpDirectoryListingResponseDelegate( | 59 FtpDirectoryListingResponseDelegate::FtpDirectoryListingResponseDelegate( |
| 62 WebURLLoaderClient* client, | 60 blink::WebURLLoaderClient* client, |
| 63 WebURLLoader* loader, | 61 blink::WebURLLoader* loader, |
| 64 const WebURLResponse& response) | 62 const blink::WebURLResponse& response) |
| 65 : client_(client), | 63 : client_(client), loader_(loader) { |
| 66 loader_(loader) { | |
| 67 if (response.GetExtraData()) { | 64 if (response.GetExtraData()) { |
| 68 // |extra_data| can be NULL during tests. | 65 // |extra_data| can be NULL during tests. |
| 69 WebURLResponseExtraDataImpl* extra_data = | 66 WebURLResponseExtraDataImpl* extra_data = |
| 70 static_cast<WebURLResponseExtraDataImpl*>(response.GetExtraData()); | 67 static_cast<WebURLResponseExtraDataImpl*>(response.GetExtraData()); |
| 71 extra_data->set_is_ftp_directory_listing(true); | 68 extra_data->set_is_ftp_directory_listing(true); |
| 72 } | 69 } |
| 73 Init(response.Url()); | 70 Init(response.Url()); |
| 74 } | 71 } |
| 75 | 72 |
| 76 void FtpDirectoryListingResponseDelegate::Cancel() { | 73 void FtpDirectoryListingResponseDelegate::Cancel() { |
| 77 client_ = nullptr; | 74 client_ = nullptr; |
| 78 loader_ = nullptr; | 75 loader_ = nullptr; |
| 79 } | 76 } |
| 80 | 77 |
| 81 void FtpDirectoryListingResponseDelegate::OnReceivedData(const char* data, | 78 void FtpDirectoryListingResponseDelegate::OnReceivedData(const char* data, |
| 82 int data_len) { | 79 int data_len) { |
| 83 buffer_.append(data, data_len); | 80 buffer_.append(data, data_len); |
| 84 } | 81 } |
| 85 | 82 |
| 86 void FtpDirectoryListingResponseDelegate::OnCompletedRequest() { | 83 void FtpDirectoryListingResponseDelegate::OnCompletedRequest(int error_code) { |
| 87 std::vector<FtpDirectoryListingEntry> entries; | 84 std::vector<FtpDirectoryListingEntry> entries; |
| 88 int rv = net::ERR_NOT_IMPLEMENTED; | 85 int rv = net::ERR_NOT_IMPLEMENTED; |
| 89 #if !BUILDFLAG(DISABLE_FTP_SUPPORT) | 86 #if !BUILDFLAG(DISABLE_FTP_SUPPORT) |
| 90 rv = net::ParseFtpDirectoryListing(buffer_, base::Time::Now(), &entries); | 87 if (error_code == net::OK) |
| 88 rv = net::ParseFtpDirectoryListing(buffer_, base::Time::Now(), &entries); |
| 89 else |
| 90 rv = error_code; |
| 91 #endif | 91 #endif |
| 92 buffer_.clear(); |
| 92 if (rv != net::OK) { | 93 if (rv != net::OK) { |
| 93 SendDataToClient("<script>onListingParsingError();</script>\n"); | 94 SendDataToClient("<script>onListingParsingError();</script>\n"); |
| 95 if (loader_) |
| 96 loader_->Cancel(); |
| 94 return; | 97 return; |
| 95 } | 98 } |
| 96 for (const FtpDirectoryListingEntry& entry : entries) { | 99 for (const FtpDirectoryListingEntry& entry : entries) { |
| 97 // Skip the current and parent directory entries in the listing. Our header | 100 // Skip the current and parent directory entries in the listing. Our header |
| 98 // always includes them. | 101 // always includes them. |
| 99 if (base::EqualsASCII(entry.name, ".") || | 102 if (base::EqualsASCII(entry.name, ".") || |
| 100 base::EqualsASCII(entry.name, "..")) | 103 base::EqualsASCII(entry.name, "..")) |
| 101 continue; | 104 continue; |
| 102 | 105 |
| 103 bool is_directory = (entry.type == FtpDirectoryListingEntry::DIRECTORY); | 106 bool is_directory = (entry.type == FtpDirectoryListingEntry::DIRECTORY); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 118 ConvertPathToUTF16(unescaped_path))); | 121 ConvertPathToUTF16(unescaped_path))); |
| 119 | 122 |
| 120 // If this isn't top level directory (i.e. the path isn't "/",) | 123 // If this isn't top level directory (i.e. the path isn't "/",) |
| 121 // add a link to the parent directory. | 124 // add a link to the parent directory. |
| 122 if (response_url.path().length() > 1) { | 125 if (response_url.path().length() > 1) { |
| 123 SendDataToClient("<script>onHasParentDirectory();</script>\n"); | 126 SendDataToClient("<script>onHasParentDirectory();</script>\n"); |
| 124 } | 127 } |
| 125 } | 128 } |
| 126 | 129 |
| 127 void FtpDirectoryListingResponseDelegate::SendDataToClient( | 130 void FtpDirectoryListingResponseDelegate::SendDataToClient( |
| 128 const std::string& data) { | 131 base::StringPiece data) { |
| 129 if (client_) { | 132 if (client_) { |
| 130 client_->DidReceiveData(data.data(), data.length()); | 133 client_->DidReceiveData(data.data(), data.length()); |
| 131 } | 134 } |
| 132 } | 135 } |
| 133 | 136 |
| 134 } // namespace content | 137 } // namespace content |
| OLD | NEW |