void logfile() { FILE *log; printf("Content-type: text/plain \r\n\r\n"); web_client_addfile("./demo.log"); printf("Fim do log\n"); };
int __ILWS_lws_list(char *inpath) { /* for type directory */ /* mime*/ char *ext; struct dirent *dire; DIR *cd; struct stat cfstat; char *dirpath=NULL; char *filepath; char *tmp; char *readfile; float filesize; char filesizeu; //// //printf("ClientInfo->request=<B>%s</B><BR>\n",ClientInfo->request); //readfile=__ILWS_malloc(strlen(ClientInfo->request)+1); readfile=ClientInfo->request; while((tmp=strstr(readfile,"./"))) { // this skip ../ also readfile=tmp+1; }; while((tmp=strstr(readfile,"//"))) { readfile=tmp+1; }; tmp=strstr(readfile,"/"); if(tmp!=NULL) { readfile=tmp+1; // must be in the first }; // skip beind dir if(strlen(readfile)) { filepath=__ILWS_malloc(strlen(inpath)+strlen(readfile)+3); snprintf(filepath,strlen(inpath)+strlen(readfile)+2,"%s%s%s",inpath,(inpath[strlen(inpath)-1]=='/')?"":"/",readfile); //printf("pum ->%s<BR>\n",filepath); if(readfile[strlen(readfile)-1]=='/') { dirpath=__ILWS_malloc(strlen(filepath)+1); memcpy(dirpath,filepath,strlen(filepath)+1); // the 0 included } else { if(!stat(filepath,&cfstat)) { // file must exist if((cfstat.st_mode & S_IFDIR) != S_IFDIR) { // search for mime ext=strrchr(filepath,'.'); tmp=strrchr(filepath,'/'); ext+=1; if(ext<=tmp) { // is not a extension ext=NULL; }; //Wed, 22 Oct 2003 16:04:04 GMT printf("Last-Modified: %s\r\n",__ILWS_date(mktime(gmtime(&cfstat.st_mtime)),"%a, %d %b %Y %H:%M:%S GMT")); // new on 0.5.3 web_client_contenttype(ext); web_client_addfile(filepath); // fopen and write, maybe? __ILWS_free(filepath); return 1; } else { web_client_HTTPdirective("HTTP/1.1 404 File Not Found"); printf("Content-type: text/html\r\n\r\n<HTML><title>file not found</title><body bgcolor=FFFFFF><font size=6>404 FILE NOT FOUND</font><BR><BR><small>The request \"%s\" wasn't found, try this <a href='%s/'>link</a></small><BR><HR><small><i>%s</i></small></body></html>\n\r",filepath,ClientInfo->request,_libwebserver_version); __ILWS_free(filepath); return 0; }; }else { web_client_HTTPdirective("HTTP/1.1 404 File Not Found"); printf("Content-type: text/html\r\n\r\n<HTML><title>file not found</title><body bgcolor=FFFFFF><font size=6>404 FILE NOT FOUND</font><BR><BR><small>The request \"%s\" wasn't found</small><BR><HR><small><i>%s</i></small></body></html>\n\r",filepath,_libwebserver_version); __ILWS_free(filepath); return 0; }; }; __ILWS_free(filepath); }; //printf("Content-type: text/html\r\n\r\n"); //fprintf(stderr,"dirpath=%s inpath=%s\n",dirpath,inpath); if(dirpath==NULL) { dirpath=__ILWS_malloc(strlen(inpath)+1); memcpy(dirpath,inpath,strlen(inpath)+1); }; cd=opendir(dirpath); if(cd!=NULL) { printf("Content-type: text/html\r\n\r\n"); printf("<HTML><HEAD><TITLE>Contents of %s</TITLE></HEAD><BODY>\n",dirpath); printf("<h1>Contents of directory %s</h1><HR>\n",dirpath); printf("<form><input type=text name=match value=\"%s\"><input type=submit name='send' value='wildcard'></form>\n",strlen(ClientInfo->Query("match"))?ClientInfo->Query("match"):"*"); printf("<PRE>\n"); while((dire=readdir(cd))) { if( ((dire->d_name[0]!='.') || (strcmp(dirpath,inpath) && !strcmp(dire->d_name,".."))) && (!fnmatch(ClientInfo->Query("match"),dire->d_name,0) || !strlen(ClientInfo->Query("match"))) ) { filepath=__ILWS_malloc(strlen(dirpath)+strlen(dire->d_name)+2); snprintf(filepath,strlen(dirpath)+strlen(dire->d_name)+2,"%s%s%s",dirpath,(dirpath[strlen(dirpath)-1]=='/')?"":"/",dire->d_name); //fprintf(stderr,"filename=%s\n",filepath); if(!stat(filepath,&cfstat)) { if((cfstat.st_mode & S_IFDIR) == S_IFDIR) { printf("%s <DIR> <a href=\"%s/\">%s</a>\n",__ILWS_date(cfstat.st_mtime,"%a, %d %b %Y %H:%M"),dire->d_name,dire->d_name); }else { filesize=(float)cfstat.st_size; filesizeu=0; while(filesize>1024) { filesize/=1024; filesizeu++; }; printf("%s %.1f%c <a href=\"%s\">%s</a>\n",__ILWS_date(cfstat.st_mtime,"%a, %d %b %Y %H:%M"),filesize,(filesizeu==2)?'M':(filesizeu==1)?'K':'b',dire->d_name,dire->d_name); }; }; __ILWS_free(filepath); }; }; printf("</PRE>\n"); printf("<HR>\n"); printf("<address>%s</address>\n",_libwebserver_version); printf("</BODY></HTML>\r\n"); __ILWS_free(dirpath); closedir(cd); } else { web_client_HTTPdirective("HTTP/1.1 404 File Not Found"); printf("Content-type: text/html\r\n\r\n<HTML><title>file not found</title><body bgcolor=FFFFFF><font size=6>404 FILE NOT FOUND</font><BR><BR><small>The request \"%s\" wasn't found</small><BR><HR><small><i>%s</i></small></body></html>\n\r",dirpath,_libwebserver_version); return 0; }; return 1; };