summaryrefslogtreecommitdiff
path: root/chromium/net/ftp/ftp_ctrl_response_buffer.h
blob: fa5c03115b3cf578ae9b1e95987cf53f3fb905b5 (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
// Copyright (c) 2011 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.


#ifndef NET_FTP_FTP_CTRL_RESPONSE_BUFFER_H_
#define NET_FTP_FTP_CTRL_RESPONSE_BUFFER_H_

#include <queue>
#include <string>
#include <vector>

#include "base/basictypes.h"
#include "net/base/net_export.h"
#include "net/base/net_log.h"

namespace net {

struct NET_EXPORT_PRIVATE FtpCtrlResponse {
  static const int kInvalidStatusCode;

  FtpCtrlResponse();
  ~FtpCtrlResponse();

  int status_code;                 // Three-digit status code.
  std::vector<std::string> lines;  // Response lines, without CRLFs.
};

class NET_EXPORT_PRIVATE FtpCtrlResponseBuffer {
 public:
  FtpCtrlResponseBuffer(const BoundNetLog& net_log);
  ~FtpCtrlResponseBuffer();

  // Called when data is received from the control socket. Returns error code.
  int ConsumeData(const char* data, int data_length);

  bool ResponseAvailable() const {
    return !responses_.empty();
  }

  // Returns the next response. It is an error to call this function
  // unless ResponseAvailable returns true.
  FtpCtrlResponse PopResponse();

 private:
  struct ParsedLine {
    ParsedLine();

    // Indicates that this line begins with a valid 3-digit status code.
    bool has_status_code;

    // Indicates that this line has the dash (-) after the code, which
    // means a multiline response.
    bool is_multiline;

    // Indicates that this line could be parsed as a complete and valid
    // response line, without taking into account preceding lines (which
    // may change its meaning into a continuation of the previous line).
    bool is_complete;

    // Part of response parsed as status code.
    int status_code;

    // Part of response parsed as status text.
    std::string status_text;

    // Text before parsing, without terminating CRLF.
    std::string raw_text;
  };

  static ParsedLine ParseLine(const std::string& line);

  void ExtractFullLinesFromBuffer();

  // We keep not-yet-parsed data in a string buffer.
  std::string buffer_;

  std::queue<ParsedLine> lines_;

  // True if we are in the middle of parsing a multi-line response.
  bool multiline_;

  // When parsing a multiline response, we don't know beforehand if a line
  // will have a continuation. So always store last line of multiline response
  // so we can append the continuation to it.
  std::string line_buf_;

  // Keep the response data while we add all lines to it.
  FtpCtrlResponse response_buf_;

  // As we read full responses (possibly multiline), we add them to the queue.
  std::queue<FtpCtrlResponse> responses_;

  BoundNetLog net_log_;

  DISALLOW_COPY_AND_ASSIGN(FtpCtrlResponseBuffer);
};

}  // namespace net

#endif  // NET_FTP_FTP_CTRL_RESPONSE_BUFFER_H_