Files
Work/proto/source/request.hpp
2024-08-07 09:16:27 -04:00

102 lines
2.0 KiB
C++

#ifndef _PROTO_HTTPREQUEST_HPP_
#define _PROTO_HTTPREQUEST_HPP_
#ifndef _COMMON_WINDOWS_HPP_
#include <common/windows.hpp>
#endif
#ifndef _COMMON_ARRAY_HPP_
#include <common/array.hpp>
#endif
#ifndef _PROTO_CONNECTION_HPP_
#include <proto/connection.hpp>
#endif
class HttpRequest
{
public:
HttpRequest();
virtual ~HttpRequest();
bool get(const Connection &connection,const String &object,Array<char> &rawData);
bool getRawData(Array<char> &rawData);
DWORD sizeHeaders(void);
void close(void);
bool isOkay(void)const;
HINTERNET getHANDLE(void)const;
private:
bool send(void);
HINTERNET mhRequest;
};
inline
HttpRequest::HttpRequest()
: mhRequest(0)
{
}
inline
HttpRequest::~HttpRequest()
{
close();
}
inline
bool HttpRequest::get(const Connection &connection,const String &object,Array<char> &rawData)
{
char *accessTypes[2]={"*/*",0};
if(!connection.isOkay())return false;
close();
mhRequest=::HttpOpenRequest(connection.getHANDLE(),String("GET"),object.str(),HTTP_VERSION,NULL,(LPCSTR*)accessTypes,INTERNET_FLAG_RELOAD|INTERNET_FLAG_NO_CACHE_WRITE,0);
if(!isOkay())return false;
if(!send())return false;
return getRawData(rawData);
return true;
}
inline
bool HttpRequest::send(void)
{
if(!isOkay())return false;
return ::HttpSendRequest(getHANDLE(),NULL,0,NULL,0);
}
inline
bool HttpRequest::getRawData(Array<char> &rawData)
{
DWORD dwSize;
if(!isOkay())return false;
rawData.size(sizeHeaders());
if(!rawData.size())return false;
dwSize=rawData.size();
if(!::HttpQueryInfo(getHANDLE(),HTTP_QUERY_RAW_HEADERS_CRLF,&rawData[0],&dwSize,NULL))return false;
return true;
}
inline
DWORD HttpRequest::sizeHeaders(void)
{
DWORD dwSize(0);
if(!isOkay())return dwSize;
::HttpQueryInfo(getHANDLE(),HTTP_QUERY_RAW_HEADERS_CRLF,NULL,&dwSize,NULL);
return dwSize;
}
inline
void HttpRequest::close(void)
{
if(!isOkay())return;
::InternetCloseHandle(mhRequest);
mhRequest=0;
}
inline
HINTERNET HttpRequest::getHANDLE(void)const
{
return mhRequest;
}
inline
bool HttpRequest::isOkay(void)const
{
return mhRequest?true:false;
}
#endif