Spinning Topp Logo BlackTopp Studios
inc
ftpresponse.h
1 // © Copyright 2010 - 2016 BlackTopp Studios Inc.
2 /* This file is part of The Mezzanine Engine.
3 
4  The Mezzanine Engine is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  The Mezzanine Engine is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with The Mezzanine Engine. If not, see <http://www.gnu.org/licenses/>.
16 */
17 /* The original authors have included a copy of the license specified above in the
18  'Docs' folder. See 'gpl.txt'
19 */
20 /* We welcome the use of the Mezzanine engine to anyone, including companies who wish to
21  Build professional software and charge for their product.
22 
23  However there are some practical restrictions, so if your project involves
24  any of the following you should contact us and we will try to work something
25  out:
26  - DRM or Copy Protection of any kind(except Copyrights)
27  - Software Patents You Do Not Wish to Freely License
28  - Any Kind of Linking to Non-GPL licensed Works
29  - Are Currently In Violation of Another Copyright Holder's GPL License
30  - If You want to change our code and not add a few hundred MB of stuff to
31  your distribution
32 
33  These and other limitations could cause serious legal problems if you ignore
34  them, so it is best to simply contact us or the Free Software Foundation, if
35  you have any questions.
36 
37  Joseph Toppi - toppij@gmail.com
38  John Blackwood - makoenergy02@gmail.com
39 */
40 
41 #ifndef _networkftpresponse_h
42 #define _networkftpresponse_h
43 
44 #include "Network/ftpmessage.h"
45 
46 namespace Mezzanine
47 {
48  namespace Network
49  {
50  ///////////////////////////////////////////////////////////////////////////////
51  /// @brief A response to a command generated by a FTP server.
52  /// @details This class supports single-line and multi-line responses. When accessing lines in
53  /// a multi-line response keep in mind that the first and last lines contain the response code
54  /// and any additional/optional text associated with the response. Intermediary lines containing
55  /// additional information regarding the command being responded to begin at index 1, and end at
56  /// one before the last line.
57  ///////////////////////////////////////
59  {
60  public:
61  /// @brief Container type for one or more lines in a response.
62  typedef std::vector<String> LineContainer;
63  /// @brief Iterator type for each line in a response.
64  typedef LineContainer::iterator LineIterator;
65  /// @brief Const Iterator type for each line in a response.
66  typedef LineContainer::const_iterator ConstLineIterator;
67  protected:
68  /// @internal
69  /// @brief Container storing each line in this response.
70  LineContainer ResponseLines;
71  /// @internal
72  /// @brief The 3 digit response code that identifies the nature of the response from the server.
74 
75  /// @internal
76  /// @brief Converts the first three characters in the range into a 3 digit response code.
77  /// @param CurrIt An iterator at the start of the range to be parsed.
78  /// @param EndIt An iterator at the end of the range to be parsed. Parsing may or may not reach this point.
79  /// @return Returns true if the parse was successful or false if there was an error, such as fewer than 3 characters in the range, or a non-digit is found.
80  Boole ParseResponseCode(StringIterator CurrIt, const StringIterator EndIt);
81 
82  /// @internal
83  /// @brief Parses/verifies a single line of a FTP response.
84  /// @param CurrIt An iterator at the start of the range to be parsed.
85  /// @param EndIt An iterator at the end of the range to be parsed. Parsing may or may not reach this point.
86  /// @param Line The String to be populated with the response line that was retrieved.
87  /// @return Returns true if the parse was successful or false if there was an error.
88  Boole GetResponseLine(StringIterator& CurrIt, const StringIterator EndIt, String& Line);
89  public:
90  /// @brief Invalid constructor.
91  /// @remarks This will initialize with an invalid response code.
92  FTPResponse();
93  /// @brief One-line response constructor.
94  /// @remarks This constructor will not insert a hyphen into the response line generated, so it is not
95  /// appropriate for use with multi-line responses.
96  /// @param Code The code describing the type of response.
97  /// @param Description The optional description text to go along with the status code.
98  FTPResponse(const Network::FTPResponseCode Code, const String& Description);
99  /// @brief String constructor.
100  /// @param Response The complete response to be parsed.
101  FTPResponse(const String& Response);
102  /// @brief Class destructor.
103  ~FTPResponse();
104 
105  ///////////////////////////////////////////////////////////////////////////////
106  // Core Operations
107 
108  /// @copydoc FTPMessage::Compose() const
109  String Compose() const;
110  /// @copydoc FTPMessage::Decompose(const String&)
111  Boole Decompose(const String& Message);
112  /// @copydoc FTPMessage::Decompose(StringIterator&,const StringIterator)
113  Boole Decompose(StringIterator& CurrIt, const StringIterator EndIt);
114 
115  ///////////////////////////////////////////////////////////////////////////////
116  // Utility
117 
118  /// @brief Checks if a number is a valid response code.
119  /// @param Code The code, as a Whole, to be checked.
120  /// @return Returns true if the number provided is a valid response code defined by an RFC, false otherwise.
121  static Boole CodeIsValid(const Whole Code);
122 
123  /// @brief Sets the code describing this response.
124  /// @param Code The response code to be set.
125  void SetCode(const Network::FTPResponseCode Code);
126  /// @brief Gets the code describing this response.
127  /// @return Returns a FTPResponseCode enum value describing the type of response this is.
128  Network::FTPResponseCode GetCode() const;
129 
130  /// @brief Adds a new line to this response.
131  /// @note This should be used with expert care. No sanity checks are performed in this method
132  /// and the standard mandates some basic things be provided. @n
133  /// For example the first line must start with 3 digits and then either a space or hyphen
134  /// (depending on multi-line) then possibly some additional text. If the response is multi-line
135  /// then the final line must contain the 3 digit response code then may contain optional text. @n
136  /// If any text follows the end line then it will be ignored and may cause problems when future
137  /// parsing is expected. In short, there are a lot of wrong ways to use this method. An
138  /// understanding of the FTP specification helps. Use with care.
139  /// @param NewLine The line to be added.
140  void AddLine(const String& NewLine);
141  /// @brief Gets a line in this response
142  /// @param Index The index of the line to be retrieved.
143  /// @return Returns the line at the specified index.
144  const String& GetLine(const Whole Index) const;
145  /// @brief Gets the number of lines in this response.
146  /// @return Returns the number of lines currently in this response. 0 would indicate an invalid state.
147  Whole GetNumLines() const;
148  /// @brief Clears all lines from this response.
149  void ClearLines();
150 
151  /// @brief Checks to see if this is a multi-line response.
152  /// @return Returns true if this response contains multiple lines of text, false otherwise.
153  Boole IsMultiline() const;
154  };//FTPResponse
155  }//Network
156 }//Mezzanine
157 
158 #endif
FTPResponseCode ResponseCode
The 3 digit response code that identifies the nature of the response from the server.
Definition: ftpresponse.h:73
bool Boole
Generally acts a single bit, true or false.
Definition: datatypes.h:173
LineContainer ResponseLines
Container storing each line in this response.
Definition: ftpresponse.h:70
LineContainer::iterator LineIterator
Iterator type for each line in a response.
Definition: ftpresponse.h:64
A base class for text based messages exchanged between FTP clients and servers.
Definition: ftpmessage.h:54
LineContainer::const_iterator ConstLineIterator
Const Iterator type for each line in a response.
Definition: ftpresponse.h:66
FTPResponseCode
This enum is a listing of the response codes that can be used in response to FTP commands.
A response to a command generated by a FTP server.
Definition: ftpresponse.h:58
String::const_iterator StringIterator
Convenience typedef for String iterators.
Definition: ftpmessage.h:58
#define MEZZ_LIB
Some platforms require special decorations to denote what is exported/imported in a share library...
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
unsigned long Whole
Whole is an unsigned integer, it will be at least 32bits in size.
Definition: datatypes.h:151
std::string String
A datatype used to a series of characters.
Definition: datatypes.h:159
std::vector< String > LineContainer
Container type for one or more lines in a response.
Definition: ftpresponse.h:62