-
-
Save anonymous/91a243c449e1da6dd75d3a09e8ecd7de 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) | |
{ | |
int length = strlen(line); | |
char buffer[length + 1]; | |
strcpy(buffer, line); | |
int space_ctr = 0; | |
int spec_char_ctr = 0; | |
int j = 0; | |
while(j < length+1) | |
{ | |
if (buffer[j] == ' ') | |
{ | |
space_ctr++; | |
if(buffer[j+1] == ' ') | |
{ | |
error(400); | |
return false; | |
} | |
} | |
if (buffer[j] == '\r' && buffer[j+1] == '\n') | |
{ | |
spec_char_ctr++; | |
break; | |
} | |
j++; | |
} | |
if (space_ctr != 2) | |
{ | |
error(400); | |
return false; | |
} | |
if (spec_char_ctr != 1) | |
{ | |
error(400); | |
return false; | |
} | |
//ensure correct method is used | |
if(buffer[0] != 'G' || buffer[1] != 'E' || buffer[2] != 'T' || buffer[3] !=' ') | |
{ | |
error(405); | |
return false; | |
} | |
//ensure that request-target starts with a / | |
if (buffer[4] != '/') | |
{ | |
error(501); | |
return false; | |
} | |
//ensure on quotation marks(") in the request-target | |
int i = 4; | |
while (buffer[i] != ' ') | |
{ | |
if (buffer[i] == '\"') | |
{ | |
error(400); | |
return false; | |
} | |
i++; | |
} | |
char* http_add = strrchr(buffer, 32); | |
if (http_add[1] !='H' || http_add[2] != 'T' || http_add[3] != 'T' || http_add[4] != 'P' || http_add[5] != '/' || | |
http_add[6] != '1' || http_add[7] != '.' || http_add[8] != '1' || http_add[9] != '\r' || http_add[10] != '\n') | |
{ | |
error(505); | |
return false; | |
} | |
int k = 4; | |
char buffer2[strlen(line) + 1]; | |
char buffer3[strlen(line) + 1]; | |
int indic = 0; | |
int when = 0; | |
while(buffer[k] != ' ') | |
{ | |
if (buffer[k] == '?' && indic == 0) | |
{ | |
indic = 1; | |
} | |
else if ( indic == 0) | |
{ | |
buffer2[k-4] = buffer[k]; | |
} | |
else | |
{ | |
buffer3[when] = buffer[k]; | |
when++; | |
} | |
k++; | |
} | |
buffer2[k - 2 - when] = '\0'; | |
buffer3[when] = '\0'; | |
strcpy(abs_path, buffer2); | |
if (indic == 1) | |
{ | |
strcpy(query , buffer3); | |
} | |
else | |
{ | |
query[0] = '\0'; | |
} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment