I need to read in only the values of a header that terminate with \r\n\r\n
Something like GETFILE OK 1024\r\n\r\n <content>
I'm trying to get the first \r\n
and then get the next pair in a subsequent recv call.
The call to this function is: read_in_header(gfr, headerRecvBuff, 1);
Issue: The logic in the while referring to \n
is completely ignored, or does not show any matches, when I know they exist. Is this the right way to compare the char newline?
int read_in_header(gfcrequest_t *gfr, char *buf, int len) {
char *s = buf;
int slen = len;
int c = 0;
int count = 0;
//get the first \r\n pair
do {
c = recv(gfr->client_fd, s, slen, 0);
printf("checking to see what s has now: %s\n", s);
count += c;
} while ((c > 0) && (s[count - 1] != '\n'));
//get the second \r\n pair
count = 0;
do {
c = recv(gfr->client_fd, s, slen, 0);
printf("checking to see what s has now: %s\n", s);
count += c;
} while ((c > 0) && (s[count - 1] != '\n'));
printf("checking to see what s has now: %s\n", s);
if (c < 0) {
return c;
} else if (c == 0) {
puts("Time to disconnect, the server is done.");
//total bytes received should not include header length
gfr->totalbytesReceived -= gfr->headerbytes_received;
return 0;
} else {
s[c - 1] = '\0';
}
gfr->totalbytesReceived += count;
return c;
}
Regarding Is this the right way to compare the char newline?
Since s is a buffer (not a single char), for the first loop the comparison method can be changed to
To require both "\r" & "\n" are there. This takes advantage of string searching to check both values are present in one line.
resulting in:
If you decided to capture the line that has all 4, i.e.
\r\n\r\n
, then make that the argument of the comparison.One side note, unless you have set socket options to non-blocking, recv() is a blocking call. Look into how to set a socket to non-blocking