Next: purgelog.bas Up: Code Listing Previous: htmlutil.bas

httpdutl.bas


'
'  Author:  Terence Kelly (unless otherwise noted within individual
'                          functions)
'  Date:    30 August 1995
'
'  This module contains functions to parse strings from a WinHTTPD
'  server.
'

Function GetURLValue (ByVal VarName$, ByVal S$) As String
    '
    '  Given a "variable name" and a URL-encoded string, return the
    '  "value" corresponding to the name, or null if no "name=value"
    '  pair exists in the given string.
    '
    '  Note that GetURLValue("foo", "&&&foo=gollybar=asdf&bar=jkl&")
    '  returns "gollybar".  This is NOT a bug; the second argument
    '  is a malformed URL string, and this function does NOT check
    '  for errors like this.
    '

    R$ = ""
        
    ' Loop over each &-delimited field
    For I% = 1 To NumOccur(S$, "&") + 1
        CurrStr$ = Split(S$, I%, "&")
        If InStr(1, CurrStr$, VarName$ & "=") = 1 Then
            R$ = Split(CurrStr$, 2, "=")
            Exit For
        End If
    Next I%

    GetURLValue = R$

End Function

Function URLDecode (ByVal S$) As String
    '
    '  Given a URL-encoded string, un-escape it, i.e. convert escape
    '  sequences back into un-encoded form.  Note that I'm assuming
    '  that URL-encoding consists entirely of converting spaces to
    '  plus signs and certain other characters to sequences of the
    '  form "%HH" where HH is a pair of hex digits.  URL encoding
    '  might be more complicated than this.  I wasn't able to find
    '  any definitive answers to this question.
    '

    R$ = ""

    For I& = 1 To Len(S$)
        C$ = Mid$(S$, I&, 1)
        Select Case C$
        Case "%"
            ' Unescape hex code
            R$ = R$ & Chr$(HexToDecimal(Mid$(S$, I& + 1, 2)))
            I& = I& + 2  ' Increment index
        Case "+"
            R$ = R$ & " "
        Case Else
            R$ = R$ & C$
        End Select
    Next
    
    URLDecode = R$

End Function


tpkelly@cs.CS.Princeton.EDU
Thu Sep 14 02:35:48 EDT 1995