1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| Bool HttpFile::Open(const std::string &fileName, File::MODE mode)
{
TCPSocket m_socket(TCPSocket::CLIENT);
if((mode & File::READ) != File::READ)
return False;
std::string host(fileName), resource;
//std::transform(host.begin(), host.end(), host.begin(), &std::tolower<std::string::value_type>);
std::transform(host.begin(), host.end(), host.begin(), tolower);
if(host.find("http://") != 0)
return false;
std::string::size_type pos = host.find("/", 8);
if(pos == std::string::npos)
{
resource = "/";
host = host.substr(7, host.length() - 7);
}
else
{
resource = host.substr(pos, host.length() - pos);
host = host.substr(7, pos - 7);
}
if(!m_socket.Connect(host.c_str(), 80))
return False;
std::string header = "GET " + resource + " HTTP/1.0\n"
+ "Host: " + host + "\n"
+ "User-Agent: HttpFile\n"
+ "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"
+ "Accept-Language: en-us,en;q=0.5\n"
+ "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\n"
+ "Connection: close\n\n";
m_socket.Send(header.c_str());
Char buffer[maxPacketSize];
uint ret;
while((ret = m_socket.Recv(buffer, maxPacketSize)) != 0)
{
GrowBuffer(ret + 1);
memcpy(ADD_POINTER(Char*, m_buffer, m_length), buffer, ret * sizeof(Char));
m_length += ret;
}
m_buffer[m_length] = 0;
std::cout << "Received: " << m_buffer << std::endl;
m_socket.Disconnect();
return True;
} |
Partager