/* * FTP/HTTP daemon * loadedfile.cc Copyright (C) 1995, 96 Alex Belits * * This source/code is public free; you can distribute it and/or modify * it under terms of the GNU General Public License (published by the * Free Software Foundation) either version two of this License, or any * later version. * */ #include "fhttpd.h" char *headerwithoutkeepalive="HTTP/1.0 200 OK\r\nServer: " SERVERSOFTWARE "\r\n"; char *headerkeepalive="Connection: Keep-Alive\r\nKeep-Alive: max=0, timeout=30\r\n"; LoadedFile::LoadedFile(char *xpath,char *xrealname,__s32 xaddress){ char prefixbuffer[1024],*typeline=NULL,numbuffer[40],filetimeline[80]; int h; struct stat statbuf; *filetimeline=0; address=xaddress; longvalue=address; h=open(xrealname,O_RDONLY); if(h>=0){ filetime=0; if(!fstat(h,&statbuf)){ filetime=statbuf.st_mtime; linefromtime(filetimeline,filetime); size=statbuf.st_size; if(size){ TypeArgs *currtype=(TypeArgs*)types.start; while(currtype){ if(wildmat(currtype->absolute?xrealname:xpath,currtype->pattern)){ typeline=currtype->typeline; currtype=NULL; } if(currtype) currtype=(TypeArgs*)currtype->next; } sprintf(numbuffer,"%lu",size); strcpy(prefixbuffer,headerwithoutkeepalive); if(typeline){ strcat(prefixbuffer,"Content-Type: "); strcat(prefixbuffer,typeline); strcat(prefixbuffer,"\r\n"); } if(*filetimeline){ strcat(prefixbuffer,"Last-Modified: "); strcat(prefixbuffer,filetimeline); strcat(prefixbuffer,"\r\n"); } strcat(prefixbuffer,"Content-Length: "); strcat(prefixbuffer,numbuffer); strcat(prefixbuffer,"\r\n"); headerwithoutkeepalivesize=strlen(prefixbuffer); strcat(prefixbuffer,headerkeepalive); keepalivesize=strlen(headerkeepalive); strcat(prefixbuffer,"\r\n"); prefixbufferlength=strlen(prefixbuffer); buffer=(char*)malloc(size+prefixbufferlength); if(buffer){ lseek(h,0,SEEK_SET); strcpy(buffer,prefixbuffer); if(read(h,buffer+prefixbufferlength,size)!=size){ free(buffer); size=0; prefixbufferlength=0; headerwithoutkeepalivesize=0; keepalivesize=0; }else{ size+=prefixbufferlength; if(xpath) path=(char*)malloc(strlen(xpath)+1); if(xrealname) realname=(char*)malloc(strlen(xrealname)+1); if(path&&realname){ strcpy(path,xpath); strcpy(realname,xrealname); charvalue=realname; charvaluelength=strlen(realname); }else{ if(path) free(path); if(realname) free(realname); free(buffer); size=0; } } }else size=0; } }else size=0; close(h); }else size=0; } LoadedFile::~LoadedFile(void){ charvalue=NULL; charvaluelength=0; if(size){ free(buffer); free(path); free(realname); } }