// This is the SIP interface definition for TQHttp, TQHttpHeader,
// TQHttpRequestHeader and TQHttpResponseHeader.
//
// Copyright (c) 2007
// 	Riverbank Computing Limited <info@riverbankcomputing.co.uk>
// 
// This file is part of PyTQt.
// 
// This copy of PyTQt 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, or (at your option) any later
// version.
// 
// PyTQt is supplied 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
// PyTQt; see the file LICENSE.  If not, write to the Free Software Foundation,
// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
 
%ExportedDoc
<Sect2><Title>TQHttp (TQt v3+)</Title>
<FuncSynopsis>
        <FuncDef>TQ_LONG <Function>readBlock</Function></FuncDef>
        <ParamDef>char *<Parameter>data</Parameter></ParamDef>
        <ParamDef>TQ_ULONG <Parameter>maxlen</Parameter></ParamDef>
</FuncSynopsis>
<Para>
This takes a single <Literal>maxlen</Literal> parameter.  The
<Literal>data</Literal> is returned if there was no error, otherwise
<Literal>None</Literal> is returned.
</Para>
</Sect2>

<Sect2><Title>TQHttpHeader (TQt v3.1+)</Title>
<Para>
<Literal>TQHttpHeader</Literal> is fully implemented.
</Para>
</Sect2>

<Sect2><Title>TQHttpRequestHeader (TQt v3.1+)</Title>
<Para>
<Literal>TQHttpRequestHeader</Literal> is fully implemented.
</Para>
</Sect2>

<Sect2><Title>TQHttpResponseHeader (TQt v3.1+)</Title>
<Para>
<Literal>TQHttpResponseHeader</Literal> is fully implemented.
</Para>
</Sect2>
%End


%If (TQt_NETWORKPROTOCOL_HTTP)

class TQHttpHeader
{
%TypeHeaderCode
#include <tqhttp.h>
%End

public:
	TQHttpHeader();
	TQHttpHeader(const TQHttpHeader &);
	TQHttpHeader(const TQString &);
	virtual ~TQHttpHeader();

	TQString value(const TQString &) const;
	void setValue(const TQString &,const TQString &);
	void removeValue(const TQString &);

	TQStringList keys() const;
	bool hasKey(const TQString &) const;

	bool hasContentLength() const;
	uint contentLength() const;
	void setContentLength(int);

	bool hasContentType() const;
	TQString contentType() const;
	void setContentType(const TQString &);

	virtual TQString toString() const;
	bool isValid() const;

	virtual int majorVersion() const = 0;
	virtual int minorVersion() const = 0;

protected:
	virtual bool parseLine(const TQString &,int);
	bool parse(const TQString &);
	void setValid(bool);
};


class TQHttpResponseHeader : TQHttpHeader
{
%TypeHeaderCode
#include <tqhttp.h>
%End

public:
	TQHttpResponseHeader();
	TQHttpResponseHeader(const TQHttpResponseHeader &);

	int statusCode() const;
	TQString reasonPhrase() const;

	int majorVersion() const;
	int minorVersion() const;

	TQString toString() const;

protected:
	bool parseLine(const TQString &,int);
};


class TQHttpRequestHeader : TQHttpHeader
{
%TypeHeaderCode
#include <tqhttp.h>
%End

public:
	TQHttpRequestHeader();
	TQHttpRequestHeader(const TQString &,const TQString &,int = 1,int = 1);
	TQHttpRequestHeader(const TQHttpRequestHeader &);
	TQHttpRequestHeader(const TQString &);

	void setRequest(const TQString &,const TQString &,int = 1,int = 1);

	TQString method() const;
	TQString path() const;

	int majorVersion() const;
	int minorVersion() const;

	TQString toString() const;

protected:
	bool parseLine(const TQString &,int);
};


class TQHttp : TQNetworkProtocol
{
%TypeHeaderCode
#include <tqhttp.h>
%End

public:
	TQHttp();
	TQHttp(TQObject * /TransferThis/,const char * = 0);
	TQHttp(const TQString &,TQ_UINT16 = 80,TQObject * /TransferThis/ = 0,
	      const char * = 0);
	virtual ~TQHttp();

	int supportedOperations() const;

	enum State {
		Unconnected,
		HostLookup,
		Connecting,
		Sending,
		Reading,
		Connected,
		Closing
	};

	enum Error {
		NoError,
		UnknownError,
		HostNotFound,
		ConnectionRefused,
		UnexpectedClose,
		InvalidResponseHeader,
		WrongContentLength,
		Aborted
	};

	int setHost(const TQString &,TQ_UINT16 = 80);

	int get(const TQString &,TQIODevice * = 0) /ReleaseGIL/;
	int post(const TQString &,TQIODevice *,TQIODevice * = 0) /ReleaseGIL/;
	int post(const TQString &,const TQByteArray &,
		 TQIODevice * = 0) /ReleaseGIL/;
	int head(const TQString &) /ReleaseGIL/;
	int request(const TQHttpRequestHeader &,TQIODevice * = 0,
		    TQIODevice * = 0) /ReleaseGIL/;
	int request(const TQHttpRequestHeader &,const TQByteArray &,
		    TQIODevice * = 0) /ReleaseGIL/;

	int closeConnection();

	unsigned long bytesAvailable() const;

	SIP_PYOBJECT readBlock(TQ_ULONG) /ReleaseGIL/ [TQ_LONG (char *,TQ_ULONG)];
%MethodCode
		char *buf;

		if ((buf = (char *)sipMalloc(a0)) == NULL)
			sipIsErr = 1;
		else
		{
			TQ_LONG actlen;

			Py_BEGIN_ALLOW_THREADS
			actlen = sipCpp -> TQHttp::readBlock(buf,a0);
			Py_END_ALLOW_THREADS

			if (actlen < 0)
			{
				Py_INCREF(Py_None);
				sipRes = Py_None;
			}
			else if ((sipRes = PyBytes_FromStringAndSize(buf,actlen)) == NULL)
				sipIsErr = 1;

			sipFree((void *)buf);
		}
%End

	TQByteArray readAll() /ReleaseGIL/;

	int currentId() const;
	TQIODevice *currentSourceDevice() const;
	TQIODevice *currentDestinationDevice() const;
	TQHttpRequestHeader currentRequest() const;
	bool hasPendingRequests() const;
	void clearPendingRequests();

	State state() const;

	Error error() const;
	TQString errorString() const;

public slots:
	void abort();

signals:
	void stateChanged(int);
	void responseHeaderReceived(const TQHttpResponseHeader &);
	void readyRead(const TQHttpResponseHeader &);
	void dataSendProgress(int,int);
	void dataReadProgress(int,int);

	void requestStarted(int);
	void requestFinished(int,bool);
	void done(bool);

protected:
	void operationGet(TQNetworkOperation *);
	void operationPut(TQNetworkOperation *);

	void timerEvent(TQTimerEvent *);
};

%End
