Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const char* lookup(const char* path)
- {
- // TODO
- if (path == NULL)
- return NULL;
- int pathLength = strlen(path);
- char* extension = calloc(1, pathLength);
- extension = strchr(path, '.');
- if(extension != NULL)
- {
- if (strcasecmp(extension, ".css") == 0)
- {
- char* ret = "text/css";
- return ret;
- }
- else if (strcasecmp(extension, ".html") == 0)
- {
- char* ret = "text/html";
- return ret;
- }
- else if (strcasecmp(extension, ".gif") == 0)
- {
- char* ret = "image/gif";
- return ret;
- }
- else if (strcasecmp(extension, ".ico") == 0)
- {
- char* ret = "image/x-icon";
- return ret;
- }
- else if (strcasecmp(extension, ".jpg") == 0)
- {
- char* ret = "image/jpeg";
- return ret;
- }
- else if (strcasecmp(extension, ".js") == 0)
- {
- char* ret = "text/javascript";
- return ret;
- }
- else if (strcasecmp(extension, ".php") == 0)
- {
- char* ret = "text/x-php";
- return ret;
- }
- else if (strcasecmp(extension, ".png") == 0)
- {
- char* ret = "image/png";
- return ret;
- }
- else
- {
- return NULL;
- }
- }
- 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)
- {
- // TODO
- int lineLength = strlen(line);
- int sp = 0;
- int sp2 = 0;
- char* method = calloc(1, lineLength);
- char* path = calloc(1, lineLength);
- char* http = calloc(1, lineLength);
- for(int i = 0, j = 0; i < lineLength; i++)
- {
- if (line[i] == ' ')
- {
- sp = i + 1;
- break;
- }
- if(line[i] != ' ' && j < 10)
- {
- method[j] = line[i];
- j++;
- }
- }
- if(strcmp(method, "GET") != 0)
- {
- error(405);
- return false;
- //printf("method is not GET, error 405\n");
- }
- for(int r = 0; sp < lineLength; sp++)
- {
- if (line[sp] == ' ')
- {
- sp2 = sp + 1;
- break;
- }
- if(line[sp] != ' ' && r < 50)
- {
- path[r] = line[sp];
- r++;
- }
- }
- if (path[0] != '/')
- {
- error(501);
- return false;
- //printf("path doesn't begin with fslash, error 501\n");
- }
- for(int c = 0; c < strlen(path); c++)
- {
- if (path[c] == '\"')
- {
- error(400);
- return false;
- //printf("path contains quotation mark, error 400\n");
- }
- }
- for(int w = 0; sp2 < lineLength; sp2++, w++)
- {
- if (line[sp2 != '\r'])
- {
- http[w] = line[sp2];
- }
- }
- if(strcmp(http, "HTTP/1.1") != 0)
- {
- error(505);
- return false;
- //printf("HTTP version is not 1.1, error 505\n");
- }
- //lets extract this .
- //getting query:
- char* qery = calloc(1, strlen(line));
- qery = strchr(path,'?');
- query = strchr(qery,'q');
- char* p = strchr(path, '?');
- *p = 0;
- char* pPosition = strchr(path, '.');
- const char* lokup = lookup(path);
- if(pPosition == NULL)
- {
- error(404);
- return false;
- //printf("the path doesn't contain a . error 404\n");
- }
- else if(lokup == NULL)
- {
- error(501);
- return false;
- //printf("the path extension is not valid here, error 501.\n");
- }
- strcpy(abs_path, path);
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement