I currently have a program that basically reads html from a webpage. After sending the web server an http request, I get back a response where I use fdopen to read the socket:
FILE *webpage = fdopen(socket, "r");
then I have a loop that uses fgets to get each line and then print them to a file:
while(!feof(webpage)){
fgets(newline, 1000, webpage);
fprintf ...
}
This part of the program works fine, and I end up getting some test file like:
HTTP/1.1 200 OK^M
Date: Fri, 18 Nov 2011 04:42:40 GMT^M
Server: Apache/2.2.14^M
Accept-Ranges: bytes^M
Cache-Control: max-age=0^M
Expires: Fri, 18 Nov 2011 04:42:40 GMT^M
Vary: Accept-Encoding^M
Content-Length: 345235^M
Connection: close^M
Content-Type: text/html^M
X-Pad: avoid browser bug^M
^M
<html lang="en">
<head>
...
I want to find this newline that is right under xpad, and insert something right when this newline is found (basically do something right after the headers are printed. However, I’m not sure how to find the line, or what the ^Ms are for.
Currently I’m trying things like
if(newline == "\r\n"){
...
}
or just “\n”, and it doesn’t work. I think its got something to do with the ^M but I’m not sure.
Thanks!
^M (Ctrl+M) is the ascii carrige return, I believe you can search for just \r and not \r\n
I think ideally you want to do a regex check and check for a new line character at the beginning for the line ie ^\n alternativly you could just check the .length and see if it contains a new line if its 0. You just want to make sure you don’t catch any false positves.