-
-
Save anonymous/cb99b7dffebbbd54b879ec7deaea28f1 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
const char* lookup(const char* path) | |
{ | |
// take path from user | |
char ch = '.'; | |
char* ret; | |
ret = strrchr(path, ch); | |
// compare | |
int a = strcasecmp(ret, ".css"); | |
int b = strcasecmp(ret, ".html"); | |
int c = strcasecmp(ret, ".gif"); | |
int d = strcasecmp(ret, ".ico"); | |
int e = strcasecmp(ret, ".jpg"); | |
int f = strcasecmp(ret, ".js"); | |
int g = strcasecmp(ret, ".php"); | |
int h = strcasecmp(ret, ".png"); | |
// check if file ext matches any stored string | |
if(a == 0) | |
{ | |
return "text/css"; | |
} | |
else if(b == 0) | |
{ | |
return "text/html"; | |
} | |
else if(c == 0) | |
{ | |
return "image/gif"; | |
} | |
else if(d == 0) | |
{ | |
return "image/x-icon"; | |
} | |
else if(e ==0) | |
{ | |
return "image/jpeg"; | |
} | |
else if(f == 0) | |
{ | |
return "text/javascript"; | |
} | |
else if(g == 0) | |
{ | |
return "text/x-php"; | |
} | |
else if(h == 0) | |
{ | |
return "image/png"; | |
} | |
else | |
{ | |
return NULL; | |
} | |
} | |
/** | |
* 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) | |
{ | |
// lenght of line | |
int l = strlen(line); | |
// search for this method | |
char* s = "GET "; | |
// search in line | |
char* ret = NULL; | |
ret = strstr(line, s); | |
// count how many spaces are used space | |
int space = 0; | |
// check if method not valid | |
if(ret == NULL || line[0] != 'G') | |
{ | |
// return error then false | |
error(405); | |
return 0; | |
} | |
// search for / in start of request-line | |
for(int i = 0; i < l; i++) | |
{ | |
if(line[i] == ' ') | |
{ | |
// increment when space is reached | |
space++; | |
} | |
else if(line[i] == 34) | |
{ | |
error(400); | |
return 0; | |
} | |
} | |
if(line[4] != '/') | |
{ | |
// respond with error 501 and return false | |
error(501); | |
return false; | |
} | |
// make sure there is 2 spaces in line | |
if(space != 2) | |
{ | |
error(400); | |
return false; | |
} | |
// check if http version is valid | |
char* http = "HTTP/1.1"; | |
char* poi; | |
poi = strstr(line, http); | |
if(poi == NULL) | |
{ | |
error(505); | |
return false; | |
} | |
//check if line ends with crlf | |
char* end = "\r\n"; | |
char* crlf= NULL; | |
crlf = strstr(line, end); | |
if(crlf == NULL) | |
{ | |
error(400); | |
return false; | |
} | |
// search for / in request-line | |
char* req = NULL; | |
req = strchr(line, '/'); | |
// lenght of abs_path | |
int k = strlen(req); | |
// number of characters in abs_path | |
int y = 0; | |
for(int z = 0; z < k; z++) | |
{ | |
// break when '?' or space is reached | |
if(req[z] == '?' || req[z] == ' ') | |
{ | |
break; | |
} | |
// else increment y | |
else | |
{ | |
y++; | |
} | |
} | |
// deeclare array with the number of characters in abs_path | |
char absl [y]; | |
// copy abs_path into the new string | |
strncpy(absl, req, y); | |
// null terminate abs_path | |
absl[y] = '\0'; | |
// store abs_path in address (NOT SURE!) | |
// check if abs_path is bigger than limit | |
if(y > LimitRequestLine + 1) | |
{ | |
abs_path = realloc(abs_path, sizeof(LimitRequestLine + y)); | |
abs_path = absl; | |
} | |
else | |
{ | |
abs_path = absl; | |
} | |
// declare string to store from query to end of line | |
char* q = NULL; | |
// search for '?' query in line | |
q = strchr(line, '?'); | |
// make sure there is query | |
if(q != NULL) | |
{ | |
// number of characters in query | |
int a = 0; | |
// length of q | |
int p = strlen(q); | |
for(int x = 0; x < p; x++) | |
{ | |
// break when end of query "space" is reached | |
if(q[x] == ' ') | |
{ | |
break; | |
} | |
// increment ad | |
else | |
{ | |
a++; | |
} | |
} | |
// declare array with size of characters in query | |
char queryc[a]; | |
// copy query to new string | |
strncpy(queryc, q, a); | |
int r,t; | |
// don't copy '?' into query | |
for(r = 0,t =0; r < p; r++) | |
{ | |
if(queryc[r] != '?') | |
{ | |
queryc[t] = queryc[r]; | |
t++; | |
} | |
} | |
// end string | |
queryc[t]='\0'; | |
// check is query is bigger than limit to store query in address (NOT SURE!) | |
if(a > LimitRequestLine + 1) | |
{ | |
query = realloc(queryc, sizeof(LimitRequestLine + a)); | |
query = queryc; | |
} | |
// if the there is no query | |
else if (a == 0) | |
{ | |
query = ""; | |
} | |
else | |
{ | |
query = queryc; | |
} | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment