From 94667140aeb3886d22e4c1301423fe99aaf3fba4 Mon Sep 17 00:00:00 2001 From: George Hazan Date: Thu, 4 Jan 2024 13:38:02 +0300 Subject: Netlib: pascal code is completely isolated from C++ core using helpers --- plugins/Utils.pas/mirutils.pas | 55 +++++++++++++++---------------- plugins/Watrack/myshows/i_cookies.inc | 51 ++++++---------------------- plugins/Watrack/myshows/i_myshows_api.inc | 1 - 3 files changed, 36 insertions(+), 71 deletions(-) (limited to 'plugins') diff --git a/plugins/Utils.pas/mirutils.pas b/plugins/Utils.pas/mirutils.pas index c6066c8ee9..fb07550f08 100644 --- a/plugins/Utils.pas/mirutils.pas +++ b/plugins/Utils.pas/mirutils.pas @@ -404,7 +404,9 @@ function SendRequest(url:PAnsiChar;rtype:int;args:PAnsiChar=nil;hNetLib:THANDLE= var nlu:TNETLIBUSER; req :TNETLIBHTTPREQUEST; - resp:PNETLIBHTTPREQUEST; + resp:THANDLE; + bufLen:int; + pBuf:PAnsiChar; hTmpNetLib:THANDLE; nlh:array [0..1] of TNETLIBHTTPHEADER; buf:array [0..31] of AnsiChar; @@ -438,15 +440,16 @@ begin hTmpNetLib:=hNetLib; resp:=Netlib_HttpTransaction(hTmpNetLib,@req); - if resp<>nil then + if resp<>0 then begin - if resp^.resultCode=200 then + if Netlib_HttpResult(resp)=200 then begin - StrDup(result,resp.pData,resp.dataLength); + pBuf := Netlib_HttpBuffer(resp,bufLen); + StrDup(result,pBuf,bufLen); end else begin - result:=PAnsiChar(int_ptr(resp^.resultCode and $0FFF)); + result:=PAnsiChar(int_ptr(Netlib_HttpResult(resp) and $0FFF)); end; Netlib_FreeHttpRequest(resp); end; @@ -464,9 +467,10 @@ function GetFile(url:PAnsiChar; save_file:PAnsiChar; hNetLib:THANDLE=0; recurse_ var nlu:TNETLIBUSER; req :TNETLIBHTTPREQUEST; - resp:PNETLIBHTTPREQUEST; + resp:THANDLE; hSaveFile:THANDLE; - i:integer; + retCode, bufLen:integer; + pBuf:PAnsiChar; begin result:=false; if recurse_count>MAX_REDIRECT_RECURSE then @@ -477,8 +481,7 @@ begin FillChar(req,SizeOf(req),0); req.requestType:=REQUEST_GET; req.szUrl :=url; - req.flags :=NLHRF_NODUMP; - + req.flags :=NLHRF_NODUMP or NLHRF_REDIRECT; FillChar(nlu,SizeOf(nlu),0); if hNetLib=0 then @@ -489,29 +492,19 @@ begin end; resp:=Netlib_HttpTransaction(hNetLib,@req); - if resp<>nil then + if resp<>0 then begin - if resp^.resultCode=200 then + retCode:=Netlib_HttpResult(resp); + if retCode=200 then begin hSaveFile:=Rewrite(save_file); if hSaveFile<>THANDLE(INVALID_HANDLE_VALUE) then begin - BlockWrite(hSaveFile,resp^.pData^,resp^.dataLength); + pBuf := Netlib_HttpBuffer(resp,bufLen); + BlockWrite(hSaveFile,pBuf^,bufLen); CloseHandle(hSaveFile); result:=true; end - end - else if (resp.resultCode>=300) and (resp.resultCode<400) then - begin - // get new location - for i:=0 to resp^.headersCount-1 do - begin - if StrCmp(resp^.headers^[i].szName,'Location')=0 then - begin - result:=GetFile(resp^.headers^[i].szValue,save_file,hNetLib,recurse_count+1); - break; - end - end; end; Netlib_FreeHttpRequest(resp); @@ -583,8 +576,9 @@ function LoadImageURL(url:PAnsiChar;size:integer=0):HBITMAP; var nlu:TNETLIBUSER; req :TNETLIBHTTPREQUEST; - resp:PNETLIBHTTPREQUEST; - hNetLib:THANDLE; + resp,hNetLib:THANDLE; + bufLen:integer; + pBuf:PAnsiChar; begin result:=0; if (url=nil) or (url^=#0) then @@ -601,10 +595,13 @@ begin hNetLib:=Netlib_RegisterUser(@nlu); resp:=Netlib_HttpTransaction(hNetLib,@req); - if resp<>nil then + if resp<>0 then begin - if resp^.resultCode=200 then - result := Image_LoadFromMem(resp.pData, resp.dataLength, FIF_JPEG); + if Netlib_HttpResult(resp)=200 then + begin + pBuf := Netlib_HttpBuffer(resp,bufLen); + result := Image_LoadFromMem(pBuf, bufLen, FIF_JPEG); + end; Netlib_FreeHttpRequest(resp); end; diff --git a/plugins/Watrack/myshows/i_cookies.inc b/plugins/Watrack/myshows/i_cookies.inc index cd01ef6621..82c12f3b3d 100644 --- a/plugins/Watrack/myshows/i_cookies.inc +++ b/plugins/Watrack/myshows/i_cookies.inc @@ -2,46 +2,14 @@ const cookies:pAnsiChar=nil; -function ExtractCookies(resp:PNETLIBHTTPREQUEST):integer; -var - cnt,len:integer; - p,pc:pAnsiChar; -begin - result:=0; - - mFreeMem(cookies); - mGetMem(cookies,1024); - - pc:=cookies; - for cnt:=0 to resp^.headersCount-1 do - begin - with resp^.headers[cnt] do - if StrCmp(szName,'Set-Cookie')=0 then - begin - len:=0; - p:=szValue; - while (p^<>#0) and (p^<>';') do - begin - inc(p); - inc(len); - end; - if pc<>cookies then - begin - pc^:=';'; inc(pc); - pc^:=' '; inc(pc); - end; - pc:=StrCopyE(pc,szValue,len); - inc(result); - end; - end; -end; - function SendRequestCookies(url:PAnsiChar;useCookies:boolean):pAnsiChar; var nlu:TNETLIBUSER; req :TNETLIBHTTPREQUEST; - resp:PNETLIBHTTPREQUEST; + resp:THANDLE; hTmpNetLib:THANDLE; + bufLen:int; + pBuf:PAnsiChar; nlh:array [0..10] of TNETLIBHTTPHEADER; begin result:=nil; @@ -66,20 +34,21 @@ begin hTmpNetLib:=Netlib_RegisterUser(@nlu); resp:=Netlib_HttpTransaction(hTmpNetLib,@req); - if resp<>nil then + if resp<>0 then begin - if resp^.resultCode=200 then + if Netlib_HttpResult(resp)=200 then begin - if resp.pData<>nil then - StrDup(result,resp.pData,resp.dataLength) + pBuf := Netlib_HttpBuffer(resp,bufLen); + if pBuf<>nil then + StrDup(result,pBuf,bufLen) else result:=PAnsiChar(200); if not useCookies then - ExtractCookies(resp); + cookies := Netlib_HttpCookies(resp); end else begin - result:=pAnsiChar(int_ptr(resp^.resultCode and $0FFF)); + result:=pAnsiChar(int_ptr(Netlib_HttpResult(resp) and $0FFF)); end; Netlib_FreeHttpRequest(resp); end; diff --git a/plugins/Watrack/myshows/i_myshows_api.inc b/plugins/Watrack/myshows/i_myshows_api.inc index f92df86b50..828fb5c650 100644 --- a/plugins/Watrack/myshows/i_myshows_api.inc +++ b/plugins/Watrack/myshows/i_myshows_api.inc @@ -88,7 +88,6 @@ begin StrReplace(request,'',buf); res:=SendRequestCookies(request,false); -// res:=SendRequest(request,REQUEST_GET); if res<>nil then begin if uint_ptr(res)<$0FFF then -- cgit v1.2.3