I am doing pset6 and for the parse function I have first started to create about the same inputs in a seperate file and with printfs I have tried to slowly tackle the parse funtion by manually giving in the input of like GET /something?q=query HTTP/1.1
and checking around what the specs want the function to be in server.c
here is the code that I have written although I think I am able to to what specs want me to do for the parse function but this code is giving me 4 unconditional jumps error on valgrind but the code is running fine and doing what I want it to do... Would really appreciate if someone tells me why I am getting these errors and how do I solve it
here is my code
int main(void)
{
const char* string;
printf("Enter your string\n");
string = GetString();
// to be used to find the space bar in the given line/string
char character = ' ';
// getting the rest of the string from the first space including the space
char* ptr = strchr(string, character);
printf("string from 1st space=%s\n",ptr);
char* method = malloc((ptr - string) + 1);
method = strncpy(method, string, ptr - string );
printf("method=%s\n", method);
// comparing if method infact is GET
if(strcmp(method, "GET") != 0)
{
printf("error 405.\n");
}
//extracting the target request from the line that is string bw the 2 space bars
//pointer for the second space bar
char* ptr_space2 = strrchr(string, character);
printf("string from second space=%s\n", ptr_space2);
/*storing target request... point to remember is that string is copied
from the pointer on right minus the pointer on left>> this copies thing
that is in the middle */
char* targetrequest = malloc((ptr_space2 - ptr) + 1);
targetrequest = strncpy(targetrequest, (ptr + 1), (ptr_space2 - ptr) );
printf("targetrequest=%s\n", targetrequest);
// to check if targetrequest begins with a forward slash
if(targetrequest[0] != '/')
{
printf("error 501\n");
}
//to check if target request contains a "
if(strchr(targetrequest, '\"') != NULL)
{
printf("error 400\n");
}
//to check version is 1.1 by getting the pointer for last occurence forward slash
char* ptr_lastslash = strrchr(string, '/');
//print the number after the slash
printf("version=%s\n", (ptr_lastslash + 1));
// comparing version of hhtp
if(strcmp(ptr_lastslash + 1, "1.1") != 0)
{
printf("error 505\n");
}
//pointer from question mark
char* ptr_questionmark = strrchr(string, '?');
// to check if there is no question mark
if(ptr_questionmark == NULL)
{
char* query = malloc(1);
query[0] = '\0';
printf("%s", query);
free(query);
}
else
{
// question mark yes but no query that means difference frm 2nd space and question mark would only be 1
if((ptr_space2 - ptr_questionmark) == 1)
{
char* query = malloc(1);
query[0] = '\0';
printf("%s", query);
free(query);
}
else
{
//createing query from the last space bar to the question mark
char* query = malloc((ptr_space2 - ptr_questionmark) + 1);
query = strncpy(query, (ptr_questionmark + 1), (ptr_space2 - ptr_questionmark));
printf("query=%s\n", query);
free(query);
}
}
free(targetrequest);
free(method);
}