Get a HTTP page from a website but do not process it in any way. This is for speed testing only. Note: The website (server) must already be connected to. No account is taken of redirected pages.
| Return Type | Function name | Arguments |
|---|---|---|
| hzEcode | hzHttpClient::TestPage | (hzChain&,hzUrl&,) |
Declared in file: hzHttpClient.h
Defined in file : hzHttpClient.cpp
Function Logic:
Function body:
hzEcode hzHttpClient::TestPage (hzChain& Z)hzUrl& url,
{
// Get a HTTP page from a website but do not process it in any way. This is for speed testing only.
//
// Note: The website (server) must already be connected to.
// No account is taken of redirected pages.
//
// Arguments: 1) Z The chain into which page content is to be received
// 2) url The URL of the page
//
// Returns: E_ARGUMENT If no URL was specified
// E_NODATA If nothing was recived
// E_OK If the response was recieved without error
_hzfunc("hzHttpClient::Testpage") ;
chIter zi ; // To iterate the returned page
chIter hi ; // To re-iterate lines of interest in the header of the returned page
chIter ti ; // Temp iterator
hzChain X ; // Temp buffer
hzCookie cookie ; // Cookie (drawn from supplied map of cookies)
hzString S ; // Temp string
hzString param ; // Header parameter name
hzString value ; // Header parameter value
hzString encoding ; // Page content is encoded, eg gzip
uint32_t nRecv ; // Bytes received
uint32_t nTry ; // Number of tries
hzEcode rc = E_OK ; // Return code
// Clear buffers
Z.Clear() ;
m_Header.Clear() ;
m_Content.Clear() ;
if (!url.Domain())
{ m_Error.Printf("TestPage: No host to locate\n") ; return E_ARGUMENT ; }
/*
** ** Formulate HTTP request
** */
m_Request.Clear() ;
if (url.Resource())
m_Request << "GET " << url.Resource() << " HTTP/1.1\r\n" ;
else
m_Request << "GET / HTTP/1.1\r\n" ;
m_Request <<
"Accept: */*\r\nAccept-Language: en-gb\r\n"
;
if (m_AuthBasic)
m_Request << "Authorization: Basic " << m_AuthBasic << "\r\n" ;
m_Request << "User-Agent: HadronZoo/0.8 Linux 2.6.18\r\n" ;
m_Request << "Host: " << url.Domain() << "\r\n" ;
if (m_Referer)
m_Request << "Referer: " << m_Referer << "\r\n" ;
m_Request << "Connection: Keep-Alive\r\n\r\n" ;
/*
** ** Send request
** */
m_Error << " Sending [" << m_Request << "] to domain " << url.Domain() << "\n" ;
rc = m_Webhost.Send(m_Request) ;
if (rc != E_OK)
{
m_Error.Printf("Could not send request to domain [%s] (error=%s)\n", *url.Domain(), Err2Txt(rc)) ;
return rc ;
}
// Garner response
for (nTry = 0; nTry < 4&& !m_Header.Size() ; nTry++)
{
rc = m_Webhost.Recv(m_buf, nRecv, HZ_MAXPACKET) ;
if (rc != E_OK)
{
if (rc == E_NOSOCKET)
m_Error.Printf("Connection closed by server\n") ;
else
m_Error.Printf("Could not recv bytes (nbytes=%d) from page %s (error=%s)\n", nRecv, *url.Resource(), Err2Txt(rc)) ;
break ;
}
if (!nRecv)
{
m_Error.Printf("Got no response, retrying ...\n") ;
sleep(1);
continue ;
}
Z.Append(m_buf, nRecv) ;
}
if (rc != E_OK)
{
m_Error.Printf("Could not process response from [%s] (error=%s)\n", *url, Err2Txt(rc)) ;
return rc ;
}
m_Referer = url ;
return rc ;
}