0

My load function loads everything except for cat.jpg and favicon.ico, cat.html will display cat.jpg though. I am unsure how to test or even begin fixing this. If anyone has any tips on how to debug or sees a problem with my code. Any help would be much appreciated thanks.

bool load(FILE* file, BYTE** content, size_t* length)
{

*content = NULL;
*length = 0;

if(file == NULL)
{
    perror("SOME ERROR");
}
char* buffer = NULL;

    int i = 0;

    for (int c = fgetc(file);c != EOF;c = fgetc(file))
    { 
        //continually allocate memory until end of file
        buffer = (char *) realloc(buffer,sizeof(char)*(i + 1));
        if(buffer == NULL)
        {
            return false;    
        }
        buffer[i] = (char) c;
        i++;
    }
    *content = &buffer[0];
    *length = i; 

return true;
}

const char* lookup(const char* path)
{
    //find '.' in path assign from '.' to NULL to needle 
    char* needle = strstr(path, ".");

    char* ext = calloc(strlen(needle),sizeof(needle));
    //temp to transfer contents from needle to ext
    char temp;
    //convert string to lowercase
    for(int i = 0;i < strlen(needle);i++)
    {
        temp = needle[i];
        temp = tolower(temp);
        ext[i] = temp;
    }

    //compare needle to all file extenstion 
    if(strcasecmp(ext,".css") == 0)
    {
        free(ext);
        return "text/css";
    }
    else if(strcmp(ext,".html") == 0){
        free(ext);
        return "text/html";
    }
    else if(strcmp(ext,".gif") == 0){
        free(ext);
        return "text/gif";
    }
    else if(strcmp(ext,".ico") == 0){
        free(ext);
        return "text/x-icon";
    }
    else if(strcmp(ext,".jpg") == 0){
        free(ext);
        return "text/jpeg";
    }
    else if(strcmp(ext,".js") == 0){
        free(ext);
        return "text/javascript";
    }
    else if(strcmp(ext,".php") == 0){
        free(ext);
        return "text/x-php";
    }
    else if(strcmp(ext,".png") == 0){
        free(ext);
        return "text/png";
    }
free(ext);
return NULL;
}

bool parse(const char* line, char* abs_path, char* query)
    {
    //var method to store GET
    char method[5];

    //set method to null for concatenation
    *method = 0;

    //set method to string starting with GET 
    char* temp = strstr(line,"GET ");
    //char* temp = "GETabc";

    if(temp == NULL){
        error(405); 
        return false;
    }
    //concatenate method to null terminator
    strncat(method,temp,4);

    if (*line != 'G')
        {
        error(405);
        return false;
    }
    else if (strcmp(method,"GET ") != 0)
        {
        error(405);
        return false;
   }

   //REQUEST_TARGET

    //point to first space in line
    char* request_target = strchr(line,' ');
    //move past sp in n
    request_target += 1;

    //test if request_target starts with '/'
    if(*request_target != '/'){
        error(501);
        return false;
    }

    //test for open quotes (") in request_target
    char* request_testval = strchr(request_target,'"');
    if (request_testval != NULL){
        error(400); 
        return false;
    }

    //HTTP VERSION

    //point to first sapce in n 
    char* httpver = strchr(request_target,' ');

    if(strcmp(httpver," HTTP/1.1\r\n") != 0){
        error(505);
        return false;
    }


    //bytes in (n) minus everything after SP indicating http version
    int bytes = httpver - request_target;
    //int len = strlen(abs_path);
    *abs_path = '\0';
    //contatenate null with bytes from n
    strncat(abs_path,request_target,bytes);

    //QUERY 

    *query = '\0';
    //temp val for query
    char* tempq = strchr(line,'?');
   //move past ?
   if(tempq != NULL){
        tempq += 1;
        //bytes for query 
        int bytesq = httpver - tempq;
        strncat(query,tempq,bytesq);
    }

return true;
}
6
1

Right. I won't analyze the other functions because I've found problems with lookup():

Read carefully what you're returning here:

return "text/jpeg";
return "text/gif";
return "text/x-icon";

Now read VERY CAREFULLY what the sandbox link is saying you should return.

If this solves your problem, please click the check mark to accept the answer.

1
  • 1
    Thanks for the help what a dumb mistake. You're the best – robot bath Nov 25 '16 at 1:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .