Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool parse(const char* line, char* abs_path, char* query)
- {
- // declaring arrays for holding all these strings (and feels)
- char abpathholder[LimitRequestLine + 1];
- char queryholder[LimitRequestLine + 1];
- char httpholder[9];
- memset(abpathholder, 0, LimitRequestLine + 1);
- memset(queryholder, 0, LimitRequestLine + 1);
- memset(httpholder, 0, 9);
- // getting the start of absolut path and the start of HTTP version - 1
- char* abpathstart = strchr(line, '/');
- char* lastspace = strrchr(line, ' ');
- char* querystart = strchr(line, '?');
- // setting up an int to see where the path and query ends
- int pathend;
- int queryend;
- // checks if there is a query and sets difference in strings accordingly, also gets the thing
- if (querystart == NULL)
- {
- pathend = strlen(lastspace) - strlen(abpathstart);
- queryholder[0] = '\0';
- }
- else
- {
- pathend = strlen(abpathstart) - strlen(querystart);
- queryend = strlen(querystart) - strlen(lastspace) - 1;
- for ( int i = 0; i < queryend; i++)
- {
- queryholder[i] = querystart[i + 1];
- }
- }
- // copies accurate number of chars for abpath
- for (int i = 0; i < pathend; i++)
- {
- abpathholder[i] = abpathstart[i];
- }
- //for (int i = 0; i < strlen(abpathholder); i++)
- // printf("This is in abpathholder: %c\n", abpathholder[i]);
- for (int i = 1; i < strlen(lastspace); i++)
- {
- if (lastspace[i] == '\r')
- {
- break;
- }
- else
- {
- httpholder[i - 1] = lastspace[i];
- }
- }
- //for (int i = 0; i < strlen(httpholder); i++)
- // printf("This is in httpholder: %c\n", httpholder[i]);
- // error checking begins here
- // checking simply if line starts with GET
- if (strncmp(line, "GET ", 4) != 0)
- {
- error(405);
- return false;
- }
- // cheking for any " in line
- for (int i = 0; i < strlen(line); i++)
- {
- if (line[i] == '"')
- {
- error(400);
- return false;
- }
- }
- // checking if request-target really begiins with a /
- if (abpathholder[0] != '/' || abpathholder[0] == 47)
- {
- error(501);
- return false;
- }
- // checking if HTTP version is correct
- if (strcasecmp(httpholder, "HTTP/1.0") == true)
- {
- error(505);
- return false;
- }
- abs_path = abpathholder;
- query = queryholder;
- for (int i = 0; i < strlen(abs_path); i++)
- printf("This is in abs_path: %c\n", abs_path[i]);
- for (int i = 0; i < strlen(query); i++)
- printf("This is in query: %c\n", query[i]);
- printf("Reached true!\n");
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement