HttpMessage.cpp
Go to the documentation of this file.
00001 /****************************************************************************
00002 ** Copyright (c) 2001-2014
00003 **
00004 ** This file is part of the QuickFIX FIX Engine
00005 **
00006 ** This file may be distributed under the terms of the quickfixengine.org
00007 ** license as defined by quickfixengine.org and appearing in the file
00008 ** LICENSE included in the packaging of this file.
00009 **
00010 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00011 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00012 **
00013 ** See http://www.quickfixengine.org/LICENSE for licensing information.
00014 **
00015 ** Contact ask@quickfixengine.org if any conditions of this licensing are
00016 ** not clear to you.
00017 **
00018 ****************************************************************************/
00019 
00020 #ifdef _MSC_VER
00021 #include "stdafx.h"
00022 #else
00023 #include "config.h"
00024 #endif
00025 
00026 #include "HttpMessage.h"
00027 #include "Utility.h"
00028 #include <sstream>
00029 #include <iomanip>
00030 
00031 namespace FIX
00032 {
00033 
00034 HttpMessage::HttpMessage() {}
00035 
00036 HttpMessage::HttpMessage( const std::string& string )
00037 throw( InvalidMessage )
00038 {
00039   setString( string );
00040 }
00041 
00042 std::string HttpMessage::toString() const
00043 {
00044   std::string str;
00045   return toString( str );
00046 }
00047 
00048 std::string& HttpMessage::toString( std::string& str ) const
00049 {
00050   str = m_root + getParameterString();
00051   return str;
00052 }
00053 
00054 void HttpMessage::setString( const std::string& string )
00055 throw( InvalidMessage )
00056 {
00057   clear();
00058 
00059   std::string::size_type eolPos = string.find( "\r\n" );
00060   if( eolPos == std::string::npos ) throw InvalidMessage();
00061   std::string line = string.substr( 0, eolPos );
00062   std::string::size_type getPos = line.find( "GET " );
00063   if( getPos != 0 ) throw InvalidMessage();
00064   std::string::size_type httpPos = line.rfind( "HTTP", std::string::npos );
00065   if( httpPos == std::string::npos ) throw InvalidMessage();
00066 
00067   m_root = line.substr( getPos + 4, httpPos - 5 );
00068   std::string::size_type paramPos = m_root.find_first_of( '?' );
00069   if( paramPos == std::string::npos ) return;
00070   std::string parameters = m_root.substr( paramPos, m_root.size() - paramPos );
00071   m_root = m_root.substr( 0, paramPos );
00072   paramPos = 0;
00073 
00074   while( paramPos != std::string::npos )
00075   {
00076     std::string::size_type sepPos = parameters.find_first_of( "=", paramPos );
00077     if( sepPos == std::string::npos ) break;
00078     std::string::size_type tempPos = paramPos;
00079     paramPos = parameters.find_first_of( "&", paramPos + 1 );
00080     std::string key = parameters.substr(tempPos + 1, sepPos - tempPos - 1);
00081     std::string value = parameters.substr(sepPos + 1, paramPos - sepPos - 1);
00082     m_parameters[key] = value;
00083   }
00084 }
00085 
00086 std::string HttpMessage::createResponse( int error, const std::string& text )
00087 {
00088   std::string errorString;
00089   switch( error )
00090   {
00091   case 100: errorString = "Continue"; break;
00092   case 101: errorString = "Switching Protocols"; break;
00093   case 200: errorString = "OK"; break;
00094   case 201: errorString = "Created"; break;
00095   case 202: errorString = "Accepted"; break;
00096   case 203: errorString = "Non-Authoritative Information"; break;
00097   case 204: errorString = "No Content"; break;
00098   case 205: errorString = "Reset Content"; break;
00099   case 206: errorString = "Partial Content"; break;
00100   case 300: errorString = "Multiple Choices"; break;
00101   case 301: errorString = "Moved Permanently"; break;
00102   case 302: errorString = "Found"; break;
00103   case 303: errorString = "See Other"; break;
00104   case 304: errorString = "Not Modified"; break;
00105   case 305: errorString = "Use Proxy"; break;
00106   case 307: errorString = "Temporary Redirect"; break;
00107   case 400: errorString = "Bad Request"; break;
00108   case 401: errorString = "Unauthorized"; break;
00109   case 402: errorString = "Payment Required"; break;
00110   case 403: errorString = "Forbidden"; break;
00111   case 404: errorString = "Not Found"; break;
00112   case 405: errorString = "Method Not Allowed"; break;
00113   case 406: errorString = "Not Acceptable"; break;
00114   case 407: errorString = "Proxy Authentication Required"; break;
00115   case 408: errorString = "Request Timeout"; break;
00116   case 409: errorString = "Conflict"; break;
00117   case 410: errorString = "Gone"; break;
00118   case 411: errorString = "Length Required"; break;
00119   case 412: errorString = "Precondition Failed"; break;
00120   case 413: errorString = "Request Entity Too Large"; break;
00121   case 414: errorString = "Request-URI Too Large"; break;
00122   case 415: errorString = "Unsupported Media Type"; break;
00123   case 416: errorString = "Requested Range Not Satisfiable"; break;
00124   case 417: errorString = "Expectation Failed"; break;
00125   case 500: errorString = "Internal Server Error"; break;
00126   case 501: errorString = "Not Implemented"; break;
00127   case 502: errorString = "Bad Gateway"; break;
00128   case 503: errorString = "Service Unavailable"; break;
00129   case 504: errorString = "Gateway Timeout"; break;
00130   case 505: errorString = "HTTP Version not supported"; break;
00131   default: errorString = "Unknown";
00132   }
00133 
00134   std::stringstream response;
00135   response << "HTTP/1.1 " << error << " " << errorString << "\r\n"
00136            << "Server: QuickFIX" << "\r\n"
00137            << "Content-Type: text/html; charset=iso-8859-1" << "\r\n\r\n"
00138            << "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">";
00139 
00140   if( error < 200 || error >= 300 )
00141     response << "<HTML><HEAD><TITLE>" << error << " " << errorString << "</TITLE></HEAD><BODY>"
00142              << "<H1>" << error << " " << errorString << "</H1>" << text << "</BODY></HTML>";
00143   else
00144     response << text;
00145 
00146   return response.str();
00147 }
00148 
00149 }

Generated on Mon Sep 15 2014 01:23:54 for QuickFIX by doxygen 1.7.6.1 written by Dimitri van Heesch, © 1997-2001