Sends a HTML or other file to the browser
| Return Type | Function name | Arguments |
|---|---|---|
| hzEcode | hzHttpEvent::SendFileHead | (const char*,const char*,uint32_t,) |
Declared in file: hzHttpServer.h
Defined in file : hzHttpServer.cpp
Function Logic:
Function body:
hzEcode hzHttpEvent::SendFileHead (const char* cpDir)const char* cpFilename, uint32_t nExpires,
{
// Sends a HTML or other file to the browser
//
// Arguments: 1) cpDir The directory of the file (can be relative to current dir)
// 2) cpFilename The file name.
// 3) nExpires Expiry date
//
// Returns: E_NOTFOUND If the directory or filename cannot be accessed.
// E_OPENFAIL If the file could not be opened.
// E_NODATA If the file is empty.
// E_WRITEFAIL If the HTML data could not be sent to the browser.
// E_OK If the operation was successful.
_hzfunc("hzHttpEvent::SendFileHead") ;
ifstream is ; // Read file stream
FSTAT fs ; // File info
hzXDate d ; // Date for header lines
hzChain Z ; // Response for browser is built here
const char* pEnd ; // Filename extension and hence type
hzString Pathname ; // File to load
uint32_t nLen = 0; // File size
hzMimetype type ; // File's HTTP type
HttpRC hrc ; // HTTP return code
hzEcode rc ; // Return code from sending function
// Establish real filename, either cpFilename or index.htm(l)
Pathname = cpDir ;
if (cpFilename[0]== CHAR_FWSLASH && cpFilename[1]== 0)
Pathname += "/index.html" ;
else
{
if (cpFilename[0]== CHAR_FWSLASH)
Pathname += cpFilename ;
else
{
Pathname += "/" ;
Pathname += cpFilename ;
}
}
if (stat(*Pathname, &fs) == -1)
{
hrc = HTTPMSG_NOTFOUND ;
nLen = 0;
}
else
{
hrc = HTTPMSG_OK ;
nLen = fs.st_size ;
}
// Determine the type of file so that the correct header can be
// sent to the browser
pEnd = strrchr(*Pathname, CHAR_PERIOD) ;
if (!pEnd)
type = HMTYPE_TXT_PLAIN ;
else
type = Filename2Mimetype(pEnd) ;
// Send the header
rc = _formhead(Z, hrc, type, nLen, nExpires, false) ;
if (rc != E_OK)
{
hzerr(rc, "Could not formulate HTTP header (sock=%d)", m_pCx->CliSocket()) ;
return rc ;
}
if (m_pCx->SendData(Z) != E_OK)
{
hzerr(E_WRITEFAIL, "hzHttpEvent %p Failed to send response to browser (sock=%d)", this, m_pCx->CliSocket()) ;
return E_WRITEFAIL ;
}
return E_OK ;
}