Starshatter_Open
Open source Starshatter engine
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
HttpServletExec.cpp
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: HttpServletExec.cpp
7  AUTHOR: John DiCamillo
8 
9 
10  OVERVIEW
11  ========
12  Network Server Pump for HTTP Server
13 */
14 
15 
16 #include "MemDebug.h"
17 #include "HttpServletExec.h"
18 #include "HttpServlet.h"
19 #include "NetLayer.h"
20 
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <ctype.h>
24 
25 // +-------------------------------------------------------------------+
26 
28 {
29 public:
31  virtual ~HttpTestServlet() { }
32 
33  virtual bool DoGet(HttpRequest& request, HttpResponse& response)
34  {
35  char buffer[1024];
36  Text content;
37 
38  content = "<html><head><title>HttpTestServlet</title></head>\n";
39  content += "<body bgcolor=\"#c0c0c0\" text=\"black\">\n<h1>HttpTestServlet</h1>\n";
40 
41  content += "<br><h3>HttpSessionId:</h3><p>\n";
42  if (session)
43  content += session->GetID();
44  else
45  content += "No Session Found";
46  content += "<br>\n";
47 
48  content += "<br><h3>URI Requested:</h3><p>\n";
49  content += request.URI();
50  content += "<br>\n";
51 
52  if (request.GetQuery().size() > 0) {
53  content += "<br><h3>Query Parameters:</h3>\n";
54 
55  ListIter<HttpParam> q_iter = request.GetQuery();
56  while (++q_iter) {
57  HttpParam* q = q_iter.value();
58  sprintf_s(buffer, "<b>%s:</b> <i>%s</i><br>\n", q->name.data(), q->value.data());
59  content += buffer;
60  }
61  }
62 
63  content += "<br><h3>Request Headers:</h3>\n";
64  ListIter<HttpParam> h_iter = request.GetHeaders();
65  while (++h_iter) {
66  HttpParam* h = h_iter.value();
67  sprintf_s(buffer, "<b>%s:</b> <i>%s</i><br>\n", h->name.data(), h->value.data());
68  content += buffer;
69  }
70 
71  if (request.GetCookies().size() > 0) {
72  content += "<br><h3>Cookies:</h3>\n";
73  ListIter<HttpParam> c_iter = request.GetCookies();
74  while (++c_iter) {
75  HttpParam* c = c_iter.value();
76  sprintf_s(buffer, "<b>%s:</b> <i>%s</i><br>\n", c->name.data(), c->value.data());
77  content += buffer;
78  }
79  }
80 
81  content += "</body></html>\n\n";
82 
84  response.AddHeader("MIME-Version", "1.0");
85  response.AddHeader("Content-Type", "text/html");
86  response.SetContent(content);
87 
88  return true;
89  }
90 };
91 
92 // +-------------------------------------------------------------------+
93 // +-------------------------------------------------------------------+
94 // +-------------------------------------------------------------------+
95 
96 DWORD WINAPI HttpServletExecSessionProc(LPVOID link);
97 
98 HttpServletExec::HttpServletExec(WORD port, int poolsize)
99  : HttpServer(port, poolsize), session_timeout(60), exec_shutdown(false)
100 {
101  http_server_name = "Generic HttpServletExec 1.0";
102 
103  DWORD thread_id = 0;
104  hsession = CreateThread(0, 4096, HttpServletExecSessionProc,
105  (LPVOID) this, 0, &thread_id);
106 }
107 
109 {
110  if (!exec_shutdown)
111  exec_shutdown = true;
112 
113  WaitForSingleObject(hsession, 1000);
114  CloseHandle(hsession);
115  hsession = 0;
116 
117  sessions.destroy();
118 }
119 
120 // +--------------------------------------------------------------------+
121 
124 {
125  return new(__FILE__,__LINE__) HttpTestServlet;
126 }
127 
128 // +--------------------------------------------------------------------+
129 
132 {
133  HttpSession* session = 0;
134  Text reqID = request.GetCookie("SessionID");
135 
136  if (reqID.length() > 0) {
138  while (++iter && !session) {
139  HttpSession* s = iter.value();
140 
141  if (s->GetID() == reqID) {
142  session = s;
143  session->Access();
144  }
145  }
146  }
147 
148  if (!session) {
149  session = new(__FILE__,__LINE__) HttpSession;
150  if (session) {
151  sessions.append(session);
152 
153  ::Print("HttpServletExec created new session '%s' for request '%s'\n",
154  (const char*) session->GetID(),
155  (const char*) request.RequestLine());
156  }
157  else {
158  ::Print("HttpServletExec out of memory for request '%s'\n",
159  (const char*) request.RequestLine());
160  }
161  }
162 
163  return session;
164 }
165 
166 // +--------------------------------------------------------------------+
167 
168 bool
170 {
171  bool result = false;
172  HttpSession* session = GetSession(request);
173  HttpServlet* servlet = GetServlet(request);
174 
175  if (servlet) {
176  servlet->SetSession(session);
177  result = servlet->Service(request, response);
178  delete servlet;
179  }
180 
181  if (result) {
182  response.SetHeader("Server", http_server_name);
183 
184  if (session)
185  response.SetCookie("SessionID", session->GetID());
186  }
187 
188  return result;
189 }
190 
191 // +--------------------------------------------------------------------+
192 
193 DWORD WINAPI HttpServletExecSessionProc(LPVOID link)
194 {
195  HttpServletExec* exec = (HttpServletExec*) link;
196 
197  if (exec)
198  return exec->CheckSessions();
199 
200  return (DWORD) E_POINTER;
201 }
202 
203 DWORD
205 {
206  while (!exec_shutdown) {
207  sync.acquire();
208 
209  if (sessions.size()) {
211  while (++iter) {
212  HttpSession* s = iter.value();
213 
215  ::Print("HttpServletExec deleting expired session '%s'\n", (const char*) s->GetID());
216  delete iter.removeItem();
217  }
218  }
219  }
220 
221  DoSyncedCheck();
222 
223  sync.release();
224  Sleep(100);
225  }
226 
227  return 0;
228 }
229 
230 void
232 {
233 }