Starshatter_Open
Open source Starshatter engine
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
HttpServer.h
Go to the documentation of this file.
1 /* Project nGenEx
2  Destroyer Studios LLC
3  Copyright © 1997-2004. All Rights Reserved.
4 
5  SUBSYSTEM: NetEx.lib
6  FILE: HttpServer.h
7  AUTHOR: John DiCamillo
8 
9 
10  OVERVIEW
11  ========
12  Network Server Pump for HTTP Server
13 */
14 
15 
16 #ifndef HttpServer_h
17 #define HttpServer_h
18 
19 #include "NetServer.h"
20 
21 // +-------------------------------------------------------------------+
22 
23 class HttpParam;
24 class HttpRequest;
25 class HttpResponse;
26 
27 // +-------------------------------------------------------------------+
28 
29 class HttpServer : public NetServer
30 {
31 public:
32  static const char* TYPENAME() { return "HttpServer"; }
33 
34  HttpServer(WORD port, int poolsize=1);
35  virtual ~HttpServer();
36 
37  int operator == (const HttpServer& l) const { return addr == l.addr; }
38 
39  virtual Text ProcessRequest(Text request, const NetAddr& addr);
40  virtual Text DefaultResponse();
41  virtual Text ErrorResponse();
42 
43  virtual bool DoGet(HttpRequest& request, HttpResponse& response);
44  virtual bool DoPost(HttpRequest& request, HttpResponse& response);
45  virtual bool DoHead(HttpRequest& request, HttpResponse& response);
46 
47  virtual Text GetServerName();
48  virtual void SetServerName(const char* name);
49 
50 protected:
52 };
53 
54 // +-------------------------------------------------------------------+
55 
56 class HttpParam
57 {
58 public:
59  static const char* TYPENAME() { return "HttpParam"; }
60 
61  HttpParam(const char* n, const char* v) : name(n), value(v) { }
62 
63  int operator == (const HttpParam& p) const { return name == p.name; }
64 
67 };
68 
69 // +-------------------------------------------------------------------+
70 
72 {
73 public:
74  static const char* TYPENAME() { return "HttpRequest"; }
75 
76  enum METHOD {
85  };
86 
87  HttpRequest(const char* request=0);
88  ~HttpRequest();
89 
90  operator Text();
91 
92  void ParseRequest(Text request);
93  void ParseCookie(const char* param);
94 
95  int Method() const { return method; }
96  Text URI() const { return uri; }
97  Text Content() const { return content; }
98  Text RequestLine() const { return request_line; }
99 
100  List<HttpParam>& GetQuery() { return query; }
101  List<HttpParam>& GetHeaders() { return headers; }
102  List<HttpParam>& GetCookies() { return cookies; }
103 
104  NetAddr GetClientAddr() const { return client_addr; }
105  void SetClientAddr(const NetAddr& a) { client_addr = a; }
106 
107  Text GetParam(const char* name);
108 
109  Text GetHeader(const char* name);
110  void SetHeader(const char* name, const char* value);
111  void AddHeader(const char* name, const char* value);
112  Text GetCookie(const char* name);
113  void SetCookie(const char* name, const char* value);
114  void AddCookie(const char* name, const char* value);
115 
116  Text DecodeParam(const char* value);
117  static Text EncodeParam(const char* value);
118 
119 private:
120  int method;
121  Text uri;
122  Text content;
123  Text request_line;
124  NetAddr client_addr;
125 
126  List<HttpParam> query;
127  List<HttpParam> headers;
128  List<HttpParam> cookies;
129 };
130 
131 // +-------------------------------------------------------------------+
132 
134 {
135 public:
136  static const char* TYPENAME() { return "HttpResponse"; }
137 
138  enum STATUS {
139  SC_CONTINUE = 100,
141 
142  SC_OK = 200,
143  SC_CREATED = 201,
144  SC_ACCEPTED = 202,
149 
152  SC_FOUND = 302,
157 
167  SC_CONFLICT = 409,
168  SC_GONE = 410,
170 
177  };
178 
179 
180  HttpResponse(int status=500, const char* content=0);
181  HttpResponse(const char* response);
182  ~HttpResponse();
183 
184  operator Text();
185 
186  void ParseResponse(Text request);
187  void ParseCookie(const char* param);
188 
189  int Status() const { return status; }
190  void SetStatus(int s) { status = s; }
191 
192  Text Content() const { return content; }
193  void SetContent(Text t) { content = t; }
194  void AddContent(Text t) { content += t; }
195 
196  List<HttpParam>& GetHeaders() { return headers; }
197  List<HttpParam>& GetCookies() { return cookies; }
198 
199  Text GetHeader(const char* name);
200  void SetHeader(const char* name, const char* value);
201  void AddHeader(const char* name, const char* value);
202  Text GetCookie(const char* name);
203  void SetCookie(const char* name, const char* value);
204  void AddCookie(const char* name, const char* value);
205 
206  void SendRedirect(const char* url);
207 
208 private:
209  int status;
210  Text content;
211 
212  List<HttpParam> headers;
213  List<HttpParam> cookies;
214 };
215 
216 
217 #endif HttpServer_h