Skip to content

Instantly share code, notes, and snippets.

/server.c Secret

Created August 22, 2016 17:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/9acdaefcab4d9fb15a248d48c9e59edb to your computer and use it in GitHub Desktop.
Save anonymous/9acdaefcab4d9fb15a248d48c9e59edb to your computer and use it in GitHub Desktop.
server.c - shared from CS50 IDE
/**
* 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[strlen(line) + 1];
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 false;
}
// 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 false;
}
// making sure it doesn't contain any '"'
for (int i = 0; i < strlen(word); i++)
{
if (word[i] == '"')
{
error(400);
return false;
}
}
// getting the third word (HTTP version)
word = strtok(NULL, " ");
// making sure it's "HTTP/1.1"
if (strcmp(word, "HTTP/1.1\r\n") != 0)
{
error(505);
return false;
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment