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

  • Today's agile organizations pose operations teams with a tremendous challenge: to deploy new releases to production immediately after development and testing is completed. To ensure that applications are deployed successfully, an automatic and transparent process is required. We refer to this process as Zero Touch Deployment™. This white paper reviews two approaches to Zero Touch Deployment--a script-based solution and a release automation platform. The article discusses how each can solve the key …

  • On-demand Event Event Date: December 18, 2014 The Internet of Things (IoT) incorporates physical devices into business processes using predictive analytics. While it relies heavily on existing Internet technologies, it differs by including physical devices, specialized protocols, physical analytics, and a unique partner network. To capture the real business value of IoT, the industry must move beyond customized projects to general patterns and platforms. Check out this webcast and join industry experts as …

Most Popular Programming Stories

More for Developers

RSS Feeds