#include "HttpClient.h"
//************************************
// ÎļþÃû³Æ£ºHttpClient.cpp
// ¹¦ÄÜ˵Ã÷£º
// QQ Ⱥ£º181926437
// QQ ºÅ£º515199494
// ×÷ ÕߣºÁ÷Äê
// ÈÕ ÆÚ£º2015/08/31
//************************************
HTTPClient::HTTPClient(void)
{
}
HTTPClient::~HTTPClient(void)
{
}
bool HTTPClient::get(HTTPRequest& req, HTTPResponse& rep, std::string& strData)
{
do
{
if (!parseUrl(req))
{
break;
}
HTTP_REQ_TYPE type = HTTP_GET;
HTTPSession session;
if (!sendHeader(type, session, req))
{
break;
}
if (!readHeader(session, rep))
{
break;
}
if (!readBody(session, rep, strData))
{
break;
}
return true;
} while (false);
return false;
}
bool HTTPClient::post(HTTPRequest& req, HTTPResponse& rep, const std::string& strData, std::string& strResult)
{
do
{
if (!parseUrl(req))
{
break;
}
HTTP_REQ_TYPE type = HTTP_POST;
HTTPSession session;
if (!sendHeader(type, session, req))
{
break;
}
if (!sendBody(session, strData))
{
break;
}
if (!readHeader(session, rep))
{
break;
}
if (rep.uContentLength > 0)
{
readBody(session, rep, strResult);
}
return true;
} while (false);
return false;
}
bool HTTPClient::startPost(HTTPSession& session, HTTPRequest& req)
{
do
{
if (!parseUrl(req))
{
break;
}
HTTP_REQ_TYPE type = HTTP_POST;
if (!sendHeader(type, session, req))
{
break;
}
return true;
} while (false);
return false;
}
bool HTTPClient::postData(HTTPSession& session, const std::string& data)
{
return sendBody(session, data);
}
void HTTPClient::stopPost(HTTPSession& session, HTTPResponse& rep, std::string& strResult)
{
readHeader(session, rep);
if (rep.uContentLength > 0)
{
readBody(session, rep, strResult);
}
session.closeSession();
}
bool HTTPClient::startGet(HTTPSession& session, HTTPResponse& rep, HTTPRequest& req)
{
do
{
if (!parseUrl(req))
{
break;
}
HTTP_REQ_TYPE type = HTTP_GET;
if (!sendHeader(type, session, req))
{
break;
}
if (!readHeader(session, rep))
{
break;
}
return true;
} while (false);
return false;
}
bool HTTPClient::getData(HTTPSession& session, HTTPResponse& rep, std::string& data)
{
return readBody(session, rep, data);
}
void HTTPClient::stopGet(HTTPSession& session)
{
session.closeSession();
}
bool HTTPClient::parseUrl(HTTPRequest& req)
{
do
{
if (req.url.length() <= strlen("http://"))
{
break;
}
char* url = (char*)req.url.c_str();
char* result = strstr(url, "http://");
if (!result || result != url)
{
break;
}
url += strlen("http://");
result = strstr(url, "/");
char* colon = strstr(url, ":");
if (colon && result && colon > result)
{
break;
}
if (result && colon)
{
req.host = std::string(url, colon - url);
std::string port(colon + 1, result - colon - 1);
req.port = (uint16_t)atoi(port.c_str());
req.strRelative = std::string(result);
}
else if (result && !colon)
{
req.host = std::string(url, result - url);
req.strRelative = std::string(result);
}
else if (!result && colon)
{
req.host = std::string(url, colon - url);
std::string port(colon + 1);
req.port = (uint16_t)atoi(port.c_str());
}
else if (!result && !colon)
{
req.host = std::string(url);
}
return true;
} while (false);
return false;
}
void HTTPClient::makeHeader(HTTP_REQ_TYPE& type, HTTPRequest& req, std::string &strHead)
{
std::string strHttpType;
switch (type)
{
case HTTP_GET: strHttpType = HTTP_REQUEST_GET; break;
case HTTP_POST: strHttpType = HTTP_REQUEST_POST; break;
case HTTP_PUT: strHttpType = HTTP_REQUEST_PUT; break;
case HTTP_DELETE: strHttpType = HTTP_REQUEST_DELETE; break;
}
std::ostringstream oss;
oss << strHttpType << " " << req.strRelative << " " << req.version << "\r\n";
oss << HTTP_HEAD_HOST << ": " << req.host;
if (req.port != 80)
{
oss << ":" << req.port;
}
oss << "\r\n";
for (std::map<:string std::string>::iterator it = req.m_headMap.begin(); it != req.m_headMap.end(); ++ it)
{
oss << it->first << ": " << it->second << "\r\n";
}
oss << "\r\n";
strHead.append(oss.str());
}
void HTTPClient::makeMultipartBody(HTTPMultipart& multipart, std::string& strBody)
{
std::ostringstream oss;
for (std::map