httpsrv.h

Go to the documentation of this file.
00001 
00002  //
00003  // XMLStorage XML-RPC server extension
00004  //
00005  // Copyright (c) 2009 Martin Fuchs <martin-fuchs@gmx.net>
00006  //
00007 
00008 
00009 /*
00010 
00011   All rights reserved.
00012 
00013   Redistribution and use in source and binary forms, with or without
00014   modification, are permitted provided that the following conditions are met:
00015 
00016   * Redistributions of source code must retain the above copyright
00017     notice, this list of conditions and the following disclaimer.
00018   * Redistributions in binary form must reproduce the above copyright
00019     notice, this list of conditions and the following disclaimer in
00020     the documentation and/or other materials provided with the
00021     distribution.
00022 
00023   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00024   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00025   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00026   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00027   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00028   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00029   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00030   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00031   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00032   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00033   POSSIBILITY OF SUCH DAMAGE.
00034 
00035 */
00036 
00037 #include "xmlrpc.h"
00038 
00039 
00040 //#ifdef _WIN32
00041 //#define USE_HTTPAPI
00042 //#endif
00043 
00044 #ifndef USE_HTTPAPI
00045 #include "sockets.h"
00046 #endif
00047 
00048 
00049 enum HTTP_COMMAND {
00050     HTTP_UNKNOWN=-1,
00051     HTTP_GET, HTTP_POST
00052 };
00053 
00054 struct HttpRequest
00055 {
00056     HTTP_COMMAND _command;
00057     string      _data;
00058 
00059 #ifdef USE_HTTPAPI
00060     ULONGLONG   _id;
00061 #endif
00062 };
00063 
00064 struct HttpServer
00065 {
00066     HttpServer();
00067     ~HttpServer();
00068 
00069     void enable_extensions(bool flag=true)
00070     {
00071         _extensions = flag;
00072     }
00073 
00074     void    register_url(const char* prefix);
00075     void    unregister_url(const char* prefix);
00076 
00077     void    receive_request(HttpRequest& request);
00078 
00079     void    send_response(HttpRequest& request, const string& str, const char* content_type);
00080     void    send_error(HttpRequest& request, u_short code, const char* status_text);
00081 
00082     void    send(u_short code, const char* status_text, const string& data, const char* content_type, bool connection_close);
00083 
00084 protected:
00085 #ifdef USE_HTTPAPI
00086     HANDLE      _request;
00087 #else
00088 #ifdef _WIN32
00089     WinSockInit _ws_init;
00090 #endif
00091 
00092     AddressList _addr;
00093     Socket      _listen_socket;
00094     string      _url_prefix;
00095 
00096     //TODO handle more than one connection simultaniously
00097     Socket  _conn_socket;
00098 #endif
00099 
00100     bool        _extensions;
00101 
00102 private:
00103     bool    handle_request(HttpRequest& request);
00104 };