18,356
社区成员
发帖
与我相关
我的任务
分享
#pragma once
#include <string>
#include <map>
#include "MainDlg.h"
#include "StringTool.h"
using namespace std;
class CHttp
{
private:
int Recv(char *buf, int len)
{
int totalRecv = 0;
int recvByte = 0;
while (totalRecv < len)
{
recvByte = ::recv(m_socket, &buf[totalRecv], len - totalRecv, 0);
if (recvByte <= 0)
{
// recvByte == 0, connection has been gracefully closed
// recvByte < 0, SOCKET_ERROR
return -1;
}
totalRecv += recvByte;
}
return totalRecv;
}
bool Send(char *buf, int len)
{
int totalSend = 0;
int sendByte = 0;
while (totalSend < len)
{
sendByte = ::send(m_socket, &buf[totalSend], len - totalSend, 0);
if (sendByte <= 0)
{
return false;
}
totalSend += sendByte;
}
return true;
}
bool GetHttpHeader(SOCKET s, char *headerBuf, int len)
{
char *pBuf = new char[1];
int i = 0;
while (i < len)
{
if (!Recv(pBuf, 1))
{
break;
}
headerBuf[i] = pBuf[0];
if (strstr(headerBuf, "\r\n\r\n") != NULL)
{
return true;
}
i++;
}
delete pBuf;
return false;
}
void ParseHttpHeader(char *headerBuf)
{
char *temp = headerBuf;
int pos = 0;
while ((temp = strstr(temp + 2, "\r\n")) != NULL)
{
int nRet = (temp - headerBuf) - pos;
if (nRet == 2)
{
// is \r\n\r\n, http header end
break;
}
char *line = new char[nRet + 1];
line[nRet] = '\0';
strncpy(line, &headerBuf[pos], nRet);
pos += nRet;
InitHttpHeaderMap(line);
delete [] line;
line = NULL;
}
}
public:
int GetHttpCode()
{
return atoi(GetHeaderField(string("http-code")).c_str());
}
int GetContentLength()
{
return atoi(GetHeaderField(string("\r\nContent-Length")).c_str());
}
string GetServer()
{
return GetHeaderField(string("\r\nServer"));
}
string GetConnection()
{
return GetHeaderField(string("\r\nConnection"));
}
string GetContentDisposition()
{
return GetHeaderField(string("\r\nContent-Disposition"));
}
string GetAcceptRanges()
{
return GetHeaderField(string("\r\nAccept-Ranges"));
}
string GetContentType()
{
return GetHeaderField(string("\r\nContent-Type"));
}
string GetLocation()
{
string location = GetHeaderField(string("\r\nLocation"));
if (location == "empty")
{
location = GetHeaderField(string("\r\nlocation"));
}
return location;
}
string GetDate()
{
return GetHeaderField(string("\r\nDate"));
}
private:
void ParseHttpCode(char *line, char *szHttpCode)
{
szHttpCode[0] = line[9];
szHttpCode[1] = line[10];
szHttpCode[2] = line[11];
szHttpCode[3] = '\0';
}
void InitHttpHeaderMap(char *line)
{
if (m_http_header_map.empty())
{
char szHttpCode[4] = {0};
ParseHttpCode(line, szHttpCode);
string s_httpCode = szHttpCode;
m_http_header_map.insert(pair<string, string>("http-code", s_httpCode));
}
else
{
char *p = strchr(line, ':');
int nKeyLen = p - line;
int nValueLen = -(line - p);
char *key = new char[nKeyLen + 1];
key[nKeyLen] = '\0';
char *value = new char[nValueLen + 1];
value[nValueLen] = '\0';
strncpy(key, line, nKeyLen);
value = p + 1;
m_http_header_map.insert(pair<string, string>(key, value));
}
}
string GetHeaderField(string key)
{
http_header_map_iter iter = m_http_header_map.find(key);
if (iter != m_http_header_map.end())
{
return (*iter).second;
}
return "empty";
}
void GetIpAddrByHost(const char *host, SOCKADDR_IN *pAddr)
{
if (strlen(host) > 0)
{
HOSTENT *lpHostEnt = gethostbyname(host);
if (!lpHostEnt)
{
return;
}
if (lpHostEnt)
{
char ipAddrBuf[20];
sprintf(ipAddrBuf, "%d.%d.%d.%d",
lpHostEnt->h_addr_list[0][0] & 0x00ff,
lpHostEnt->h_addr_list[0][1] & 0x00ff,
lpHostEnt->h_addr_list[0][2] & 0x00ff,
lpHostEnt->h_addr_list[0][3] & 0x00ff);
pAddr->sin_addr.S_un.S_addr = inet_addr(ipAddrBuf);
}
}
}
private:
static const unsigned int HTTP_HEADER_BUFFER_SIZE = 1024;
private:
map<string, string> m_http_header_map;
typedef map<string, string>::iterator http_header_map_iter;
char m_http_header[HTTP_HEADER_BUFFER_SIZE];
private:
SOCKET m_socket;
};