Trong Posted January 15, 2023 Share Posted January 15, 2023 Standalone code: expandcollapse popupOpt("MustDeclareVars", 1) ;#include <String.au3> ;#include "WinHttp.au3" ; #INDEX# =================================================================================== ; Title ...............: _URL_Split ; Author... ...........: Dao Van Trong - TRONG.LIVE ; =========================================================================================== ; #CONSTANTS# =============================================================================== Global Const $hWINHTTPDLL__WINHTTP = DllOpen("winhttp.dll") DllOpen("winhttp.dll") ; making sure reference count never reaches 0 ;============================================================================================ Global $sURL_Input = 'https://Trong:Pa$$wo0d@files.trong.live:443/prv/test.jpg' Global $sURL_Protocol, $sURL_Protocol_Number, $sURL_Domain, $sURL_Port, $sURL_User, $sURL_Password, $sURL_FilePathName, $sURL_FilePATH, $sURL_FileName _URL_Split($sURL_Input, $sURL_Protocol, $sURL_Domain, $sURL_Port, $sURL_User, $sURL_Password, $sURL_FilePathName, $sURL_FilePATH, $sURL_FileName) ConsoleWrite("! URL : " & $sURL_Input & @CRLF) ConsoleWrite("+ _URL_GetDomain() > " & _URL_GetDomain($sURL_Input) & @CRLF) ConsoleWrite("+ _URL_Split() > " & @CRLF) ConsoleWrite("- Protocol : " & $sURL_Protocol & @CRLF) ConsoleWrite("- Domain : " & $sURL_Domain & @CRLF) ConsoleWrite("- Port : " & $sURL_Port & @CRLF) ConsoleWrite("- User : " & $sURL_User & @CRLF) ConsoleWrite("- Password : " & $sURL_Password & @CRLF) ConsoleWrite("- FilePathName: " & $sURL_FilePathName & @CRLF) ConsoleWrite("- FilePath : " & $sURL_FilePath & @CRLF) ConsoleWrite("- FileName : " & $sURL_FileName & @CRLF) Func _URL_Split($sURL_Input, ByRef $sURL_Protocol, ByRef $sURL_Domain, ByRef $sURL_Port, ByRef $sURL_User, ByRef $sURL_Password, ByRef $sURL_FilePathName, ByRef $sURL_FilePATH, ByRef $sURL_FileName) If StringStripWS($sURL_Input, 8) = '' Then Return SetError(1, 0, 0) $sURL_Input = StringReplace($sURL_Input, "\", "/") If (Not StringInStr($sURL_Input, '://')) Then $sURL_Input = 'http://' & $sURL_Input __URL_Split($sURL_Input, $sURL_Protocol, $sURL_Domain, $sURL_FilePATH, $sURL_FileName) StringReplace($sURL_Input, '@', '') If @extended > 1 Then Local $sUser, $aUser = _StringBetween($sURL_Input, $sURL_Protocol & "://", '@') If IsArray($aUser) Then $sUser = $aUser[0] $sURL_Input = StringReplace($sURL_Input, $sURL_Protocol & "://" & $sUser & '@', $sURL_Protocol & "://" & $sUser & '%40') EndIf EndIf Local $ORG_URL_Protocol = $sURL_Protocol, $sFake_Protocol = 'http', $iFake_Protocol = False Switch $sURL_Protocol Case 'http', 'https', 'ftp' Case Else $iFake_Protocol = True $sURL_Input = StringReplace($sURL_Input, $sURL_Protocol & "://", $sFake_Protocol & "://") EndSwitch Local $aUrl = _WinHttpCrackUrl($sURL_Input) If IsArray($aUrl) Then If $iFake_Protocol Then $sURL_Protocol = $ORG_URL_Protocol Else $sURL_Protocol = ($aUrl[0] == '' ? $sURL_Protocol : $aUrl[0]) EndIf ;$sURL_Protocol_Number = $aUrl[1] $sURL_Domain = ($aUrl[2] == '' ? $sURL_Domain : $aUrl[2]) $sURL_Port = $aUrl[3] $sURL_User = StringReplace($aUrl[4], '%40', '@') $sURL_Password = $aUrl[5] $sURL_FilePathName = ($aUrl[6] == '' ? $sURL_FilePATH & $sURL_FileName : $aUrl[6]) ;$sURL_ExtraInfo = $aUrl[7] Return $aUrl EndIf Return SetError(1, 0, "") EndFunc ;==>_URL_Split Func __URL_Split($sURL_Input, ByRef $sURL_Protocol, ByRef $sURL_Domain, ByRef $sURL_FilePATH, ByRef $sURL_FileName) Local $sURL_Protocol_Number_Pattern = '^(?s)(?i)(http|ftp|sftp|ftps|https|file)://(.*?/|.*$)(.*/){0,}(.*)$' Local $sURL_Pattern = "^(?i)(?:(?:[a-z]+):\/\/)?" & "(?:(?:(?:[^@:]+))" & "(?::(?:[^@]+))?@)?" & "([^\/:]+)" & "(?::(?:\d+))?" & "(?:\/(?:[^?]+)?)?" & "(?:\?\N+)?" Local $aURL_Pattern = StringRegExp($sURL_Input, $sURL_Protocol_Number_Pattern, 2) If Not IsArray($aURL_Pattern) Or UBound($aURL_Pattern) - 1 <> 4 Then Return SetError(1, 0, 0) If StringRight($aURL_Pattern[2], 1) = '/' Then $aURL_Pattern[2] = StringTrimRight($aURL_Pattern[2], 1) $aURL_Pattern[3] = '/' & $aURL_Pattern[3] EndIf $sURL_Protocol = $aURL_Pattern[1] Local $aHost = StringRegExp($sURL_Input, $sURL_Pattern, 1) If Not @error And IsArray($aHost) Then $sURL_Domain = $aHost[0] Else $sURL_Domain = $aURL_Pattern[2] EndIf $sURL_FilePATH = $aURL_Pattern[3] $sURL_FileName = $aURL_Pattern[4] Return $aURL_Pattern EndFunc ;==>__URL_Split Func _URL_GetDomain($sURL_Input) Local $sURL_Pattern = "^(?i)(?:(?:[a-z]+):\/\/)?" & _ ; Protocol "(?:(?:(?:[^@:]+))" & _ ; Username "(?::(?:[^@]+))?@)?" & _ ; Password "([^\/:]+)" & _ ; Host "(?::(?:\d+))?" & _ ; Port "(?:\/(?:[^?]+)?)?" & _ ; Path "(?:\?\N+)?" ; Query Local $aHost = StringRegExp($sURL_Input, $sURL_Pattern, 1) If IsArray($aHost) Then Return $aHost[0] Return "" EndFunc ;==>_URL_GetDomain Func _WinHttpCrackUrl($sURL, $iFlag = Default) __WinHttpDefault($iFlag, 0x80000000) Local $tURL_COMPONENTS = DllStructCreate("dword StructSize;" & "ptr SchemeName;" & "dword SchemeNameLength;" & "int Scheme;" & "ptr HostName;" & "dword HostNameLength;" & "word Port;" & "ptr UserName;" & "dword UserNameLength;" & "ptr Password;" & "dword PasswordLength;" & "ptr UrlPath;" & "dword UrlPathLength;" & "ptr ExtraInfo;" & "dword ExtraInfoLength") DllStructSetData($tURL_COMPONENTS, 1, DllStructGetSize($tURL_COMPONENTS)) Local $tBuffers[6] Local $iURLLen = StringLen($sURL) For $i = 0 To 5 $tBuffers[$i] = DllStructCreate("wchar[" & $iURLLen + 1 & "]") Next DllStructSetData($tURL_COMPONENTS, "SchemeNameLength", $iURLLen) DllStructSetData($tURL_COMPONENTS, "SchemeName", DllStructGetPtr($tBuffers[0])) DllStructSetData($tURL_COMPONENTS, "HostNameLength", $iURLLen) DllStructSetData($tURL_COMPONENTS, "HostName", DllStructGetPtr($tBuffers[1])) DllStructSetData($tURL_COMPONENTS, "UserNameLength", $iURLLen) DllStructSetData($tURL_COMPONENTS, "UserName", DllStructGetPtr($tBuffers[2])) DllStructSetData($tURL_COMPONENTS, "PasswordLength", $iURLLen) DllStructSetData($tURL_COMPONENTS, "Password", DllStructGetPtr($tBuffers[3])) DllStructSetData($tURL_COMPONENTS, "UrlPathLength", $iURLLen) DllStructSetData($tURL_COMPONENTS, "UrlPath", DllStructGetPtr($tBuffers[4])) DllStructSetData($tURL_COMPONENTS, "ExtraInfoLength", $iURLLen) DllStructSetData($tURL_COMPONENTS, "ExtraInfo", DllStructGetPtr($tBuffers[5])) Local $aCall = DllCall($hWINHTTPDLL__WINHTTP, "bool", "WinHttpCrackUrl", "wstr", $sURL, "dword", $iURLLen, "dword", $iFlag, "struct*", $tURL_COMPONENTS) If @error Or Not $aCall[0] Then Return SetError(1, 0, 0) Local $aRet[8] = [DllStructGetData($tBuffers[0], 1), DllStructGetData($tURL_COMPONENTS, "Scheme"), DllStructGetData($tBuffers[1], 1), DllStructGetData($tURL_COMPONENTS, "Port"), DllStructGetData($tBuffers[2], 1), DllStructGetData($tBuffers[3], 1), DllStructGetData($tBuffers[4], 1), DllStructGetData($tBuffers[5], 1)] Return $aRet EndFunc ;==>_WinHttpCrackUrl Func __WinHttpDefault(ByRef $vInput, $vOutput) If $vInput = Default Or Number($vInput) = -1 Then $vInput = $vOutput EndFunc ;==>__WinHttpDefault Func _StringBetween($sString, $sStart, $sEnd, $iMode = 0, $bCase = False) $sStart = $sStart ? "\Q" & $sStart & "\E" : "\A" If $iMode <> 1 Then $iMode = 0 If $iMode = 0 Then $sEnd = $sEnd ? "(?=\Q" & $sEnd & "\E)" : "\z" Else $sEnd = $sEnd ? "\Q" & $sEnd & "\E" : "\z" EndIf If $bCase = Default Then $bCase = False EndIf Local $aRet = StringRegExp($sString, "(?s" & (Not $bCase ? "i" : "") & ")" & $sStart & "(.*?)" & $sEnd, 3) If @error Then Return SetError(1, 0, 0) Return $aRet EndFunc ;==>_StringBetween ;Dao Van Trong - TRONG.LIVE noellarkin 1 Regards, Link to comment Share on other sites More sharing options...
noellarkin Posted January 16, 2023 Share Posted January 16, 2023 Thanks, I was meaning to write something similar but I'll test out yours :) Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now