Click here to Skip to main content
15,896,153 members
Articles / Programming Languages / C++

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 197.6K   3.8K   108  
A C-language web server with embedded files and CGI.
/* webio.c
 *
 * Part of the Webio Open Source lightweight web server.
 *
 * Copyright (c) 2007 by John Bartas
 * All rights reserved.
 *
 * Use license: Modified from standard BSD license.
 * 
 * Redistribution and use in source and binary forms are permitted
 * provided that the above copyright notice and this paragraph are
 * duplicated in all such forms and that any documentation, advertising 
 * materials, Web server pages, and other materials related to such
 * distribution and use acknowledge that the software was developed
 * by John Bartas. The name "John Bartas" may not be used to 
 * endorse or promote products derived from this software without 
 * specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 */

/* This file is only for testing or making a plain standalone web
 * server app. It is not needed in a web server when the server 
 * is built as a library for inclusion in an application
 * program or an embedded system.
 */

#include <stdio.h>

#include "websys.h"
#include "webio.h"
#include "webfs.h"
#include "wsfdata.h"

extern void	exit(int code);

/* Sample authentication code & "database" */
static char test_name[32] = {"john"};
static char test_passwd[32] = {"bartas"};
int   wfs_auth(void * fd, char * name, char * password);

u_long  cticks = 0;

int
main( int argc, char * argv[] )
{
   int error;

   printf("Webio server starting...\n");

#ifdef _WINSOCKAPI_
   {
      WSADATA wsaData;
      error = WSAStartup(MAKEWORD(2,2), &wsaData);
      if(error)
      {
         dprintf("winsock startup error %d\n", error);
         exit(1);
      }
   }
#endif

   error = wi_init();
   if(error < 0)
   {
      dprintf("wi_init error %d\n", error);
      exit(1);
   }

   /* Install our port-local authentication routine */
   emfs.wfs_fauth = wfs_auth;


   error = wi_thread();   /* blocks here until killed */
   if(error < 0)
   {
      dprintf("wi_init error %d\n", error);
      exit(1);
   }

   return 0;
}


/* Return true if user gets access to the embedded file, 0 if not. */

wfs_auth(void * fd, char * name, char * password)
{

   /* See if this file requires authentication. */
   EOFILE *    eofile;
   em_file *   emf;

   eofile = (EOFILE *)fd;
   emf = eofile->eo_emfile;

   if(emf->em_flags & EMF_AUTH)
   {
      if(stricmp(name, test_name))
         return 0;
      if(stricmp(password, test_passwd))
         return 0;
   }
   return 1;
}


void
ws_dtrap(void)
{
   printf("dtrap - need breakpoint");
}

void
panic(char * msg)
{
   printf("panic: %s", msg);
   dtrap();
   exit(1);
}

/* memory_ssi()
 *
 * Sample SII routine to dump memory statistics into an output html file. 
 *
 */

extern u_long   wi_blocks;
extern u_long   wi_bytes;
extern u_long   wi_maxbytes;
extern u_long   wi_totalblocks;


int 
memory_ssi(wi_sess * sess, EOFILE * eofile)
{
   /* print memory stats to the session's TX buffers */
   wi_printf(sess, "Current blocks: %d <br>", wi_blocks );
   wi_printf(sess, "Current bytes: %d <br>", wi_bytes );
   wi_printf(sess, "Total blocks: %d <br>", wi_totalblocks );
   wi_printf(sess, "Max. bytes: %d <br>", wi_maxbytes );

   return 0;      /* OK return code */
}


int
wi_cvariables(wi_sess * sess, int token)
{
   int   e;

   switch(token)
   {
   case MEMHITS_VAR8:
      e = wi_putlong(sess, (u_long)(wi_totalblocks));
      break;
   }
   return e;
}


/* testaction_cgi
 * Stub routine for form processing
 * 
 * Returns ????
 */

char username[512];

char * 
testaction_cgi(wi_sess * sess,  EOFILE * eofile)
{
   char *   your_name;

   your_name = wi_formvalue(sess, "your_name");   /* default: John */
   
   /* Save local copy of name in global saved user name variable */
   strncpy(username, your_name, sizeof(username)-1);

   wi_redirect(sess, "gotname.html");

   return( NULL );
}

/* getname_ssi()
 *
 * SSI routine stub
 */

int
getname_ssi(wi_sess * sess, EOFILE * eofile)
{
    /* output saved user name to session, if it's set */
    if(username[0] == 0)
        wi_printf(sess, "(no user name)");
    else
        wi_printf(sess, "%s", username);
   
    return 0;
}

/* PUSH
 *
 * pushtest_func routine stub
 */

int
pushtest_func(wi_sess * sess, EOFILE * eofile)
{
   /* Add your code here */
   return 0;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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