-
-
Save anonymous/86bfd699e66a7d55efe8c8fb0d3cfc6c to your computer and use it in GitHub Desktop.
server.c - shared from CS50 IDE
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Parses a request-line, storing its absolute-path at abs_path | |
* and its query string at query, both of which are assumed | |
* to be at least of length LimitRequestLine + 1. | |
*/ | |
bool parse(const char* line, char* abs_path, char* query) | |
{ | |
char message[100]; | |
strcpy(message, line); | |
char* word; | |
// getting the first word (method) | |
word = strtok(message, " "); | |
// making sure it's "GET" | |
if (strcmp(word, "GET") != 0) | |
{ | |
error(405); | |
return 1; | |
} | |
// getting the second word (request-target) | |
word = strtok(NULL, " "); | |
// copies it into abs_path | |
strcpy(abs_path, word); | |
// making sure it starts with '/' | |
if (word[0] != '/') | |
{ | |
error(501); | |
return 1; | |
} | |
// making sure it doesn't contain any '"' | |
for (int i = 0; i < strlen(word); i++) | |
{ | |
if (word[i] == '"') | |
{ | |
error(400); | |
return 1; | |
} | |
} | |
// getting the third word (HTTP version) | |
word = strtok(NULL, " "); | |
// making sure it's "HTTP/1.1" | |
if (strcmp(word, "HTTP/1.1") != 0) | |
{ | |
error(505); | |
return 1; | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment