HttpMessage.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 ** Copyright (c) 2001-2014
3 **
4 ** This file is part of the QuickFIX FIX Engine
5 **
6 ** This file may be distributed under the terms of the quickfixengine.org
7 ** license as defined by quickfixengine.org and appearing in the file
8 ** LICENSE included in the packaging of this file.
9 **
10 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
11 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
12 **
13 ** See http://www.quickfixengine.org/LICENSE for licensing information.
14 **
15 ** Contact ask@quickfixengine.org if any conditions of this licensing are
16 ** not clear to you.
17 **
18 ****************************************************************************/
19 
20 #ifdef _MSC_VER
21 #include "stdafx.h"
22 #else
23 #include "config.h"
24 #endif
25 
26 #include "HttpMessage.h"
27 #include "Utility.h"
28 #include <sstream>
29 #include <iomanip>
30 
31 namespace FIX
32 {
33 
35 
36 HttpMessage::HttpMessage( const std::string& string )
37 throw( InvalidMessage )
38 {
39  setString( string );
40 }
41 
42 std::string HttpMessage::toString() const
43 {
44  std::string str;
45  return toString( str );
46 }
47 
48 std::string& HttpMessage::toString( std::string& str ) const
49 {
50  str = m_root + getParameterString();
51  return str;
52 }
53 
54 void HttpMessage::setString( const std::string& string )
55 throw( InvalidMessage )
56 {
57  clear();
58 
59  std::string::size_type eolPos = string.find( "\r\n" );
60  if( eolPos == std::string::npos ) throw InvalidMessage();
61  std::string line = string.substr( 0, eolPos );
62  std::string::size_type getPos = line.find( "GET " );
63  if( getPos != 0 ) throw InvalidMessage();
64  std::string::size_type httpPos = line.rfind( "HTTP", std::string::npos );
65  if( httpPos == std::string::npos ) throw InvalidMessage();
66 
67  m_root = line.substr( getPos + 4, httpPos - 5 );
68  std::string::size_type paramPos = m_root.find_first_of( '?' );
69  if( paramPos == std::string::npos ) return;
70  std::string parameters = m_root.substr( paramPos, m_root.size() - paramPos );
71  m_root = m_root.substr( 0, paramPos );
72  paramPos = 0;
73 
74  while( paramPos != std::string::npos )
75  {
76  std::string::size_type sepPos = parameters.find_first_of( "=", paramPos );
77  if( sepPos == std::string::npos ) break;
78  std::string::size_type tempPos = paramPos;
79  paramPos = parameters.find_first_of( "&", paramPos + 1 );
80  std::string key = parameters.substr(tempPos + 1, sepPos - tempPos - 1);
81  std::string value = parameters.substr(sepPos + 1, paramPos - sepPos - 1);
82  m_parameters[key] = value;
83  }
84 }
85 
86 std::string HttpMessage::createResponse( int error, const std::string& text )
87 {
88  std::string errorString;
89  switch( error )
90  {
91  case 100: errorString = "Continue"; break;
92  case 101: errorString = "Switching Protocols"; break;
93  case 200: errorString = "OK"; break;
94  case 201: errorString = "Created"; break;
95  case 202: errorString = "Accepted"; break;
96  case 203: errorString = "Non-Authoritative Information"; break;
97  case 204: errorString = "No Content"; break;
98  case 205: errorString = "Reset Content"; break;
99  case 206: errorString = "Partial Content"; break;
100  case 300: errorString = "Multiple Choices"; break;
101  case 301: errorString = "Moved Permanently"; break;
102  case 302: errorString = "Found"; break;
103  case 303: errorString = "See Other"; break;
104  case 304: errorString = "Not Modified"; break;
105  case 305: errorString = "Use Proxy"; break;
106  case 307: errorString = "Temporary Redirect"; break;
107  case 400: errorString = "Bad Request"; break;
108  case 401: errorString = "Unauthorized"; break;
109  case 402: errorString = "Payment Required"; break;
110  case 403: errorString = "Forbidden"; break;
111  case 404: errorString = "Not Found"; break;
112  case 405: errorString = "Method Not Allowed"; break;
113  case 406: errorString = "Not Acceptable"; break;
114  case 407: errorString = "Proxy Authentication Required"; break;
115  case 408: errorString = "Request Timeout"; break;
116  case 409: errorString = "Conflict"; break;
117  case 410: errorString = "Gone"; break;
118  case 411: errorString = "Length Required"; break;
119  case 412: errorString = "Precondition Failed"; break;
120  case 413: errorString = "Request Entity Too Large"; break;
121  case 414: errorString = "Request-URI Too Large"; break;
122  case 415: errorString = "Unsupported Media Type"; break;
123  case 416: errorString = "Requested Range Not Satisfiable"; break;
124  case 417: errorString = "Expectation Failed"; break;
125  case 500: errorString = "Internal Server Error"; break;
126  case 501: errorString = "Not Implemented"; break;
127  case 502: errorString = "Bad Gateway"; break;
128  case 503: errorString = "Service Unavailable"; break;
129  case 504: errorString = "Gateway Timeout"; break;
130  case 505: errorString = "HTTP Version not supported"; break;
131  default: errorString = "Unknown";
132  }
133 
134  std::stringstream response;
135  response << "HTTP/1.1 " << error << " " << errorString << "\r\n"
136  << "Server: QuickFIX" << "\r\n"
137  << "Content-Type: text/html; charset=iso-8859-1" << "\r\n\r\n"
138  << "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">";
139 
140  if( error < 200 || error >= 300 )
141  response << "<HTML><HEAD><TITLE>" << error << " " << errorString << "</TITLE></HEAD><BODY>"
142  << "<H1>" << error << " " << errorString << "</H1>" << text << "</BODY></HTML>";
143  else
144  response << text;
145 
146  return response.str();
147 }
148 
149 }
FIX::HttpMessage::createResponse
static std::string createResponse(int error=0, const std::string &text="")
Definition: HttpMessage.cpp:103
FIX::HttpMessage::getParameterString
const std::string getParameterString() const
Definition: HttpMessage.h:110
FIX
Definition: Acceptor.cpp:34
FIX::HttpMessage::setString
void setString(const std::string &string)
Definition: HttpMessage.cpp:71
FIX::HttpMessage::toString
std::string toString() const
Get a string representation of the message.
Definition: HttpMessage.cpp:59
FIX::HttpMessage::m_root
std::string m_root
Definition: HttpMessage.h:153
FIX::InvalidMessage
Not a recognizable message.
Definition: Exceptions.h:97
HttpMessage.h
FIX::HttpMessage::HttpMessage
HttpMessage()
Definition: HttpMessage.cpp:51
Utility.h

Generated on Wed Apr 29 2020 19:41:30 for QuickFIX by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2001