0

Below is my parse function in server.c pset6; The code is not fully commented yet, my bad; Nonetheless, why are all exit code 0 in check50, where is the loophole? Thank you.

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

    char * version = " HTTP/1.1";
    char * method = NULL;
    char * target = NULL;
    char x = '?', y = '/', z = '"';

    int n = strlen(query);
    int p = strlen(target);
    int a = strlen(line);
    int b = strlen(method);
    int c = strlen(version);
    int d = (a - b);
    int e = (d - c);
    int q = (p - n);

    if(line != NULL)
    {
        if(strchr(line, z) == 0)
        {
            error(400);
        }

        strncpy(method, line, 3);
        method[3] = '\0';

        if(strstr(method, "GET") != 0)
        {
            error(405);
        }
        else if(strstr(method, "GET") == 0)
        {
            for(int i = 0; i <= e; i++)
            {
            strncpy(target, line+4, d);
            }

            while(target != NULL)
            {

                if(strchr(target, z) == 0)
                {
                   error(400);
                }

                if(target[1] != y)
                {
                    error(501);
                }
                if(strstr(target, version) != 0)
                {
                   error(505); 
                }

                query = strchr(target, x);
                strncpy(abs_path, target, q);

                if(strchr(abs_path, '.') != 0)
                {
                    error(501);
                }

            }
        }

    }
    error(501);
    return false;
}

1 Answer 1

0

I have to tell you, with some regret, that you are a little far from the correct solution, look at the following statement:

char * method = NULL;
char * target = NULL;

Something later in your program you do:

int p = strlen(target);

To the function strlen we pass a pointer, but in your case the pointer is null, hence Segmentation fault. If you want you can see my answer in the link:

cs50.stackexchange.com/questions/22217/pset6-parse-help-what-does-it-want-me-to-do/22221#22221

3
  • Thanks you very much @MARS. I read your link and it was pretty informative. I chose the strtok() option. It however only corrects the bits about the method. I don't clearly understand why it keep.s returning that error(405) for the request and the version.
    – Salva Tore
    Commented Dec 10, 2016 at 23:12
  • It is necessary to know the functions well in order to use it properly, from man pages: "The strtok() function breaks a string into a sequence of zero or more nonempty tokens. On the first call to strtok() the string to be parsed should be specified in str. In each subsequent call that should parse the same string, str must be NULL". There are people to whom this can be enough, I (who am a little clumsy) what I do are small programs try to check how these functions really work.
    – MARS
    Commented Dec 11, 2016 at 13:39
  • Below is a link to my recent improvement with only a question. Thank you. Why is strtok() not picking the '?' character from line? - Stack Overflow stackoverflow.com/questions/41090434/…
    – Salva Tore
    Commented Dec 11, 2016 at 20:00

You must log in to answer this question.

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