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
3// found in the LICENSE file.
4
5
6#ifndef NET_FTP_FTP_CTRL_RESPONSE_BUFFER_H_
7#define NET_FTP_FTP_CTRL_RESPONSE_BUFFER_H_
8
9#include <queue>
10#include <string>
11#include <vector>
12
13#include "base/basictypes.h"
14#include "net/base/net_export.h"
15#include "net/base/net_log.h"
16
17namespace net {
18
19struct NET_EXPORT_PRIVATE FtpCtrlResponse {
20  static const int kInvalidStatusCode;
21
22  FtpCtrlResponse();
23  ~FtpCtrlResponse();
24
25  int status_code;                 // Three-digit status code.
26  std::vector<std::string> lines;  // Response lines, without CRLFs.
27};
28
29class NET_EXPORT_PRIVATE FtpCtrlResponseBuffer {
30 public:
31  FtpCtrlResponseBuffer(const BoundNetLog& net_log);
32  ~FtpCtrlResponseBuffer();
33
34  // Called when data is received from the control socket. Returns error code.
35  int ConsumeData(const char* data, int data_length);
36
37  bool ResponseAvailable() const {
38    return !responses_.empty();
39  }
40
41  // Returns the next response. It is an error to call this function
42  // unless ResponseAvailable returns true.
43  FtpCtrlResponse PopResponse();
44
45 private:
46  struct ParsedLine {
47    ParsedLine();
48
49    // Indicates that this line begins with a valid 3-digit status code.
50    bool has_status_code;
51
52    // Indicates that this line has the dash (-) after the code, which
53    // means a multiline response.
54    bool is_multiline;
55
56    // Indicates that this line could be parsed as a complete and valid
57    // response line, without taking into account preceding lines (which
58    // may change its meaning into a continuation of the previous line).
59    bool is_complete;
60
61    // Part of response parsed as status code.
62    int status_code;
63
64    // Part of response parsed as status text.
65    std::string status_text;
66
67    // Text before parsing, without terminating CRLF.
68    std::string raw_text;
69  };
70
71  static ParsedLine ParseLine(const std::string& line);
72
73  void ExtractFullLinesFromBuffer();
74
75  // We keep not-yet-parsed data in a string buffer.
76  std::string buffer_;
77
78  std::queue<ParsedLine> lines_;
79
80  // True if we are in the middle of parsing a multi-line response.
81  bool multiline_;
82
83  // When parsing a multiline response, we don't know beforehand if a line
84  // will have a continuation. So always store last line of multiline response
85  // so we can append the continuation to it.
86  std::string line_buf_;
87
88  // Keep the response data while we add all lines to it.
89  FtpCtrlResponse response_buf_;
90
91  // As we read full responses (possibly multiline), we add them to the queue.
92  std::queue<FtpCtrlResponse> responses_;
93
94  BoundNetLog net_log_;
95
96  DISALLOW_COPY_AND_ASSIGN(FtpCtrlResponseBuffer);
97};
98
99}  // namespace net
100
101#endif  // NET_FTP_FTP_CTRL_RESPONSE_BUFFER_H_
102