Spinning Topp Logo BlackTopp Studios
inc
httpmessage.h
1 // © Copyright 2010 - 2015 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 _networkhttpmessage_h
42 #define _networkhttpmessage_h
43 
44 #include "version.h"
45 #include "Network/networkenumerations.h"
46 
47 namespace Mezzanine
48 {
49  namespace Network
50  {
51  ///////////////////////////////////////////////////////////////////////////////
52  /// @brief Convenience class for storing Header fields in internet messaging protocols.
53  ///////////////////////////////////////
55  {
56  /// @brief The name of the header field.
58  /// @brief The value of the header field.
60 
61  /// @brief Constructor.
62  /// @param Name The name of the Header Field.
63  /// @param Value The value of the Header Field.
64  HeaderField(const String& Name, const String& Value) :
65  HeaderName(Name),
66  HeaderValue(Value)
67  { }
68  };//HeaderField
69 
70  ///////////////////////////////////////////////////////////////////////////////
71  /// @brief A base class for HTTP text parsing classes.
72  ///////////////////////////////////////
74  {
75  public:
76  /// @brief Convenience typedef for String iterators.
77  typedef String::const_iterator StringIterator;
78  /// @brief Container type for Header Fields in this class.
79  typedef std::vector<HeaderField> HeaderFieldContainer;
80  /// @brief Iterator type for Header Fields in this class.
81  typedef HeaderFieldContainer::iterator HeaderFieldIterator;
82  /// @brief Const Iterator type for Header Fields in this class.
83  typedef HeaderFieldContainer::const_iterator ConstHeaderFieldIterator;
84  protected:
85  /// @internal
86  /// @brief The maximum number of allowed header fields in this message.
88  /// @internal
89  /// @brief The version component for this response.
91  /// @internal
92  /// @brief The body of the message.
94  /// @internal
95  /// @brief A container of fields to populate the message header with.
96  HeaderFieldContainer MessageFields;
97 
98  /// @internal
99  /// @brief Parses the HTTP version from a provided string.
100  /// @param ToParse The String to retrieve the version information from.
101  /// @return Returns true if the parse was successful, false if it failed.
102  Boole ParseHTTPVersion(const String& ToParse);
103  /// @internal
104  /// @brief Parses the information contained in the Message Header.
105  /// @param CurrIt An iterator to the current character being processed in the string being parsed.
106  /// @param EndIt An iterator to the last character in the string to be parsed.
107  /// @return Returns true if the parse was successful, false if it failed.
108  virtual Boole ParseHTTPHeader(StringIterator& CurrIt, const StringIterator EndIt) = 0;
109  /// @internal
110  /// @brief Parses the HTTP fields from a provided string.
111  /// @param CurrIt An iterator to the current character being processed in the string being parsed.
112  /// @param EndIt An iterator to the last character in the string to be parsed.
113  /// @return Returns true if the parse was successful, false if it failed.
114  Boole ParseHTTPFields(StringIterator& CurrIt, const StringIterator EndIt);
115 
116  /// @internal
117  /// @brief Extracts all characters from CurrIt until the first space encountered or EndIt, whichever comes first.
118  /// @param CurrIt An iterator to the current character being processed in the string being parsed.
119  /// @param EndIt An iterator to the last character in the string to be parsed.
120  /// @param Component The string to which the extracted characters will be appended.
121  /// @return Returns true if the end of line was detected while parsing, false otherwise.
122  Boole GetMessageComponent(StringIterator& CurrIt, const StringIterator EndIt, String& Component);
123  /// @internal
124  /// @brief Convenience method that will ignore and advance passed all characters until one passed the first newline character detected.
125  /// @param CurrIt An iterator to the current character being processed in the string being parsed.
126  /// @param EndIt An iterator to the last character in the string to be parsed.
127  void AdvanceToNewline(StringIterator& CurrIt, const StringIterator EndIt);
128  //public:
129  /// @brief Blank Constructor.
130  HTTPMessage();
131  /// @brief UInt Version Constructor.
132  HTTPMessage(const UInt16 VerMajor, const UInt16 VerMinor);
133  /// @brief Version Constructor.
134  HTTPMessage(const SimpleVersion& Version);
135  /// @brief Class destructor.
136  virtual ~HTTPMessage();
137  public:
138  ///////////////////////////////////////////////////////////////////////////////
139  // Core Operations
140 
141  /// @brief Creates a completed Message that can be sent across an HTTP connection.
142  /// @return Returns a String containing the generated Message.
143  virtual String Compose() const = 0;
144  /// @brief Populates the members of this class with data from a text HTTP Message.
145  /// @param Message The String containing the Message to be decomposed.
146  /// @return Returns true if the String was successfully decomposed.
147  virtual Boole Decompose(const String& Message) = 0;
148  /// @brief Populates the members of this class with data from a text HTTP Message.
149  /// @param CurrIt An iterator at the start of the range to be parsed.
150  /// @param EndIt An iterator at the end of the range to be parsed. Parsing may or may not reach this point.
151  /// @return Returns true if the String was successfully decomposed.
152  virtual Boole Decompose(StringIterator& CurrIt, const StringIterator EndIt) = 0;
153 
154  ///////////////////////////////////////////////////////////////////////////////
155  // Parameter setting/configuration
156 
157  /// @brief Sets both the major and minor version of this message via SimpleVersion.
158  /// @param Version The version to be applied to this message.
159  void SetHTTPVersion(const SimpleVersion& Version);
160  /// @brief Gets both the major and minor version of this message.
161  /// @return Returns a const reference to a SimpleVersion containing the major and minor versions of this message.
162  const SimpleVersion& GetHTTPVersion() const;
163 
164  /// @brief Sets both the major and minor version of this message.
165  /// @param Major The major portion of the version to set.
166  /// @param Minor The minor portion of the version to set.
167  void SetHTTPVersion(const UInt16 Major, const UInt16 Minor);
168 
169  /// @brief Sets the major version of this message.
170  /// @param Major The major portion of the version to set.
171  void SetHTTPMajorVersion(const UInt16 Major);
172  /// @brief Gets the major version of this message.
173  /// @return Returns the major version component of this message.
174  UInt16 GetHTTPMajorVersion() const;
175  /// @brief Sets the minor version of this message.
176  /// @param Minor The minor portion of the version to set.
177  void SetHTTPMinorVersion(const UInt16 Minor);
178  /// @brief Gets the minor version of this message.
179  /// @return Returns the minor version component of this message.
180  UInt16 GetHTTPMinorVersion() const;
181 
182  /// @brief Sets the message body containing additional information.
183  /// @remarks Request bodies are optional or just unnecessary for most request methods. Be sure you need a body for your request before setting one on a request.
184  /// @param Body The body of the message.
185  void SetBody(const String& Body);
186  /// @brief Gets the message body containing additional information.
187  /// @return Returns a String containing the body of the message.
188  const String& GetBody() const;
189 
190  /// @brief Sets how many header fields can be added to this message during parsing.
191  /// @remarks Setting a sane maximum number of header fields is designed to prevent certain types of DDOS attacks on servers.
192  /// @param MaxFields The maximum number of header fields that can be parsed by this message. Initial value: 100.
193  void SetMaxFields(const Whole MaxFields);
194  /// @brief Gets how many header fields can be added to this message during parsing.
195  /// @return Returns a whole representing how many header fields can be parsed.
196  Whole GetMaxFields() const;
197 
198  /// @brief Sets a Name-Value pair for a header field in the message.
199  /// @param FieldName The name of the field to be set.
200  /// @param FieldValue The value to be set for the named header field.
201  void SetField(const String& FieldName, const String& FieldValue);
202  /// @brief Gets a Name-Value pair for a header field in the message.
203  /// @param FieldName The name of the field to retrieve.
204  /// @return Returns a String containing the set value for the named header. If none is set or the named field does not exist then an empty String is returned.
205  const String& GetField(const String& FieldName) const;
206  /// @brief Checks if this message has the named header field.
207  /// @note Fields should only ever be created when "SetField" is called, but there is no guarantee that the value for that field will be set.
208  /// @param FieldName The name of the field to check for.
209  /// @return Returns true if this message has the named field, false otherwise.
210  Boole HasField(const String& FieldName) const;
211  /// @brief Removes a specific field from this message by name.
212  /// @param FieldName The name of the field to be removed.
213  void RemoveField(const String& FieldName);
214  /// @brief Removes every field from this message.
215  void RemoveAllFields();
216 
217  ///////////////////////////////////////////////////////////////////////////////
218  // Recommended Header Field Convenience Methods
219 
220  /// @brief Sets the Connection Header.
221  /// @remarks In most cases this will just specify whether the connection is expected to be closed immediately after the transaction or kept open (close vs keep-alive).
222  /// @param Connection Control options for the current HTTP connection.
223  void SetConnectionHeader(const String& Connection);
224  /// @brief Gets the Connection Header.
225  /// @return Returns a String containing control options for the current HTTP connection, or blank if the field is not set.
226  const String& GetConnectionHeader() const;
227  /// @brief Sets the Content-Length Header.
228  /// @remarks This header is required any time a message body has contents and is not chunked.
229  /// @param ContentLength The length of the message body in bytes.
230  void SetContentLengthHeader(const String& ContentLength);
231  /// @brief Gets the Content-Length Header.
232  /// @return Returns a String containing containing the length of the message body in bytes, or blank if the field is not set.
233  const String& GetContentLengthHeader() const;
234  /// @brief Sets the Content-Type Header.
235  /// @remarks This header is required any time a message body has contents.
236  /// @param ContentType One or more descriptors on how the data in the message body is presented, such if it's plain text, XML, or a form.
237  void SetContentTypeHeader(const String& ContentType);
238  /// @brief Gets the Content-Type Header.
239  /// @return Returns a String containing one or more descriptors on how the data in the message body is presented, such if it's plain text, XML, or a form. Can also return blank if not set.
240  const String& GetContentTypeHeader() const;
241  };//HTTPMessage
242  }//Network
243 }//Mezzanine
244 
245 #endif
A base class for the state of a connection made between peers.
Definition: connection.h:56
String::const_iterator StringIterator
Convenience typedef for String iterators.
Definition: httpmessage.h:77
A base class for HTTP text parsing classes.
Definition: httpmessage.h:73
bool Boole
Generally acts a single bit, true or false.
Definition: datatypes.h:173
std::vector< HeaderField > HeaderFieldContainer
Container type for Header Fields in this class.
Definition: httpmessage.h:79
SimpleVersion MessageVersion
The version component for this response.
Definition: httpmessage.h:90
uint16_t UInt16
An 16-bit unsigned integer.
Definition: datatypes.h:122
HeaderFieldContainer::const_iterator ConstHeaderFieldIterator
Const Iterator type for Header Fields in this class.
Definition: httpmessage.h:83
A very basic class for expressing an API or syntax version.
Definition: version.h:53
String HeaderName
The name of the header field.
Definition: httpmessage.h:57
String HeaderValue
The value of the header field.
Definition: httpmessage.h:59
Whole MaxHeaderFields
The maximum number of allowed header fields in this message.
Definition: httpmessage.h:87
HeaderField(const String &Name, const String &Value)
Constructor.
Definition: httpmessage.h:64
Convenience class for storing Header fields in internet messaging protocols.
Definition: httpmessage.h:54
#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
HeaderFieldContainer MessageFields
A container of fields to populate the message header with.
Definition: httpmessage.h:96
unsigned long Whole
Whole is an unsigned integer, it will be at least 32bits in size.
Definition: datatypes.h:151
String MessageBody
The body of the message.
Definition: httpmessage.h:93
HeaderFieldContainer::iterator HeaderFieldIterator
Iterator type for Header Fields in this class.
Definition: httpmessage.h:81
std::string String
A datatype used to a series of characters.
Definition: datatypes.h:159