/******************************************************************* * * sws.c - Simple program to emulate (rather poorly I * might add) a webserver in order to log what * data a browser sends to it. * * Copyright 2001 by Sean Conner. All Rights Reserved. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * Comments, questions and criticisms can be sent to: sean@conman.org * * ------------------------------------------------------------- * * To use: * * Intended for use under Unix in conjunction with inetd. * Compile the program and modify /etc/inetd.conf * to execute the program on a port of your choice. If * you don't know how to do this, consult with your local * neighborhood sysadmin. * * Have a nice day. * ***********************************************************************/ #include #include #include #include #include #include #define SERVER_NAME "SWS" #define SERVER_VERSION "1.0.0" #define SERVER_OS "Linux/2.0.36" #if 0 # define SERVER SERVER_NAME "/" SERVER_VERSION "(" SERVER_OS ")" #else # define SERVER "Apache/1.3.9 (Unix) PHP/3.0.12" #endif /**********************************************************************/ int sws (FILE *,FILE *,FILE *); void map_file (char *); int get_line (char *,size_t,FILE *); /**********************************************************************/ static char g_starttime[BUFSIZ]; static size_t g_size; static char *g_page = "\n" "\n" " Lorem Ipsum\n" "\n" "\n" "

Lorem Ipsum

\n" "\n" "

\n" "\n" "Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam\n" "nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat\n" "volutpat. Ut wisi enim ad minim veniam, quis nostrud exercitation\n" "ulliam corper suscipit lobortis nisl ut aliquip ex ea commodo\n" "consequat. Duis autem veleum iriure dolor in hendrerit in vulputate\n" "velit esse molestie consequat, vel willum lunombro dolore eu feugiat\n" "nulla facilisis at vero eros et accumsan et iusto odio dignissim qui\n" "blandit praesent luptatum zzril delenit augue duis dolore te feugait\n" "nulla facilisi.\n" "\n" "

\n" "\n" "Li Europan lingues es membres del sam familie. Lor separat\n" "existentie es un myth. Por scientie, musica, sport etc., li tot\n" "Europa usa li sam vocabularium. Li lingues differe solmen in li\n" "grammatica, li pronunciation e li plu commun vocabules. Omnicos\n" "directe al desirabilit^! de un nov lingua franca: on refusa\n" "continuar payar custosi traductores. It solmen va esser necessi far\n" "uniform grammatica, pronunciation e plu sommun paroles.\n" "\n" "

\n" "\n" "Ma quande lingues coalesce, li grammatica del resultant lingue es\n" "plu simplic e regulari quam ti del coalescent lingues. Li nov lingua\n" "franca va esser plu simplic e regulari quam li existent Europan\n" "lingues. It va esser tam simplic quam Occidental: in fact, it va\n" "esser Occidental. A un Angleso it va semblar un simplificat Angles,\n" "quam un skeptic Cambridge amico dit me que Occidental es.\n" "\n" "

\n" "\n" "\n" "\n"; /**********************************************************************/ int main(int argc,char *argv[]) { time_t now; struct tm *ptm; FILE *fplog; now = time(NULL); ptm = gmtime(&now); if (ptm == NULL) ptm = localtime(&now); strftime(g_starttime,sizeof(g_starttime),"%a, %d %b %Y %H:%M:%S GMT",ptm); setvbuf(stdin,NULL,_IONBF,BUFSIZ); setvbuf(stdout,NULL,_IONBF,BUFSIZ); if (argc == 1) { fprintf(stderr,"usage: %s logfile [defaultpage]\n",argv[0]); return(EXIT_FAILURE); } fplog = fopen(argv[1],"a"); if (fplog == NULL) { fprintf(stderr,"%s: could not open %s for output\n",argv[0],argv[1]); return(EXIT_FAILURE); } if (argc == 2) g_size = strlen(g_page); else map_file(argv[2]); sws(stdin,stdout,fplog); fclose(fplog); return(EXIT_SUCCESS); } /***********************************************************************/ int sws(FILE *fpin,FILE *fpout,FILE *fplog) { char buffer [BUFSIZ]; char modtime[BUFSIZ]; time_t now; struct tm *ptm; assert(fpin != NULL); assert(fpout != NULL); while(get_line(buffer,BUFSIZ,fpin)) { fputs(buffer,fplog); fputc('\n',fplog); } fputc('\n',fplog); now = time(NULL); ptm = gmtime(&now); if (ptm == NULL) ptm = localtime(&now); strftime(modtime,sizeof(modtime),"%a, %d %b %Y %H:%M:%S GMT",ptm); fprintf( fpout, "HTTP/1.0 200 Okay, found it!\r\n" "Server: " SERVER "\r\n" "Date: %s\r\n" "Last-Modified: %s\r\n" "Content-type: text/html\r\n" "Content-length: %lu\r\n" "\r\n" "%s", modtime, g_starttime, (unsigned long)g_size, g_page ); return(0); } /***********************************************************************/ void map_file(char *fname) { FILE *fp; long size; char *page; size_t nobjs; assert(fname != NULL); if ((fp = fopen(fname,"r")) == NULL) goto get_file_error; if (fseek(fp,0,SEEK_END) != 0) goto get_file_error; if ((size = ftell(fp)) == -1) goto get_file_error; if (fseek(fp,0,SEEK_SET) != 0) goto get_file_error; if ((page = calloc(size + 1, sizeof(char))) == NULL) goto get_file_error; if ((nobjs = fread(page,sizeof(char),size,fp)) != size) goto get_file_error; fclose(fp); g_size = size; g_page = page; return; get_file_error: fclose(fp); g_size = strlen(g_page); } /***********************************************************************/ int get_line(char *s,size_t size,FILE *fpin) { int c; int clean = 1; assert(s != NULL); assert(size > 0); assert(fpin != NULL); while(size) { c = fgetc(fpin); if (c == EOF) break; if (c == '\r') { c = fgetc(fpin); if (c == '\n') break; ungetc(c,fpin); break; } if (c == '\n') { c = fgetc(fpin); if (c == '\r') break; ungetc(c,fpin); break; } if (iscntrl(c)) c = ' '; clean = isspace(c); *s++ = c; size--; } *s = '\0'; return(!clean); } /*********************************************************************/