1 /******************************************************************************
5 ** This file is part of the ABYSS Web server project.
7 ** Copyright (C) 2000 by Moez Mahfoudh <mmoez@bigfoot.com>.
8 ** All rights reserved.
10 ** Redistribution and use in source and binary forms, with or without
11 ** modification, are permitted provided that the following conditions
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.
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
33 ******************************************************************************/
45 #include <sys/signal.h>
49 #include "xmlrpc-c/abyss.h"
51 void Answer(TSession *r, uint16_t statuscode, char *buffer)
55 ResponseStatus(r,statuscode);
57 ResponseContentType(r,"text/html");
61 HTTPWrite(r,"<HTML><BODY>",12);
63 HTTPWrite(r,buffer,strlen(buffer));
65 HTTPWrite(r,"</BODY></HTML>",14);
70 abyss_bool HandleTime(TSession *r)
76 if (strcmp(r->uri,"/time")!=0)
79 if (!RequestAuth(r,"Mot de passe","moez","hello"))
83 DateFromGMT(&date,ltime);
86 strcpy(z,"The time is ");
87 DateToString(&date,z+strlen(z));
94 abyss_bool HandleDump(TSession *r)
98 if (strcmp(r->uri,"/name")!=0)
101 sprintf(z,"Server name is %s", (r->server)->name );
107 abyss_bool HandleStatus(TSession *r)
111 if (sscanf(r->uri,"/status/%d",&status)<=0)
114 ResponseStatus(r,(uint16_t)status);
119 abyss_bool HandleMIMEType(TSession *r)
123 if (strncmp(r->uri,"/mime/",6)!=0)
126 m=MIMETypeFromExt(r->uri+6);
136 static void sigterm(int sig)
138 TraceExit("Signal %d received. Exiting...\n",sig);
144 sigchld(int const signalClass) {
146 abyss_bool childrenLeft;
152 /* Reap defunct children until there aren't any more. */
153 while (childrenLeft && !error) {
156 rc = waitpid((pid_t) -1, &status, WNOHANG);
159 childrenLeft = false;
161 /* because of ptrace */
162 if (errno == EINTR) {
163 /* ptrace causes this */
167 /* We reaped a child. */
168 pid_t const pid = rc;
169 ThreadHandleSigchld(pid);
177 int main(int argc,char **argv)
180 char *p,*conffile=DEFAULT_CONF_FILE;
181 abyss_bool err=FALSE;
186 if ((*p=='-') && (*(p+1)))
216 ServerCreate(&srv,"HTTPServer",80,DEFAULT_DOCS,NULL);
218 ConfReadServerFile(conffile,&srv);
220 ServerAddHandler(&srv,HandleTime);
221 ServerAddHandler(&srv,HandleDump);
222 ServerAddHandler(&srv,HandleStatus);
223 ServerAddHandler(&srv,HandleMIMEType);
228 /* Catch various termination signals. */
229 signal(SIGTERM,sigterm);
230 signal(SIGINT,sigterm);
231 signal(SIGHUP,sigterm);
232 signal(SIGUSR1,sigterm);
234 /* Catch defunct children. */
235 signal(SIGCHLD,sigchld);
238 ServerDaemonize(srv);