// HTTPWinSocket.h
//
//////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2000,2001 Robert Cunnings. All rights reserved.
//
// You may copy, modify, distribute or publish this code free of charge.
//
// NO WARRANTY: This software is provided AS IS, without warranty of any kind.
//
//////////////////////////////////////////////////////////////////////
//
// WMHTTPRequestSock class - a class for HTTP protocol support.
// Uses Winsock2, supports HTTP Version 1.0.
// 
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_HTTPWINSOCKET_H__00E7FF13_3B59_11D4_9E68_00105AABC815__INCLUDED_)
#define AFX_HTTPWINSOCKET_H__00E7FF13_3B59_11D4_9E68_00105AABC815__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#define BUFFER_SIZE 4096

#include <string>
#include <map>
#include <list>
using namespace std;

#include "WMWinSocket2.h"

class WMHostEnt : public HOSTENT
{
public:
	WMHostEnt();
	WMHostEnt(LPHOSTENT pHostEnt);
	virtual ~WMHostEnt();
};

class WMHTTPRequestSock : public WMWinSocket2
{
public:
	WMHTTPRequestSock(int nPort = 80, DWORD dwTimeOut = 30000);
	virtual ~WMHTTPRequestSock();

	bool SendHTTPRequest(  int nPort,
						   const string& strMethod,
						   const string& strHost,
						   const string& strRequestURI,
						   const string& strQuery,
						   const string& strHeaders,
						   const string& strBody);
	int GetStatus();
	const string& GetReasonPhrase();
	WORD GetHTTPVersion();
	int GetContentLength();
	const string& GetContentType();
	char *GetContent();
	string GetHeader(const char *pszName);
	void Clear();

protected:
	// overrides
	bool OnSockRead();
	bool OnSockWrite();
	void OnSockClose();
	// implementation
	bool ReadHeader();
	bool ParseHeader();
	bool ParseStatusLine(const string& strStatusLine);
	const string URIEscape(const string& strComponent);
	// HTTP 
	int				m_nPort;
	string			m_strStatus;
	string			m_strReasonPhrase;
	DWORD			m_dwContentLength;
	string			m_strContentType;
	int				m_nHTTPMajorVersion;
	int				m_nHTTPMinorVersion;
	bool			m_bHTTPVersionFound;
	// storage
	char			m_szRcvBuffer[BUFFER_SIZE];
	DWORD			m_dwIndex;
	char			*m_pszBuffer;
	list<string>	m_listHeaders;
	// state
	bool			m_bHeaderRead;
	// HOSTENT list
	typedef map<string, WMHostEnt *> HostEntCache;
	HostEntCache m_mapHostEnts; 
	// request parameters
	string			m_strMethod;
	string			m_strURI;
	string			m_strHeaders;
	string			m_strBody;
};

#endif // !defined(AFX_HTTPWINSOCKET_H__00E7FF13_3B59_11D4_9E68_00105AABC815__INCLUDED_)
