STL-based Web Server and Utility Classes


Desktop-as-a-Service Designed for Any Cloud ? Nutanix Frame

The code for the web server was adapted from the httpsvr 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 ref 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 url and then forms a response.

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

There are also several utility classes that can be used to replace mfc classes. They support Files, Strings, Pipes, Events, Threads, Locks, Sockets and ServerSockets. Only the class for strings is a drop in replacement. Sorry, but I dont have time to manufacture the MFC interfaces, but if you want they are their for you to use.

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, please feel free to email me.


Download source - 143 Kb


  • Nice sample, but full of bugs :-(

    Posted by Legacy on 08/26/2003 07:00am

    Originally posted by: Mandelbrot

    like :

    // Reading the HTTP request
    long length = sock.getReceiveSize();
    if ( length == 0 )
    return false;

    char buffer[1024];
    buffer[length] = '\0'; // what happens when length > 1024... ?
    length = sock.receive( buffer, 1024 ); // and here... ?


    return true;

    This may explain why it is so easy to exploit buffer overrun vulnerabilities in web servers ;)

  • Seems to lock files

    Posted by Legacy on 05/16/2003 07:00am

    Originally posted by: Mandelbrot

    Apparently it fails to close handles due to the order in which things are released :

    void TextFile::release ()
    Lock::release(); // release lock

    close(); // close file

    void TextFile::close ()
    if ( !lock() )


    Releasing the lock AFTER closing the file seems to work :

    void TextFile::release ()
    close(); // close file (needs the lock)
    Lock::release(); // destroy lock

    Strange you didn't notice this...

  • Bug Report

    Posted by Legacy on 05/02/2002 07:00am

    Originally posted by: Andrew

    I downloaded your web server- thanks for publishing, it's been a great help. A couple problems though. In the TextFile class, any place you check for an invalid handle, you need also to check for null. Also, in the case that either of these conditions occurs, you must unlock before returning.


    long TextFile::size ()
    if ( !lock() )
    return 0;

    if ( _hFile == NULL)
    return 0;

    if ( _hFile == INVALID_HANDLE_VALUE )
    return 0;

    DWORD dwSize = ::GetFileSize(_hFile, NULL);

    return dwSize;

  • Multithreaded

    Posted by Legacy on 04/23/2002 07:00am

    Originally posted by: Andr�

    One thread per request would be much better.

  • Does it provide SSL function?

    Posted by Legacy on 07/19/2001 07:00am

    Originally posted by: Darren

    I have to write a simple web server with SSL ability. But I don't know how to implement SSL function. Would you please help me? Dose any MFC or STL class have SSL function.


  • please help me

    Posted by Legacy on 07/12/2001 07:00am

    Originally posted by: daby

    -STL-based Web Server and Utility Classes -
    This program source helped me a lot to understand the processes taken places when a server starts. So, I completed HTTP server Program with HTTP Client program.
    Now, I can see the webpage of HTTP server by WebBrowser. But my HTTP client program can't see it.
    Client Program is producted by 'wininet'. Client program is pda webbrowser.
    Unfortunetly, i can see other webpage with client program. yahoo..and so on..
    Client program is use of HTTP 1.1 protocol(evc++, wininet funtion).

    Please help me.
    Sorry, i don't speak well in English.

  • Features?

    Posted by Legacy on 06/07/2001 07:00am

    Originally posted by: Alastair Growcott

    What features does this code have. My company has asked me to develop an HTTP server interface. it sounds like this one has a very similar purpose, although its object structure is a little more complex than I would use, but I guess that's a hold-over of converting from MFC.

    I am interested in what features this thing has (without having to download and spend hours looking - I'm lazy). Does it provide any cookie functionality? Does it provide any SSL functionality?

  • Excellent Work !

    Posted by Legacy on 04/20/2001 07:00am

    Originally posted by: Pallav Khandhar

    As a part of my final Semester Training I was given to develop an HTTP Server. I started from scratch and all of sudden I got your code from codeguru. It helped me a lot to understand the processes taken places when a server starts. Now I am sure that I can try to develop a Web Server.
    Excellent Work !
    Great !!

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date