I implemented the parse function as below. The main idea is to divide the line into method, request, http version, check if they are consistent with rules. And then parse the request into abs_path and query. I used check50 and receive 2 failure info: [![enter image description here][1]][1]
For the 404 error, I understand that the abs_path and query variables are not really passed to the main function. Thus, in the main function that error check is skipped. But I don't know how to fix it. If using double pointer, should I change also the main function when calling the procedure?
bool parse(const char* line, char* abs_path, char* query)
{
char* tmp = malloc(sizeof(char) * (strlen(line) + 1));
strcpy(tmp, line);
//get the method, request, http version
char* method = strtok(tmp, " ");
char* request_target = strtok(NULL, " ");
char* http_ver = strtok(NULL, "\r\n");
//check if method is "GET"
if (strcmp(method, "GET") != 0)
{
error(405);
return false;
}
// check if request-target begins with /
if (request_target[0] != '/')
{
error(501);
return false;
}
// check if request-target contains "
if (strchr(request_target, '\"') != NULL)
{
error(400);
return false;
}
//check if http version is HTTP/1.1
if (strcmp(http_ver, "HTTP/1.1") != 0)
{
error(505);
return false;
}
//parse path and query
char* haystack = request_target;
char* needle = strchr(haystack, '?');
// if a query doesn't exist
if (needle == NULL)
{
strcpy(abs_path, haystack);
abs_path[strlen(haystack)] = '\0';
query[0] = '\0';
}
//if the request ends with ?
else if (strlen(needle) == 1)
{
strncpy(abs_path, haystack, needle - haystack);
abs_path[strlen(haystack) - strlen(needle)] = '\0';
query[0] = '\0';
}
//if a query exists, parse path and query
else
{
strncpy(abs_path, haystack, needle - haystack);
abs_path[strlen(haystack) - strlen(needle)] = '\0';
strcpy(query, needle + 1);
}
error(501);
return false;
}
EDIT: rolled the edits back to what the answer was about, otherwise this question won't benefit future users.