Initial
This commit is contained in:
167
proto/source/HTTPConnection.hpp
Normal file
167
proto/source/HTTPConnection.hpp
Normal file
@@ -0,0 +1,167 @@
|
||||
#ifndef _PROTO_HTTPCONNECTION_HPP_
|
||||
#define _PROTO_HTTPCONNECTION_HPP_
|
||||
#ifndef _COMMON_STRING_HPP_
|
||||
#include <common/string.hpp>
|
||||
#endif
|
||||
#ifndef _COMMON_BLOCK_HPP_
|
||||
#include <common/block.hpp>
|
||||
#endif
|
||||
#ifndef _COMMON_ARRAY_HPP_
|
||||
#include <common/array.hpp>
|
||||
#endif
|
||||
#ifndef _PROTO_INTERNET_HPP_
|
||||
#include <proto/internet.hpp>
|
||||
#endif
|
||||
#ifndef _PROTO_CONNECTION_HPP_
|
||||
#include <proto/connection.hpp>
|
||||
#endif
|
||||
#ifndef _PROTO_HTTPHEADER_HPP_
|
||||
#include <proto/HTTPHeader.hpp>
|
||||
#endif
|
||||
#ifndef _PROTO_HTTPDATA_HPP_
|
||||
#include <proto/HTTPData.hpp>
|
||||
#endif
|
||||
|
||||
class HTTPConnection : private Connection
|
||||
{
|
||||
public:
|
||||
HTTPConnection(void);
|
||||
HTTPConnection(const Internet &internet,const String &strServer,int port=INTERNET_DEFAULT_HTTP_PORT);
|
||||
virtual ~HTTPConnection();
|
||||
bool connect(const Internet &internet,const String &strServer,int port=INTERNET_DEFAULT_HTTP_PORT);
|
||||
bool get(HTTPHeader &httpHeader,HTTPData &httpData);
|
||||
bool get(const String &action,HTTPHeader &httpHeader,HTTPData &httpData);
|
||||
bool post(const String &action,HTTPHeader &httpHeader,HTTPData &httpData);
|
||||
void close(void);
|
||||
private:
|
||||
HTTPConnection(const HTTPConnection &httpConnection);
|
||||
HTTPConnection &operator=(const HTTPConnection &httpConnection);
|
||||
void closeRequest(void);
|
||||
void createHeaderLines(String &strLines,Block<String> &headerLines);
|
||||
|
||||
HINTERNET mhRequest;
|
||||
};
|
||||
|
||||
inline
|
||||
HTTPConnection::HTTPConnection()
|
||||
: mhRequest(0)
|
||||
{
|
||||
}
|
||||
|
||||
inline
|
||||
HTTPConnection::HTTPConnection(const HTTPConnection &httpConnection)
|
||||
{ // private
|
||||
}
|
||||
|
||||
inline
|
||||
HTTPConnection &HTTPConnection::operator=(const HTTPConnection &httpConnection)
|
||||
{ // private
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline
|
||||
HTTPConnection::HTTPConnection(const Internet &internet,const String &strServer,int port)
|
||||
{
|
||||
connect(internet,strServer,port);
|
||||
}
|
||||
|
||||
inline
|
||||
HTTPConnection::~HTTPConnection()
|
||||
{
|
||||
close();
|
||||
}
|
||||
|
||||
inline
|
||||
bool HTTPConnection::connect(const Internet &internet,const String &strServer,int port)
|
||||
{
|
||||
if(!internet.isOkay())return false;
|
||||
return Connection::connect(internet,strServer,INTERNET_SERVICE_HTTP,port);
|
||||
}
|
||||
|
||||
bool HTTPConnection::get(HTTPHeader &httpHeader,HTTPData &httpData)
|
||||
{
|
||||
return get(String(),httpHeader,httpData);
|
||||
}
|
||||
|
||||
bool HTTPConnection::get(const String &action,HTTPHeader &httpHeader,HTTPData &httpData)
|
||||
{
|
||||
DWORD dwSize(0);
|
||||
String strHeaders;
|
||||
// String strHeader;
|
||||
bool sendResult=false;
|
||||
|
||||
LPSTR accessTypes[2]={"*/*",0};
|
||||
if(!isOkay())return false;
|
||||
mhRequest=::HttpOpenRequest(getHANDLE(),"GET",action.str(),HTTP_VERSION,0,(const char**)&accessTypes,INTERNET_FLAG_RELOAD|INTERNET_FLAG_NO_CACHE_WRITE,0);
|
||||
if(!mhRequest)return false;
|
||||
// strHeader=httpHeader.serialize();
|
||||
if(!::HttpSendRequest(mhRequest,0,0,0,0))return false;
|
||||
// if(httpData.size()&&!strHeader.isNull())sendResult=::HttpSendRequest(mhRequest,strHeader.str(),strHeader.length(),&httpData[0],httpData.size());
|
||||
// else sendResult=::HttpSendRequest(mhRequest,0,0,0,0);
|
||||
// if(!sendResult)return false;
|
||||
::HttpQueryInfo(mhRequest,HTTP_QUERY_RAW_HEADERS_CRLF,0,&dwSize,0);
|
||||
if(!dwSize)return false;
|
||||
strHeaders.reserve(dwSize+1);
|
||||
::HttpQueryInfo(mhRequest,HTTP_QUERY_RAW_HEADERS_CRLF,strHeaders,&dwSize,0);
|
||||
httpHeader=strHeaders;
|
||||
httpData.size(httpHeader.getContentLength());
|
||||
if(!httpData.size())httpData.size(32768);
|
||||
::InternetReadFile(mhRequest,&httpData[0],httpData.size(),&dwSize);
|
||||
if(dwSize<httpData.size())
|
||||
{
|
||||
HTTPData tmpData;
|
||||
tmpData.size(dwSize);
|
||||
for(int index=0;index<dwSize;index++)tmpData[index]=httpData[index];
|
||||
httpData=tmpData;
|
||||
}
|
||||
closeRequest();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool HTTPConnection::post(const String &action,HTTPHeader &httpHeader,HTTPData &httpData)
|
||||
{
|
||||
DWORD dwSize(0);
|
||||
String strHeader;
|
||||
String strHeaders;
|
||||
|
||||
LPSTR accept[2]={"Accept: */*",0};
|
||||
if(!isOkay())return false;
|
||||
mhRequest=::HttpOpenRequest(getHANDLE(),"GET",action.str(),HTTP_VERSION,0,(const char**)&accept,0,1);
|
||||
if(!mhRequest)return false;
|
||||
strHeader=httpHeader.serialize();
|
||||
if(!::HttpSendRequest(mhRequest,strHeader.str(),strHeader.length(),&httpData[0],httpData.size()))return false;
|
||||
|
||||
::HttpQueryInfo(mhRequest,HTTP_QUERY_RAW_HEADERS_CRLF,0,&dwSize,0);
|
||||
if(!dwSize)return false;
|
||||
strHeaders.reserve(dwSize+1);
|
||||
::HttpQueryInfo(mhRequest,HTTP_QUERY_RAW_HEADERS_CRLF,strHeaders,&dwSize,0);
|
||||
httpHeader=strHeaders;
|
||||
httpData.size(httpHeader.getContentLength());
|
||||
if(!httpData.size())httpData.size(32768);
|
||||
::InternetReadFile(mhRequest,&httpData[0],httpData.size(),&dwSize);
|
||||
if(dwSize<httpData.size())
|
||||
{
|
||||
HTTPData tmpData;
|
||||
tmpData.size(dwSize);
|
||||
for(int index=0;index<dwSize;index++)tmpData[index]=httpData[index];
|
||||
httpData=tmpData;
|
||||
}
|
||||
closeRequest();
|
||||
return true;
|
||||
}
|
||||
|
||||
inline
|
||||
void HTTPConnection::close(void)
|
||||
{
|
||||
Connection::close();
|
||||
closeRequest();
|
||||
}
|
||||
|
||||
inline
|
||||
void HTTPConnection::closeRequest(void)
|
||||
{
|
||||
if(!mhRequest)return;
|
||||
::InternetCloseHandle(mhRequest);
|
||||
mhRequest=0;
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user