Board index » delphi » Re: How to use TIdHTTP.Get to get just the first n bytes from a file?

Re: How to use TIdHTTP.Get to get just the first n bytes from a file?


2006-12-01 10:46:58 PM
delphi187
Ricardo Villela Coppola writes:
Quote
Hi folks,

We have an HTTP server that hosts ".asf" video files from a 24/7
surveillance system in 5 minutes chunks for playback.
We use a customized version of VLC to play them but from inside a
GUI front end develop in Delphi. Every thing works just fine but
recently our costumers asked for a better interface and one of the
aspects they required is the ability to see the files' actual
duration since they can be in the middle of a 5 minute chunk
recording or might be incomplete due to some hardware or software
fault. To do this I need to query each file's header that resides
in the first 134 bytes. I can get the header using the OnWork event
and issuing a Disconnect after at least 134 bytes are received.
My problem is the time it gets to fill the whole list that can easily
get to more than 1500 files. Each header fetch does not take really
much time but for 1500 files the extra bytes accumulate to a undesired
amount of time. I can very well wait to fetch each file's duration by
the moment the user selects it, actually this is the approach I'm
taking right now, but I would like to be able to construct a time line of
areas and cameras so the user can have a more complete view of its
surveillance solution.

So, my question is: How can I get just 134 bytes form the start of
each file using TIdHTTP?

TIA,
Use the idHTTP1.Request.ContentRangeStart and
idHTTP1.Request.ContentRangeEnd properties.
--
 
 

Re: How to use TIdHTTP.Get to get just the first n bytes from a file?

Hi folks,
We have an HTTP server that hosts ".asf" video files from a 24/7
surveillance system in 5 minutes chunks for playback.
We use a customized version of VLC to play them but from inside a
GUI front end develop in Delphi. Every thing works just fine but
recently our costumers asked for a better interface and one of the
aspects they required is the ability to see the files' actual
duration since they can be in the middle of a 5 minute chunk
recording or might be incomplete due to some hardware or software
fault. To do this I need to query each file's header that resides
in the first 134 bytes. I can get the header using the OnWork event
and issuing a Disconnect after at least 134 bytes are received.
My problem is the time it gets to fill the whole list that can easily
get to more than 1500 files. Each header fetch does not take really
much time but for 1500 files the extra bytes accumulate to a undesired
amount of time. I can very well wait to fetch each file's duration by
the moment the user selects it, actually this is the approach I am taking
right now, but I would like to be able to construct a time line of areas and
cameras so the user can have a more complete view of its surveillance
solution.
So, my question is: How can I get just 134 bytes form the start of each
file using TIdHTTP?
TIA,
--
Ricardo Villela Coppola (CSP Controle e Automação Ltda)
 

Re: How to use TIdHTTP.Get to get just the first n bytes from a file?

Eddie Shipman writes:
Quote

Use the idHTTP1.Request.ContentRangeStart and
idHTTP1.Request.ContentRangeEnd properties.


Can you show me some example code? I tried to set the
Request.ContentRangeEnd to 134 but left the other untouched.
Should I set them both? Since I actually want to start from 0,
I thought the last one would suffice.
--
Ricardo Villela Coppola (CSP Controle e Automação Ltda)
 

Re: How to use TIdHTTP.Get to get just the first n bytes from a file?

"Ricardo Villela Coppola" <XXXX@XXXXX.COM>writes
Quote
How can I get just 134 bytes form the start of each file using TIdHTTP?
If the server supports HTTP 1.1 and byte ranges, then you can set the
TIdHTTP's Request.ContentRangeStart and Request.ContentRangeEnd properties
before calling Get(). The server's response will tell you whether the range
was satisfied or not. A response code of 206 means that partial data is
sent. A response code of 200 means that the entire file is being sent
instead. You can optionally call Head() before Get() to see if the server's
'Accept-Ranges' response header contains 'bytes' or not. If it does, then
you can request a byte range. Otherwise, you will have to fallback to what
you were previously doing.
Gambit
 

Re: How to use TIdHTTP.Get to get just the first n bytes from a file?

"Ricardo Villela Coppola" <XXXX@XXXXX.COM>writes
Quote
Can you show me some example code?
var
DownloadingRange: Boolean = False;
procedure TForm1.Download;
begin
IdHTTP1.Head(URL);
IdHTTP1.Request.ContentRangeStart := 0;
if Pos('bytes',
LowerCase(IdHTTP1.Response.RawHeaders.Values['Accept-Ranges'])) <>0 then
begin
IdHTTP1.Request.ContentRangeEnd := iif(DownloadingRange, 134,
0);
IdHTTP1.ProtocolVersion := pv1_1;
IdHTTP1.OnHeadersAvailable := nil;
DownloadingRange := True;
end else
begin
IdHTTP1.Request.ContentRangeEnd := 0;
IdHTTP1.OnHeadersAvailable := TweakHeaders;
DownloadingRange := False;
end;
IdHTTP1.Get(URL, Stream);
//...
end;
procedure TForm1.TweakHeaders(Sender: TObject; AHeaders: TIdHeaderList;
var VContinue: Boolean);
var
Len: Integer;
begin
Len := StrToIntDef(Trim(AHeaders.Values['Content-Length']), 0);
if Len>134 then
begin
AHeaders.Values['Content-Length'] := '134';
AHeaders.Values['Connection'] := 'close';
end;
end;
Quote
I tried to set the Request.ContentRangeEnd to 134 but left the other
untouched.
Should I set them both?
Not unless you want the download to begin in the middle of the file.
Gambit
 

Re: How to use TIdHTTP.Get to get just the first n bytes from a file?

Remy Lebeau (TeamB) writes:
Quote

"Ricardo Villela Coppola" <XXXX@XXXXX.COM>writes
news:XXXX@XXXXX.COM...

>How can I get just 134 bytes form the start of each file using
>TIdHTTP?

If the server supports HTTP 1.1 and byte ranges, then you can set the
TIdHTTP's Request.ContentRangeStart and Request.ContentRangeEnd
properties before calling Get(). The server's response will tell you
whether the range was satisfied or not. A response code of 206 means
that partial data is sent. A response code of 200 means that the
entire file is being sent instead. You can optionally call Head()
before Get() to see if the server's 'Accept-Ranges' response header
contains 'bytes' or not. If it does, then you can request a byte
range. Otherwise, you will have to fallback to what you were
previously doing.


Gambit
Thanks for the explaination, Remy
--
 

Re: How to use TIdHTTP.Get to get just the first n bytes from a file?

Remy Lebeau (TeamB) writes:
Quote

var
DownloadingRange: Boolean = False;

procedure TForm1.Download;
begin
IdHTTP1.Head(URL);

IdHTTP1.Request.ContentRangeStart := 0;

if Pos('bytes',
LowerCase(IdHTTP1.Response.RawHeaders.Values['Accept-Ranges'])) <>0 then
begin
IdHTTP1.Request.ContentRangeEnd := iif(DownloadingRange, 134,
0);
IdHTTP1.ProtocolVersion := pv1_1;
IdHTTP1.OnHeadersAvailable := nil;
DownloadingRange := True;
end else
begin
IdHTTP1.Request.ContentRangeEnd := 0;
IdHTTP1.OnHeadersAvailable := TweakHeaders;
DownloadingRange := False;
end;

IdHTTP1.Get(URL, Stream);
//...
end;

procedure TForm1.TweakHeaders(Sender: TObject; AHeaders: TIdHeaderList;
var VContinue: Boolean);
var
Len: Integer;
begin
Len := StrToIntDef(Trim(AHeaders.Values['Content-Length']), 0);
if Len>134 then
begin
AHeaders.Values['Content-Length'] := '134';
AHeaders.Values['Connection'] := 'close';
end;
end;



Gambit


Thank you very much, master Gambit.
I tried you example but it doesn't seem to work with Indy '10.0.52'.
So I changed it back to 9 after downloading the 'DevSnapshot' -- where
IdHTTP.pas has timestamp of 11/17/2006-21:33:14 -- and everything works
fine now.
--
Ricardo Villela Coppola (CSP Controle e Automação Ltda)
 

Re: How to use TIdHTTP.Get to get just the first n bytes from a file?

"Remy Lebeau (TeamB)" <XXXX@XXXXX.COM>writes
Quote
if Pos('bytes',
LowerCase(IdHTTP1.Response.RawHeaders.Values['Accept-Ranges'])) <>0 then
begin
IdHTTP1.Request.ContentRangeEnd := iif(DownloadingRange, 134,
0);
Whoops, minor correction:
IdHTTP1.Request.ContentRangeEnd := 134;
Gambit
 

Re: How to use TIdHTTP.Get to get just the first n bytes from a file?

"Ricardo Villela Coppola" <XXXX@XXXXX.COM>writes
Quote
I tried you example but it doesn't seem to work with Indy '10.0.52'.
That is a very old build of Indy 10. The current snapshot is 10.1.6.
Gambit
 

Re: How to use TIdHTTP.Get to get just the first n bytes from a file?

Remy Lebeau (TeamB) writes:
Quote
"Ricardo Villela Coppola" <XXXX@XXXXX.COM>writes
news:45745519$XXXX@XXXXX.COM...

>I tried you example but it doesn't seem to work with Indy '10.0.52'.

That is a very old build of Indy 10. The current snapshot is 10.1.6.


Gambit


Ok, I will give it a try later on.
Thanks again.
--
Ricardo Villela Coppola (CSP Controle e Automação Ltda)