Skip to content
Snippets Groups Projects
Select Git revision
  • e3d1e54bd5b5ce8e2a843101caf1ef39c6685262
  • master default protected
  • revert-a279f36e
  • tag11-static-site
  • 10-404-clean
  • 08-simple-get|&&|09-404
  • 07-fdopen
  • 06-SIGCHLD
  • 05-fork1
  • 04-SIGPIPE
  • 03-REUSEADDR
  • 02-serveur-tcp-simple
  • 01-mise-en-place
13 results

client.c

Blame
  • 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){