// $Id: Internet.h 8019 2007-07-14 22:30:11Z trick $

/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Deadly Rooms of Death.
*
* The Initial Developer of the Original Code is
* Caravel Software.
* Portions created by the Initial Developer are Copyright (C) 2002
* Caravel Software. All Rights Reserved.
*
* Contributor(s):
*
* ***** END LICENSE BLOCK ***** */

#include <curl/curl.h>  //must come first
#include "StretchyBuffer.h"

#include <map>
#include <string>
#include <SDL_thread.h>
using std::string;

#ifndef INTERNET_H
#define INTERNET_H

//****************************************************************************
class CInternet_Thread_Info
{
public:
   CStretchyBuffer* pBuffer;
	string strUrl;
	SDL_Thread* pThread;
	CURLcode eRetVal;
	long responseCode;
   UINT bytesComplete;

	CInternet_Thread_Info() :
		pBuffer(NULL),
		pThread(NULL),
		eRetVal((CURLcode)-1), responseCode(0), bytesComplete(0)
	{}
};

//****************************************************************************
class CInternet
{
public:
	static void AddCookie(const string& strName, const string& value);
	static void AddPostData(const string& strName, const CStretchyBuffer& value);
	static void AddPostData(const string& strName, const string& value);
	static void ClearCookies() { CInternet::strCookies = ""; }
	static void Deinit();
   static UINT GetBytesCompleted(const UINT handle);
	static string GetCookies() { return CInternet::strCookies; }
   static curl_httppost* GetFormPost();
   static curl_httppost* GetFormEnd();
	static CStretchyBuffer* GetResults(const UINT handle);
	static int GetStatus(const UINT handle);
	static UINT HttpGet(const string& strUrl);
	static bool Init(const char* pUserAgent, const bool bSSL=false);
	static void  OutputError(const int nRes);


   static curl_httppost* pFormPostBegin;
   static curl_httppost* pFormPostEnd;
	static string userAgent;
private:

	static CURLM* pHandle;
	static string strCookies;
	static int nHandles;
	static bool bInit;

	static std::map<UINT, CInternet_Thread_Info*> threadInfo;
};

#endif // INTERNET_H
