int FindIndexFile(struct AmmServer_Instance * instance,char * webserver_root,char * directory,char * indexfile,unsigned int indexFileLength) { unsigned int unused=0; //TODO : This code can become much better and avoid re making all the strings again and again and again.. snprintf(indexfile,indexFileLength,"%s%sindex.html",webserver_root,directory); ReducePathSlashes_Inplace(indexfile); //strcpy(indexfile,webserver_root); strcat(indexfile,directory); strcat(indexfile,"index.html"); ReducePathSlashes_Inplace(indexfile); if ((cache_FindResource(instance,indexfile,&unused))||(FileExistsAmmServ(indexfile))) { return 1; } snprintf(indexfile,indexFileLength,"%s%sindex.htm",webserver_root,directory); ReducePathSlashes_Inplace(indexfile); //strcpy(indexfile,webserver_root); strcat(indexfile,directory); strcat(indexfile,"index.htm"); ReducePathSlashes_Inplace(indexfile);// <- TODO : notice that i can just change the extension to reduce copying around if ((cache_FindResource(instance,indexfile,&unused))||(FileExistsAmmServ(indexfile))) { return 1; } //strcpy(indexfile,webserver_root); strcat(indexfile,directory); strcat(indexfile,"home.htm"); ReducePathSlashes_Inplace(indexfile); // <- TODO : notice that i can just change the extension to reduce copying around //if ((cache_FindResource(instance,indexfile,&unused))||(FileExistsAmmServ(indexfile))) { return 1; } //strcpy(indexfile,webserver_root); strcat(indexfile,directory); strcat(indexfile,"home.html"); ReducePathSlashes_Inplace(indexfile);// <- TODO : notice that i can just change the extension to reduce copying around //if ((cache_FindResource(instance,indexfile,&unused))||(FileExistsAmmServ(indexfile))) { return 1; } //strcpy(indexfile,webserver_root); strcat(indexfile,directory); strcat(indexfile,"index.php"); ReducePathSlashes_Inplace(indexfile);// <- TODO : notice that i can just change the extension to reduce copying around //if ((cache_FindResource(instance,indexfile,&unused))||(FileExistsAmmServ(indexfile))) { return 1; } indexfile[0]=0; return 0; }
void decideAboutHowToHandleRequestedResource ( struct AmmServer_Instance * instance, struct HTTPTransaction * transaction, char * servefile, int * resource_is_a_directory, int * resource_is_a_file , int * resource_is_a_template , int * generate_directory_list ) { *resource_is_a_template=0; // STEP 0 : Is the resource an obvious directory , or obvious file ? Check for it..! if ( isResourceADirectory(transaction->incomingHeader.resource , strlen(transaction->incomingHeader.resource) ) ) { *resource_is_a_directory=1; } strncpy(servefile,transaction->incomingHeader.verified_local_resource,MAX_FILE_PATH); //servefile variable now contains just the verified_local_resource as generated by http_header_analysis.c //we have checked transaction->incomingHeader.verified_local_resource for .. , weird ascii characters etc, so it should be safe for usage from now on..! //There are some virtual files we want to re-route to their real path..! if (cache_ChangeRequestIfTemplateRequested(instance,servefile,MAX_FILE_PATH/*,instance->templates_root*/) ) { //Skip disk access times for checking for directories and other stuff..! //We know that the resource is a file from our cache indexes..! *resource_is_a_directory=0; *resource_is_a_file=1; *resource_is_a_template=1; } //STEP 0 : Check with cache! if (cache_ResourceExists(instance,servefile,&transaction->resourceCacheID) ) { //Skip disk access times for checking for directories and other stuff..! //We know that the resource is a file from our cache indexes..! //Bonus points we now have the index id of the cached instance of the file for future use.. *resource_is_a_directory=0; *resource_is_a_file=1; } else {//Start of file not in cache scenario // STEP 1 : If we can't obviously determine if the request is a directory , lets check on disk to find out if it is a directory after all..! if (!*resource_is_a_directory) { if (DirectoryExistsAmmServ(servefile)) { *resource_is_a_directory=1; //If the resource had an / in the string end we would have already come to the conclusion that this was a directory // so lets append the slash now on the request to help the code that follows work as intended strcat(transaction->incomingHeader.resource,"/"); strcat(servefile,"/"); fprintf(stderr,"TODO: this path is a little problematic because the web browser on the client will think\n"); fprintf(stderr,"that we are on the / directory , a location field in the response header will clarify things\n"); } } // STEP 2 : If we are sure that we dont have a directory then we have to find out accessing disk , could it be that our client wants a file ? if ( (!*resource_is_a_directory) && (FileExistsAmmServ(servefile)) ) { *resource_is_a_file=1; } // STEP 3 : If after these steps the resource turned out to be a directory , we cant serve raw directories , so we will either look for an index.html // and if an index file cannot be found we will generate a directory list and send that instead..! if (*resource_is_a_directory) { // *resource_is_a_directory means we got something like directory1/directory2/ so we should check for index file at the path given..! if ( FindIndexFile(instance,instance->webserver_root,transaction->incomingHeader.resource,servefile,MAX_FILE_PATH) ) { /*servefile should contain a valid index file , lets make it look like it was a file we wanted all along :P! */ *resource_is_a_directory=0; *resource_is_a_file=1; } else { *generate_directory_list=1; } } } //End of file not in cache scenario }
int AmmServer_FileExists(const char * filename) { return FileExistsAmmServ(filename); }