0

My server fails the last check in check50-server2 - "request two files in a row" Because it only fails that test I'm assuming the problem is somewhere in the parse or lookup functions. Valgrind shows that there are no memory leaks but that there are uninitialized variables, but as far as i can tell they are initialized before i use them. I'm not sure wha the problem is. Any ideas or suggestion would be really helpful! thank you!

Here is the code for the lookup function:

const char* lookup(const char* path)
{    

    const char ch = '.';
    char* dot = strrchr(path, ch);

    // test that dot is not NULL pointer "\0"
    if (dot == NULL)
    {
        return NULL;
    }

    dot++;

    char ch2 = '\0';
    char* term = strrchr(path, ch2); 
    int len = term - dot - 1; 
    char* suffix = malloc(sizeof(char) * (len + 1));


    /** get the part after the last . operator into a new string
    char suffix[5];
    int i = 0;
    */
    int i = 0; 
    while (*(dot + i) != '\0') 
    {
       *(suffix + i) = *(dot + i);
       i++;
    }
    *(suffix + i) = '\0';

    const char* css = "text/css";
    const char* html = "text/html";
    const char* js = "text/javascript";
    const char* php = "text/x-php";
    const char* gif = "image/gif";
    const char* ico = "image/x-icon";
    const char * jpg = "image/jpeg";
    const char* png = "image/png";

    // compare the new string to the path options
    if (strcasecmp(suffix, "css") == 0)
    {
        free(suffix);
        return css;
    }
    else if (strcasecmp(suffix, "html") == 0)
    {
        free(suffix);
        return html;
    }
    else if (strcasecmp(suffix, "js") == 0)
    {
        free(suffix);
        return js;
    }
    else if (strcasecmp(suffix, "php") == 0)
    {
        free(suffix);
        return php;
    }
    else if (strcasecmp(suffix, "gif") == 0)
    {
        free(suffix);
        return gif;   
    }
    else if (strcasecmp(suffix, "ico") == 0)
    {
        free(suffix);
        return ico;
    }
    else if (strcasecmp(suffix, "jpg") == 0)
    {
        free(suffix);
        return jpg;    
    }
    else if (strcasecmp(suffix, "png") == 0)
    {
        free(suffix);
        return png;     
    }
    else 
    {
        free(suffix);
        return NULL;
    }
}

here is the code for the parse function:

bool parse(const char* line, char* abs_path, char* query)
{

    char* start = strchr(line, ' ');
    char* end = strrchr(line, ' ');
    char* question = strchr(line, '?');
    char* car = strchr(line, '\r');

    // check for all the characters above
    if ((start == NULL) || (end == NULL) || (car == NULL) || (start == end))
    {
        error(400);
        return false; 
    }

    //set up i, j variables
    int i = 0;
    int j= 0; 

    // set method
    int l = start - line + 1;
    char* method = calloc(l + 1, sizeof(char));
    for (i = 0; i < l; i++)
    {
        *(method + i) = *(line + i);    
    }
    *(method + i + 1) = '\0';

    // check compatability of method
    if (strcmp(method, "GET ") != 0)
    {
        error(405);
        free(method);
        return false;
    }

    free(method);
    // set up abs_path 

    // check for extra spaces
    if (*(line + i) == ' ')
    {
        error(400);
        return false;
    }

    // check for predicate '/'

    if (*(line + i) != '/')
    {
        error(501);
        return false;
    }

    // check if there is a question mark
    if (question == NULL)
    {
        // set query to \0
        *query = '\0';

        // populate abs_ path unti next space
        while (*(line + i) != ' ')
        {
            if (*(line + i) == '"')
            {
                error(400);
                return false;
            }
            else
            {
                *(abs_path + j) = *(line + i);
                i++;
                j++;
            }    
        }
        *(abs_path + j) = '\0';
    }

    // if there is ? -> populate *abs_path until ?
    else
    {
        while(*(line + i) != '?')
        {
            if (*(line + i) == '"')
            {
                error(400);
                return false;
            }
            else
            {
                *(abs_path + j) = *(line + i);
                i++;
                j++;
            }
        }
        *(abs_path + j) = '\0';

        // skip the ?
        i++;
        // initialize j
        j = 0; 

        while (*(line + i) != ' ')
        {
            // check for ' " '
            if (*(line + i) == '"')
            {
                error(400);
                return false;
            }
            else
            {
                *(query + j) = *(line + i);
                i++;
                j++;
            }
        }
        *(query + j) = '\0';
    }


    // set up HTTP Version
    i++; 
    j = 0;
    int l2 = car - end - 1; 
    char* http = malloc(sizeof(char) * (l2 + 1));

    while ((line + i) != car)
    {
        *(http +j) = *(line + i);
        i++;
        j++;
    }
    *(http + l2) = '\0';

    // check http for compatability with version 1.1
    if (strcmp(http, "HTTP/1.1") != 0)
    {
        error(505);
        free(http);
        return false; 
    }

    free(http);
    return true; 
}
0

I'll ask some questions... you have to answer me so you'll see the mistake by yourself.

Given this block of your code:

char* start = strchr(line, ' ');
int i = 0;
int l = start - line + 1;
char* method = (char*) malloc(sizeof(char) * (l + 1));
for (i = 0; i < l; i++)
{
    *(method + i) = *(line + i);    
}
*(method + i + 1) = '\0';

Tell me:

  1. Given a valid request line, what's the value of l ?
  2. If you think of method like it was an array, how many "boxes" of 1 byte will it have? And what will be the index of the last box?
  3. What will be the value of i when the loop breaks?
  4. Given that value of i, tell me exactly at which index this '\0' will be inserted: *(method + i + 1) = '\0';

If this is enough to solve your problem, please click the check mark to accept the answer.

  • Thank you so much! I had the same setup with the http var and there i used l2. as soon as i changed i + 1 to l (which here equals i) everything worked fine. Thank you! – lyric enav Nov 25 '16 at 2:40
  • Great! Good luck on the next pset! – Yuri Laguardia Nov 25 '16 at 2:41

You must log in to answer this question.

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