Bitty HTTP

Development » GET Vars

1Reading GET vars

This example will show reading GET vars.

Bitty HTTP stores GET vars in a block of RAM as the headers are processed. This means it needs to know the names of all the GET vars before the page is requested. This is done by filling in the struct WSPageProp.Gets variable when the page properties are requested.

The Gets property is an array of pointers to strings with the last one being set to NULL to mark the end of the list. This list is scanned when the page is requested and the value of any vars in this list are added to a RAM array.

You then call WS_GET() to get the value that was saved (or NULL if it wasn't found).

Using things this way means a low memory foot print as we only remember vars that we are going to pull out later.

Files

FileServer.c

The file server is changed to provide a form on the root page and a new page to display the values from the form.

const char *GetArgs[]= { "Name", NULL };

We add a list of GET vars that we may want to read when processing a page later. We end the list with NULL to mark the end of the list. The code will loop though this list looking for a NULL entry to stop at.

When a match is made the value of the var will be copied to an internal RAM buffer.

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

A new page has been added called /Display.html that will display what the user has typed into the form from the root page. This includes a pointer to the GetArgs as the 4'th argument.

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/>" "<form action='/Display.html' method='GET'>" "Input your name:<input name='Name'></input> " "<input type='submit'></input>" "</form>" "<br/>" "</div>" "<div id='bottom'></div>" "</body>" "</html>"; void File_Root(struct WebServer *Web) { WS_WriteWhole(Web,RootHTML,sizeof(RootHTML)-1); }

This now just outputs a basic form with a get method asking the user for their name and going to /Display.html.

const char DisplayHTML_Start[]= "<!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'>"; const char DisplayHTML_End[]= "</div>" "<div id='bottom'></div>" "</body>" "</html>"; void File_Display(struct WebServer *Web) { char buff[100]; const char *Name; WS_WriteChunk(Web,DisplayHTML_Start,sizeof(DisplayHTML_Start)-1); Name=WS_GET(Web,"Name"); if(Name==NULL) { WS_WriteChunk(Web,"Name was not set.",17); } else { sprintf(buff,"Your name is:%s",Name); WS_WriteChunk(Web,buff,strlen(buff)); } WS_WriteChunk(Web,"<br/><br/><a href='/'>Try again</a>",35); WS_WriteChunk(Web,"<br/>",5); WS_WriteChunk(Web,DisplayHTML_End,sizeof(DisplayHTML_End)-1); }

File_Display() uses WS_GET() to read out the get vars from internal RAM. You call it with the name of the GET var to read. This name also has to be in the GetArgs array as well.

If WS_GET() does not find the var (because it wasn't passed in) it returns NULL.

It WS_GET() does find the var then it returns a string pointer to the value that was read.