#ifndef _KVI_HTTP_H_
#define _KVI_HTTP_H_
//
//   File : kvi_http.h
//   Creation date : Sat Aug 17 13:43:31 2002 GMT by Szymon Stefanek
//
//   This file is part of the KVirc irc client distribution
//   Copyright (C) 2002 Szymon Stefanek (pragma at kvirc dot net)
//
//   This program is FREE software. You can redistribute it and/or
//   modify it under the terms of the GNU General Public License
//   as published by the Free Software Foundation; either version 2
//   of the License, or (at your opinion) any later version.
//
//   This program 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 General Public License for more details.
//
//   You should have received a copy of the GNU General Public License
//   along with this program. If not, write to the Free Software Foundation,
//   Inc. ,59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//

#include "kvi_settings.h"
#include "kvi_string.h"
#include "kvi_thread.h"
#include "kvi_sockettype.h"

#include "kvi_url.h"

#include <qobject.h>
#include <qfile.h>

class KviDns;

class KviHttpRequestThread;

class KviHttpRequest : public QObject
{
	Q_OBJECT
public:
	enum ProcessingType { WholeFile, Blocks, Lines, StoreToFile };
public:
	KviHttpRequest();
	~KviHttpRequest();
protected:
	KviUrl                 m_url;
	KviStr                 m_szIp;
	KviDns               * m_pDns;
	KviHttpRequestThread * m_pThread;
	KviStr                 m_szLastError;
	bool                   m_bHeaderProcessed;
	KviStr                 m_szBuffer;
	KviStr                 m_szHeader;
	KviStr                 m_szResponse;
	ProcessingType         m_processingType;
	QFile                * m_pFile;
protected:
	void reset();
	bool startDnsLookup();
	virtual bool event(QEvent *e);
	void processData(KviStr * data);
	void emitLines();
public:
	const KviStr & lastError(){ return m_szLastError; };
	bool get(const KviUrl &u,ProcessingType p = WholeFile,const char * szFileName = 0);
	void abort(){ reset(); };
protected slots:
	void dnsLookupDone(KviDns *d);
	void haveServerIp();
signals:
	void status(const char * message);
	void terminated(bool bSuccess);
	void data(const KviStr &data);
};





class KviHttpRequestThread : public KviSensitiveThread
{
	friend class KviHttpRequest;
protected:
	KviHttpRequestThread(KviHttpRequest * r,const char * szIp,const char * szPath);
public:
	~KviHttpRequestThread();
protected:
	KviHttpRequest * m_pRequest;
	KviStr           m_szIp;
	KviStr           m_szPath;

	kvi_socket_t     m_sock;
protected:

	int selectForReadStep();
	bool selectForRead(int iTimeoutInSecs);
	bool readData();
	bool sendBuffer(const char * buffer,int bufLen,int iTimeoutInSecs);
	bool failure(const char * error = 0);
	bool selectForWrite(int iTimeoutInSecs);
	bool connectToRemoteHost();
	bool processInternalEvents();

	virtual void run();
};


#endif //_KVI_HTTP_H_
