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 @@
int copy(FILE* in, FILE* out){
int ch;
if(in && out){
while((ch =( fgetc(in))!= EOF) ){
ch = fgetc(in);
while(ch != EOF ){
fputc(ch,out);
ch=fgetc(in);
}
}else{
//error reading
......@@ -35,21 +37,31 @@ int get_file_size(int fd){
}
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,"/");
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");
}
char* rewrite_target(char *target){
char* ret = strrchr(target, '?');
char* rep = malloc(strlen(target)*sizeof(char));
if (ret!= NULL){
size_t i = (int)(strlen(target)-strlen(ret));
rep = strncpy(rep,target,i);
}
return rep;
printf("rewrite target ret : %s\n",ret);
if(ret != NULL){
char* rep = malloc(strlen(target)*sizeof(char));
size_t i = (int)(strlen(target)-strlen(ret));
rep = strncpy(rep,target+1,i-1);
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
int traitementClient(int socket_client){
/* On peut maintenant dialoguer avec le client */
char *motd="Votre requète est bonne";
printf("test");
printf("test\n");
char str[8000];
http_request request;
FILE *fd;
fd = fdopen(socket_client, "a+");
if(fd == NULL) {
perror("Error opening file");
perror("Error opening file\n");
exit(1);
}
//fprintf(fd,"pas d'erreurs d'ouverture\n");
......@@ -118,7 +130,7 @@ int traitementClient(int socket_client){
}else{
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);
//---
}else{
......@@ -128,16 +140,18 @@ int traitementClient(int socket_client){
//partie 7
char* absolute_path = rewrite_target(request.target);
printf("test - - - - - - - - %s\n",absolute_path);
FILE* fichier = check_and_open(absolute_path, "document.txt");
fprintf(fd,"fichier : \n");
if(fichier == NULL){
send_response(fd, 404, "Not Found", "Not Found");
}
int size = get_file_size(fileno(fichier));
if(copy(fichier,fd)==-1){
send_response(fd, 404, "Not Found2", "Not Found2");
}else{
int size = get_file_size(fileno(fichier));
if(copy(fichier,fd)==-1){
//error
}
fprintf(fd,"Size : %d",size);
}
fprintf(fd,"Size : %d\n",size);
}
fclose(fd);
return 0;
return 0;
}
No preview for this file type
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