0

I have extracted the method from the request line, but I'm stuck at extracting the request-target. Like if I have "cat.html HTTP/1.1", I can't throw away the "HTTP/1.1".

char *temp;
temp = strchr(line, '/');
temp = temp + 1;
int i;
for(i = 0; i > strlen(temp); i++)
{
    if(strcmp(temp[i], ' ') != 0)
    break;
}
strncpy(temp, temp, i);
printf("%s", temp);
4
  • so you don't know how to extract cat.html from cat.html HTTP/1.1?
    – kzidane
    Oct 2 '15 at 9:09
  • yes, I can't figure that out.
    – Karan
    Oct 2 '15 at 11:06
  • would you like to show us some code?
    – kzidane
    Oct 2 '15 at 11:10
  • you may edit your question and add your code!
    – kzidane
    Oct 2 '15 at 17:11
1

A typical request might look like this:

GET /cat.html HTTP/1.1

Since you have successfully extracted the method I will move on to the request_target. I see you have already found the beginning of the target using

char *temp;
temp = strchr(line, '/');
temp = temp + 1;

to help you avoid future problems, take note that the / is part of the request_target so you should probably remove the temp = temp + 1 line.

Now you have to also find the end of the request_target, and then copy all the chars in between into a new string.

The end of the request_target is signaled by a space character ' ', and you have implemented a for loop to find the ' '. You could have used the strchr() again but it's up to you.

Now first take a look at the condition of your for loop. You have i > strlen(temp). When the loop runs for the first time, i is 0 and strlen(temp) is probably greater than 0 because temp is actually /cat.html HTTP/1.1. So i is not greater than strlen(temp) and the loop exits, not executing even once. It should be instead:

int i;
for (i = 0; i < strlen(temp); i++)

or even better

int i = 0;
for (int length = strlen(temp); i < length; i++)

so that the length is not computed on every iteration.

If you fix that, you should have a compile time error because you are using strcmp() wrong. strcmp() is for comparing two strings while you are in fact comparing two chars. You should instead have:

if (temp[i] == ' ')

Then you should also probably indent the break; line to fall under the if condition to be stylistically correct.

Now on to the strncpy() call. You are using it wrong. According to its man page:

#include <string.h>

char *strcpy(char *dest, const char *src);

char *strncpy(char *dest, const char *src, size_t n);

The strcpy() function copies the string pointed to by src, including the terminating null byte ('\0'), to the buffer pointed to by dest. The strings may not overlap, and the destination string dest must be large enough to receive the copy. Beware of buffer overruns! (See BUGS.)

The strncpy() function is similar, except that at most n bytes of src are copied. Warning: If there is no null byte among the first n bytes of src, the string placed in dest will not be null-terminated.

If the length of src is less than n, strncpy() writes additional null bytes to dest to ensure that a total of n bytes are written.

So first of all you can't use the same string for both src and dest. then you should also terminate it with a null byte (as there won't be one on the first n chars).

I'll leave it to you to solve this one so that I don't take away all the fun from you. As a tip, consider creating an array of chars of length i (plus something maybe???) to store inside there the request_target.


If this answers your question please accept it by clicking the gray check-mark to the left, so that it becomes green. You can also vote it up by pressing the up arrow above the check-mark. And don't forget to keep coding!

You must log in to answer this question.

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