Select Git revision
-
Romain Gabet authoredRomain Gabet authored
client.c 3.67 KiB
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include "socket.h"
#include <netinet/in.h>
#include <sys/wait.h>
#include <stdlib.h>
#include "client.h"
#include "http_parse.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int copy(FILE* in, FILE* out){
int ch;
if(in && out){
ch = fgetc(in);
while(ch != EOF ){
fputc(ch,out);
ch=fgetc(in);
}
}else{
//error reading
exit(1);
}
return 0;
}
int get_file_size(int fd){
struct stat sb;
if(fstat(fd,&sb)==-1){
perror("stat");
exit(1);
}
return sb.st_size;
return 0;
}
FILE* check_and_open(const char* target, const char *document_root){
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, '?');
// 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;
}
}
char* fgets_or_exit(char * buffer, int size, FILE *fd){
char * ret = fgets(buffer,size,fd);
if(ret){