/***************************************************************************
                          SIMPLY POWERFUL TOOLKIT (SPTK)
                          CHttpConnect.cpp  -  description
                             -------------------
    begin                : July 19 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"
 ***************************************************************************/

#include <sptk3/sptk.h>
#include <sptk3/CHttpConnect.h>
#include <sptk3/CHttpParams.h>

using namespace std;
using namespace sptk;

CHttpConnect::CHttpConnect() {
   m_port = 80;
}

CHttpConnect::~CHttpConnect() {
   close();
}

#define RSP_BLOCK_SIZE 1024

void CHttpConnect::getResponse() {

   m_readBuffer.reset();

   CBuffer read_buffer(RSP_BLOCK_SIZE);

   int bytes;

   do {
      bytes = read(read_buffer);

      //puts(read_buffer.data());

      if (bytes <=0 ) { // No more data
         bytes = 0;
         close();
      } else {
         //read_buffer.data()[read_buffer.bytes()] = 0; //terminating NULL
         m_readBuffer.append(read_buffer.data());
      }
   } while (bytes);
}

void CHttpConnect::sendCommand(string cmd) {
   cmd += "\r\n";
   if (!active())
      throw CException("Socket isn't open");
   write(cmd.c_str(),cmd.length());
}

void CHttpConnect::cmd_get(string pageName,CHttpParams& postData) {

   m_readBuffer.checkSize(1024);

   CBuffer buffer;
   postData.encode(buffer);

   string parameters(buffer.data());

   string command = "GET http://" + m_host + "/" + pageName;
   if (parameters.length()) command += "?" + parameters;
   command += " HTTP/1.0\r\n";
   //command += "Host: " + serverName + ":"+ string(m_port) + "\n";
   sendCommand(command);

   getResponse();
}

void CHttpConnect::cmd_post(string pageName,CHttpParams& postData) {
   string command = "Location http://" + m_host + "/" + pageName + "\nContent-type: application/x-www-form-urlencoded\r\n\r\n";
   CBuffer buffer;
   postData.encode(buffer);
   command += buffer.data();
   command += "\r\n";
   sendCommand(command);

   getResponse();
}

void CHttpConnect::cmd_post(string pageName,string contentType,CBuffer& document) {
   string command = "Location http://" + m_host + "/" + pageName + "\nContent-type: "+contentType+"\r\n\r\n";
   command += document.data();
   command += "\r\n";
   sendCommand(command);

   getResponse();
}
