Skip to content
Snippets Groups Projects
Commit 788ad4f8 authored by Romain Gabet's avatar Romain Gabet :speech_balloon:
Browse files

recherche fichier ok

parent 917e5fa2
Branches
Tags
No related merge requests found
No preview for this file type
File added
File added
...@@ -15,8 +15,10 @@ ...@@ -15,8 +15,10 @@
int copy(FILE* in, FILE* out){ int copy(FILE* in, FILE* out){
int ch; int ch;
if(in && out){ if(in && out){
while((ch =( fgetc(in))!= EOF) ){ ch = fgetc(in);
while(ch != EOF ){
fputc(ch,out); fputc(ch,out);
ch=fgetc(in);
} }
}else{ }else{
//error reading //error reading
...@@ -35,21 +37,31 @@ int get_file_size(int fd){ ...@@ -35,21 +37,31 @@ int get_file_size(int fd){
} }
FILE* check_and_open(const char* target, const char *document_root){ FILE* check_and_open(const char* target, const char *document_root){
char*tmp=""; printf("check and open test\n");
char tmp[300]="";
strcat(tmp,target); strcat(tmp,target);
strcat(tmp,"/"); strcat(tmp,"/");
strcat(tmp,document_root); strcat(tmp,document_root);
printf("testons");
printf("check and open tmp : %s/%s \n tmp : %s \n",target,document_root,tmp);
return fopen(tmp,"r"); return fopen(tmp,"r");
} }
char* rewrite_target(char *target){ char* rewrite_target(char *target){
char* ret = strrchr(target, '?'); char* ret = strrchr(target, '?');
char* rep = malloc(strlen(target)*sizeof(char)); printf("rewrite target ret : %s\n",ret);
if (ret!= NULL){ if(ret != NULL){
size_t i = (int)(strlen(target)-strlen(ret)); char* rep = malloc(strlen(target)*sizeof(char));
rep = strncpy(rep,target,i); size_t i = (int)(strlen(target)-strlen(ret));
} rep = strncpy(rep,target+1,i-1);
return rep; return rep;
}else{
char* rep = malloc(strlen(target)*sizeof(char));
size_t i = (int)(strlen(target));
rep = strncpy(rep,target+1,i);
return rep;
}
} }
...@@ -99,13 +111,13 @@ void send_response(FILE *client, int code, const char *reason_phrase, const char ...@@ -99,13 +111,13 @@ void send_response(FILE *client, int code, const char *reason_phrase, const char
int traitementClient(int socket_client){ int traitementClient(int socket_client){
/* On peut maintenant dialoguer avec le client */ /* On peut maintenant dialoguer avec le client */
char *motd="Votre requète est bonne"; char *motd="Votre requète est bonne";
printf("test"); printf("test\n");
char str[8000]; char str[8000];
http_request request; http_request request;
FILE *fd; FILE *fd;
fd = fdopen(socket_client, "a+"); fd = fdopen(socket_client, "a+");
if(fd == NULL) { if(fd == NULL) {
perror("Error opening file"); perror("Error opening file\n");
exit(1); exit(1);
} }
//fprintf(fd,"pas d'erreurs d'ouverture\n"); //fprintf(fd,"pas d'erreurs d'ouverture\n");
...@@ -118,7 +130,7 @@ int traitementClient(int socket_client){ ...@@ -118,7 +130,7 @@ int traitementClient(int socket_client){
}else{ }else{
send_response(fd, 400, "Bad Request", "Bad request"); send_response(fd, 400, "Bad Request", "Bad request");
} }
}else if (strcmp(request.target, "/test?aled") == 0){ }else if (strcmp(request.target, "/") == 0){
send_response(fd, 200, "OK", motd); send_response(fd, 200, "OK", motd);
//--- //---
}else{ }else{
...@@ -128,16 +140,18 @@ int traitementClient(int socket_client){ ...@@ -128,16 +140,18 @@ int traitementClient(int socket_client){
//partie 7 //partie 7
char* absolute_path = rewrite_target(request.target); char* absolute_path = rewrite_target(request.target);
printf("test - - - - - - - - %s\n",absolute_path); printf("test - - - - - - - - %s\n",absolute_path);
FILE* fichier = check_and_open(absolute_path, "document.txt"); FILE* fichier = check_and_open(absolute_path, "document.txt");
fprintf(fd,"fichier : \n");
if(fichier == NULL){ if(fichier == NULL){
send_response(fd, 404, "Not Found", "Not Found"); send_response(fd, 404, "Not Found2", "Not Found2");
} }else{
int size = get_file_size(fileno(fichier)); int size = get_file_size(fileno(fichier));
if(copy(fichier,fd)==-1){ if(copy(fichier,fd)==-1){
//error //error
} }
fprintf(fd,"Size : %d",size); fprintf(fd,"Size : %d\n",size);
}
fclose(fd); fclose(fd);
return 0; return 0;
} }
No preview for this file type
testons testons
le
fopen
No preview for this file type
testons
le
fopen
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment