I've heard you guys rock here, so here's my first question:
My server.c code passes all tests but for one,
:( Requesting hello.php? returns 200, text/html, and correct output
\ expected output, not an exit code of 0
The server segfaults when I request /hello.php?. I've narrowed it down to a peculiar finding, when I ran valgrind on it and made that request it showed me this:
GET /hello.php? HTTP/1.1
==8442== Invalid read of size 1
==8442== at 0x4C2E0E2: strlen (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==8442== by 0x402765: parse (server.c:822)
==8442== by 0x40191A: main (server.c:189)
==8442== Address 0x0 is not stack'd, malloc'd or (recently) free'd
==8442==
It seems that line is not initialized at this point. When I connect and have the server print the contents of line as it is passed into the parse function, nothing shows up whatever request I make...but the server gives me the cat picture, says Hello Kathleen, etc.
Here's my parse code, let me know what you make of this mess! Much obliged :)
bool parse(const char* line, char* abs_path, char* query){
printf("^%s^ line\n", line);
char* linestr = malloc(sizeof(BYTES) * (strlen(line) + 1));
strcpy (linestr, line);
//printf("***%s*&\n", linestr);
char* first_space = strchr(linestr, ' ') + 1;
char* next_space = strchr(first_space, ' ');
//printf("*%s* first_space\n", first_space);
// printf("$%s$ next_space\n", next_space);
if ((first_space[0] == ' ') || (next_space[1] == ' ')) {
error(400);
//printf("error400\n");
free(linestr);
return false;
}
else {
//method is 1st token
char* method = strtok(linestr, " ");
//make sure method is GET
if(strcmp(method, "GET") != 0) {
free(linestr);
error(405);
return false;
}
else {
//request-line is next token
char* request_line = strtok (NULL, " ");
//printf("$%s$ requestline\n", request_line);
//http version is next token
char* http_version = strtok (NULL, "\r\n");
//if request_line is null, strtok found two spaces OR if there's a " in request line, error 400
if (strchr(request_line, '"') != NULL) {
free(linestr);
error(400);
return false;
}
//requestline must start with /
else if(request_line[0] != '/') {
free(linestr);
error(501);
return false;
}
//now check http version
else if (strcmp(http_version, "HTTP/1.1") != 0) {
free(linestr);
error(505);
return false;
}
else {
//if there's a query present
if(strchr(request_line, '?') != NULL) {
//tokenize query on ?
//set absolute path
char* absolute_path = strtok(request_line, "?");
//printf("(%s) absolutepath with query\n", absolute_path);
char* possible_query = strtok(NULL, "?");
//printf("$%s$ possiblequery\n", possible_query);
//if query has a word after the ?
if(strlen(possible_query) > 3) {
//abs_path stays the same
strcpy(query, possible_query);
strcpy(abs_path, absolute_path);
free(linestr);
return true;
}
//it's not a valid query
else {
strcpy(abs_path, absolute_path);
//abs_path stays the same
query[0] = '\0';
free(linestr);
return true;
}
}
else {
//if there's no query
strcpy(abs_path, request_line);
//printf("$%sabspath with no query\n", abs_path);
query[0] = '\0';
free(linestr);
return true;
//query scope
}
//request line scope
}
//method scope
}
//multiple space detector
}
//fxn def
}