BittyHTTP has a few functions you must provide and a few functions you must call from your application (provided you use the POSIX SocketsCon.c).

The functions you must provide are:

The functions you must call are:

ReadElapsedClock()

This function is called by the web server to figure out if a connection has typed out.  It returns the number of seconds that have passed.  This does not have to start a 0 but can.

If you are have a real time clock you can return it's value (the seconds anyway).

If your system does not have a real time clock returning a timer that starts at 0 on boot work fine.

So long as this value continues to go up every second BittyHTTP is happy.

You can have this always return 0, however this will disable time outs and a badly behaving browser could lock out your web server).

So for example:

t_ElapsedTime ReadElapsedClock(void)
{
   return (uint32_t)time(NULL);
}

FS_GetFileProperties()

This function is called when the web server gets a request for a file.  This first argument to the function is the filename that was requested.  This is a standard C string so you can do things like strcmp() on it.  Note that the filename includes the path part as well.

The seconds argument is a WSPageProp structure.  You must fill this in if you want to serve this page.

So for example if you support the filename "Example.html" you might use code like this:

bool FS_GetFileProperties(const char *Filename,struct WSPageProp *PageProp)
{
   if(strcmp(FIlename,"Example.html")==0)
   {
      PageProp->FileID=1;
      PageProp->DynamicFile=true;
      PageProp->Cookies=NULL;
      PageProp->Gets=NULL;
      PageProp->Posts=NULL;
      return true;
   }
   else
   {
      return false;
   }
}

The Cookies, Gets, and Posts pointers are used if your page supports one or more of them.

The FileID is where you signal to the FS_SendFile() function what file it should send.  This is large enough to store a pointer.

The reason sending a file is split into 2 functions is because FS_GetFileProperties() is called before all the headers have been processed.  The buffer used for the filename is the same buffer used to process headers.  By the time all the headers have been processed the filename has been lost.

FS_SendFile()

This function is called after all the headers are processed and the system is ready to send the requested file.  You take the FileID you setup in FS_GetFileProperties() and use it to decide what file to send.

void FS_SendFile(struct WebServer *Web,uintptr_t FileID)
{
   if(FileID==1)
   {
      WS_WriteWhole(Web,"Hello World",11);
   }
}

In this case we handle the file directly in FS_SendFile() instead of calling a different function.  We just check the FileID is 1 and call WS_WriteWhole() to send "Hello World"

SocketsCon_InitSocketConSystem()

This function is from the POSIX socket file SocketsCon.c file.  It readies the sockets for use.  If you are using your own sockets you don't need this function.

WS_Init()

This function init's the web servers state machines and other data.

WS_Start()

This function tells BittyHTTP to start listening for incoming connections on the provided port.  Note that it will not actually accept the incoming connection unless WS_Tick() is being called.

WS_Tick()

This function needs to be called regularly for the BittyHTTP state machine to accept connections, read data, process headers, and call backs FS_GetFileProperties() and FS_SendFile().

WS_Shutdown()

This shuts down the web server.  It stops it from listening and closes all the sockets.


SocketsCon_ShutdownSocketConSystem()

This function is from the POSIX socket file SocketsCon.c file.  It frees any resources that the socket connection system was using.  If you are using your own sockets you don't need this function.

main()

Here is an example of a main function.

bool g_Quit;
int main(void)
{
    SocketsCon_InitSocketConSystem();
    WS_Init();

    if(!WS_Start(3000))
    {
        printf("Failed to start web server\n");
        return 1;
    }

    printf("Waiting for connections on port 3000\n");

    g_Quit=false;
    while(!g_Quit)
        WS_Tick();

    WS_Shutdown();
    SocketsCon_ShutdownSocketConSystem();

    return 0;
}