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