HTTP Get Request..How big to make the buffer

Started by
4 comments, last by shmoove 18 years, 9 months ago
Hi, I'm coding in C++ with winsock, and am trying to do some things with HTTP. One of my first questions is..how does a web browser work? Does it send a single get request and download the whole page? Or does it just retrieve the HEAD, and then look at the length..and then send another request for the whole page? I ask this because I have no idea how big to make my buffer. Currently i'm only interested in the header, so I just set my char buffer to 1000 and it seems to grab enough of it.. when I say my char buffer I mean something like this

char buffer[1000];

//winsock recv function.
buffer = recv(param, param, param);
this method doesn't seem like the way it should be done..any suggestions would be nice.. thanks
Advertisement
Quote:Original post by kag1
One of my first questions is..how does a web browser work?

Mozilla source code could come in handy.

I haven't written any browsers but I imagine you would make a small buffer, read in the data, parse what you have, get some more if necessary until you reach the end of file or you have what you're looking for.

shmoove
I've written some, propably quite unorthodox, http code once. Like shmoove said, I had a smaller buffer, which I used in a loop to recieve the stuff in. Then I applied the data in a bigger buffer, which I reallocated bigger each time.

About the content-length - field, it seemed that it wasn't always there. I think it's more like a bonus than a required thing, so it can't be trusted.
http://en.wikipedia.org/wiki/Http
http://www.jmarshall.com/easy/http/

Anyways, I would expand my buffer on incoming data but I've been dealing with some pretty small and speacial case sessions. You can install ethereal and watch packet sniff for a bit, then filter to "http". This page is probably more then 4k worth of data of text, and the images etc are seperate downloads. So a non-growing 1k buffer isn't going to cut cheese :)

Edit:
shmoove
The vast majority of Mozilla is a rendering engine(Gecko), then framework. Very very small portion is it is the network layer that handles and processes the transactions.
-Scott
For cases like this, I think it is fairly safe to allocate a 4K buffer on the stack that will get used 99% of the time. The other one percent, you can use a std::string or other growable buffer. After you decide which buffer to use, make a pointer to it and use it later on in the function to do your processing. Safe, easy, fast. Alternatively, use a growable buffer that won't go out of scope at the end of a function (e.g. member variable).
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
Quote:Original post by MustEatYemen
shmoove
The vast majority of Mozilla is a rendering engine(Gecko), then framework. Very very small portion is it is the network layer that handles and processes the transactions.

That's means there is less relevant code to go through ;p

shmoove

This topic is closed to new replies.

Advertisement