0

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);

}

1 Answer 1

0

Great approach!

Here's one of the leaks, you should be able to apply this to the others. Assuming string
GET /something?q=query HTTP/1.1.

char* method = malloc((ptr - string) + 1);: allocate method for 4 (indexes 0 through 3)
method = strncpy(method, string, ptr - string );: copy in 3 characters

What is missing? The null terminator in the 4th byte. You could do the math and figure out which is the last byte, and do an explicit set, but there is a way that the computer will do that for you. Review man calloc. The difference between calloc and malloc (besides the number of arguments!) is that calloc initializes the allocated bytes to 0. And that's exactly what you need! If you did a
char* method = calloc((ptr - string) + 1,1); it would allocate the 4 bytes and set them all to 0. Then after you copy in the 3 bytes (GET), the string would already be null-terminated.

You might consider using "%s<-" as your string format. By adding a "marker" after the string, you will be sure you are seeing the whole string, in case there are non-printing characters in there that the eye will not detect.

3
  • calloc did silenced the error but couldnt understand what %s<- was supposed to do cuz nothing happened just got <- printed in the printf Commented Aug 29, 2016 at 15:33
  • If you change printf("method=%s\n", method); to printf("method=%s<-\n", method); it should print method=GET<-. That way you know there is not a non-printing character after the T. Anything that would print "<-" means the string is blank. Commented Aug 29, 2016 at 15:38
  • okay got your point Commented Aug 29, 2016 at 15:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .