initial load of upstream version 1.06.32
[xmlrpc-c] / lib / abyss / src / main.c
1 /******************************************************************************
2 **
3 ** main.c
4 **
5 ** This file is part of the ABYSS Web server project.
6 **
7 ** Copyright (C) 2000 by Moez Mahfoudh <mmoez@bigfoot.com>.
8 ** All rights reserved.
9 **
10 ** Redistribution and use in source and binary forms, with or without
11 ** modification, are permitted provided that the following conditions
12 ** are met:
13 ** 1. Redistributions of source code must retain the above copyright
14 **    notice, this list of conditions and the following disclaimer.
15 ** 2. Redistributions in binary form must reproduce the above copyright
16 **    notice, this list of conditions and the following disclaimer in the
17 **    documentation and/or other materials provided with the distribution.
18 ** 3. The name of the author may not be used to endorse or promote products
19 **    derived from this software without specific prior written permission.
20 ** 
21 ** THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 ** ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25 ** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 ** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 ** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 ** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 ** SUCH DAMAGE.
32 **
33 ******************************************************************************/
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <time.h>
38 #include <fcntl.h>
39
40 #ifdef WIN32
41 #include <io.h>
42 #endif  /* WIN32 */
43
44 #ifdef _UNIX
45 #include <sys/signal.h>
46 #include <sys/wait.h>
47 #endif
48
49 #include "xmlrpc-c/abyss.h"
50
51 void Answer(TSession *r, uint16_t statuscode, char *buffer)
52 {
53     ResponseChunked(r);
54
55     ResponseStatus(r,statuscode);
56
57     ResponseContentType(r,"text/html");
58
59     ResponseWrite(r);
60     
61     HTTPWrite(r,"<HTML><BODY>",12);
62     
63     HTTPWrite(r,buffer,strlen(buffer));
64
65     HTTPWrite(r,"</BODY></HTML>",14);
66
67     HTTPWriteEnd(r);
68 }
69
70 abyss_bool HandleTime(TSession *r)
71 {
72     char z[50];
73     time_t ltime;
74     TDate date;
75
76     if (strcmp(r->uri,"/time")!=0)
77         return FALSE;
78
79     if (!RequestAuth(r,"Mot de passe","moez","hello"))
80         return TRUE;
81
82     time( &ltime );
83     DateFromGMT(&date,ltime);
84     
85
86     strcpy(z,"The time is ");
87     DateToString(&date,z+strlen(z));
88
89     Answer(r,200,z);
90
91     return TRUE;
92 }
93
94 abyss_bool HandleDump(TSession *r)
95 {
96     char z[50];
97
98     if (strcmp(r->uri,"/name")!=0)
99         return FALSE;
100
101     sprintf(z,"Server name is %s", (r->server)->name );
102     Answer(r,200,z);
103
104     return TRUE;
105 }
106
107 abyss_bool HandleStatus(TSession *r)
108 {
109     uint32_t status;
110
111     if (sscanf(r->uri,"/status/%d",&status)<=0)
112         return FALSE;
113
114     ResponseStatus(r,(uint16_t)status);
115
116     return TRUE;
117 }
118
119 abyss_bool HandleMIMEType(TSession *r)
120 {
121     char *m;
122
123     if (strncmp(r->uri,"/mime/",6)!=0)
124         return FALSE;
125
126     m=MIMETypeFromExt(r->uri+6);
127     if (!m)
128         m="(none)";
129
130     Answer(r,200,m);
131
132     return TRUE;
133 }
134
135 #ifdef _UNIX
136 static void sigterm(int sig)
137 {
138     TraceExit("Signal %d received. Exiting...\n",sig);
139 }
140
141
142
143 static void
144 sigchld(int const signalClass) {
145
146     abyss_bool childrenLeft;
147     abyss_bool error;
148
149     childrenLeft = true;
150     error = false;
151
152     /* Reap defunct children until there aren't any more. */
153     while (childrenLeft && !error) {
154         int status;
155         pid_t rc;
156         rc = waitpid((pid_t) -1, &status, WNOHANG);
157
158         if (rc == 0)
159             childrenLeft = false;
160         else if (rc < 0) {
161             /* because of ptrace */
162             if (errno == EINTR) {
163                 /* ptrace causes this */
164             } else
165                 error = true;
166         } else {
167             /* We reaped a child. */
168             pid_t const pid = rc;
169             ThreadHandleSigchld(pid);
170         }
171     }
172 }
173 #endif _UNIX
174
175
176
177 int main(int argc,char **argv)
178 {
179     TServer srv;
180     char *p,*conffile=DEFAULT_CONF_FILE;
181     abyss_bool err=FALSE;
182     char *name=argv[0];
183
184     while (p=*(++argv))
185     {
186         if ((*p=='-') && (*(p+1)))
187             if (*(p+2)=='\0')
188                 switch (*(p+1))
189                 {
190                 case 'c':
191                     argv++;
192                     if (*argv)
193                         conffile=*argv;
194                     else
195                         err=TRUE;
196                     break;
197                 default:
198                     err=TRUE;
199                 }
200             else
201                 err=TRUE;
202         else
203             err=TRUE;
204     };
205
206     if (err)
207     {
208         help(name);
209         exit(1);
210     };
211
212     DateInit();
213
214     MIMETypeInit();
215
216     ServerCreate(&srv,"HTTPServer",80,DEFAULT_DOCS,NULL);
217
218     ConfReadServerFile(conffile,&srv);
219
220     ServerAddHandler(&srv,HandleTime);
221     ServerAddHandler(&srv,HandleDump);
222     ServerAddHandler(&srv,HandleStatus);
223     ServerAddHandler(&srv,HandleMIMEType);
224
225     ServerInit(&srv);
226
227 #ifdef _UNIX
228     /* Catch various termination signals. */
229     signal(SIGTERM,sigterm);
230     signal(SIGINT,sigterm);
231     signal(SIGHUP,sigterm);
232     signal(SIGUSR1,sigterm);
233
234     /* Catch defunct children. */
235     signal(SIGCHLD,sigchld);
236 #endif
237
238     ServerDaemonize(srv);
239
240     ServerRun(&srv);
241
242     return 0;
243 }