summaryrefslogtreecommitdiffstats
path: root/cpukit/httpd/webmain.c
blob: c9c0b9cc15511f9338ee3256810274324f3ac537 (plain) (blame)
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
/*
 * main.c -- Main program for the GoAhead WebServer (RTEMS version)
 *
 * Copyright (c) Go Ahead Software Inc., 1995-1999. All Rights Reserved.
 *
 * See the file "license.txt" for usage and redistribution license requirements
 *
 *  $Id$
 */

/******************************** Description *********************************/

/*
 *	Main program for for the GoAhead WebServer. This is a demonstration
 *	main program to initialize and configure the web server.
 */

/********************************* Includes ***********************************/

#include	"uemf.h"
#include	"wsIntrn.h"
#include	<signal.h>
#include	<sys/time.h>
#include 	<pthread.h>

#include	<rtems.h>
#include	<rtems/error.h>

#ifdef WEBS_SSL_SUPPORT
#include	"websSSL.h"
#endif

#ifdef USER_MANAGEMENT_SUPPORT
#include	"um.h"
void	formDefineUserMgmt(void);
#endif

/*********************************** Locals ***********************************/
/*
 *	Change configuration here
 */

extern const char *tftpServer;
/* static char_t		*rootWeb = T("goahead");			* Root web directory */
static char_t		*password = T("");				/* Security password */
static int			port = 80;						/* Server port */
static int			retries = 5;					/* Server port retries */
static int			finished;						/* Finished flag */

/*
 *	Structure to hold timer events
 */
typedef struct {
	void	(*routine)(long arg);	/* Timer routine */
	long	arg;					/* Argument to routine */
} websTimer_t;

/* The following holds the pointer to an allocated websTimer_t structure .
 * Using this method only one timer can be active at a time, but
 * for the WebServer, this should be OK. 
 */
websTimer_t *tp;

/****************************** Forward Declarations **************************/

static int 	initWebs();
static int	aspTest(int eid, webs_t wp, int argc, char_t **argv);
static void formTest(webs_t wp, char_t *path, char_t *query);
static int  websHomePageHandler(webs_t wp, char_t *urlPrefix, char_t *webDir,
				int arg, char_t* url, char_t* path, char_t* query);
static void timerProc(int signo);
#if B_STATS
static void printMemStats(int handle, char_t *fmt, ...);
static void memLeaks();
#endif
static timer_t timer_id;
static void rtems_httpd_daemon();
 
/*********************************** Code *************************************/
/*
 *	Main -- entry point from RTEMS
 */
int rtems_initialize_webserver()
{
  rtems_status_code   sc;
  rtems_id            tid;
  int		      priority;

  /***********************************************************************
   * Default HTTPD priority.
   **********************************************************************/
  priority = 40;
  
  sc = rtems_task_create(rtems_build_name('H', 'T', 'P', 'D'),
			 priority, 8*1024,
			 RTEMS_PREEMPT | RTEMS_NO_TIMESLICE | RTEMS_NO_ASR |
			 RTEMS_INTERRUPT_LEVEL(0),
			 RTEMS_NO_FLOATING_POINT | RTEMS_LOCAL,
			 &tid);
   if (sc != RTEMS_SUCCESSFUL)
   {
      return(RTEMS_UNSATISFIED);
   }

   sc = rtems_task_start(tid, rtems_httpd_daemon, 0);
   if (sc != RTEMS_SUCCESSFUL)
   {
      return(RTEMS_UNSATISFIED);
   }   

   return(RTEMS_SUCCESSFUL);

}

static void
rtems_httpd_daemon()
{
/*
 *	Initialize the memory allocator. Allow use of malloc and start with a 
 *	10K heap.
 */
	bopen(NULL, (10 * 1024), B_USE_MALLOC);

/*
 *	Initialize the web server
 */
	if (initWebs() < 0) {
	  rtems_panic("Unable to initialize Web server !!\n");
	}

#ifdef WEBS_SSL_SUPPORT
	websSSLOpen();
#endif

/*
 *	Basic event loop. SocketReady returns true when a socket is ready for
 *	service. SocketSelect will block until an event occurs. SocketProcess
 *	will actually do the servicing.
 */
	while (!finished) {
	  if (socketReady(-1) || socketSelect(-1, 2000)) {
			socketProcess(-1);
	  }
	  /*websCgiCleanup();*/
	  emfSchedProcess();
	}

#ifdef WEBS_SSL_SUPPORT
	websSSLClose();
#endif

#ifdef USER_MANAGEMENT_SUPPORT
	umClose();
#endif

/*
 *	Close the socket module, report memory leaks and close the memory allocator
 */
	websCloseServer();
	websDefaultClose();
	socketClose();
	symSubClose();
#if B_STATS
	memLeaks();
#endif
	bclose();
        rtems_task_delete( RTEMS_SELF );
}

/******************************************************************************/
/*
 *	Initialize the web server.
 */

static int initWebs()
{
	struct hostent*	hp;
	struct in_addr	intaddr;
	char			host[128], webdir[128];
	char_t			wbuf[128];

/*
 *	Initialize the socket subsystem
 */
	socketOpen();

/*
 *	Define the local Ip address, host name, default home page and the 
 *	root web directory.
 */
	if (gethostname(host, sizeof(host)) < 0) {
		error(E_L, E_LOG, T("Can't get hostname"));
		return -1;
		}
	
/*	intaddr.s_addr = (unsigned long) hostGetByName(host); */
	if ((hp = gethostbyname(host)) == NULL) {
		error(E_L, E_LOG, T("Can't get host address"));
		return -1;
	}
	memcpy((char *) &intaddr, (char *) hp->h_addr_list[0],
		(size_t) hp->h_length);

#if 0
/*
 *	Set /TFTP/x.y.z.w/goahead as the root web. Modify to suit your needs
 */
	sprintf(webdir, "/TFTP/%s/%s", tftpServer, rootWeb);
#else
	sprintf(webdir, "/");
#endif
/*
 *	Configure the web server options before opening the web server
 */
	websSetDefaultDir(webdir);
	ascToUni(wbuf, inet_ntoa(intaddr), sizeof(wbuf));
	websSetIpaddr(wbuf);
	ascToUni(wbuf, host, sizeof(wbuf));
	websSetHost(wbuf);

/*
 *	Configure the web server options before opening the web server
 */
#if 0
	websSetDefaultPage(T("default.asp"));
#else
	websSetDefaultPage(T("index.html"));
#endif
	websSetPassword(password);

/* 
 *	Open the web server on the given port. If that port is taken, try
 *	the next sequential port for up to "retries" attempts.
 */
	websOpenServer(port, retries);

/*
 * 	First create the URL handlers. Note: handlers are called in sorted order
 *	with the longest path handler examined first. Here we define the security 
 *	handler, forms handler and the default web page handler.
 */
	websUrlHandlerDefine(T(""), NULL, 0, websSecurityHandler, 
		WEBS_HANDLER_FIRST);
	websUrlHandlerDefine(T("/goform"), NULL, 0, websFormHandler, 0);
	websUrlHandlerDefine(T(""), NULL, 0, websDefaultHandler, 
		WEBS_HANDLER_LAST); 

/*
 *	Now define two test procedures. Replace these with your application
 *	relevant ASP script procedures and form functions.
 */
	websAspDefine(T("aspTest"), aspTest);
	websFormDefine(T("formTest"), formTest);

/*
 *	Create a handler for the default home page
 */
	websUrlHandlerDefine(T("/"), NULL, 0, websHomePageHandler, 0); 
	return 0;
}

/******************************************************************************/
/*
 *	Test Javascript binding for ASP. This will be invoked when "aspTest" is
 *	embedded in an ASP page. See web/asp.asp for usage. Set browser to 
 *	"localhost/asp.asp" to test.
 */

static int aspTest(int eid, webs_t wp, int argc, char_t **argv)
{
	char_t	*name, *address;

	if (ejArgs(argc, argv, T("%s %s"), &name, &address) < 2) {
		websError(wp, 400, T("Insufficient args\n"));
		return -1;
	}
	return websWrite(wp, T("Name: %s, Address %s"), name, address);
}
/******************************************************************************/
/*
 *	Test form for posted data (in-memory CGI). This will be called when the
 *	form in web/asp.asp is invoked. Set browser to "localhost/asp.asp" to test.
 */

static void formTest(webs_t wp, char_t *path, char_t *query)
{
	char_t	*name, *address;

	name = websGetVar(wp, T("name"), T("Joe Smith")); 
	address = websGetVar(wp, T("address"), T("1212 Milky Way Ave.")); 

	websHeader(wp);
	websWrite(wp, T("<body><h2>Name: %s, Address: %s</h2>\n"), name, address);
	websFooter(wp);
	websDone(wp, 200);
}

/******************************************************************************/
/*
 *	Create a timer to invoke the routine in "delay" milliseconds.
 */

void *emfCreateTimer(int delay, void (*routine)(long arg), 	long arg)
{
/* this variable is only used in the if'ed 0 section below */
#if 0
	struct sigaction	act;
#endif
	struct itimerspec its = { {0,0}, {0,0} };
	struct sigevent se;
	int 	status;

	if ((tp = balloc(B_L, sizeof(websTimer_t)))) {
		tp->routine = routine;
		tp->arg = arg;
	}
	else {
		return NULL;
	}

	se.sigev_notify = SIGEV_THREAD;
	se.sigev_value.sival_ptr = tp;
	se.sigev_notify_function = (void (*)(union sigval)) timerProc;

	/*
	 * NOT POSIX?
	 * se.sigev_notify_attributes = NULL;
	 */


	status = timer_create(CLOCK_REALTIME, &se, &timer_id);
	if (status != 0) {
		bfree(B_L, tp);
		return NULL;
	}
	/* convert delay millisecs to secs and usecs required by struct */
	its.it_value.tv_sec = delay / 1000;
	its.it_value.tv_nsec = (delay % 1000) * 1000000;

	status = timer_settime(timer_id, 0, &its, 0);
	if (status != 0) {
		bfree(B_L, tp);
		return NULL;
	}
	
#if 0
	act.sa_flags = 0;
	sigemptyset(&act.sa_mask);
	act.sa_handler = timerProc;
	sigaction(SIGALRM, &act, NULL);

	/* convert delay millisecs to secs and usecs required by struct */
	its.it_value.tv_sec = delay / 1000;
	its.it_value.tv_usec = (delay % 1000) * 1000;

	if (setitimer(ITIMER_REAL, &its, NULL) ==  -1) {
		bfree(B_L, tp);
		return NULL;
	}
#endif
	return tp;
}

/******************************************************************************/
/*
 *	Delete a timer
 */

void emfDeleteTimer(void * id)
{
	websTimer_t *wtp;
	/*struct itimerval its = { {0,0}, {0,0} };*/

	wtp = (websTimer_t *)id;
	/*	setitimer(ITIMER_REAL, &its, NULL);*/
	timer_delete(timer_id);
	bfree(B_L, wtp);
}

/******************************************************************************/
/*
 *	Timer handler
 */

static void timerProc(int signo)
{
	websTimer_t wtp = *tp;

/* Copy the timer structure to a local first and delete it before calling
 * the function, since the function could create another timer.  In this 
 * implementation, only one timer can be allocated at a time.
 */

	bfree(B_L, tp);
	(wtp.routine)(wtp.arg);
}

/******************************************************************************/
/*
 *	Home page handler
 */

static int websHomePageHandler(webs_t wp, char_t *urlPrefix, char_t *webDir,
	int arg, char_t* url, char_t* path, char_t* query)
{
/*
 *	If the empty or "/" URL is invoked, redirect default URLs to the home page
 */
	if (*url == '\0' || gstrcmp(url, T("/")) == 0) {
#if 0
		websRedirect(wp, T("home.asp"));
#else
		websRedirect(wp, T("index.html"));
#endif
		return 1;
	}
	return 0;
}

/******************************************************************************/

#if B_STATS
static void memLeaks() 
{
	int		fd=1;

	/* if ((fd = gopen(T("leak.txt"), O_CREAT | O_TRUNC | O_WRONLY)) >= 0) { */
		bstats(fd, printMemStats);
		/*
		close(fd);
	}
		*/
}

/******************************************************************************/
/*
 *	Print memory usage / leaks
 */

static void printMemStats(int handle, char_t *fmt, ...)
{
	va_list		args;
	char_t		buf[256];

	va_start(args, fmt);
	vsprintf(buf, fmt, args);
	va_end(args);
	write(handle, buf, strlen(buf));
}
#endif

/*****************************************************************************/

/*****************************************************************************/
/*
 *	Default error handler.  The developer should insert code to handle
 *	error messages in the desired manner.
 */

void defaultErrorHandler(int etype, char_t *msg)
{
#if 1
	write(1, msg, gstrlen(msg));
#endif
}

/*****************************************************************************/
/*
 *	Trace log. Customize this function to log trace output
 */

void defaultTraceHandler(int level, char_t *buf)
{
/*
 *	The following code would write all trace regardless of level
 *	to stdout.
 */
#if 1
	if (buf) {
		write(1, buf, gstrlen(buf));
	}
#endif
}

/*****************************************************************************/
/*
 *	Returns a pointer to an allocated qualified unique temporary file name.
 *	This filename must eventually be deleted with bfree();
 */

char_t *websGetCgiCommName()
{
	char_t	*pname1, *pname2;

	pname1 = tempnam(NULL, T("cgi"));
	pname2 = bstrdup(B_L, pname1);
	free(pname1);
	return pname2;
}