Starshatter_Open
Open source Starshatter engine
Main Page
Classes
Files
File List
File Members
All
Classes
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Macros
HttpServlet.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: HttpServlet.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 "
HttpServlet.h
"
18
#include "
NetLayer.h
"
19
20
#include <stdlib.h>
21
#include <stdio.h>
22
#include <ctype.h>
23
24
// +-------------------------------------------------------------------+
25
26
HttpServlet::HttpServlet
()
27
: session(0)
28
{ }
29
30
HttpServlet::~HttpServlet
()
31
{ }
32
33
// +--------------------------------------------------------------------+
34
35
bool
36
HttpServlet::Service
(
HttpRequest
& request,
HttpResponse
& response)
37
{
38
bool
result =
false
;
39
40
switch
(request.
Method
()) {
41
case
HttpRequest::HTTP_GET
:
42
result =
DoGet
(request, response);
43
break
;
44
45
case
HttpRequest::HTTP_POST
:
46
result =
DoPost
(request, response);
47
break
;
48
49
case
HttpRequest::HTTP_HEAD
:
50
result =
DoHead
(request, response);
51
break
;
52
53
default
:
54
break
;
55
}
56
57
return
result;
58
}
59
60
// +--------------------------------------------------------------------+
61
62
bool
63
HttpServlet::DoGet
(
HttpRequest
& request,
HttpResponse
& response)
64
{
65
return
false
;
66
}
67
68
bool
69
HttpServlet::DoPost
(
HttpRequest
& request,
HttpResponse
& response)
70
{
71
return
DoGet
(request, response);
72
}
73
74
bool
75
HttpServlet::DoHead
(
HttpRequest
& request,
HttpResponse
& response)
76
{
77
if
(
DoGet
(request, response)) {
78
int
len = response.
Content
().
length
();
79
80
char
buffer[256];
81
sprintf(buffer,
"%d"
, len);
82
response.
SetHeader
(
"Content-Length"
, buffer);
83
response.
SetContent
(
""
);
84
85
return
true
;
86
}
87
88
return
false
;
89
}
90
91
// +--------------------------------------------------------------------+
92
// +--------------------------------------------------------------------+
93
// +--------------------------------------------------------------------+
94
95
HttpSession::HttpSession
()
96
{
97
id
=
GenerateUniqueID
();
98
access_time
=
NetLayer::GetUTC
();
99
}
100
101
HttpSession::~HttpSession
()
102
{
103
attributes
.
destroy
();
104
}
105
106
// +--------------------------------------------------------------------+
107
108
Text
109
HttpSession::GetAttribute
(
const
char
* name)
110
{
111
ListIter<HttpParam>
iter =
attributes
;
112
while
(++iter) {
113
HttpParam
* p = iter.
value
();
114
115
if
(p->
name
== name)
116
return
p->
value
;
117
}
118
119
return
Text
();
120
}
121
122
void
123
HttpSession::SetAttribute
(
const
char
* name,
const
char
* value)
124
{
125
ListIter<HttpParam>
iter =
attributes
;
126
while
(++iter) {
127
HttpParam
* p = iter.
value
();
128
129
if
(p->
name
== name) {
130
p->
value
= value;
131
return
;
132
}
133
}
134
135
HttpParam
* param =
new
(__FILE__,__LINE__)
HttpParam
(name, value);
136
if
(param)
137
attributes
.
append
(param);
138
}
139
140
void
141
HttpSession::DelAttribute
(
const
char
* name)
142
{
143
ListIter<HttpParam>
iter =
attributes
;
144
while
(++iter) {
145
HttpParam
* p = iter.
value
();
146
147
if
(p->
name
== name) {
148
delete
iter.
removeItem
();
149
return
;
150
}
151
}
152
}
153
154
// +--------------------------------------------------------------------+
155
156
int
157
HttpSession::GetIntAttribute
(
const
char
* name)
158
{
159
ListIter<HttpParam>
iter =
attributes
;
160
while
(++iter) {
161
HttpParam
* p = iter.
value
();
162
163
if
(p->
name
== name) {
164
int
result = ::atoi(p->
value
.
data
());
165
return
result;
166
}
167
}
168
169
return
0;
170
}
171
172
void
173
HttpSession::SetIntAttribute
(
const
char
* name,
int
value)
174
{
175
char
buf[32];
176
sprintf(buf,
"%d"
, value);
177
178
ListIter<HttpParam>
iter =
attributes
;
179
while
(++iter) {
180
HttpParam
* p = iter.
value
();
181
182
if
(p->
name
== name) {
183
p->
value
= buf;
184
return
;
185
}
186
}
187
188
HttpParam
* param =
new
(__FILE__,__LINE__)
HttpParam
(name, buf);
189
if
(param)
190
attributes
.
append
(param);
191
}
192
193
void
194
HttpSession::DelIntAttribute
(
const
char
* name)
195
{
196
DelAttribute
(name);
197
}
198
199
// +--------------------------------------------------------------------+
200
201
Text
202
HttpSession::GenerateUniqueID
()
203
{
204
char
unique[17];
205
206
for
(
int
i = 0; i < 16; i++) {
207
char
c = rand() % 25 +
'a'
;
208
unique[i] = c;
209
}
210
211
unique[16] = 0;
212
return
unique;
213
}
214
215
// +--------------------------------------------------------------------+
216
217
void
218
HttpSession::Access
()
219
{
220
access_time
=
NetLayer::GetUTC
();
221
}
222
223
NetEx
HttpServlet.cpp
Generated on Tue Jun 5 2012 20:46:17 for Starshatter_Open by
1.8.1