Skip to content

Commit d966854

Browse files
committed
Added check if url is valid for webstreaming.(2)
1 parent 854c7ef commit d966854

File tree

2 files changed

+180
-164
lines changed

2 files changed

+180
-164
lines changed

examples/uos_httpgetthread.pas

Lines changed: 90 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -12,137 +12,145 @@
1212
interface
1313

1414
uses
15-
Classes, SysUtils, Pipes;
15+
Classes,
16+
SysUtils,
17+
Pipes;
1618

1719
type
18-
19-
{ TThreadHttpGetter }
20+
21+
{ TThreadHttpGetter }
2022

2123
TThreadHttpGetter = class(TThread)
2224
private
2325
FOutStream: TOutputPipeStream;
24-
FWantedURL: String;
25-
FIcyMetaInt: Int64;
26+
FWantedURL: string;
27+
FIcyMetaInt: int64;
2628
FOnIcyMetaInt: TNotifyEvent;
27-
property OnIcyMetaInt: TNotifyEvent read FOnIcyMetaInt write FOnIcyMetaInt;
29+
property OnIcyMetaInt: TNotifyEvent read FOnIcyMetaInt write FOnIcyMetaInt;
2830
procedure DoIcyMetaInt;
29-
function GetRedirectURL(AResponseStrings: TStrings): String;
31+
function GetRedirectURL(AResponseStrings: TStrings): string;
3032
procedure Headers(Sender: TObject);
3133
protected
3234
procedure Execute; override;
3335
public
3436
FIsRunning: Boolean;
3537
ICYenabled: Boolean;
36-
property IcyMetaInt: Int64 read FIcyMetaInt;
38+
property IcyMetaInt: int64 read FIcyMetaInt;
3739
property IsRunning: Boolean read FIsRunning;
38-
constructor Create(AWantedURL: String; AOutputStream: TOutputPipeStream);
39-
end;
40+
constructor Create(AWantedURL: string; AOutputStream: TOutputPipeStream);
41+
end;
4042

4143
implementation
44+
4245
uses
4346
fphttpclient,
4447
openssl, { This implements the procedure InitSSLInterface }
4548
opensslsockets;
4649

47-
{ TThreadHttpGetter }
50+
{ TThreadHttpGetter }
4851

49-
function TThreadHttpGetter.GetRedirectURL(AResponseStrings: TStrings): String;
52+
function TThreadHttpGetter.GetRedirectURL(AResponseStrings: TStrings): string;
5053
var
51-
S: String;
52-
F: Integer;
53-
Search: String = 'location:';
54+
S: string;
55+
F: integer;
56+
Search: string = 'location:';
5457
begin
5558
Result := '';
56-
for S In AResponseStrings do
59+
for S in AResponseStrings do
5760
begin
58-
// WriteLn(S);
61+
// WriteLn(S);
5962
F := Pos(Search, Lowercase(s));
6063

6164
if F > 0 then
6265
begin
6366
Inc(F, Length(Search));
64-
Exit(Trim(Copy(S, F, Length(S)-F+1)));
67+
Exit(Trim(Copy(S, F, Length(S) - F + 1)));
6568
end;
6669
end;
6770
end;
6871

69-
procedure TThreadHttpGetter.DoIcyMetaInt;
70-
begin
71-
if Assigned(FOnIcyMetaInt) then
72-
FOnIcyMetaInt(Self);
73-
end;
74-
75-
procedure TThreadHttpGetter.Headers(Sender: TObject );
76-
begin
77-
FIcyMetaInt := StrToInt64Def(TFPHTTPClient(Sender).GetHeader(TFPHTTPClient(Sender).ResponseHeaders, 'icy-metaint'),0);
78-
if (FIcyMetaInt>0) and (FOnIcyMetaInt<>nil) then
79-
Synchronize(@DoIcyMetaInt);
72+
procedure TThreadHttpGetter.DoIcyMetaInt;
73+
begin
74+
if Assigned(FOnIcyMetaInt) then
75+
FOnIcyMetaInt(Self);
76+
end;
77+
78+
procedure TThreadHttpGetter.Headers(Sender: TObject);
79+
begin
80+
FIcyMetaInt := StrToInt64Def(TFPHTTPClient(Sender).GetHeader(TFPHTTPClient(Sender).ResponseHeaders, 'icy-metaint'), 0);
81+
if (FIcyMetaInt > 0) and (FOnIcyMetaInt <> nil) then
82+
Synchronize(@DoIcyMetaInt);
8083
end;
8184

8285
procedure TThreadHttpGetter.Execute;
8386
var
8487
Http: TFPHTTPClient;
85-
URL: String;
88+
URL: string;
8689
err: shortint = 0;
8790
begin
88-
InitSSLInterface;
89-
Http := TFPHTTPClient.Create(nil);
90-
http.AllowRedirect := true;
91-
http.IOTimeout := 2000;
92-
URL := FWantedURL;
93-
repeat
94-
try
95-
Http.RequestHeaders.Clear;
96-
if ICYenabled = true then
97-
Http.OnHeaders := @Headers;
98-
// writeln(' avant http.get');
99-
Http.Get(URL, FOutStream);
100-
// writeln(' apres http.get');
101-
except
102-
on e: EHTTPClient do
103-
begin
104-
// writeln(' Http.ResponseStatusCode ' +inttostr(Http.ResponseStatusCode));
105-
if (Http.ResponseStatusCode > 399) or (Http.ResponseStatusCode < 1) then // not accessible
106-
begin
107-
FIsRunning:=False;
108-
break;
91+
URL := FWantedURL;
92+
if pos(' ', URL) > 0 then
93+
FIsRunning := False
94+
else
95+
begin
96+
InitSSLInterface;
97+
Http := TFPHTTPClient.Create(nil);
98+
http.AllowRedirect := True;
99+
http.IOTimeout := 2000;
100+
repeat
101+
try
102+
Http.RequestHeaders.Clear;
103+
if ICYenabled = True then
104+
Http.OnHeaders := @Headers;
105+
// writeln(' avant http.get');
106+
Http.Get(URL, FOutStream);
107+
// writeln(' apres http.get');
108+
except
109+
on e: EHTTPClient do
110+
begin
111+
// writeln(' Http.ResponseStatusCode ' +inttostr(Http.ResponseStatusCode));
112+
if (Http.ResponseStatusCode > 399) or (Http.ResponseStatusCode < 1) then // not accessible
113+
begin
114+
FIsRunning := False;
115+
break;
116+
end;
117+
if Http.ResponseStatusCode = 302 then
118+
begin
119+
URL := GetRedirectURL(Http.ResponseHeaders);
120+
if URL <> '' then
121+
Continue;
122+
end
123+
else
124+
Break;
125+
// raise E;
126+
end;
127+
on e: Exception do
128+
begin
129+
// WriteLn(e.Message);
130+
end
131+
else
132+
// Raise;
133+
Break;
109134
end;
110-
if Http.ResponseStatusCode = 302 then
111-
begin
112-
URL := GetRedirectURL(Http.ResponseHeaders);
113-
if URL <> '' then Continue;
114-
end
115-
else
116-
Break;
117-
// raise E;
118-
end;
119-
on e: Exception do
120-
begin
121-
// WriteLn(e.Message);
122-
end
123-
else
124-
// Raise;
125135
Break;
126-
end;
127-
Break;
128-
until (False);
129-
130-
try
131-
//FOutStream.Free;
132-
Http.Free;
133-
finally
134-
// make sure this is set to false when done
135-
FIsRunning:=False;
136+
until (False);
137+
try
138+
//FOutStream.Free;
139+
Http.Free;
140+
finally
141+
// make sure this is set to false when done
142+
FIsRunning := False;
143+
end;
136144
end;
137145
end;
138-
139-
constructor TThreadHttpGetter.Create(AWantedURL: String; AOutputStream: TOutputPipeStream);
146+
147+
constructor TThreadHttpGetter.Create(AWantedURL: string; AOutputStream: TOutputPipeStream);
140148
begin
141149
inherited Create(True);
142-
ICYenabled:=false;
143-
FIsRunning:=True;
144-
FWantedURL:=AWantedURL;
145-
FOutStream:=AOutputStream;
150+
ICYenabled := False;
151+
FIsRunning := True;
152+
FWantedURL := AWantedURL;
153+
FOutStream := AOutputStream;
146154
// Start;
147155
end;
148156

0 commit comments

Comments
 (0)