summaryrefslogtreecommitdiffstats
path: root/net/ftp/ftp_directory_listing_buffer.cc
blob: 8399f131ab8a94197b9c784a3c00a548c08dc428 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// Copyright (c) 2009 The Chromium Authors. All rights reserved.  Use of this
// source code is governed by a BSD-style license that can be found in the
// LICENSE file.

#include "net/ftp/ftp_directory_listing_buffer.h"

#include "base/i18n/icu_encoding_detection.h"
#include "base/i18n/icu_string_conversions.h"
#include "base/stl_util-inl.h"
#include "base/string_util.h"
#include "net/base/net_errors.h"
#include "net/ftp/ftp_directory_listing_parser_ls.h"
#include "net/ftp/ftp_directory_listing_parser_mlsd.h"
#include "net/ftp/ftp_directory_listing_parser_netware.h"
#include "net/ftp/ftp_directory_listing_parser_vms.h"
#include "net/ftp/ftp_directory_listing_parser_windows.h"

namespace net {

FtpDirectoryListingBuffer::FtpDirectoryListingBuffer(
    const base::Time& current_time)
    : current_parser_(NULL) {
  parsers_.insert(new FtpDirectoryListingParserLs(current_time));
  parsers_.insert(new FtpDirectoryListingParserMlsd());
  parsers_.insert(new FtpDirectoryListingParserNetware(current_time));
  parsers_.insert(new FtpDirectoryListingParserVms());
  parsers_.insert(new FtpDirectoryListingParserWindows());
}

FtpDirectoryListingBuffer::~FtpDirectoryListingBuffer() {
  STLDeleteElements(&parsers_);
}

int FtpDirectoryListingBuffer::ConsumeData(const char* data, int data_length) {
  buffer_.append(data, data_length);

  if (!encoding_.empty() || buffer_.length() > 1024) {
    int rv = ExtractFullLinesFromBuffer();
    if (rv != OK)
      return rv;
  }

  return ParseLines();
}

int FtpDirectoryListingBuffer::ProcessRemainingData() {
  int rv = ExtractFullLinesFromBuffer();
  if (rv != OK)
    return rv;

  if (!buffer_.empty())
    return ERR_INVALID_RESPONSE;

  rv = ParseLines();
  if (rv != OK)
    return rv;

  rv = OnEndOfInput();
  if (rv != OK)
    return rv;

  return OK;
}

bool FtpDirectoryListingBuffer::EntryAvailable() const {
  return (current_parser_ ? current_parser_->EntryAvailable() : false);
}

FtpDirectoryListingEntry FtpDirectoryListingBuffer::PopEntry() {
  DCHECK(EntryAvailable());
  return current_parser_->PopEntry();
}

FtpServerType FtpDirectoryListingBuffer::GetServerType() const {
  return (current_parser_ ? current_parser_->GetServerType() : SERVER_UNKNOWN);
}

bool FtpDirectoryListingBuffer::ConvertToDetectedEncoding(
    const std::string& from, string16* to) {
  std::string encoding(encoding_.empty() ? "ascii" : encoding_);
  return base::CodepageToUTF16(from, encoding.c_str(),
                               base::OnStringConversionError::FAIL, to);
}

int FtpDirectoryListingBuffer::ExtractFullLinesFromBuffer() {
  if (encoding_.empty()) {
    if (!base::DetectEncoding(buffer_, &encoding_))
      return ERR_ENCODING_DETECTION_FAILED;
  }

  int cut_pos = 0;
  // TODO(phajdan.jr): This code accepts all endlines matching \r*\n. Should it
  // be more strict, or enforce consistent line endings?
  for (size_t i = 0; i < buffer_.length(); ++i) {
    if (buffer_[i] != '\n')
      continue;
    int line_length = i - cut_pos;
    if (i >= 1 && buffer_[i - 1] == '\r')
      line_length--;
    std::string line(buffer_.substr(cut_pos, line_length));
    cut_pos = i + 1;
    string16 line_converted;
    if (!ConvertToDetectedEncoding(line, &line_converted)) {
      buffer_.erase(0, cut_pos);
      return ERR_ENCODING_CONVERSION_FAILED;
    }
    lines_.push_back(line_converted);
  }
  buffer_.erase(0, cut_pos);
  return OK;
}

int FtpDirectoryListingBuffer::ParseLines() {
  while (!lines_.empty()) {
    string16 line = lines_.front();
    lines_.pop_front();
    if (current_parser_) {
      if (!current_parser_->ConsumeLine(line))
        return ERR_FAILED;
    } else {
      ParserSet::iterator i = parsers_.begin();
      while (i != parsers_.end()) {
        if ((*i)->ConsumeLine(line)) {
          i++;
        } else {
          delete *i;
          parsers_.erase(i++);
        }
      }
      if (parsers_.empty())
        return ERR_UNRECOGNIZED_FTP_DIRECTORY_LISTING_FORMAT;
      if (parsers_.size() == 1)
        current_parser_ = *parsers_.begin();
    }
  }

  return OK;
}

int FtpDirectoryListingBuffer::OnEndOfInput() {
  ParserSet::iterator i = parsers_.begin();
  while (i != parsers_.end()) {
    if ((*i)->OnEndOfInput()) {
      i++;
    } else {
      delete *i;
      parsers_.erase(i++);
    }
  }

  if (parsers_.size() != 1) {
    current_parser_ = NULL;

    // We may hit an ambiguity in case of listings which have no entries. That's
    // fine, as long as all remaining parsers agree that the listing is empty.
    bool all_listings_empty = true;
    for (ParserSet::iterator i = parsers_.begin(); i != parsers_.end(); ++i) {
      if ((*i)->EntryAvailable())
        all_listings_empty = false;
    }
    if (all_listings_empty)
      return OK;

    return ERR_UNRECOGNIZED_FTP_DIRECTORY_LISTING_FORMAT;
  }

  current_parser_ = *parsers_.begin();
  return OK;
}

}  // namespace net