VirtualBox

Changeset 58199 in vbox


Ignore:
Timestamp:
Oct 12, 2015 3:27:00 PM (9 years ago)
Author:
vboxsync
Message:

Runtime/http-curl: introduced RTHttpGetHeaderText() and RTHttpGetHeaderBinary()

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/iprt/http.h

    r57926 r58199  
    7373
    7474/**
    75  * Perform a simple blocking HTTP request.
     75 * Perform a simple blocking HTTP GET request.
    7676 *
    7777 * This is a just a convenient wrapper around RTHttpGetBinary that returns a
     
    8282 * @param   hHttp           The HTTP client instance.
    8383 * @param   pszUrl          URL.
    84  * @param   ppszNotUtf8     Where to return the poitner to the HTTP response.
     84 * @param   ppszNotUtf8     Where to return the pointer to the HTTP response.
    8585 *                          The string is of course zero terminated.  Use
    8686 *                          RTHttpFreeReponseText to free.
     
    102102
    103103/**
     104 * Perform a simple blocking HTTP HEAD request.
     105 *
     106 * This is a just a convenient wrapper around RTHttpGetBinary that returns a
     107 * different type and sheds a parameter.
     108 *
     109 * @returns iprt status code.
     110 *
     111 * @param   hHttp           The HTTP client instance.
     112 * @param   pszUrl          URL.
     113 * @param   ppszNotUtf8     Where to return the pointer to the HTTP response.
     114 *                          The string is of course zero terminated.  Use
     115 *                          RTHttpFreeReponseText to free.
     116 *
     117 * @remarks BIG FAT WARNING!
     118 *
     119 *          This function does not guarantee the that returned string is valid UTF-8 or
     120 *          any other kind of text encoding!
     121 *
     122 *          The caller must determine and validate the string encoding _before_
     123 *          passing it along to functions that expect UTF-8!
     124 *
     125 *          Also, this function does not guarantee that the returned string
     126 *          doesn't have embedded zeros and provides the caller no way of
     127 *          finding out!  If you are worried about the response from the HTTPD
     128 *          containing embedded zero's, use RTHttpGetHeaderBinary instead.
     129 */
     130RTR3DECL(int) RTHttpGetHeaderText(RTHTTP hHttp, const char *pszUrl, char **ppszNotUtf8);
     131
     132/**
    104133 * Frees memory returned by RTHttpGetText.
    105134 *
     
    109138
    110139/**
    111  * Perform a simple blocking HTTP request.
     140 * Perform a simple blocking HTTP GET request.
    112141 *
    113142 * @returns iprt status code.
     
    120149 */
    121150RTR3DECL(int) RTHttpGetBinary(RTHTTP hHttp, const char *pszUrl, void **ppvResponse, size_t *pcb);
     151
     152/**
     153 * Perform a simple blocking HTTP HEAD request.
     154 *
     155 * @returns iprt status code.
     156 *
     157 * @param   hHttp           The HTTP client instance.
     158 * @param   pszUrl          The URL.
     159 * @param   ppvResponse     Where to store the HTTP response data.  Use
     160 *                          RTHttpFreeResponse to free.
     161 * @param   pcb             Size of the returned buffer.
     162 */
     163RTR3DECL(int) RTHttpGetHeaderBinary(RTHTTP hHttp, const char *pszUrl, void **ppvResponse, size_t *pcb);
    122164
    123165/**
  • trunk/src/VBox/Runtime/generic/http-curl.cpp

    r57974 r58199  
    22102210    }
    22112211
     2212    /*
     2213     * Use GET by default.
     2214     */
     2215    rcCurl = curl_easy_setopt(pThis->pCurl, CURLOPT_NOBODY, 0L);
     2216    if (CURL_FAILURE(rcCurl))
     2217        return VERR_HTTP_CURL_ERROR;
     2218
    22122219    return VINF_SUCCESS;
    22132220}
     
    22812288 * @param   hHttp               The HTTP/HTTPS client instance.
    22822289 * @param   pszUrl              The URL.
     2290 * @param   fNoBody             Set to suppress the body.
    22832291 * @param   ppvResponse         Where to return the pointer to the allocated
    22842292 *                              response data (RTMemFree).  There will always be
     
    22902298 *          threads, because that will probably blow up!
    22912299 */
    2292 static int rtHttpGetToMem(RTHTTP hHttp, const char *pszUrl, uint8_t **ppvResponse, size_t *pcb)
     2300static int rtHttpGetToMem(RTHTTP hHttp, const char *pszUrl, bool fNoBody, uint8_t **ppvResponse, size_t *pcb)
    22932301{
    22942302    PRTHTTPINTERNAL pThis = hHttp;
     
    23222330        if (!CURL_FAILURE(rcCurl))
    23232331            rcCurl = curl_easy_setopt(pThis->pCurl, CURLOPT_WRITEDATA, (void *)pThis);
     2332        if (   fNoBody
     2333            && !CURL_FAILURE(rcCurl))
     2334            rcCurl = curl_easy_setopt(pThis->pCurl, CURLOPT_NOBODY, 1L);
    23242335        if (!CURL_FAILURE(rcCurl))
    23252336        {
     
    23552366    uint8_t *pv;
    23562367    size_t   cb;
    2357     int rc = rtHttpGetToMem(hHttp, pszUrl, &pv, &cb);
     2368    int rc = rtHttpGetToMem(hHttp, pszUrl, false /*fNoBody*/, &pv, &cb);
    23582369    if (RT_SUCCESS(rc))
    23592370    {
     
    23692380
    23702381
     2382RTR3DECL(int) RTHttpGetHeaderText(RTHTTP hHttp, const char *pszUrl, char **ppszNotUtf8)
     2383{
     2384    Log(("RTHttpGetText: hHttp=%p pszUrl=%s\n", hHttp, pszUrl));
     2385    uint8_t *pv;
     2386    size_t   cb;
     2387    int rc = rtHttpGetToMem(hHttp, pszUrl, true /*fNoBody*/, &pv, &cb);
     2388    if (RT_SUCCESS(rc))
     2389    {
     2390        if (pv) /* paranoia */
     2391            *ppszNotUtf8 = (char *)pv;
     2392        else
     2393            *ppszNotUtf8 = (char *)RTMemDup("", 1);
     2394    }
     2395    else
     2396        *ppszNotUtf8 = NULL;
     2397    return rc;
     2398
     2399}
     2400
     2401
    23712402RTR3DECL(void) RTHttpFreeResponseText(char *pszNotUtf8)
    23722403{
     
    23782409{
    23792410    Log(("RTHttpGetBinary: hHttp=%p pszUrl=%s\n", hHttp, pszUrl));
    2380     return rtHttpGetToMem(hHttp, pszUrl, (uint8_t **)ppvResponse, pcb);
     2411    return rtHttpGetToMem(hHttp, pszUrl, false /*fNoBody*/, (uint8_t **)ppvResponse, pcb);
     2412}
     2413
     2414
     2415RTR3DECL(int) RTHttpGetHeaderBinary(RTHTTP hHttp, const char *pszUrl, void **ppvResponse, size_t *pcb)
     2416{
     2417    Log(("RTHttpGetBinary: hHttp=%p pszUrl=%s\n", hHttp, pszUrl));
     2418    return rtHttpGetToMem(hHttp, pszUrl, true /*fNoBody*/, (uint8_t **)ppvResponse, pcb);
    23812419}
    23822420
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette