summaryrefslogtreecommitdiffhomepage
path: root/NetEx/HttpServer.h
blob: fb74fbcd4c0e0799c44ea86f77911a1372168313 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*  Starshatter OpenSource Distribution
     Copyright (c) 1997-2004, Destroyer Studios LLC.
     All Rights Reserved.

     Redistribution and use in source and binary forms, with or without
     modification, are permitted provided that the following conditions are met:

     * Redistributions of source code must retain the above copyright notice,
        this list of conditions and the following disclaimer.
     * Redistributions in binary form must reproduce the above copyright notice,
        this list of conditions and the following disclaimer in the documentation
        and/or other materials provided with the distribution.
     * Neither the name "Destroyer Studios" nor the names of its contributors
        may be used to endorse or promote products derived from this software
        without specific prior written permission.

     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
     AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
     LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     POSSIBILITY OF SUCH DAMAGE.

     SUBSYSTEM:    NetEx.lib
     FILE:         HttpServer.h
     AUTHOR:       John DiCamillo


     OVERVIEW
     ========
     Network Server Pump for HTTP Server
*/


#ifndef HttpServer_h
#define HttpServer_h

#include "NetServer.h"

// +-------------------------------------------------------------------+

class HttpParam;
class HttpRequest;
class HttpResponse;

// +-------------------------------------------------------------------+

class HttpServer : public NetServer
{
public:
    static const char* TYPENAME() { return "HttpServer"; }

    HttpServer(WORD port, int poolsize=1);
    virtual ~HttpServer();

    int operator == (const HttpServer& l) const { return addr == l.addr; }

    virtual Text      ProcessRequest(Text request, const NetAddr& addr);
    virtual Text      DefaultResponse();
    virtual Text      ErrorResponse();

    virtual bool      DoGet(HttpRequest& request,  HttpResponse& response);
    virtual bool      DoPost(HttpRequest& request, HttpResponse& response);
    virtual bool      DoHead(HttpRequest& request, HttpResponse& response);

    virtual Text      GetServerName();
    virtual void      SetServerName(const char* name);

protected:
    Text              http_server_name;
};

// +-------------------------------------------------------------------+

class HttpParam
{
public:
    static const char* TYPENAME() { return "HttpParam"; }

    HttpParam(const char* n, const char* v) : name(n), value(v) { }

    int operator == (const HttpParam& p) const { return name == p.name; }

    Text  name;
    Text  value;
};

// +-------------------------------------------------------------------+

class HttpRequest
{
public:
    static const char* TYPENAME() { return "HttpRequest"; }

    enum METHOD {
        HTTP_OPTIONS,
        HTTP_GET,
        HTTP_HEAD,
        HTTP_POST,
        HTTP_PUT,
        HTTP_DELETE,
        HTTP_TRACE,
        HTTP_CONNECT
    };

    HttpRequest(const char* request=0);
    ~HttpRequest();

    operator Text();

    void              ParseRequest(Text request);
    void              ParseCookie(const char* param);

    int               Method()       const { return method;        }
    Text              URI()          const { return uri;           }
    Text              Content()      const { return content;       }
    Text              RequestLine()  const { return request_line;  }

    List<HttpParam>&  GetQuery()           { return query;         }
    List<HttpParam>&  GetHeaders()         { return headers;       }
    List<HttpParam>&  GetCookies()         { return cookies;       }

    NetAddr           GetClientAddr()            const { return client_addr; }
    void              SetClientAddr(const NetAddr& a)  { client_addr = a;    }

    Text              GetParam(const char* name);

    Text              GetHeader(const char* name);
    void              SetHeader(const char* name, const char* value);
    void              AddHeader(const char* name, const char* value);
    Text              GetCookie(const char* name);
    void              SetCookie(const char* name, const char* value);
    void              AddCookie(const char* name, const char* value);

    Text              DecodeParam(const char* value);
    static Text       EncodeParam(const char* value);

private:
    int               method;
    Text              uri;
    Text              content;
    Text              request_line;
    NetAddr           client_addr;

    List<HttpParam>   query;
    List<HttpParam>   headers;
    List<HttpParam>   cookies;
};

// +-------------------------------------------------------------------+

class HttpResponse
{
public:
    static const char* TYPENAME() { return "HttpResponse"; }

    enum STATUS {
        SC_CONTINUE             = 100,
        SC_SWITCHING_PROTOCOLS  = 101,

        SC_OK                   = 200,
        SC_CREATED              = 201,
        SC_ACCEPTED             = 202,
        SC_NON_AUTHORITATIVE    = 203,
        SC_NO_CONTENT           = 204,
        SC_RESET_CONTENT        = 205,
        SC_PARTIAL_CONTENT      = 206,

        SC_MULTIPLE_CHOICES     = 300,
        SC_MOVED_PERMANENTLY    = 301,
        SC_FOUND                = 302,
        SC_SEE_OTHER            = 303,
        SC_NOT_MODIFIED         = 304,
        SC_USE_PROXY            = 305,
        SC_TEMPORARY_REDIRECT   = 307,

        SC_BAD_REQUEST          = 400,
        SC_UNAUTHORIZED         = 401,
        SC_PAYMENT_REQUIRED     = 402,
        SC_FORBIDDEN            = 403,
        SC_NOT_FOUND            = 404,
        SC_METHOD_NOT_ALLOWED   = 405,
        SC_NOT_ACCEPTABLE       = 406,
        SC_PROXY_AUTH_REQ       = 407,
        SC_REQUEST_TIME_OUT     = 408,
        SC_CONFLICT             = 409,
        SC_GONE                 = 410,
        SC_LENGTH_REQUIRED      = 411,

        SC_SERVER_ERROR         = 500,
        SC_NOT_IMPLEMENTED      = 501,
        SC_BAD_GATEWAY          = 502,
        SC_SERVICE_UNAVAILABLE  = 503,
        SC_GATEWAY_TIMEOUT      = 504,
        SC_VERSION_NOT_SUPPORTED= 505
    };


    HttpResponse(int status=500, const char* content=0);
    HttpResponse(const char* response);
    ~HttpResponse();

    operator Text();

    void              ParseResponse(Text request);
    void              ParseCookie(const char* param);

    int               Status()       const { return status;  }
    void              SetStatus(int s)     { status = s;     }

    Text              Content()      const { return content; }
    void              SetContent(Text t)   { content = t;    }
    void              AddContent(Text t)   { content += t;   }

    List<HttpParam>&  GetHeaders()         { return headers; }
    List<HttpParam>&  GetCookies()         { return cookies; }

    Text              GetHeader(const char* name);
    void              SetHeader(const char* name, const char* value);
    void              AddHeader(const char* name, const char* value);
    Text              GetCookie(const char* name);
    void              SetCookie(const char* name, const char* value);
    void              AddCookie(const char* name, const char* value);

    void              SendRedirect(const char* url);

private:
    int               status;
    Text              content;

    List<HttpParam>   headers;
    List<HttpParam>   cookies;
};


#endif HttpServer_h