Click here to Skip to main content
15,867,330 members
Articles / Programming Languages / C++
Article

Webio - An embedded web server

Rate me:
Please Sign up or sign in to vote.
4.88/5 (33 votes)
20 Jul 2008BSD3 min read 195.5K   3.8K   108   63
A C-language web server with embedded files and CGI.

Introduction

Webio is a small-footprint web server, designed to be embedded in an application or an embedded system. It's useful when you want to implement a complex browser based GUI (which can be accessed by everything from PCs to cell phones) in a very efficient manner. Webio compiles and runs equally well under Linux and Windows, and should be easy to port to most other platforms. It comes with a programmer's manual (progman.html) which explains how to use it and how to port it.

Background

In 1996, I wrote one of the first web servers designed for embedded devices. In those days, most embedded devices had no file systems, so I created the "HTML compiler" to embed the files into the code image. Similarly, the lack of a file system led to C-language CGI functions.

Creating basic GUIs with this system was so easy that I started using it in Windows applications in preference to the Windows GUI. Back then, before JavaScript and CSS, it was somewhat limited - for example, it wouldn't make a very good "photo shop" type program - but for basic GUIs, it was great.

As my company started using Linux and Browser-enabled hand-held devices, a really huge advantage became obvious - my new applications worked everywhere, not just on Windows. The user was not tied on one OS or one type of device. They didn't even have to be near the machine running the application.

In 2007, I needed a similar server for an open-source project. I had left the previous company, which still retained the rights to the my old server. They wouldn't open source it, and nothing suitable was available in the public domain. I decided to create a second generation version of the server and release it under the BSD license so I would never have to write it again.

The result is Webio - my second (and hopefully last) embedded web server.

Using the code

The Windows version is probably of most interest to CodeProject readers, and so the .zip file is made available here. Follow these steps:

  1. Unzip it (preserving the directory structure).
  2. Type buildfs to compile the embedded file system.
  3. Open the project file with Visual C++ 6.0 or newer, and click Build.

You should get a little application which, when run, allows your PC to act as a web server - point a browser at it. You can do this in loopback by typing "http://127.1" in your browser's location bar.

Points of interest

Webio has a few improvements over my first embedded web server:

  • The "HTML compiler" is now a full-fledged file system builder, designed from the group up to generate not only file images in your C code, but also generate code for C-language CGI.
  • The server buffers all code-generated output, allowing accurate Content-Length fields on files with variable sized SSIs.
  • A fast path for binary files improves performance.
  • Portability across Windows/Linux/Embedded systems is enhanced.

History

  • July 2008 - First public release.
  • July 27th - Updated, call this release 1.1. Changes:
    • Added command line option to set the HTTP port (default is still 80).
    • Error message is more helpful if another web server already has port 80.
    • Fixed some typos and omissions in the manual.

License

This article, along with any associated source code and files, is licensed under The BSD License


Written By
Chief Technology Officer praemio.com
United States United States
See my bio here:

http://www.bartas.net/resume.htm

Comments and Discussions

 
GeneralWt -> webtoolkit.eu Pin
pgquiles13-Sep-08 1:23
pgquiles13-Sep-08 1:23 
QuestionImpressive - but what about boost/asio? Pin
Ellers6-Aug-08 13:25
Ellers6-Aug-08 13:25 
GeneralLink error Pin
Nagrom28-Jul-08 3:02
Nagrom28-Jul-08 3:02 
GeneralRe: Link error Pin
jbartas29-Jul-08 5:31
jbartas29-Jul-08 5:31 
GeneralRe: Link error Pin
Nagrom30-Jul-08 8:14
Nagrom30-Jul-08 8:14 
Generalthanks for sharing Pin
beejoy26-Jul-08 22:31
beejoy26-Jul-08 22:31 
GeneralA Recipe for displaying dynamic content Pin
ravenspoint23-Jul-08 7:04
ravenspoint23-Jul-08 7:04 
GeneralRe: A Recipe for displaying dynamic content Pin
ravenspoint23-Jul-08 10:02
ravenspoint23-Jul-08 10:02 
A simple, one line change to webio has allowed me to simplify the recipe for displaying dynamic content. You no longer need to muck about with BOTH a string identifier and a string token. ( The token is not only unnecessary, but liable to change from time to time, which I would consider a bug ) Here is the recipe first:

- Choose a unique identifier for the string to be displayed. Let's call it XYZ.

- Create a function that returns a pointer to a buffer containing the string to be displayed. Unfortunately, this has to be a function in the global namespace, because I do not know how to store pointer to member functions. Something like this

char * getDisplayStringXYZ();


- Add html to place the string where you want it in the web page. Something like this

Display Sting XYZ goes just here: <!--#include file="XYZ" -->


- Add a line to the file "filelist" like this

XYZ -e u_long x


( The "u_long x" does not have any significance, but must be there anyhow )

- Run this command

fsbuilder -g wsfcode.c filelist


- Add code like this, before you start the server

cWebio::SetLink( "XYZ", &getDisplayStringXYZ );


- Rebuild your application.

-------------------------------------

The trick to achieving this is to change the call to wi_cvariables ( line 639, webutils.c ) to

error = wi_cvariables(sess, ssifname );


Now wi_cvariables can be implemented as a one line routine that need never be changed

int
wi_cvariables(wi_sess * sess,  char * idname)
{
 return cWebio::ResolveLink( sess, idname );
}


cWebio is needed to make this work, of course, because it maintains a private map of idnames to function pointers.

----------------------------------------

I would really like to avoid using filelist altogether. However, I cannot immediatly see how to manage this. The issue seems to be that without running fsbuilder with the string identifier specified in filelist, line 729 in webio.c is not executed. Perhaps John Bartas could explain this mystery to me?
GeneralRe: A Recipe for displaying dynamic content Pin
ravenspoint23-Jul-08 10:39
ravenspoint23-Jul-08 10:39 
GeneralRe: A Recipe for displaying dynamic content Pin
ravenspoint23-Jul-08 13:11
ravenspoint23-Jul-08 13:11 
GeneralRe: A Recipe for displaying dynamic content Pin
ravenspoint24-Jul-08 9:30
ravenspoint24-Jul-08 9:30 
GeneralUsing Webio in a C++ program Pin
ravenspoint23-Jul-08 4:07
ravenspoint23-Jul-08 4:07 
GeneralBuild & Run workarounds Pin
ravenspoint22-Jul-08 8:34
ravenspoint22-Jul-08 8:34 
GeneralRe: Build & Run workarounds Pin
ravenspoint22-Jul-08 9:02
ravenspoint22-Jul-08 9:02 
GeneralRe: Build & Run workarounds Pin
jbartas22-Jul-08 18:52
jbartas22-Jul-08 18:52 
Generalbuild error in VC++7.0 Pin
sms9121-Jul-08 17:49
sms9121-Jul-08 17:49 
GeneralRe: build error in VC++7.0 Pin
vva13321-Jul-08 19:24
vva13321-Jul-08 19:24 
GeneralNice project! Pin
Rynus_Rein20-Jul-08 10:43
Rynus_Rein20-Jul-08 10:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.