/***************************************************************************
                          SIMPLY POWERFUL TOOLKIT (SPTK)
                          CHttpConnect.h  -  description
                             -------------------
    begin                : July 22 2003
    copyright            : (C) 2003-2006 by Alexey Parshin
    email                : alexeyp@gmail.com
 ***************************************************************************/

/***************************************************************************
   This library is free software; you can redistribute it and/or modify it 
   under the terms of the GNU Library General Public License as published by
   the Free Software Foundation; either version 2 of the License, or (at 
   your option) any later version.

   This library is distributed in the hope that it will be useful, but 
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library
   General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; if not, write to the Free Software Foundation, 
   Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.

   Please report all bugs and problems to "alexeyp@gmail.com"
 ***************************************************************************/

#ifndef __CHTTPCONNECT_H__
#define __CHTTPCONNECT_H__

#include <sptk3/CSocket.h>
#include <sptk3/CStrings.h>
#include <sptk3/CHttpParams.h>

namespace sptk {

   /// @addtogroup utility Utility Classes
   /// @{

   /// @brief HTTP socket
   ///
   /// Implements the GET method of HTTP protocol. 
   /// Allows to define the host, port, submit information, and then GET the page from the server.
	class CHttpConnect : public CSocket {
		CBuffer        m_readBuffer;   ///< Internal read buffer

	protected:
      /// @brief Sends a single command to HTTP server
		///
		/// CRLF characters are automatically appended to the command.
      /// @param cmd std::string, HTTP command
		void sendCommand(std::string cmd);

	public:

      /// @brief Default constructor
		CHttpConnect();

      /// @brief Destructor 
		~CHttpConnect();

      /// @brief Retrieves the server response on the command
		///
		/// Stops when HTTP server closes the connection. The server response can then be 
      /// accessed through the htmlData() method.
		void getResponse();

      /// @brief Returns the internal read buffer
		///
		/// The buffer makes sense
      /// only after sending a command to the server (if that command calls
      /// getResponse() method internally).
      /// @returns internal read buffer referrence
		const CBuffer& htmlData() const { return m_readBuffer; }

      /// @brief Sends the GET command to the server
		///
      /// Retrieves the server response into internal read buffer.
      /// @param pageName std::string, the name of the page without the server name.
      /// @param postData CHttpParams&, the list of HTTP data to pass to the server
		void cmd_get(std::string pageName,CHttpParams& postData);

      /// @brief Sends the POST command to the server
		///
      /// Retrieves the server response into internal read buffer.
      /// @param pageName std::string, the name of the page without the server name.
      /// @param postData CHttpParams&, the list of HTTP data to pass to the server
		void cmd_post(std::string pageName,CHttpParams& postData);

      /// @brief Sends the POST command to the server,
		///
      /// Retrieves the server response into internal read buffer.
      /// @param pageName std::string, the name of the page without the server name.
      /// @param contentType std::string, the type of the content.
      /// @param document CBuffer&, document to send
		void cmd_post(std::string pageName,std::string contentType,CBuffer& document);
	};
   /// @}
}
#endif
