Guest User

http downloader

a guest
Nov 30th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 7.40 KB | None | 0 0
  1. unit hiHTTP_Get;
  2.  
  3. interface
  4.  
  5. {$I share.inc}
  6.  
  7. uses Kol,Share,WinInet,Windows,Debug,hiCharset;
  8.  
  9. type
  10.   THIHTTP_Get = class(TDebug)
  11.    private
  12.     th:PThread;
  13.     FStop:boolean;
  14.     GData:TData;
  15.     FSize:cardinal;
  16.     FBusy:boolean;
  17.  
  18.     fs:PStream;
  19.  
  20.     procedure ShowInfo;
  21.     procedure EndDownload;
  22.     procedure OnDownload;
  23.     function Execute(Sender:PThread): Integer;
  24.    public
  25.     _prop_URL:string;
  26.     _prop_FileName:string;
  27.     _prop_Wait:boolean;
  28.     _prop_Proxy:string;
  29.     _prop_ProxyUsername:string;
  30.     _prop_ProxyPassword:string;
  31.     _prop_Length:cardinal;
  32.     _prop_UserAgent:PChar;
  33.     _prop_Method:integer;
  34.  
  35.     _data_FileName:THI_Event;
  36.     _data_URL:THI_Event;
  37.     _data_Position:THI_Event;
  38.     _data_Length:THI_Event;
  39.     _data_Proxy:THI_Event;
  40.     _data_ProxyUsername:THI_Event;
  41.     _data_ProxyPassword:THI_Event;
  42.     _data_PostData:THI_Event;
  43.     _event_onURLSize:THI_Event;
  44.     _event_onDownload:THI_Event;
  45.     _event_onStatus:THI_Event;
  46.     _event_onStop:THI_Event;
  47.  
  48.    procedure _work_doDownload(var _Data:TData; Index:word);
  49.    procedure _work_doStop(var _Data:TData; Index:word);
  50.    procedure _work_GetURLSize(var _Data:TData; Index:word);
  51.    procedure _var_Busy(var _Data:TData; Index:word);
  52.   end;
  53.  
  54. implementation
  55.  
  56. function GetUrlInfo(const FileURL, agent: string):cardinal;
  57. var
  58.   hSession, hFile: hInternet;
  59.   dwBuffer:array[0..20] of char;
  60.   dwBufferLen, dwIndex: DWORD;
  61. begin
  62.   Result := 0;
  63.   hSession := InternetOpen(PChar(agent),INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
  64.   if Assigned(hSession) then
  65.    begin
  66.     hFile := InternetOpenURL(hSession, PChar(FileURL),nil,0,INTERNET_FLAG_RELOAD, 0);
  67.     dwIndex := 0;
  68.     dwBufferLen := 20;
  69.     if HttpQueryInfo(hFile, HTTP_QUERY_CONTENT_LENGTH, @dwBuffer[0], dwBufferLen, dwIndex)
  70.       then Result := str2int(dwBuffer);
  71.     if Assigned(hFile) then InternetCloseHandle(hFile);
  72.     InternetCloseHandle(hsession);
  73.    end;
  74. end;
  75.  
  76. function THIHTTP_Get.Execute;
  77. var
  78.   NetHandle: HINTERNET;
  79.   UrlHandle: HINTERNET;
  80.   ConHandle: HINTERNET;
  81.  
  82.   Buffer: array[0..1024] of char;
  83.   BytesRead, len: cardinal;
  84.   Url,Fname,Head:string;
  85.   PI:TInternetProxyInfo;
  86.   dwStatus,dwStatusSize:cardinal;
  87.   dwNil:DWORD;
  88.   dt:TData; s,s1:string;
  89.   i:integer;
  90. begin
  91.    FBusy := true; dtNull(dt);
  92.    NetHandle := InternetOpen(_prop_UserAgent, INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
  93.    s := ReadString(dt, _data_Proxy, _prop_Proxy);
  94.    if s<>'' then begin
  95.      PI.dwAccessType := INTERNET_OPEN_TYPE_PROXY;
  96.      PI.lpszProxy := PChar(s);
  97.      PI.lpszProxyByPass := nil;
  98.      InternetSetOption(NetHandle,INTERNET_OPTION_PROXY,@PI,sizeof(PI));
  99.    end;
  100.    FStop := false;
  101.    if Assigned(NetHandle) then
  102.     begin
  103.      Url := ReadString(GData,_data_URL,_prop_URL);
  104.  
  105.      BytesRead := ReadInteger(GData,_data_Position,0);
  106.      len := ReadInteger(GData,_data_Length,_prop_Length);
  107.      if BytesRead > 0 then
  108.        Head := 'Range: bytes=' + Int2Str(BytesRead) + '-' + Int2Str(BytesRead + Len)
  109.      else Head := '';
  110.  
  111.      if _prop_Method = 1 then
  112.        begin
  113.          if pos('https', Url) > 0 then
  114.            begin
  115.              dwStatus := INTERNET_DEFAULT_HTTPS_PORT;
  116.              dwStatusSize := INTERNET_FLAG_SECURE;
  117.            end
  118.          else
  119.            begin
  120.              dwStatus := INTERNET_DEFAULT_HTTP_PORT;
  121.              dwStatusSize := 0;
  122.            end;
  123.          i := pos('//', Url);
  124.          if i <> -1 then
  125.            delete(url, 1, i + 1);
  126.          i := pos('/', Url);
  127.          if i = -1 then
  128.            begin
  129.              s := url;
  130.              s1 := '';
  131.            end
  132.          else
  133.            begin
  134.              s := copy(url, 1, i-1);
  135.              s1 := copy(url, i+1, length(url));
  136.            end;
  137.          ConHandle := InternetConnect(NetHandle,PChar(s),dwStatus,nil,nil,INTERNET_SERVICE_HTTP, 0, 0);
  138.          UrlHandle := HttpOpenRequest(ConHandle,'POST',PChar(s1),nil,nil,0,INTERNET_FLAG_KEEP_CONNECTION or dwStatusSize,0);
  139.          s := 'Content-Type: application/x-www-form-urlencoded';  
  140.          s1 := ReadString(GData, _data_PostData);
  141.          HttpSendRequest(UrlHandle, PChar(s), length(s), PAnsiChar(s1), Length(s1));
  142.        end
  143.      else
  144.         UrlHandle := InternetOpenUrl(NetHandle, PChar(Url), PChar(Head), cardinal(-1), INTERNET_FLAG_RELOAD+INTERNET_FLAG_NO_AUTH, 0);
  145.  
  146.      if Assigned(UrlHandle) then
  147.       begin
  148.        dwStatusSize := sizeof(dwStatus);
  149.        dwNil := 0;
  150.        HttpQueryInfo(UrlHandle, HTTP_QUERY_FLAG_NUMBER or
  151.          HTTP_QUERY_STATUS_CODE, @dwStatus, dwStatusSize, dwNil);
  152.        if dwStatus=HTTP_STATUS_PROXY_AUTH_REQ then begin
  153.          s := ReadString(dt, _data_ProxyUsername, _prop_ProxyUsername);
  154.          s1 := ReadString(dt, _data_ProxyPassword, _prop_ProxyPassword);
  155.          if (s<>'') and (s1<>'') then begin
  156.            repeat InternetReadFile(UrlHandle, @Buffer, SizeOf(Buffer), BytesRead); until BytesRead=0;
  157.            HttpSendRequest(UrlHandle,PAnsiChar('Proxy-Authorization: Basic '+Base64_Code(s+':'+s1)+#13#10),DWORD(-1),nil,0);
  158.          end;
  159.        end;
  160.        
  161.        FillChar(Buffer, SizeOf(Buffer), 0);
  162.        FSize := 0;
  163.        Fname := ReadString(GData,_data_FileName,_prop_FileName);
  164.        if Fname = '' then fs := NewMemoryStream
  165.        else fs := NewWriteFileStream(Fname);
  166.        repeat
  167.         FillChar(Buffer, SizeOf(Buffer), 0);
  168.         InternetReadFile(UrlHandle, @Buffer, SizeOf(Buffer), BytesRead);
  169.         fs.Write(Buffer,BytesRead);
  170.         inc(FSize,BytesRead);
  171.         if _prop_Wait then
  172.          ShowInfo
  173.         else th.Synchronize( ShowInfo );
  174.         //ProcessMessages;
  175.        until (BytesRead = 0)or FStop;
  176.        InternetCloseHandle(UrlHandle);
  177.        InternetCloseHandle(NetHandle);
  178.        
  179.        if Fname = '' then
  180.          fs.Position := 0
  181.        else
  182.         begin
  183.           fs.Free;
  184.           fs := nil;
  185.         end;
  186.        if _prop_Wait then
  187.         OnDownload
  188.        else th.Synchronize( OnDownload );
  189.       end
  190.      else ;//MessageBox(0,'Can''t open URL!','Error',MB_OK);
  191.      InternetCloseHandle(NetHandle);
  192.     end
  193.    else ;//MessageBox(0,'I can not connect to Internet!','Error',MB_OK);
  194.    Result := 0;
  195.    //th.Free;
  196.    FBusy := false;
  197.    if _prop_Wait then
  198.      EndDownload
  199.    else th.Synchronize( EndDownload );
  200. end;
  201.  
  202. procedure THIHTTP_Get.ShowInfo;
  203. begin
  204.    _hi_OnEvent(_event_onStatus,integer(fsize));
  205. end;
  206.  
  207. procedure THIHTTP_Get.EndDownload;
  208. begin
  209.    _hi_OnEvent(_event_onStop);
  210. end;
  211.  
  212. procedure THIHTTP_Get.OnDownload;
  213. begin
  214.   if fs = nil then
  215.      _hi_OnEvent(_event_onDownload)
  216.   else
  217.    begin
  218.      _hi_OnEvent(_event_onDownload,fs);
  219.      fs.Free;
  220.    end;
  221. end;
  222.  
  223. procedure THIHTTP_Get._work_doDownload;
  224. begin
  225.    GData := _Data;
  226.    //if th <> nil then
  227.    //  th.Free;
  228.    if _prop_Wait then
  229.     Execute(nil)
  230.    else
  231.     begin
  232.      {$ifdef F_P}
  233.      th := NewThreadForFPC;
  234.      {$else}
  235.      th := NewThread;
  236.      {$endif}
  237.      th.OnExecute := Execute;
  238.      th.AutoFree := true;
  239.      th.Resume;
  240.     end;
  241. end;
  242.  
  243. procedure THIHTTP_Get._work_doStop;
  244. begin
  245.    FStop := true;
  246.    //_debug('1');
  247.    //th.WaitFor;
  248.    //_debug('2');
  249.    //th.Free;
  250.    //th := nil;
  251. end;
  252.  
  253. procedure THIHTTP_Get._var_Busy;
  254. begin
  255.    dtInteger(_data,byte(FBusy));
  256. end;
  257.  
  258. procedure THIHTTP_Get._work_GetURLSize;
  259. var
  260.    Url:string;
  261. begin
  262.    Url := ReadString(_Data,_data_URL,_prop_URL);
  263.    _hi_OnEvent(_event_onURLSize,integer(GetUrlInfo(URL, _prop_UserAgent)));
  264. end;
  265.  
  266. end.
Add Comment
Please, Sign In to add comment