HttpMessage.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* -*- C++ -*- */
  2. /****************************************************************************
  3. ** Copyright (c) 2001-2014
  4. **
  5. ** This file is part of the QuickFIX FIX Engine
  6. **
  7. ** This file may be distributed under the terms of the quickfixengine.org
  8. ** license as defined by quickfixengine.org and appearing in the file
  9. ** LICENSE included in the packaging of this file.
  10. **
  11. ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
  12. ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  13. **
  14. ** See http://www.quickfixengine.org/LICENSE for licensing information.
  15. **
  16. ** Contact ask@quickfixengine.org if any conditions of this licensing are
  17. ** not clear to you.
  18. **
  19. ****************************************************************************/
  20. #ifndef FIX_HTTPMESSAGE
  21. #define FIX_HTTPMESSAGE
  22. #ifdef _MSC_VER
  23. #pragma warning( disable: 4786 )
  24. #endif
  25. #include "Exceptions.h"
  26. #include <map>
  27. namespace FIX
  28. {
  29. /**
  30. * HTTP Message that implemented GET functionality
  31. */
  32. class HttpMessage
  33. {
  34. public:
  35. typedef std::map<std::string, std::string> Parameters;
  36. HttpMessage();
  37. /// Construct a message from a string
  38. HttpMessage( const std::string& string )
  39. throw( InvalidMessage );
  40. HttpMessage( const HttpMessage& copy )
  41. {
  42. m_root = copy.m_root;
  43. m_parameters = copy.m_parameters;
  44. }
  45. public:
  46. /// Get a string representation of the message
  47. std::string toString() const;
  48. /// Get a string representation without making a copy
  49. std::string& toString( std::string& ) const;
  50. void setString( const std::string& string )
  51. throw( InvalidMessage );
  52. void clear()
  53. {
  54. #if defined(_MSC_VER) && _MSC_VER < 1300
  55. m_root = "";
  56. #else
  57. m_root.clear();
  58. #endif
  59. m_parameters.clear();
  60. }
  61. const std::string& getRootString() const
  62. { return m_root; }
  63. const std::string getParameterString() const
  64. {
  65. std::string result;
  66. Parameters::const_iterator i;
  67. for( i = m_parameters.begin(); i != m_parameters.end(); ++i )
  68. {
  69. result += (i == m_parameters.begin()) ? "?" : "&";
  70. result += i->first + "=" + i->second;
  71. }
  72. return result;
  73. }
  74. const Parameters& getParameters() const
  75. { return m_parameters; }
  76. bool hasParameter( const std::string& key ) const
  77. {
  78. Parameters::const_iterator find = m_parameters.find( key );
  79. return find != m_parameters.end();
  80. }
  81. const std::string& getParameter( const std::string& key ) const
  82. throw( std::logic_error )
  83. {
  84. Parameters::const_iterator find = m_parameters.find( key );
  85. if( find == m_parameters.end() )
  86. throw std::logic_error( "Parameter " + key + " not found" );
  87. return find->second;
  88. }
  89. void addParameter( const std::string& key, const std::string& value )
  90. {
  91. m_parameters[key] = value;
  92. }
  93. void removeParameter( const std::string& key )
  94. {
  95. m_parameters.erase( key );
  96. }
  97. static std::string createResponse( int error = 0, const std::string& text = "" );
  98. private:
  99. std::string m_root;
  100. Parameters m_parameters;
  101. };
  102. /*! @} */
  103. inline std::ostream& operator <<
  104. ( std::ostream& stream, const HttpMessage& message )
  105. {
  106. std::string str;
  107. stream << message.toString( str );
  108. return stream;
  109. }
  110. }
  111. #endif //FIX_HTTPMESSAGE