0

I feel like I'm going in circles trying to parse the request line into its constituents. It looks like method and requesttarget are ok, but I keep getting too many characters for HTTPver. For "GET / HTTP/1.1", it tells me that the length of HTTPver is 10 when it should be 8. I've tried changing the number of characters I read in with strncopy, but get no where. Can you spot an error in the code?

// extract request's request-line // http://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html const char* haystack = request; char* needle = strstr(haystack, "\r\n"); if (needle == NULL) { error(400); continue; } else if (needle - haystack + 2 > LimitRequestLine) { error(414); continue; }
char line[needle - haystack + 2 + 1]; strncpy(line, haystack, needle - haystack + 2); line[needle - haystack + 2] = '\0';

        // log request-line
        printf("%s", line);
        char* needle1 = strchr(line,32);
        if (needle1 == NULL)
        {
            error(400);
            continue;
        }
        char method[needle1-line+2+1];
        strncpy(method,line,needle1-line);
        method[needle1-line+2]='\0';
        printf("The method is %s.  The length is %lu.\n", method,strlen(method));//test line
        char* needle2 = strrchr(line,32);
        if (needle2 == NULL)
        {
            error(400);
            continue;
        }
        char requesttarget[needle2-needle1+2];
        strncpy(requesttarget,needle1+1,needle2-needle1-1);
        requesttarget[needle2-needle1+1]='\0';
        printf("The request-target is %s; it is %lu in length.\n", requesttarget,strlen(requesttarget));//test line

        char HTTPver[strlen(line)-(line-needle2)+2];
        strncpy(HTTPver,needle2+1,strlen(line)-(line-needle2)-1);
        HTTPver[strlen(line)-(line-needle2)+1]='\0';
        printf("The version is %s!\n", HTTPver);//test line 
0

Ok. I figured out that I was not taking into account the "\r\n" at the end of the line. Here is what I changed for HTTPver:

char requesttarget[needle2-needle1+2]; strncpy(requesttarget,needle1+1,needle2-needle1-1); requesttarget[needle2-needle1+1]='\0'; printf("The request-target is %s; it is %lu in length.\n", requesttarget,strlen(requesttarget));//test line

        char* needle3 = strstr(line,"\r\n");
        if (needle3 == NULL)
        {
            error(400);
            continue;
        }
        char HTTPver[needle3-needle2];
        strncpy(HTTPver,needle2+1,needle3-needle2-1);
        HTTPver[needle3-needle2-1]='\0';
        printf("The version is %s! The length is %lu.\n", HTTPver, strlen(HTTPver));//test line

This seems to give me the right answer. Now, I'm having problems with validation for absolute-path containing a period and segmentation fault.

When I run Valgrind to see the cause of the segmentation fault, the program is not reading the method correctly and I get a 405 error from server.c.

| improve this answer | |

You must log in to answer this question.

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