Jump to content

Recommended Posts

I'm trying to get the url's account and password but getting an error when the account contains @.
I tried to use _StringBetween() but found that solution quite bad, I don't know how to use REGEX, would love to help:.

Opt("MustDeclareVars", 1)
Global Const $hWINHTTPDLL__WINHTTP = DllOpen("winhttp.dll")
Global $httpUserName = "ftpuser@ftp.trong.live"
Global $httpPassword = "ftp.trong.live"
Global $sURL_Input = 'https://' & $httpUserName & ':' & $httpPassword & '@ftp.trong.live:4438/prv/test.jpg'

Local $sURL_Protocol, $sURL_Protocol_Number, $sURL_Domain, $sURL_Port, $sURL_User, $sURL_Password, $sURL_FilePathName, $sURL_FilePATH, $sURL_FileName
_WinHttp_SplitURL($sURL_Input, $sURL_Protocol, $sURL_Domain, $sURL_Port, $sURL_User, $sURL_Password, $sURL_FilePathName, $sURL_FilePATH, $sURL_FileName)
ConsoleWrite("! URL_Input   : " & $sURL_Input & @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 _WinHttp_SplitURL($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 $aUrl = _WinHttpCrackUrl($sURL_Input)
    If IsArray($aUrl) Then
        $sURL_Protocol = ($aUrl[0] == '' ? $sURL_Protocol : $aUrl[0])
        $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])
        Return $aUrl
    EndIf
    Return SetError(1, 0, "")
EndFunc   ;==>_WinHttp_SplitURL
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 _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

Debug Ouput:

! URL_Input   : https://ftpuser@ftp.trong.live:ftp.trong.live@ftp.trong.live:443/prv/test.jpg
- Protocol    : https
- Domain      : ftp.trong.live
- Port        : 4438
- User        : ftpuser@ftp.trong.live
- Password    : ftp.trong.live
- FilePathName: /prv/test.jpg
- FilePath    : /prv/
- FileName    : test.jpg

 

Edited by VIP

Regards,
 

Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...