Spinning Topp Logo BlackTopp Studios
inc
httpresponse.cpp
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 _networkhttpresponse_cpp
42 #define _networkhttpresponse_cpp
43 
44 #include "Network/httpresponse.h"
45 
46 #include "stringtool.h"
47 
48 namespace
49 {
50  /// @brief Converts a String containing hex characters to a Whole.
51  /// @param ToConvert The Hexadecimal String to be converted.
52  /// @return Returns a Whole containing the converted value.
53  Mezzanine::Whole ConvertToWholeHex(const Mezzanine::String& ToConvert)
54  {
55  Mezzanine::Whole Ret = 0;
56  Mezzanine::StringStream Converter;
57  Converter << std::hex << ToConvert;
58  Converter >> Ret;
59  return Ret;
60  }
61 }
62 
63 namespace Mezzanine
64 {
65  namespace Network
66  {
68  ResponseCode(Network::HSC_Invalid)
69  { }
70 
71  HTTPResponse::HTTPResponse(const String& ToDecompose)
72  { this->Decompose(ToDecompose); }
73 
75  { }
76 
78  {
79  // A response header has 3 components: A version, a response code, and that codes' description.
80  Boole EoL = false;
81  String HeaderTemp;
82  // Version
83  EoL = this->GetMessageComponent(CurrIt,EndIt,HeaderTemp);
84  this->ParseHTTPVersion(HeaderTemp);
85  if( EoL ) {
86  return false;
87  }
88  // Response Code
89  HeaderTemp.clear();
90  EoL = this->GetMessageComponent(CurrIt,EndIt,HeaderTemp);
91  this->ResponseCode = StringTools::ConvertToWhole(HeaderTemp);
92  if( EoL ) {
93  return false;
94  }
95  // Response Description
96  this->ResponseDescription.clear();
97  while( CurrIt != EndIt )
98  {
99  if( (*CurrIt) == '\r' ) {
100  // Skip
101  }else if( (*CurrIt) == '\n' ) {
102  ++CurrIt;
103  break;
104  }else{
105  this->ResponseDescription.push_back( *CurrIt );
106  }
107  ++CurrIt;
108  }
109  return true;
110  }
111 
112  ///////////////////////////////////////////////////////////////////////////////
113  // Utility
114 
116  {
117  StringStream ResponseStream;
118  // Header
119  ResponseStream << "HTTP/" << this->MessageVersion.Major << "." << this->MessageVersion.Minor << " ";
120  ResponseStream << StringTools::ConvertToString( this->ResponseCode ) << " " << this->ResponseDescription << "\r\n";
121  // Fields
122  for( HeaderFieldContainer::const_iterator FieldIt = this->MessageFields.begin() ; FieldIt != this->MessageFields.end() ; ++FieldIt )
123  {
124  ResponseStream << (*FieldIt).HeaderName << ": " << (*FieldIt).HeaderValue << "\r\n";
125  }
126  ResponseStream << "\r\n";
127  // Body
128  ResponseStream << this->MessageBody;
129  return ResponseStream.str();
130  }
131 
133  {
134  StringIterator CurrIt = Message.begin();
135  return this->Decompose(CurrIt,Message.end());
136  }
137 
139  {
140  if( CurrIt != EndIt ) {
141  // Start decomposing
142  if( !this->ParseHTTPHeader(CurrIt,EndIt) ) {
143  return false;
144  }
145  if( !this->ParseHTTPFields(CurrIt,EndIt) ) {
146  return false;
147  }
148 
149  // The message body may or may not be chunked
150  this->MessageBody.clear();
151  if( this->GetField("transfer-encoding") != "chunked" ) {
152  String Length = this->GetContentLengthHeader();
153  if( !Length.empty() ) {
154  this->MessageBody.reserve( StringTools::ConvertToWhole( Length ) );
155  }
156  this->MessageBody.assign(CurrIt,EndIt);
157  }else{
158  String ChunkSizeStr;
159  this->GetMessageComponent(CurrIt,EndIt,ChunkSizeStr);
160  size_t ChunkLength = ConvertToWholeHex(ChunkSizeStr);
161  while( ChunkLength != 0 )
162  {
163  this->MessageBody.reserve( this->MessageBody.size() + ChunkLength );
164  while( (*CurrIt) != '\r' && CurrIt != EndIt )
165  {
166  this->MessageBody.push_back(*CurrIt);
167  ++CurrIt;
168  }
169 
170  ChunkSizeStr.clear();
171  this->GetMessageComponent(CurrIt,EndIt,ChunkSizeStr);
172  ChunkLength = ConvertToWholeHex(ChunkSizeStr);
173  }
174  // When the body is chunked, there may be optional trailing headers
175  if( !this->ParseHTTPFields(CurrIt,EndIt) ) {
176  return false;
177  }
178  }
179  return true;
180  }
181 
182  return false;
183  }
184 
186  { this->ResponseCode = Code; }
187 
189  { return this->ResponseCode; }
190 
192  { this->ResponseDescription = Message; }
193 
195  { return this->ResponseDescription; }
196 
197  ///////////////////////////////////////////////////////////////////////////////
198  // Recommended Header Field Convenience Methods
199 
201  { this->SetField("Allow",Allow); }
202 
204  { return this->GetField("Allow"); }
205 
206  void HTTPResponse::SetContentEncodingHeader(const String& ContentEncoding)
207  { this->SetField("Content-Encoding",ContentEncoding); }
208 
210  { return this->GetField("Content-Encoding"); }
211 
213  { this->SetField("Date",Date); }
214 
216  { return this->GetField("Date"); }
217 
219  { this->SetField("Server",Server); }
220 
222  { return this->GetField("Server"); }
223  }//Network
224 }//Mezzanine
225 
226 #endif
String::const_iterator StringIterator
Convenience typedef for String iterators.
Definition: httpmessage.h:77
String ResponseDescription
The textual message delivered to explain the status code.
Definition: httpresponse.h:63
Boole ParseHTTPFields(StringIterator &CurrIt, const StringIterator EndIt)
Parses the HTTP fields from a provided string.
bool Boole
Generally acts a single bit, true or false.
Definition: datatypes.h:173
const String & GetAllowHeader() const
Gets the Allow Header.
const String & GetServerHeader() const
Gets the Server Header.
String ConvertToString(const Vector2 &ToConvert)
Converts a Vector2 into a string.
Definition: stringtool.cpp:291
void SetAllowHeader(const String &Allow)
Sets the Allow Header.
UInt16 Major
The Major component of the version to be expressed.
Definition: version.h:68
Used for internal error conditions, not an actual status code.
const String & GetField(const String &FieldName) const
Gets a Name-Value pair for a header field in the message.
Boole Decompose(const String &Message)
Populates the members of this class with data from a text HTTP Message.
std::stringstream StringStream
A Datatype used for streaming operations with strings.
Definition: datatypes.h:176
SimpleVersion MessageVersion
The version component for this response.
Definition: httpmessage.h:90
UInt16 Minor
The Minor component of the version to be expressed.
Definition: version.h:70
void SetStatusCode(const Whole Code)
Sets the status code explaining the result of a HTTP request.
Boole GetMessageComponent(StringIterator &CurrIt, const StringIterator EndIt, String &Component)
Extracts all characters from CurrIt until the first space encountered or EndIt, whichever comes first...
const String & GetContentLengthHeader() const
Gets the Content-Length Header.
void SetField(const String &FieldName, const String &FieldValue)
Sets a Name-Value pair for a header field in the message.
~HTTPResponse()
Class destructor.
Boole ParseHTTPHeader(StringIterator &CurrIt, const StringIterator EndIt)
Parses the information contained in the Message Header.
const String & GetResponseDescription() const
Gets the textual description to explain the status code contained in the response.
Whole ConvertToWhole(const String &ToConvert)
Converts a string into an Whole.
Definition: stringtool.cpp:408
Whole ResponseCode
The HTTP method to use for the response.
Definition: httpresponse.h:60
Whole GetStatusCode() const
Gets the status code explaining the result of a HTTP request.
void SetDateHeader(const String &Date)
Sets the Date Header.
Boole ParseHTTPVersion(const String &ToParse)
Parses the HTTP version from a provided string.
const String & GetContentEncodingHeader() const
Gets the Content-Encoding Header.
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
void SetContentEncodingHeader(const String &ContentEncoding)
Sets the Content-Encoding Header.
String Compose() const
Creates a completed Message that can be sent across an HTTP connection.
void SetResponseDescription(const String &Message)
Sets the textual description to explain the status code contained in the response.
HTTPResponse()
Class constructor.
std::string String
A datatype used to a series of characters.
Definition: datatypes.h:159
const String & GetDateHeader() const
Gets the Date Header.
void SetServerHeader(const String &Server)
Sets the Server Header.