STL-based Web Server and Utility Classes

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.

Downloads

Download source - 143 Kb


Comments

  • Nice sample, but full of bugs :-(

    Posted by Legacy on 08/26/2003 12: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 ;)

    Reply
  • Seems to lock files

    Posted by Legacy on 05/16/2003 12: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() )
    return;
    [...]
    ::CloseHandle(_hFile);


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

    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...

    Reply
  • Bug Report

    Posted by Legacy on 05/02/2002 12: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.

    Example:

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

    if ( _hFile == NULL)
    {
    unlock();
    return 0;
    }

    if ( _hFile == INVALID_HANDLE_VALUE )
    {
    unlock();
    return 0;
    }

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

    unlock();
    return dwSize;
    }

    Reply
  • Multithreaded

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

    Originally posted by: Andr�

    One thread per request would be much better.

    Reply
  • Does it provide SSL function?

    Posted by Legacy on 07/19/2001 12: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.

    Darren

    Reply
  • please help me

    Posted by Legacy on 07/12/2001 12: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.

    Reply
  • Features?

    Posted by Legacy on 06/07/2001 12: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?

    Reply
  • Excellent Work !

    Posted by Legacy on 04/20/2001 12: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 !!

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

Top White Papers and Webcasts

  • Live Event Date: September 19, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT In response to the rising number of data breaches and the regulatory and legal impact that can occur as a result of these incidents, leading analysts at Forrester Research have developed five important design principles that will help security professionals reduce their attack surface and mitigate vulnerabilities. Check out this upcoming eSeminar and join Chris Sherman of Forrester Research to learn how to deal with the influx of new device …

  • Specialization and efficiency are always in need. Whether it's replacing an aging roof, getting a haircut, or tuning up a car, most seek the assistance of trusted experts. The same is true in the business world, where an increasing number of companies are seeking the help of others to administer their IT systems and services. This special edition of Unleashing IT highlights a new breed of IT caretaker -- Cisco Powered service providers -- and the business advantages and operational efficiencies they …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds