0

I have updated this according to your suggestion. And it is giving different results everytime with check50.sometimes it says some are right and other times others are right. I am totally confused Please help can anyone please help me with my parse function from pset6. Please help fast It is not working properly Heres my code

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

        int count = 0;
        int a = 0;
        int b = 0;
        int c = 0;
        const char* space = malloc(3*sizeof(char));
        const char space1 = ' ';
        space = &space1 ;
        const char* crawler = strstr(line,space);
        const char* sec_crawl = strrchr(crawler,' ');
        for(int i = 0,len = strlen(line); i < len; i++)
        {
            if(line[i] == ' ')
            {
                count++;
                if(a == 0)
                {
                    a = i;
                }
                else
                {
                   if(b == 0)
                   {
                       b = i;
                   }
                   else
                   {
                       if(c == 0)
                       {
                           c = i;
                       }
                   }
                }
            }
        }
        int j = a + 1;
        int k = b - 1;


        while(j < k)
        {
            if((int)line[j] == 34 )
            {
                error(400);
                return false;
            }

            j++;

        }

        if(line[0] != 'G' || line[1] != 'E' || line[2] != 'T')
        {
            error(405);
            return false;
        }
        else if(count != 2 || line[4] != '/')
        {
            error(400);
            return false;
        }
        else if(sec_crawl[1] !='H' || sec_crawl[2] != 'T' || sec_crawl[3] != 'T' || sec_crawl[4] != 'P' 
                || sec_crawl[5] != '/' || sec_crawl[6] != '1' || sec_crawl[7] != '.' || sec_crawl[8] != '1' )
        {
            error(505);
            return false;
        }
        else
        {
            int p = 1;
            int z = 0;
            while(crawler[p] != '?' || crawler[p] != ' ')
            {
                abs_path[z] = crawler[p];
                p++;
                z++;
            }
            const char* query1 = strchr(crawler,'?');
            if(query1 != NULL)
            {
                int r = 0;
                int g = 1;
                while(query1[g] != ' ')
                {
                    query[r] = query1[g];
                    r++;
                    g++;
                }
            }
            return true;
        }



    }

Hey i have updated my code to this thanks for your help but it is still not working help me out please. its giving this output

1

From man strstr:

The strstr() function finds the first occurrence of the substring needle in the string haystack.

If you run server under gdb, set a break at parse, and evaluate crawler and sec_crawler at this line for(int i = 0,len = strlen(line); i < len; i++), what do you think the result will be? Spoiler alert: You will discover they are the same. Why? The pointer that strstr returns (crawler) includes the space; const char* sec_crawl = strstr(crawler,space); finds that same space.

If I am reading your code correctly, you use the variable count to count spaces. A valid request line contains exactly two spaces. Why are you testing for 3?

In a valid request-line, the character after "HTTP/1.1" is '\r', not space. This test sec_crawl[9] != ' ' in your big sec_crawl else-if will always be true (after you "fix" sec_crawl).

"Crawling" char-by-char to parse the request line is definitely one approach to this pset. However, it can be an awesome opportunity to learn how to use the string functions (especially the ones mentioned in the spec) to create more readable, more elegant, faster code. It is also a fabulous opportunity to exercise your gdb muscles.

I have only pointed out some of the low-hanging fruit. This parse function is not complete. It does not properly terminate abs_path, and query is not built at all. If you get to the point where all tests pass check50:server1 except cat.exe, that would indicate a "bad" abs_path. If you have new issue after you make fixes, you should post a new question with the specific problems in the specific functions. Here's some tips on using gdb to help you troubleshoot your code.

2
  • hey man thanks for your help but it is not doing the work even now
    – Karan Garg
    Apr 27 '16 at 6:42
  • abs_path and query are not properly null terminated. Use gdb to troubleshoot your function. If you have new issues after you make fixes, you should post a new question with specific problems. Apr 27 '16 at 10:48

You must log in to answer this question.

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