1The Basics
Here we are going to go over what files you will need and how to build the example. The basic list of files needed in the examples are (and their .h files):- main.c
- Options.h
- WebServer.c
- WebServer.h
- SocketsCon.c
- SocketsCon.h
- FileServer.c
These are in the examples directorys
Building
All the examples are built with gcc using a line like:To keep the examples simple there are just simple build.sh scripts in the example directories. These scripts are a single line that just runs GCC directly. This could have been done with a makefile but a script is simpler.
main.c
The main.c file is where main() lives. This is the heart of your program and in our case will call the init functions and enter a while(1) loop. We also have misc function needed by the web server.
Options.h
This file has defines in it that are used to customize the web server.
WebServer.c
This is the work horse of the system. It handles accepting new connections, process the HTTP requests, send any errors to the client, and call the functions in FileServer.c to get requested web pages.
The API functions in this file are:WS_Init() | Init the web server |
WS_Shutdown() | Shut down the web server |
WS_Start() | Start listening for incoming connections |
WS_Tick() | Called in the main loop. Runs the web server |
WS_WriteWhole() | Write a static page |
WS_WriteChunk() | Write some of page using chunked HTTP data |
WS_Header() | Add a HTTP header |
WS_GET() | Read a GET var |
WS_COOKIE() | Read a cookie |
WS_POST() | Read a POST var |
WS_SetCookie() | Set a cookie |
WS_URLEncode() | Encode a string for use as a URL |
WS_URLDecode() | Decode a string from a URL |
WS_URLDecodeInPlace() | Decode a string from a URL writting back out to the same buffer |
WebServer.h
This file has the access function prototypes and other needed things for the web server.
It includes the following call back functions that will be called when the web server needs something:FS_GetFileProperties() | Get info about the requested file (not the file contents) |
FS_SendFile() | Send the contents of the file that was looked up with FS_GetFileProperties() |
ReadElapsedClock() | Get the number of seconds elapsed since the time this function was called |
These will be described later in the FileServer.c and main.c descritions.
SocketsCon.c
This is a wrapper file around the sockets interface. It does 3 things:- it allows the web server to use non standard socket interfaces (this does happen)
- it changes everything over to nonblocking calls
- it can be used to handle SSL sockets without having to change the main web server code (you only have to call SocketsCon_Read() instead of read() or SSL_read())
FileServer.c
This is where the callbacks for processing page requests live.
This file has the following callback functions in it:FS_GetFileProperties() | This is called when the first line in the HTTP request is processed. It is to return info about this page (like if it should be cached, what GET/POST/COOKIE vars this page is going to use, etc) |
FS_SendFile() | This is called when the you should send the contents of the requested page (as setup in FS_GetFileProperties()) |
In the examples we also have the functions to send the each page.