Brandon
2013-01-01 12:59:29
How can I read a socket containing Chunked Data?
The following reads chunked data from a webpage. It sends a request and the server responds with:
Look at the last line.. 0xFFF8. That says the size of the next chunk to read in bytes.
At the end of the request, it looks like:
Notice the 4 and the 0? 0 is the end chunk and indicates there is nothing left to read.. 4 is the amount to read since the previous chunk..
Simba's sockets doesn't seem to have anything to indicate how many bytes was read so how can I detect how many was read? OR how can I tell how large the next chunk will be? I know there is already a GetPage function but I like writing my own stuffs. How do you specify chunk transfer-encoding?
I can't seem to find anything on this at all.. So I'm hoping someone can shed light on it and shed light on some of Simba's most un-used functions.
My code is below:
The following reads chunked data from a webpage. It sends a request and the server responds with:
Connected to: 85.17.90.18 Port: 80
HTTP/1.1 200 OK
Set-Cookie: bblastvisit=1357062696; expires=Wed, 01-Jan-2014 17:51:36 GMT; path=/
Set-Cookie: bblastactivity=0; expires=Wed, 01-Jan-2014 17:51:36 GMT; path=/
Cache-Control: private
Pragma: private
Content-Type: text/html; charset=ISO-8859-1
X-UA-Compatible: IE=7
Connection: close
Transfer-Encoding: chunked
Date: Tue, 01 Jan 2013 17:51:36 GMT
Server: lighttpd
fff8
Look at the last line.. 0xFFF8. That says the size of the next chunk to read in bytes.
At the end of the request, it looks like:
</script><!-- vBadvanced 1-3-9-4-8-0 -->
</body>
</h
4
tml>
0
Notice the 4 and the 0? 0 is the end chunk and indicates there is nothing left to read.. 4 is the amount to read since the previous chunk..
Simba's sockets doesn't seem to have anything to indicate how many bytes was read so how can I detect how many was read? OR how can I tell how large the next chunk will be? I know there is already a GetPage function but I like writing my own stuffs. How do you specify chunk transfer-encoding?
I can't seem to find anything on this at all.. So I'm hoping someone can shed light on it and shed light on some of Simba's most un-used functions.
My code is below:
procedure GetPageEx(Page, Port: String);
var
Socket: Integer;
IP, Header, Response: String;
begin
Header := 'GET / HTTP/1.1' + #13#10;
Header := Header + 'Host: ' + Page + #13#10;
Header := Header + 'Connection: close' + #13#10;
Header := Header + #13#10;
Socket := CreateSocket;
ConnectSocket(Socket, Page, Port);
SocketInfo(Socket, IP, Port);
SetTimeout(Socket, 5000);
writeln('Connected to: ' + IP + ' Port: ' + Port + #13#10#13#10);
SendSocket(Socket, Header);
Response := RecvSocket(Socket);
try
while (Response <> '') do
Response := Response + RecvSocket(Socket);
except
end;
FreeSocket(Socket);
writeln(Response);
end;
begin
GetAllPageEx('villavu.com', '80');
end.