1

here are all of my functions and the output I don't know exactly what's wrong but I am guessing it's something with either load or indexes cause server1 check50 works fine I tried using gdb to pinpoint my problem but I don't know what input to give to the server

load:

bool load(FILE* file, BYTE** content, size_t* length){
if(file == NULL) return false;
// initiate the array, its max len and current len 
BYTE buffer = 0;
length = 0;
int max_length = 1;
*content = malloc(max_length * sizeof(BYTE));
if(*content == NULL){
    return false;
}
while(true){
    buffer = fgetc(file);
    if(buffer == EOF) break;
    (*content)[(*length)++] = buffer;
    if(*length >= max_length){
        max_length *= 2;
        *content = realloc(*content, max_length);
        if(*content == NULL){
            return false;
        }
    }
}
*content = realloc(*content, *length * sizeof(BYTE));
if(*content == NULL) return false;
return true;
}

indexes:

char* indexes(const char* path){
if(access(path, R_OK) == -1){
    return NULL;
}
int path_len = strlen(path);
bool slash = false;
if(path[path_len - 1] == '/'){
    slash = true;
} else {
    path_len++;
}
char* fin_path = malloc(path_len + 1);
strcpy(fin_path, path);
if(!slash){
    fin_path[path_len - 1] = '/';
    fin_path[path_len] = '\0';
}

char* php = "index.php";
fin_path = realloc(fin_path, path_len + strlen(php) + 1);
strcat(fin_path, php);
if(access(fin_path, R_OK) != -1){
    return fin_path;
}

char* html = "index.html";
fin_path = realloc(fin_path, path_len + strlen(html) + 1);
strcat(fin_path, html);
if(access(fin_path, R_OK) != -1){
    return fin_path;
}
free(fin_path);
return NULL;
}

lookup:

const char* lookup(const char* path)
{
    const char* ext = strrchr(path, '.');
    if(ext != NULL){
        char* body = NULL;
        char* result = NULL;
        if(strcasecmp(ext, ".css") == 0){
            body = "text/css";
        } 
        else if (strcasecmp(ext, ".html") == 0){
            body = "text/html";
        }
        else if (strcasecmp(ext, ".gif") == 0){
            body = "image/gif";
        }
        else if (strcasecmp(ext, ".ico") == 0){
            body = "image/x-icon";
        }
        else if (strcasecmp(ext, ".jpg") == 0){
            body = "image/jpeg";
        }
        else if (strcasecmp(ext, ".js") == 0){
            body = "text/javascript";
        }
        else if (strcasecmp(ext, ".php") == 0){
            body = "text/x-php";
        }
        else if (strcasecmp(ext, ".png") == 0){
            body = "image/png";
        }
        else{
            return NULL;
        }
        result = malloc(strlen(body) + 1);
        strcpy(result, body);
        return result;
    }
    return NULL;
}

parse:

bool parse(const char* line, char* abs_path, char* query)
{
    // make sure the line has two spaces
    const char* haystack = line;
    char* firstSP = strchr(haystack, ' ');
    if(firstSP == NULL){
        error(400);
        return false;
    }
    char* secondSP = strchr((firstSP + 1), ' ');
    if(secondSP == NULL){
        error(400);
        return false;
    }

    // making sure there are no extra spaces
    if(strchr((secondSP + 1), ' ') != NULL){
        error(400);
        return false;
    }

    // checking the method is equal to "GET"
    int methodLen = firstSP - haystack;
    if (strncmp(haystack, "GET", methodLen) != 0){
        error(405);
        return false;
    }

    // checking the httpversion and the termination of the lin
    if(strcmp((secondSP + 1), "HTTP/1.1\r\n") != 0){
        error(505);
        return false;
    }

    // isolating the reqline
    int reqTarLen = secondSP - (firstSP + 1);
    char reqTar[reqTarLen + 1];
    strncpy(reqTar, (firstSP + 1), reqTarLen);
    reqTar[reqTarLen] = '\0';
    if(reqTar[0] != '/'){
        error(501);
        return false;
    }
    if(strchr(reqTar,'\"') != NULL){
        error(400);
        return false;
    }

the out put I got was:

:) server.c exists
:) server compiles
:( Requesting cat.jpg returns 200, image/jpeg, and correct image
:( Requesting cat.html returns 200, text/html, and correct file
:( Requesting cat2.HTML returns 200, text/html, and correct file
:( Requesting cat3.HtMl returns 200, text/html, and correct file
:( Requesting cat.gif returns 200, image/gif, and correct file
:( Requesting favicon.ico returns 200, image/x-icon, and correct file
:( Requesting test.css returns 200, text/css, and correct file
:( Requesting test.js returns 200, text/javascript, and correct file
:) Requesting hello.php returns 200, text/html, and correct output
:) Requesting hello.php? returns 200, text/html, and correct output
:( Requesting hello.php?name=Alice returns 200, text/html, and correct output
   \ expected output, but not "X-Powered-By: PHP/5.5.3\r\n"
:) Requesting /test redirects to /test/
:( Requesting /test/ outputs /test/index.html
:) Requesting directory containing index.php outputs index.php
:( Requesting two files in a row (cat.html then cat.jpg) succeeds
https://sandbox.cs50.net/checks/4fdfb14952ff43d1b7e1de3c619f22e1

update: sorry for earlier this is my first time using the stackexchange

3
  • 1
    To down-voters: Even though you have the right to down-vote a question, when you do so, it would be really helpful to also leave a comment as to why you did so. It's not constructive otherwise. To Amr: You should include more than just your code and expect for answers. State exactly what the problem is, and the output you get. Also what did you try to solve the problem. Be a little more descriptive, and then, I'm sure everyone will be eager to help you.
    – ChrisG
    Aug 3 '16 at 5:51
  • I am sorry for earlier. It's my first time using stackexchange so I didn't know what is appropriate and what's not. I added my output and my trial to fix it and how I wasn't so successful doing it. and thank you Aug 3 '16 at 14:00
  • @AmrAboulkhair when you say "weird output", specifically what kind of output are you getting? Sep 30 '16 at 18:24
0

A least you can see the expected an your output clicking on https://sandbox.cs50.net you can see such link after running the check50 .

After use the debug50 ./server public With putting the breakpoints inside the function load and step by step looking how your variables changed.

You must log in to answer this question.

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