Bitty HTTP

Development » 404 Errors

1404 Errors

This example will show handling a page not found error.

We handle 404 Errors in the FS_SendFile() function. Basicly when FS_GetFileProperties() is called if we do not find the requested page and we set the FileID to 0 (NULL). Then when FS_SendFile() is called we detect that the FileID is set to NULL and send a 404 error page.

Files

FileServer.c

The file server is updated to have the 3 basic pages plus an error page that we send if the user goes to an unknown page.

struct FileInfo m_Files[]= { /* Filename, Dynamic, Cookies, Gets, Posts, Callback */ {"/",false,NULL,NULL,NULL,File_Root}, {"/SomeStyle.css",false,NULL,NULL,NULL,File_SomeStyle}, {"/quit.html",true,NULL,NULL,NULL,File_Quit}, };

We setup the 3 known pages (css, quit, and root).

bool FS_GetFileProperties(const char *Filename,struct WSPageProp *PageProp) { int r; PageProp->FileID=0; for(r=0;r<sizeof(m_Files)/sizeof(struct FileInfo);r++) { if(strcmp(Filename,m_Files[r].Filename)==0) { PageProp->FileID=(uintptr_t)&m_Files[r]; PageProp->DynamicFile=m_Files[r].Dynamic; PageProp->Cookies=m_Files[r].Cookies; PageProp->Gets=m_Files[r].Gets; PageProp->Posts=m_Files[r].Posts; return true; } } return true; }

The FS_GetFileProperties() function is changed to set PageProp->FileID to 0 (NULL) and always returns true even if the page was not found. This is because FS_SendFile() will send a 404 page if we didn't find the page.

void FS_SendFile(struct WebServer *Web,uintptr_t FileID) { struct FileInfo *File=(struct FileInfo *)FileID; if(File==NULL) { File_Error404(Web); return; } File->WriteFile(Web); }

The FS_SendFile() is changed to call File_Error404() if File==NULL (page not found). If it isn't NULL then the page is sent as normal.

const char Error404HTML[]= "<!DOCTYPE html>" "<html>" "<head>" "<title>Bitty HTTP - Root</title>" "<link rel='stylesheet' href='/SomeStyle.css'>" "</head>" "<body>" "<div id='top'>Bitty HTTP Example - Root</div>" "<div id='quitbttn'><a href='/quit.html'>QUIT</a></div>" "<div id='content'>" "<h1 style='text-align:center;font-size:10em;margin:0'>404</h1>" "<h3 style='text-align:center'>Oops! Page not found</h3>" "<br/>" "</div>" "<div id='bottom'></div>" "</body>" "</html>"; void File_Error404(struct WebServer *Web) { WS_SetHTTPStatusCode(Web,e_ReplyStatus_NotFound); WS_WriteWhole(Web,Error404HTML,sizeof(Error404HTML)-1); }

This is the 404 error page. It is sent like any normal page.

const char RootHTML[]= "<!DOCTYPE html>" "<html>" "<head>" "<title>Bitty HTTP - Root</title>" "<link rel='stylesheet' href='/SomeStyle.css'>" "</head>" "<body>" "<div id='top'>Bitty HTTP Example - Root</div>" "<div id='quitbttn'><a href='/quit.html'>QUIT</a></div>" "<div id='content'>" "GET example<br/>" "<br/>" "<a href='/nopagehere.html'>Non existing page</a>" "</div>" "<div id='bottom'></div>" "</body>" "</html>"; void File_Root(struct WebServer *Web) { WS_WriteWhole(Web,RootHTML,sizeof(RootHTML)-1); }

This is the root page. It simply displays a page with a link to a page that doesn't exist (/nopagehere.html).