7

My main issue is doing this without using the heap, I think. I set up arrays for each of the pieces of the request-line (e.g. method, request-target, etc.) and set all of them to be of length = strlen(line) (i.e., the length of the entire request-line) to avoid segfaults, but all the extra elements in the array are garbage values that I can't seem to convince the program to ignore.

I tried tracking the last character I put into an array before moving on to the next one, and then going to request_line_array[final_character_index + 1] and setting that equal to '\0', but then when I go to take the value from that same array and tell it to stop once it hits a \0 character, it continues printing a bunch of question marks embedded in little diamonds as the garbage characters.

At this point, I am working on making a set of counters that all keep track of how many characters are in each request-line substring so that I can re-make those same arrays with the exact length needed to fit what I need to fit, but I am wondering if there is an easier route than this.

12

I have come up with my own solution after doing some research on pointer math.

I used the strchr command to find the first single space in the line (handling a 400 error if that could not be found). Then, I stored the index of the space in an int. I then called strchr() again to find a single space, passing in ptr+1 instead of line (therefore, I looked through the string starting from the place after the first single space I found). I found the second space and recorded that index as well. I made the same function call again to confirm that there was no third single space (or else, throw another error).

Then, with these indexes (along with the index of the CRLF found in a similar manner), I was able to separate the line into individual strings for the method, request-target, and HTTP-version with three fairly small for loops.

I had to put the terminal \0s in myself, but the hardest part of that was just figuring out the index of the final element in the string.

My new solution is much more eloquent than my last. I hope my notes here are useful to someone else working on the pset.

1
  • How did you add the terminal \0? I tried doing str[n] = \0 (where n was the last character + 1) but got errors.
    – Norcim133
    Dec 30 '15 at 11:21

You must log in to answer this question.

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