QuickFIX: /home/omiller/Development/autobuild/quickfix/src/C++/HttpMessage.cpp Source File

Index Source Files Annotated Class List Alphabetical Class List Class Hierarchy Graphical Class Hierarchy

# HttpMessage.cpp

Go to the documentation of this file.

    00001 /****************************************************************************
    00002 ** Copyright (c) quickfixengine.org  All rights reserved.
    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 #include "CallStack.h"
    00026
    00027 #include "HttpMessage.h"
    00028 #include "Utility.h"
    00029 #include <sstream>
    00030 #include <iomanip>
    00031
    00032 namespace FIX
    00033 {
    00034
    00035 HttpMessage::HttpMessage() {}
    00036
    00037 HttpMessage::HttpMessage( const std::string& string )
    00038 throw( InvalidMessage )
    00039 {
    00040   setString( string );
    00041 }
    00042
    00043 std::string HttpMessage::toString() const
    00044 { QF_STACK_PUSH(HttpMessage::toString)
    00045
    00046   std::string str;
    00047   return toString( str );
    00048
    00049   QF_STACK_POP
    00050 }
    00051
    00052 std::string& HttpMessage::toString( std::string& str ) const
    00053 { QF_STACK_PUSH(HttpMessage::toString)
    00054
    00055   str = m_root + getParameterString();
    00056   return str;
    00057
    00058   QF_STACK_POP
    00059 }
    00060
    00061 void HttpMessage::setString( const std::string& string )
    00062 throw( InvalidMessage )
    00063 { QF_STACK_PUSH(HttpMessage::setString)
    00064
    00065   clear();
    00066
    00067   std::string::size_type eolPos = string.find( "\r\n" );
    00068   if( eolPos == std::string::npos ) throw InvalidMessage();
    00069   std::string line = string.substr( 0, eolPos );
    00070   std::string::size_type getPos = line.find( "GET " );
    00071   if( getPos != 0 ) throw InvalidMessage();
    00072   std::string::size_type httpPos = line.rfind( "HTTP", std::string::npos );
    00073   if( httpPos == std::string::npos ) throw InvalidMessage();
    00074
    00075   m_root = line.substr( getPos + 4, httpPos - 5 );
    00076   std::string::size_type paramPos = m_root.find_first_of( '?' );
    00077   if( paramPos == std::string::npos ) return;
    00078   std::string parameters = m_root.substr( paramPos, m_root.size() - paramPos );
    00079   m_root = m_root.substr( 0, paramPos );
    00080   paramPos = 0;
    00081
    00082   while( paramPos != std::string::npos )
    00083   {
    00084     std::string::size_type sepPos = parameters.find_first_of( "=", paramPos );
    00085     if( sepPos == std::string::npos ) break;
    00086     std::string::size_type tempPos = paramPos;
    00087     paramPos = parameters.find_first_of( "&", paramPos + 1 );
    00088     std::string key = parameters.substr(tempPos + 1, sepPos - tempPos - 1);
    00089     std::string value = parameters.substr(sepPos + 1, paramPos - sepPos - 1);
    00090     m_parameters[key] = value;
    00091   }
    00092
    00093   QF_STACK_POP
    00094 }
    00095
    00096 std::string HttpMessage::createResponse( int error, const std::string& text )
    00097 { QF_STACK_PUSH(HttpMessage::createResponse)
    00098
    00099   std::string errorString;
    00100   switch( error )
    00101   {
    00102   case 100: errorString = "Continue"; break;
    00103   case 101: errorString = "Switching Protocols"; break;
    00104   case 200: errorString = "OK"; break;
    00105   case 201: errorString = "Created"; break;
    00106   case 202: errorString = "Accepted"; break;
    00107   case 203: errorString = "Non-Authoritative Information"; break;
    00108   case 204: errorString = "No Content"; break;
    00109   case 205: errorString = "Reset Content"; break;
    00110   case 206: errorString = "Partial Content"; break;
    00111   case 300: errorString = "Multiple Choices"; break;
    00112   case 301: errorString = "Moved Permanently"; break;
    00113   case 302: errorString = "Found"; break;
    00114   case 303: errorString = "See Other"; break;
    00115   case 304: errorString = "Not Modified"; break;
    00116   case 305: errorString = "Use Proxy"; break;
    00117   case 307: errorString = "Temporary Redirect"; break;
    00118   case 400: errorString = "Bad Request"; break;
    00119   case 401: errorString = "Unauthorized"; break;
    00120   case 402: errorString = "Payment Required"; break;
    00121   case 403: errorString = "Forbidden"; break;
    00122   case 404: errorString = "Not Found"; break;
    00123   case 405: errorString = "Method Not Allowed"; break;
    00124   case 406: errorString = "Not Acceptable"; break;
    00125   case 407: errorString = "Proxy Authentication Required"; break;
    00126   case 408: errorString = "Request Timeout"; break;
    00127   case 409: errorString = "Conflict"; break;
    00128   case 410: errorString = "Gone"; break;
    00129   case 411: errorString = "Length Required"; break;
    00130   case 412: errorString = "Precondition Failed"; break;
    00131   case 413: errorString = "Request Entity Too Large"; break;
    00132   case 414: errorString = "Request-URI Too Large"; break;
    00133   case 415: errorString = "Unsupported Media Type"; break;
    00134   case 416: errorString = "Requested Range Not Satisfiable"; break;
    00135   case 417: errorString = "Expectation Failed"; break;
    00136   case 500: errorString = "Internal Server Error"; break;
    00137   case 501: errorString = "Not Implemented"; break;
    00138   case 502: errorString = "Bad Gateway"; break;
    00139   case 503: errorString = "Service Unavailable"; break;
    00140   case 504: errorString = "Gateway Timeout"; break;
    00141   case 505: errorString = "HTTP Version not supported"; break;
    00142   default: errorString = "Unknown";
    00143   }
    00144
    00145   std::stringstream response;
    00146   response << "HTTP/1.1 " << error << " " << errorString << "\r\n"
    00147            << "Server: QuickFIX" << "\r\n"
    00148            << "Content-Type: text/html; charset=iso-8859-1" << "\r\n\r\n"
    00149            << "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">";
    00150
    00151   if( error < 200 || error >= 300 )
    00152     response << "<HTML><HEAD><TITLE>" << error << " " << errorString << "</TITLE></HEAD><BODY>"
    00153              << "<H1>" << error << " " << errorString << "</H1>" << text << "</BODY></HTML>";
    00154   else
    00155     response << text;
    00156
    00157   return response.str();
    00158
    00159   QF_STACK_POP
    00160 }
    00161
    00162 }