1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
/**
* 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* temp;
temp = malloc((LimitRequestLine+1)*sizeof(char*));
char* method;
method = malloc((LimitRequestLine+1)*sizeof(char*));
abs_path = malloc((LimitRequestLine+1)*sizeof(char*));
query = malloc((LimitRequestLine+1)*sizeof(char*));
char* protocol;
protocol = malloc((LimitRequestLine+1)*sizeof(char*));
sscanf(temp, "%s %s?%s %s" , method, abs_path, query, protocol);
for ( int i=0 ; i < strlen(method); i++)
{
if (method[i] == "GET"[i])
{
respond(405, "405 Method Not Allowed", "405 Method Not Allowed\n", sizeof("405 Method Not Allowed\n"));
return false;
}
}
if (abs_path[0] != '/')
{
respond(400, "400 Bad Request", "400 Bad Request\n", sizeof("400 Bad Request\n"));
return false;
}
for ( int i=0 ; i < strlen(query); i++)
{
if (query[i] == '"')
{
respond(400, "400 Bad Request", "400 Bad Request\n", sizeof("400 Bad Request\n"));
return false;
}
}
for (int i =0 ; i < 8 ; i++)
{
if (protocol[i] != "HTTP/1.1"[i])
{
respond(505, "505 HTTP Version Not Supported\n", "505 HTTP Version Not Supported\n", sizeof("505 HTTP Version Not Supported\n"));
return false;
}
}
return true;
} |
Partager