Click here to Skip to main content
15,867,686 members
Articles / Desktop Programming / MFC
Article

STL WebServer

Rate me:
Please Sign up or sign in to vote.
4.89/5 (5 votes)
8 May 2000 89.7K   2.5K   47   10
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 dghubbard@megsinet.com.

    License

    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


    Written By
    United States United States
    This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

    Comments and Discussions

     
    GeneralBUG REPORT: when input http://127.0.0.1/something.rar , IE dose NOT download the file but just show the rar file's content in IE itself Pin
    adroitadroit203-Dec-06 16:17
    adroitadroit203-Dec-06 16:17 
    GeneralRe: BUG REPORT: when input http://127.0.0.1/something.rar , IE dose NOT download the file but just show the rar file's content in IE itself Pin
    nickhalfasleep31-May-07 3:22
    nickhalfasleep31-May-07 3:22 
    GeneralStream to output port Pin
    Robert Bielik11-Apr-06 21:12
    Robert Bielik11-Apr-06 21:12 
    GeneralWhen user often click it is very busy Pin
    giga24-Apr-05 17:11
    giga24-Apr-05 17:11 
    Generalsome bug fixes Pin
    22-Jan-02 6:11
    suss22-Jan-02 6:11 
    GeneralRe: some bug fixes Pin
    James Curran29-May-02 11:32
    James Curran29-May-02 11:32 
    A few more.....

    The code frequently has "using namespace std;", which is bad enough in general, but here goes for the ultimate evil use: Inside headers for library functions!

    Properly, when one is writing a library for general use, you should fully qualify standard class:

    [code]
    class HttpRequest :
    public HttpBuffer
    {
    public:

    std::string _url;
    std::string _path;
    std::string _pathInfo;
    // :
    };[/code]

    If that gets too tedious, the next best option is to use a "using" declaration, but just within the exact scope needed

    [code]
    class HttpRequest :
    public HttpBuffer
    {
    public:
    using std::string; // affects only clas HttpRequest
    string _url;
    string _path;
    string _pathInfo;
    // :
    };[/code]


    It seems that the original author just didn't understand namespaces at all. The class StringUtil, which exist merely to group a collection of related functions, really should be a namespace.

    Further, the code above demostrates another problem: Leading underscores. Most identifiers with a leading underscore are reserved, one way or another, usually for the library writer or the compiler vender. Best to avoid them entirely in your own code. If you need an member indicator, use a leading "m_" or a trailing underscore.



    Truth,
    James
    GeneralRe: some bug fixes Pin
    unitrunker10-Oct-03 23:30
    unitrunker10-Oct-03 23:30 
    GeneralRe: some bug fixes Pin
    James Curran11-Oct-03 3:50
    James Curran11-Oct-03 3:50 
    Questiona Suggestion ?? Pin
    27-Jan-01 20:25
    suss27-Jan-01 20:25 
    AnswerRe: a Suggestion ?? Pin
    James Curran29-May-02 11:13
    James Curran29-May-02 11:13 

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

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