VFPCGI Day11

出自VFP Wiki

(修訂版本間差異)
跳轉到: 導航, 搜尋
 
第5行: 第5行:
常會看到一些 +%76 之類的字串,會這樣編碼的原因,就是為了要避開一些特殊字元。
常會看到一些 +%76 之類的字串,會這樣編碼的原因,就是為了要避開一些特殊字元。
這部份的定義可以參考
這部份的定義可以參考
-
[url=http://www.faqs.org/rfcs/rfc1738.html]RFC 1738 (rfc1738) - Uniform Resource Locators (URL)[/url]的 2.2 節,這份 rfc 的最後也提供了語法定義。
+
[http://www.faqs.org/rfcs/rfc1738.html RFC 1738 (rfc1738) - Uniform Resource Locators (URL)]的 2.2 節,這份 rfc 的最後也提供了語法定義。
ok, 那麼 VFP 有沒有相關函數呢?答案是沒有。
ok, 那麼 VFP 有沒有相關函數呢?答案是沒有。

在2006年11月22日 (三) 14:12的最新修訂版本

VFPCGI的第十一天

善用別人造好的輪子之一

常會看到一些 +%76 之類的字串,會這樣編碼的原因,就是為了要避開一些特殊字元。 這部份的定義可以參考 RFC 1738 (rfc1738) - Uniform Resource Locators (URL)的 2.2 節,這份 rfc 的最後也提供了語法定義。

ok, 那麼 VFP 有沒有相關函數呢?答案是沒有。 可是幸運的是,已經有人寫好了:Url Decode - Visual FoxPro Wiki。 所以就...用吧~

下面的程式就直接轉貼自上面的連結,我們新增一個 httputility.prg,然後把程式放到裡面去:

Function URLDecode
    ***
    *** If tcInput contains "%00", the string will be terminated at that
    *** character.
    ***

    *** URLDecodes a text string:
    * Replaces %hh tokens with ascii characters

    *** Input: tcInput - Text string to decode
    *** Return: Decoded string
    * Author: Albert Ballinger

    Lparameter tcInput, nFlag

    tcInput = Chrtran(tcInput, "+", " ")

    *!* UrlUnescape
    *!* Converts escape sequences back into ordinary characters.

    *!* HRESULT UrlUnescape(
    *!* LPTSTR pszURL,
    *!* LPTSTR pszUnescaped,
    *!* LPDWORD pcchUnescaped,
    *!* DWORD dwFlags
    *!* );

    *!* Parameters
    *!* pszURL - [in/out] Pointer to a NULL-terminated string with
    *!* the URL. If dwFlags is set to URL_UNESCAPE_INPLACE, the
    *!* converted string is returned through this parameter.
    *!* pszUnescaped - [out] Pointer to a buffer that will receive a
    *!* NULL-terminated string containing the unescaped version
    *!* of pszURL. If URL_UNESCAPE_INPLACE is set in dwFlags,
    *!* this parameter is ignored.
    *!* pcchUnescaped - [in/out] Number of characters in the buffer
    *!* pointed to by pcchUnescaped. On entry, the value
    *!* pcchUnescaped points to is set to the size of the
    *!* buffer. If the function returns a success code, the
    *!* value that pcchUnescaped points to is set to the number
    *!* of characters written to that buffer, not counting the
    *!* terminating NULL character. If an E_POINTER error code
    *!* is returned, the buffer was too small, and the value
    *!* pcchUnescaped points to is set to the required number of
    *!* characters that the buffer must be able to contain. If
    *!* any other errors are returned, the value that
    *!* pcchUnescaped points to is undefined.
    *!* dwFlags - [in] Flags that control which characters are
    *!* unescaped. It can be a combination of the following
    *!* flags. Flag Description
    *!* URL_DONT_UNESCAPE_EXTRA_INFO Don't convert the #
    *!* or ? character, or any characters following them
    *!* in the string.
    *!* URL_UNESCAPE_INPLACE Use pszURL to return the
    *!* converted string instead of pszUnescaped.

    *!* Return Values
    *!* Returns an OLE success code if successful. If the
    *!* URL_UNESCAPE_INPLACE flag is not set, the value pointed to
    *!* by pcchUnescaped will be set to the number of characters in
    *!* the output buffer pointed to by pszUnescaped. Returns
    *!* E_POINTER if the URL_UNESCAPE_INPLACE flag is not set and
    *!* the output buffer is too small. The pcchUnescaped parameter
    *!* will be set to the required buffer size. Otherwise, returns
    *!* an OLE error value.

    *!* Remarks
    *!* An escape sequence has the form "%xy".

    *!* Requirements
    *!* Version 5.00 and later of Shlwapi.dll

    *!* Windows NT/2000: Requires Windows 2000 (or Windows NT 4.0
    *!* with Internet Explorer 5.0 or later).
    *!* Windows 95/98: Requires Windows 98 (or Windows 95 with
    *!* Internet Explorer 5.0 or later).
    *!* Header: Declared in shlwapi.h.
    *!* Import Library: shlwapi.lib.

    Declare Integer UrlUnescape In shlwapi.Dll As UrlUnescape ;
        string pszURL, ;
        string @ pszUnescaped, ;
        integer @ pcchUnescaped, ;
        integer dwFlags

    Local lcOutput, lnLength
    lnLength = Len(tcInput) + 1
    lcOutput = Replicate(Chr(0), lnLength)

    If 0 = UrlUnescape(tcInput, @lcOutput, @lnLength, nFlag)
        lcOutput = Left(lcOutput, lnLength)
        Return lcOutput
    Else
        Return ""
    Endif
ENDFUNC

從程式我們可以看到,作者也是很聰明的直接調用了 Windows API 來達成這件事情。 ok, 下次就來看要怎麼把這個函數套用到之前的 cgilib 裡面去。