|
12 | 12 | interface
|
13 | 13 |
|
14 | 14 | uses
|
15 |
| - Classes, SysUtils, Pipes; |
| 15 | + Classes, |
| 16 | + SysUtils, |
| 17 | + Pipes; |
16 | 18 |
|
17 | 19 | type
|
18 |
| - |
19 |
| - { TThreadHttpGetter } |
| 20 | + |
| 21 | + { TThreadHttpGetter } |
20 | 22 |
|
21 | 23 | TThreadHttpGetter = class(TThread)
|
22 | 24 | private
|
23 | 25 | FOutStream: TOutputPipeStream;
|
24 |
| - FWantedURL: String; |
25 |
| - FIcyMetaInt: Int64; |
| 26 | + FWantedURL: string; |
| 27 | + FIcyMetaInt: int64; |
26 | 28 | FOnIcyMetaInt: TNotifyEvent;
|
27 |
| - property OnIcyMetaInt: TNotifyEvent read FOnIcyMetaInt write FOnIcyMetaInt; |
| 29 | + property OnIcyMetaInt: TNotifyEvent read FOnIcyMetaInt write FOnIcyMetaInt; |
28 | 30 | procedure DoIcyMetaInt;
|
29 |
| - function GetRedirectURL(AResponseStrings: TStrings): String; |
| 31 | + function GetRedirectURL(AResponseStrings: TStrings): string; |
30 | 32 | procedure Headers(Sender: TObject);
|
31 | 33 | protected
|
32 | 34 | procedure Execute; override;
|
33 | 35 | public
|
34 | 36 | FIsRunning: Boolean;
|
35 | 37 | ICYenabled: Boolean;
|
36 |
| - property IcyMetaInt: Int64 read FIcyMetaInt; |
| 38 | + property IcyMetaInt: int64 read FIcyMetaInt; |
37 | 39 | property IsRunning: Boolean read FIsRunning;
|
38 |
| - constructor Create(AWantedURL: String; AOutputStream: TOutputPipeStream); |
39 |
| - end; |
| 40 | + constructor Create(AWantedURL: string; AOutputStream: TOutputPipeStream); |
| 41 | + end; |
40 | 42 |
|
41 | 43 | implementation
|
| 44 | + |
42 | 45 | uses
|
43 | 46 | fphttpclient,
|
44 | 47 | openssl, { This implements the procedure InitSSLInterface }
|
45 | 48 | opensslsockets;
|
46 | 49 |
|
47 |
| -{ TThreadHttpGetter } |
| 50 | + { TThreadHttpGetter } |
48 | 51 |
|
49 |
| -function TThreadHttpGetter.GetRedirectURL(AResponseStrings: TStrings): String; |
| 52 | +function TThreadHttpGetter.GetRedirectURL(AResponseStrings: TStrings): string; |
50 | 53 | var
|
51 |
| - S: String; |
52 |
| - F: Integer; |
53 |
| - Search: String = 'location:'; |
| 54 | + S: string; |
| 55 | + F: integer; |
| 56 | + Search: string = 'location:'; |
54 | 57 | begin
|
55 | 58 | Result := '';
|
56 |
| - for S In AResponseStrings do |
| 59 | + for S in AResponseStrings do |
57 | 60 | begin
|
58 |
| - // WriteLn(S); |
| 61 | + // WriteLn(S); |
59 | 62 | F := Pos(Search, Lowercase(s));
|
60 | 63 |
|
61 | 64 | if F > 0 then
|
62 | 65 | begin
|
63 | 66 | Inc(F, Length(Search));
|
64 |
| - Exit(Trim(Copy(S, F, Length(S)-F+1))); |
| 67 | + Exit(Trim(Copy(S, F, Length(S) - F + 1))); |
65 | 68 | end;
|
66 | 69 | end;
|
67 | 70 | end;
|
68 | 71 |
|
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); |
80 | 83 | end;
|
81 | 84 |
|
82 | 85 | procedure TThreadHttpGetter.Execute;
|
83 | 86 | var
|
84 | 87 | Http: TFPHTTPClient;
|
85 |
| - URL: String; |
| 88 | + URL: string; |
86 | 89 | err: shortint = 0;
|
87 | 90 | 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; |
109 | 134 | 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; |
125 | 135 | 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; |
136 | 144 | end;
|
137 | 145 | end;
|
138 |
| - |
139 |
| -constructor TThreadHttpGetter.Create(AWantedURL: String; AOutputStream: TOutputPipeStream); |
| 146 | + |
| 147 | +constructor TThreadHttpGetter.Create(AWantedURL: string; AOutputStream: TOutputPipeStream); |
140 | 148 | begin
|
141 | 149 | 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; |
146 | 154 | // Start;
|
147 | 155 | end;
|
148 | 156 |
|
|
0 commit comments