Bitty HTTP

Development » URL Encode

1Encoding/Decoding URLs

This example will show using the functions for encoding and decoding URLs. This is also known as Percent-encoding and is basically escaping a string for use on the URL line.

This is needed when you build links to other pages and other places where you need to escape URL data

This example will use WS_URLEncode(), WS_URLDecode(), and WS_URLDecodeInPlace()

Files

FileServer.c

In this example file server only serves simple page showing encoded and decoded strings.

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}, };

This goes at the top and defines the 3 standard pages we serve.

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_Root(struct WebServer *Web) { char buff[300]; const char *Msg="100% of chars are <i>escapable</i>"; char EncodedStr[200]; char DecodedStr[200]; WS_WriteChunk(Web,DisplayHTML_Start,sizeof(DisplayHTML_Start)-1); sprintf(buff,"Before Encoding:%s",Msg); WS_WriteChunk(Web,buff,strlen(buff)); WS_WriteChunk(Web,"<br/>",5); WS_URLEncode(Msg,EncodedStr,sizeof(EncodedStr)); sprintf(buff,"After Encoding:%s",EncodedStr); WS_WriteChunk(Web,buff,strlen(buff)); WS_WriteChunk(Web,"<br/>",5); WS_URLDecode(EncodedStr,DecodedStr,sizeof(DecodedStr)); sprintf(buff,"After Decoding:%s",DecodedStr); WS_WriteChunk(Web,buff,strlen(buff)); WS_WriteChunk(Web,"<br/>",5); WS_URLDecodeInPlace(EncodedStr); sprintf(buff,"After Decoding in place:%s",EncodedStr); WS_WriteChunk(Web,buff,strlen(buff)); WS_WriteChunk(Web,"<br/>",5); WS_WriteChunk(Web,DisplayHTML_End,sizeof(DisplayHTML_End)-1); }

The page starts off by writing out the string "100% of chars are <i>escapable</i>".

The WS_URLEncode() function is called to encode the string as a URL string. The output string will be placed in the buffer EncodedStr. This buffer should be 3 times the size of the source string because the string can be made up to 3 times bigger. In this case the string is 34 bytes and the output buffer is 200 bytes long. The last argument to WS_URLEncode() is the size of the output buffer

The next thing the example does is decode the string we just encoded using the WS_URLDecode() function. This will take a URL encoded string and convert it back to a normal string. Again The output string will be stored in DecodedStr The last argument to WS_URLDecode() is the size of the output buffer

Finally the WS_URLDecodeInPlace() function is used to decode the string again. This time the input buffer is used to store the new string. This works because the decoded string will always be shorter than the source string.