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_
13 #include "base/basictypes.h"
14 #include "net/base/net_export.h"
15 #include "net/log/net_log.h"
19 struct NET_EXPORT_PRIVATE FtpCtrlResponse
{
20 static const int kInvalidStatusCode
;
25 int status_code
; // Three-digit status code.
26 std::vector
<std::string
> lines
; // Response lines, without CRLFs.
29 class NET_EXPORT_PRIVATE FtpCtrlResponseBuffer
{
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();
49 // Indicates that this line begins with a valid 3-digit status code.
52 // Indicates that this line has the dash (-) after the code, which
53 // means a multiline response.
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).
61 // Part of response parsed as status code.
64 // Part of response parsed as status text.
65 std::string status_text
;
67 // Text before parsing, without terminating CRLF.
71 static ParsedLine
ParseLine(const std::string
& line
);
73 void ExtractFullLinesFromBuffer();
75 // We keep not-yet-parsed data in a string buffer.
78 std::queue
<ParsedLine
> lines_
;
80 // True if we are in the middle of parsing a multi-line response.
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_
;
96 DISALLOW_COPY_AND_ASSIGN(FtpCtrlResponseBuffer
);
101 #endif // NET_FTP_FTP_CTRL_RESPONSE_BUFFER_H_