OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/ftp/ftp_directory_listing_parser.h" | 5 #include "net/ftp/ftp_directory_listing_parser.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/i18n/icu_encoding_detection.h" | 9 #include "base/i18n/encoding_detection.h" |
10 #include "base/i18n/icu_string_conversions.h" | 10 #include "base/i18n/icu_string_conversions.h" |
11 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
12 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
13 #include "base/strings/string_split.h" | 13 #include "base/strings/string_split.h" |
14 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
15 #include "net/base/net_errors.h" | 15 #include "net/base/net_errors.h" |
16 #include "net/ftp/ftp_directory_listing_parser_ls.h" | 16 #include "net/ftp/ftp_directory_listing_parser_ls.h" |
17 #include "net/ftp/ftp_directory_listing_parser_vms.h" | 17 #include "net/ftp/ftp_directory_listing_parser_vms.h" |
18 #include "net/ftp/ftp_directory_listing_parser_windows.h" | 18 #include "net/ftp/ftp_directory_listing_parser_windows.h" |
19 #include "net/ftp/ftp_server_type_histograms.h" | 19 #include "net/ftp/ftp_server_type_histograms.h" |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 entries->clear(); | 77 entries->clear(); |
78 return ERR_UNRECOGNIZED_FTP_DIRECTORY_LISTING_FORMAT; | 78 return ERR_UNRECOGNIZED_FTP_DIRECTORY_LISTING_FORMAT; |
79 } | 79 } |
80 | 80 |
81 // Detects encoding of |text| and parses it as an FTP directory listing. | 81 // Detects encoding of |text| and parses it as an FTP directory listing. |
82 // Fills in |entries| and |server_type| and returns network error code. | 82 // Fills in |entries| and |server_type| and returns network error code. |
83 int DecodeAndParse(const std::string& text, | 83 int DecodeAndParse(const std::string& text, |
84 const base::Time& current_time, | 84 const base::Time& current_time, |
85 std::vector<FtpDirectoryListingEntry>* entries, | 85 std::vector<FtpDirectoryListingEntry>* entries, |
86 FtpServerType* server_type) { | 86 FtpServerType* server_type) { |
87 const char* const kNewlineSeparators[] = { "\n", "\r\n" }; | 87 std::string encoding; |
| 88 if (!base::DetectEncoding(text, &encoding)) |
| 89 return ERR_ENCODING_DETECTION_FAILED; |
| 90 const char* encoding_name = encoding.c_str(); |
88 | 91 |
89 std::vector<std::string> encodings; | 92 base::string16 converted_text; |
90 if (!base::DetectAllEncodings(text, &encodings)) | 93 if (base::CodepageToUTF16(text, encoding_name, |
91 return ERR_ENCODING_DETECTION_FAILED; | 94 base::OnStringConversionError::FAIL, |
| 95 &converted_text)) { |
| 96 const char* const kNewlineSeparators[] = {"\n", "\r\n"}; |
92 | 97 |
93 // Use first encoding that can be used to decode the text. | 98 for (size_t j = 0; j < arraysize(kNewlineSeparators); j++) { |
94 for (size_t i = 0; i < encodings.size(); i++) { | 99 int rv = ParseListing(converted_text, |
95 base::string16 converted_text; | 100 base::ASCIIToUTF16(kNewlineSeparators[j]), |
96 if (base::CodepageToUTF16(text, | 101 encoding_name, current_time, entries, server_type); |
97 encodings[i].c_str(), | 102 if (rv == OK) |
98 base::OnStringConversionError::FAIL, | 103 return rv; |
99 &converted_text)) { | |
100 for (size_t j = 0; j < arraysize(kNewlineSeparators); j++) { | |
101 int rv = ParseListing(converted_text, | |
102 base::ASCIIToUTF16(kNewlineSeparators[j]), | |
103 encodings[i], | |
104 current_time, | |
105 entries, | |
106 server_type); | |
107 if (rv == OK) | |
108 return rv; | |
109 } | |
110 } | 104 } |
111 } | 105 } |
112 | 106 |
113 entries->clear(); | 107 entries->clear(); |
114 *server_type = SERVER_UNKNOWN; | 108 *server_type = SERVER_UNKNOWN; |
115 return ERR_UNRECOGNIZED_FTP_DIRECTORY_LISTING_FORMAT; | 109 return ERR_UNRECOGNIZED_FTP_DIRECTORY_LISTING_FORMAT; |
116 } | 110 } |
117 | 111 |
118 } // namespace | 112 } // namespace |
119 | 113 |
120 FtpDirectoryListingEntry::FtpDirectoryListingEntry() | 114 FtpDirectoryListingEntry::FtpDirectoryListingEntry() |
121 : type(UNKNOWN), | 115 : type(UNKNOWN), |
122 size(-1) { | 116 size(-1) { |
123 } | 117 } |
124 | 118 |
125 int ParseFtpDirectoryListing(const std::string& text, | 119 int ParseFtpDirectoryListing(const std::string& text, |
126 const base::Time& current_time, | 120 const base::Time& current_time, |
127 std::vector<FtpDirectoryListingEntry>* entries) { | 121 std::vector<FtpDirectoryListingEntry>* entries) { |
128 FtpServerType server_type = SERVER_UNKNOWN; | 122 FtpServerType server_type = SERVER_UNKNOWN; |
129 int rv = DecodeAndParse(text, current_time, entries, &server_type); | 123 int rv = DecodeAndParse(text, current_time, entries, &server_type); |
130 UpdateFtpServerTypeHistograms(server_type); | 124 UpdateFtpServerTypeHistograms(server_type); |
131 return rv; | 125 return rv; |
132 } | 126 } |
133 | 127 |
134 } // namespace net | 128 } // namespace net |
OLD | NEW |