Click here to Skip to main content
Click here to Skip to main content

STL WebServer

, 8 May 2000
Rate this:
Please Sign up or sign in to vote.
A set of classes written in STL that implement a web server
  • Download source files - 76 Kb
  • Introduction

    This article presents code for a web server implemented using sockets and STL. The code for the web server was adapted from the httpsvr sample in MSDN. The visual interface has been removed. All of the the MFC code has been ripped out and converted to STL, and socket classes were implemented.

    The socket classes are:

    • Socket
    • ServerSocket

    These feed handler classes:

    • SocketHandler
    • ServerSocketHandler
    • HttpSocketHander

    Socket notification comes from the abstract class SocketNotify which SocketHandler implements. A reference can be passed in to override the notification.

    The http request classes are:

    • HttpBuffer
    • HttpRequest
    • HttpRquestProcess
    • HttpResponse

    The request class allows you to create requests or to store a request. The request process processes requests by passing in a pointer to a buffer. The request process class gets initial info like the URL and then forms a response.

    The startup code is in Server.cpp which declares an instance of HttpServer. To set up the code to run set the directory where your web pages are and then compile and run.

    I would like to here any comments on how to improve performance or how to better implement the web server. So if you get time email me at


    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here


    About the Author

    David Hubbard

    United States United States
    No Biography provided

    Comments and Discussions

    GeneralBUG REPORT: when input , IE dose NOT download the file but just show the rar file's content in IE itself Pinmemberadroitadroit203-Dec-06 17:17 
    GeneralRe: BUG REPORT: when input , IE dose NOT download the file but just show the rar file's content in IE itself Pinmembernickhalfasleep31-May-07 4:22 
    GeneralStream to output port PinmemberRobert Bielik11-Apr-06 22:12 
    GeneralWhen user often click it is very busy Pinmembergiga24-Apr-05 18:11 
    Generalsome bug fixes PinmemberMark Timperley22-Jan-02 7:11 
    First I would like to thank Dave for creating and posting this code. It is obviously a work in progress, although it appears as if it has been abandoned by the author after being posted in May of 2000. Anyway, I was looking for a chunk of code that I could use as a prototype embedded web server for a quick demo. This code fit nicely with some modifications. Along the way I discovered some bugs in the original code so here they are with corrections. Hopefully this will be of use to somebody.
    Note that the source file is identified for each bug but the line number will be just approximate. You should be able to find the offending code by comparing the original statements, contained in the comment, and replacing them with the statements (if any) following the comment (some bug fixes just remove bad code).
    1) \Socket\TextFile.cpp(93):
    /* BUG MET 21Jan02
    Socket\Socket\TextFile.cpp(104): Lock::release();
    Don't release the lock before calling close() since close() must use
    (acquire) the lock to do its work. The lock should be maintained
    for the lifetime of this object. The effect of this bug is such
    that once the server opens an html file, it never releases it.
    This is annoying in the least, and at the worst, if the web site
    has many files, the server may eventually run out of file handles.
    // release lock

    2) \Socket\HttpRequest.h(248):
    /* BUG MET 21Jan02
    The following bug prevented adding the header data to the request.
    Therefor, functions like getHeaderValue() would always fail, since
    there wasn't any header data. This was particularly annoying because
    since bodySent() uses the idParamContentLength header value to determine
    if there is any body in the message. Therefor, every message sent to this
    server appeared as if it had no body (yikes! just a head).
    if ( param.empty() == 0 || value.empty() )
    if ( param.empty() || value.empty() )

    3) \Socket\HttpResponse.cpp(104):
    /* BUG MET 22Jan02
    else if ( size() > 0 && getProcessState() == REQ_DONE &&
    else if ( getProcessState() == REQ_DONE &&

    4) \Socket\HttpResponse.cpp(169):
    /* BUG MET 21Jan02
    !HttpUtil::ifModSince( _hFile, timeIfMod ) )
    !HttpUtil::ifModSince( _file, timeIfMod ) )

    5) \Socket\HttpResponse.cpp(182):
    /* BUG MET 21Jan02
    CloseHandle( _hFile );
    6) \Socket\HttpResponse.cpp(196):
    /* BUG MET 21Jan02
    _hFile NOT INITIALIZED, GetFileSize( _hFile, NULL ) WILL FAIL
    // set content type and length
    long contentLength = GetFileSize( _hFile, NULL );

    7) \Socket\HttpResponse.cpp(205):
    /* BUG MET 21Jan02
    NO NEED TO CREATE TEMP contentLength IF WE USE _file.size() DIRECTLY
    addHeader( idParamContentLength, contentLength );
    addHeader( idParamContentLength, _file.size() );

    8) \Socket\HttpResponse.h(43):
    /* BUG MET 21Jan02
    HANDLE _hFile; // handle to file associated with response

    9) \Socket\HttpUtil.h(65):
    /* BUG MET 21Jan02
    #define idParamContentLength "Content-Length"
    #define idParamLastModified "Last-Modified"
    #define idParamIfModifiedSince "If-Modified-Since"
    #define idParamContentType "Content-Type"
    #define idParamContentLength "content-length"
    #define idParamLastModified "last-modified"
    #define idParamIfModifiedSince "if-modified-since"
    #define idParamContentType "content-type"

    GeneralRe: some bug fixes PinmemberJames Curran29-May-02 12:32 
    GeneralRe: some bug fixes Pinmemberrfmobile11-Oct-03 0:30 
    GeneralRe: some bug fixes PinmemberJames Curran11-Oct-03 4:50 
    Questiona Suggestion ?? 21:25 
    AnswerRe: a Suggestion ?? PinmemberJames Curran29-May-02 12:13 

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

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

    | Advertise | Privacy | Terms of Use | Mobile
    Web02 | 2.8.150123.1 | Last Updated 9 May 2000
    Article Copyright 2000 by David Hubbard
    Everything else Copyright © CodeProject, 1999-2015
    Layout: fixed | fluid