Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / net / ftp / ftp_ctrl_response_buffer.h
blob92aaf533c1f04ab582cdfa55e30accf664627a8c
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.
6 #ifndef NET_FTP_FTP_CTRL_RESPONSE_BUFFER_H_
7 #define NET_FTP_FTP_CTRL_RESPONSE_BUFFER_H_
9 #include <queue>
10 #include <string>
11 #include <vector>
13 #include "base/basictypes.h"
14 #include "net/base/net_export.h"
15 #include "net/log/net_log.h"
17 namespace net {
19 struct NET_EXPORT_PRIVATE FtpCtrlResponse {
20 static const int kInvalidStatusCode;
22 FtpCtrlResponse();
23 ~FtpCtrlResponse();
25 int status_code; // Three-digit status code.
26 std::vector<std::string> lines; // Response lines, without CRLFs.
29 class NET_EXPORT_PRIVATE FtpCtrlResponseBuffer {
30 public:
31 FtpCtrlResponseBuffer(const BoundNetLog& net_log);
32 ~FtpCtrlResponseBuffer();
34 // Called when data is received from the control socket. Returns error code.
35 int ConsumeData(const char* data, int data_length);
37 bool ResponseAvailable() const {
38 return !responses_.empty();
41 // Returns the next response. It is an error to call this function
42 // unless ResponseAvailable returns true.
43 FtpCtrlResponse PopResponse();
45 private:
46 struct ParsedLine {
47 ParsedLine();
49 // Indicates that this line begins with a valid 3-digit status code.
50 bool has_status_code;
52 // Indicates that this line has the dash (-) after the code, which
53 // means a multiline response.
54 bool is_multiline;
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;
61 // Part of response parsed as status code.
62 int status_code;
64 // Part of response parsed as status text.
65 std::string status_text;
67 // Text before parsing, without terminating CRLF.
68 std::string raw_text;
71 static ParsedLine ParseLine(const std::string& line);
73 void ExtractFullLinesFromBuffer();
75 // We keep not-yet-parsed data in a string buffer.
76 std::string buffer_;
78 std::queue<ParsedLine> lines_;
80 // True if we are in the middle of parsing a multi-line response.
81 bool multiline_;
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_;
88 // Keep the response data while we add all lines to it.
89 FtpCtrlResponse response_buf_;
91 // As we read full responses (possibly multiline), we add them to the queue.
92 std::queue<FtpCtrlResponse> responses_;
94 BoundNetLog net_log_;
96 DISALLOW_COPY_AND_ASSIGN(FtpCtrlResponseBuffer);
99 } // namespace net
101 #endif // NET_FTP_FTP_CTRL_RESPONSE_BUFFER_H_