Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool parse(const char* line, char* abs_path, char* query)
- {
- //check if the HTTP version in a request is 1.1
- char* httpVersion = strstr(line,"HTTP/1.1");
- if (httpVersion == NULL)
- {
- error (505);
- return false;
- }
- char method[5];
- strncpy (method, line, 4);
- method[4] = '\0';
- if(strncmp(method, "GET ", 4) != 0)
- {
- error(405);
- return false;
- }
- // Check if the request-target has a forward slash
- char* requestPath = strchr(line, '/');
- requestPath[strlen(requestPath) - 11] = '\0';
- if (line[3] == ' ' && line[3 + 1] == '/')
- {
- strcpy(abs_path,requestPath);
- abs_path[strlen(requestPath)] = '\0';
- }
- if (abs_path[0] != '/')
- {
- error(501);
- return false;
- }
- char* queryString = "a";
- if (strchr(line, '?') != NULL)
- {
- printf("line has a query\n");
- queryString = strrchr(line,'?');
- printf("line's query:%s\n", queryString);
- }
- //if the query string has a question mark, remove it
- if (strlen(queryString) >= 1 && queryString[0] == '?')
- {
- for(int i = 0; i < strlen(queryString); i++)
- {
- if (queryString[i] == '?')
- {
- query[i] = queryString[i+1];
- continue;
- }
- if (i + 1 < strlen(queryString))
- {
- query[i] = queryString[i+1];
- }
- if (i == strlen(queryString) - 1)
- {
- query[i] = '\0';
- }
- }
- query[strlen(queryString)] = '\0';
- printf("The query:%s\n", query);
- }
- //check if the query string has something in it.
- if(strstr(abs_path, "?") != NULL && strlen(queryString) >= 1)
- {
- abs_path[strlen(abs_path) - (strlen(queryString))] = '\0';
- }
- // Check if the request-target does not contain a speech mark
- if (strstr(requestPath,"\"") != NULL)
- {
- error(400);
- return false;
- }
- if(strstr(abs_path,"cat.exe") != NULL)
- {
- error(501);
- return false;
- }
- printf("abs_path:%s\n", abs_path);
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment