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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
|
/*@@
@file Server.c
@date Wed Sep 13 20:10:24 2000
@author Tom Goodale
@desc
Web serving and utility routines.
@enddesc
@version $Header$
@@*/
#include <stdio.h>
#include <stdlib.h>
#include "cctk.h"
#include "util_Hash.h"
#include "httpd.h"
static char *rcsid = "$Header$";
CCTK_FILEVERSION(DevThorns_httpd_Server_c)
/********************************************************************
********************* Local Data Types ***********************
********************************************************************/
typedef struct
{
int (*function)(cGH *, httpRequest *, void *);
void *data;
} httpPage;
/********************************************************************
********************* Local Routine Prototypes *********************
********************************************************************/
static httpPage *CreatePageData(int (*function)(cGH *,httpRequest *, void *), void *data);
static httpPage *FindPage(char *path, char **residual);
/********************************************************************
********************* Other Routine Prototypes *********************
********************************************************************/
/********************************************************************
********************* Local Data *****************************
********************************************************************/
static uHash *pages = NULL;
static const char *notfound_page =
"<HTML>\n<HEAD><TITLE>Error 404: Not Found</TITLE></HEAD>\
<BODY>The URI you requested could not be found</BODY>\n<HTML>\n";
static const char *notimplemented_page =
"<HTML>\n<HEAD><TITLE>Error 501: Not Implemented</TITLE></HEAD>\
<BODY>The requested method is not implemented</BODY>\n<HTML>\n";
#define INITIAL_SIZE 16
/********************************************************************
********************* External Routines **********************
********************************************************************/
/*@@
@routine HTTP_RequestGet
@date Wed Sep 13 20:10:24 2000
@author Tom Goodale
@desc
Routine to deal with a GET request.
@enddesc
@calls
@calledby
@history
@endhistory
@@*/
int HTTP_RequestGET(cGH *cctkGH, httpRequest *request)
{
httpPage *pagedata;
int retval;
char message[1024];
if((pagedata = FindPage(request->uri, &(request->residual))))
{
retval = pagedata->function(cctkGH, request, pagedata->data);
}
else
{
retval = -1;
}
if(retval < 0)
{
strcpy(message,"HTTP/1.0 404 Not Found\r\n");
HTTP_Write(request, message, strlen(message));
strcpy(message,"Content-Type: text/html\r\n\r\n");
HTTP_Write(request, message, strlen(message));
HTTP_Write(request, notfound_page, strlen(notfound_page));
}
return retval;
}
/*@@
@routine HTTP_RequestUnsupported
@date Thu Sep 14 12:18:56 2000
@author Tom Goodale
@desc
@enddesc
@calls
@calledby
@history
@endhistory
@@*/
int HTTP_RequestUnsupported(cGH *cctkGH, httpRequest *request)
{
char message[1024];
strcpy(message,"HTTP/1.0 501 Not Implemented\r\n");
HTTP_Write(request, message, strlen(message));
strcpy(message,"Content-Type: text/html\r\n\r\n");
HTTP_Write(request, message, strlen(message));
HTTP_Write(request, notimplemented_page, strlen(notimplemented_page));
return 0;
}
/*@@
@routine HTTP_RegisterPage
@date Wed Sep 13 20:10:24 2000
@author Tom Goodale
@desc
Routine to register a web page.
@enddesc
@calls
@calledby
@history
@endhistory
@@*/
int HTTP_RegisterPage(const char *path, int (*function)(cGH *, httpRequest *, void *), void *data)
{
int retval;
httpPage *pagedata;
/* Create the hash table if it's not already been created */
if(! pages)
{
pages = Util_HashCreate(INITIAL_SIZE);
}
if(Util_HashData(pages, strlen(path), path, 0))
{
fprintf(stderr, "Page %s already exists\n", path);
retval = -1;
}
else
{
pagedata = CreatePageData(function, data);
if(pagedata)
{
retval = Util_HashStore(pages, strlen(path), path, 0, (void *)pagedata);
}
else
{
retval = -2;
}
}
return 0;
}
/*@@
@routine HTTP_Write
@date Fri Sep 15 18:47:41 2000
@author Tom Goodale
@desc
Writes part or all of an HTTP reply.
@enddesc
@calls
@calledby
@history
@endhistory
@@*/
int HTTP_Write(httpRequest *request, const char *buffer, size_t count)
{
int retval;
/* Currently don't do anything fancy. */
retval = HTTP_Write(request, buffer, count);
return retval;
}
/********************************************************************
********************* Local Routines *************************
********************************************************************/
/*@@
@routine CreatePageData
@date Wed Sep 13 20:10:24 2000
@author Tom Goodale
@desc
Creates a httpPage structure.
@enddesc
@calls
@calledby
@history
@endhistory
@@*/
static httpPage *CreatePageData(int (*function)(cGH *, httpRequest *, void *), void *data)
{
httpPage *pagedata;
pagedata = (httpPage *)malloc(sizeof(httpPage));
if(pagedata)
{
pagedata->function = function;
pagedata->data = data;
}
return pagedata;
}
/*@@
@routine FindPage
@date Wed Sep 13 20:10:24 2000
@author Tom Goodale
@desc
Finds a page, if it exists.
@enddesc
@calls
@calledby
@history
@endhistory
@@*/
static httpPage *FindPage(char *path, char **residual)
{
httpPage *pagedata;
char *position;
char *temp;
pagedata = NULL;
if(pages)
{
/* Check for index.html */
if(path[strlen(path)-1] == '/')
{
#ifdef HTTP_DEBUG
printf("Looking for '%sindex.html'\n", path);
#endif
temp = (char *)malloc(sizeof(path)+11);
sprintf(temp,"%sindex.html", path);
pagedata = Util_HashData(pages, strlen(temp), temp, 0);
*residual = NULL;
free(temp);
}
else
{
#ifdef HTTP_DEBUG
printf("Looking for '%s'\n", path);
#endif
/* Or exact path */
pagedata = Util_HashData(pages, strlen(path), path, 0);
*residual = NULL;
}
if(!pagedata)
{
/* Ok, now cycle through. Know it doesn't end with a slash */
for(position = path+strlen(path)-1; position >= path; position--)
{
if(*position == '/')
{
#ifdef HTTP_DEBUG
printf("Looking for '%s' less '%s' \n", path, position);
#endif
if((pagedata = Util_HashData(pages, position-path, path, 0)))
{
*residual = position+1;
break;
}
}
}
}
}
else
{
pagedata = NULL;
}
return pagedata;
}
|