0

I am having an issue with my parse function when running check50 where requesting cat.exe is returning error 404 as opposed to error 501.

I've taken a look at the other questions related to cat.exe errors posted on here and have come to the conclusion that my abs_path is likely wrong but am having some trouble pinpointing why.

I am also quite confused as to why abc/hello.php returns 501 but cat.exe returns 404.

Any help is greatly appreciated! I have included my parse function below for reference.

bool parse(const char* line, char* abs_path, char* query)
{
    if(line == NULL)
    {
        error(501);
        return false;
    }
    printf("%s\n", line);
    // allocates memory to a char array called method of size line
    char* method = malloc(sizeof(line));
    if (method == NULL)
    {
        error(404);
        return false;
    }
    // allocate memory to temp_query
    char* temp_query = malloc(sizeof(line));
    if (temp_query == NULL)
    {
        error(404);
        return false;
    }
    // iterate over line
    for (int i = 0, k = strlen(line); i < k; i++)
    {
        // break if line reaches whitespace
        if(line[i] == ' ')
        {
            break;
        }
        // copies char from line to method at array index i
        else
        {
            method[i] = line[i];
        }
    }
    // compares method to GET returning 501 if not equal
    if (strcmp(method, "GET") != 0)
    {
        error(405);
        return false;
    }

    // checks line to see if HTTP/1.1 is in it returning error 505 if not
    char* http_check = strstr(line, "HTTP/1.1");
    if (http_check == NULL)
    {
        error(505);
        return false;
    }    

    // checks abs_path to make sure it's not NULL
    if (abs_path == NULL)
    {
        error(501);
        return false;
    }

    char* request_target = strchr(line, ' ');
    if (request_target == NULL)
    {
        error(501);
        return false;
    }

    else
        printf("request target[1] is /\n");
    for (int i = 1, k = strlen(request_target); i < k; i++)
    {
        if (request_target[i] == ' ')
        {
            break;
        }
        if (request_target[i] == '"')
        {
            error(400);
        }
        else if(request_target[i] == '?')
        {
            strcpy(temp_query, &request_target[i+1]);
        }
        else
        {
            abs_path[i-1] = request_target[i];
        }
    }
    if (abs_path[0] != '/')
    {
        error(501);
        return false;
    }        
    else
        printf("abs path starts with /\n");
    printf("%s\n", abs_path);


    temp_query = strchr(line, '?');
    if (temp_query == NULL)
    {
        error(501);
        return false;
    }
    for(int i = 1, k = strlen(temp_query); i < k; i++)
    {
        if(temp_query[i] == ' ')
        {
            break;
        }
        else
        {
            query[i-1] = temp_query[i];
        }
    }

    return true;
}
1

Just wanted to give an update on this. Upon further inspection and running through my parse function line by line, I have solved my issue above.

Essentially my attempt to code in query made it so that my code was always looking for the "?" char and upon not finding it was returning 501.

Thanks to anyone who took the time to look at my code and question! Onto properly implementing something for the query portion now.

You must log in to answer this question.

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