summaryrefslogtreecommitdiffstats
path: root/src/VBox/Runtime/generic/http.cpp
blob: aecc4af7c667972aa0e47d9ac652c71faffb7887 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/* $Id: http.cpp $ */
/** @file
 * IPRT - HTTP common API.
 */

/*
 * Copyright (C) 2012-2023 Oracle and/or its affiliates.
 *
 * This file is part of VirtualBox base platform packages, as
 * available from https://www.virtualbox.org.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, in version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses>.
 *
 * The contents of this file may alternatively be used under the terms
 * of the Common Development and Distribution License Version 1.0
 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
 * in the VirtualBox distribution, in which case the provisions of the
 * CDDL are applicable instead of those of the GPL.
 *
 * You may elect to license modified versions of this file under the
 * terms and conditions of either the GPL or the CDDL or both.
 *
 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
 */


/*********************************************************************************************************************************
*   Header Files                                                                                                                 *
*********************************************************************************************************************************/
#define LOG_GROUP RTLOGGROUP_HTTP
#include <iprt/http-common.h>
#include "internal/iprt.h"

#include <iprt/asm.h>
#include <iprt/assert.h>
#include <iprt/ctype.h>
#include <iprt/log.h>
#include <iprt/mem.h>
#include <iprt/string.h>

#include "internal/magics.h"


/*********************************************************************************************************************************
*   Defined Constants And Macros                                                                                                 *
*********************************************************************************************************************************/
/** Validates a handle and returns VERR_INVALID_HANDLE if not valid. */
#define RTHTTPHEADERLIST_VALID_RETURN_RC(hList, a_rc) \
    do { \
        AssertPtrReturn((hList), (a_rc)); \
        AssertReturn((hList)->u32Magic == RTHTTPHEADERLIST_MAGIC, (a_rc)); \
    } while (0)

/** Validates a handle and returns VERR_INVALID_HANDLE if not valid. */
#define RTHTTPHEADERLIST_VALID_RETURN(hList) RTHTTPHEADERLIST_VALID_RETURN_RC((hList), VERR_INVALID_HANDLE)

/** Validates a handle and returns (void) if not valid. */
#define RTHTTPHEADERLIST_VALID_RETURN_VOID(hList) \
    do { \
        AssertPtrReturnVoid(hList); \
        AssertReturnVoid((hList)->u32Magic == RTHTTPHEADERLIST_MAGIC); \
    } while (0)


/*********************************************************************************************************************************
*   Internal structs.                                                                                                            *
*********************************************************************************************************************************/
/**
 * HTTP header list, internal definition.
 */
typedef struct RTHTTPHEADERLISTINTERNAL
{
    /** The list node. */
    RTLISTANCHOR        List;
    /** Magic value. */
    uint32_t            u32Magic;
} RTHTTPHEADERLISTINTERNAL;
/** Pointer to an internal HTTP header list. */
typedef RTHTTPHEADERLISTINTERNAL *PRTHTTPHEADERLISTINTERNAL;


/*********************************************************************************************************************************
*   Prototypes.                                                                                                                  *
*********************************************************************************************************************************/
static void rtHttpHeaderListRemoveAll(PRTHTTPHEADERLISTINTERNAL pThis);


/*********************************************************************************************************************************
*   Lookup / conversion functions                                                                                                *
*********************************************************************************************************************************/

RTR3DECL(const char *) RTHttpMethodToStr(RTHTTPMETHOD enmMethod)
{
    switch (enmMethod)
    {
        case RTHTTPMETHOD_INVALID:  return "invalid";
        case RTHTTPMETHOD_GET:      return "GET";
        case RTHTTPMETHOD_PUT:      return "PUT";
        case RTHTTPMETHOD_POST:     return "POST";
        case RTHTTPMETHOD_PATCH:    return "PATCH";
        case RTHTTPMETHOD_DELETE:   return "DELETE";
        case RTHTTPMETHOD_HEAD:     return "HEAD";
        case RTHTTPMETHOD_OPTIONS:  return "OPTIONS";
        case RTHTTPMETHOD_TRACE:    return "TRACE";
#ifdef IPRT_HTTP_WITH_WEBDAV
        case RTHTTPMETHOD_PROPFIND: return "PROPFIND";
#endif
        case RTHTTPMETHOD_END:
            RT_FALL_THROUGH();
        case RTHTTPMETHOD_32BIT_HACK:
            break;
    }
    return "unknown";
}

RTR3DECL(const char *) RTHttpStatusToStr(RTHTTPSTATUS enmSts)
{
    switch (enmSts)
    {
        case RTHTTPSTATUS_OK                           : return "OK";
        case RTHTTPSTATUS_CREATED                      : return "Created";
        case RTHTTPSTATUS_ACCEPTED                     : return "Accepted";
        case RTHTTPSTATUS_NONAUTHORITATIVEINFORMATION  : return "Non-Authoritative Information";
        case RTHTTPSTATUS_NOCONTENT                    : return "No Content";
        case RTHTTPSTATUS_RESETCONTENT                 : return "Reset Content";
        case RTHTTPSTATUS_PARTIALCONTENT               : return "Partial Content";
        case RTHTTPSTATUS_MULTISTATUS                  : return "Multi-Status";
        case RTHTTPSTATUS_ALREADYREPORTED              : return "Already Reported";
        case RTHTTPSTATUS_IMUSED                       : return "IM Used";

        case RTHTTPSTATUS_BADREQUEST                   : return "Bad Request";
        case RTHTTPSTATUS_UNAUTHORIZED                 : return "Unauthorized";
        case RTHTTPSTATUS_PAYMENTREQUIRED              : return "Payment Required";
        case RTHTTPSTATUS_FORBIDDEN                    : return "Forbidden";
        case RTHTTPSTATUS_NOTFOUND                     : return "Not Found";
        case RTHTTPSTATUS_METHODNOTALLOWED             : return "Method Not Allowed";
        case RTHTTPSTATUS_NOTACCEPTABLE                : return "Not Acceptable";
        case RTHTTPSTATUS_PROXYAUTHENTICATIONREQUIRED  : return "Proxy Authentication Required";
        case RTHTTPSTATUS_REQUESTTIMEOUT               : return "Request Timeout";
        case RTHTTPSTATUS_CONFLICT                     : return "Conflict";
        case RTHTTPSTATUS_GONE                         : return "Gone";
        case RTHTTPSTATUS_LENGTHREQUIRED               : return "Length Required";
        case RTHTTPSTATUS_PRECONDITIONFAILED           : return "Precondition Failed";
        case RTHTTPSTATUS_PAYLOADTOOLARGE              : return "Payload Too Large";
        case RTHTTPSTATUS_URITOOLONG                   : return "URI Too Long";
        case RTHTTPSTATUS_UNSUPPORTEDMEDIATYPE         : return "Unsupported Media Type";
        case RTHTTPSTATUS_RANGENOTSATISFIABLE          : return "Range Not Satisfiable";
        case RTHTTPSTATUS_EXPECTATIONFAILED            : return "Expectation Failed";
        case RTHTTPSTATUS_IMATEAPOT                    : return "I'm a teapot";
        case RTHTTPSTATUS_UNPROCESSABLEENTITY          : return "Unprocessable Entity";
        case RTHTTPSTATUS_LOCKED                       : return "Locked";
        case RTHTTPSTATUS_FAILEDDEPENDENCY             : return "Failed Dependency";
        case RTHTTPSTATUS_UPGRADEREQUIRED              : return "Upgrade Required";
        case RTHTTPSTATUS_PRECONDITIONREQUIRED         : return "Precondition Required";
        case RTHTTPSTATUS_TOOMANYREQUESTS              : return "Too Many Requests";
        case RTHTTPSTATUS_REQUESTHEADERFIELDSTOOLARGE  : return "Request Header Fields Too Large";
        case RTHTTPSTATUS_UNAVAILABLEFORLEGALREASONS   : return "Unavailable For Legal Reasons";

        case RTHTTPSTATUS_INTERNALSERVERERROR          : return "Internal Server Error";
        case RTHTTPSTATUS_NOTIMPLEMENTED               : return "Not Implemented";
        case RTHTTPSTATUS_BADGATEWAY                   : return "Bad Gateway";
        case RTHTTPSTATUS_SERVICEUNAVAILABLE           : return "Service Unavailable";
        case RTHTTPSTATUS_GATEWAYTIMEOUT               : return "Gateway Time-out";
        case RTHTTPSTATUS_HTTPVERSIONNOTSUPPORTED      : return "HTTP Version Not Supported";
        case RTHTTPSTATUS_VARIANTALSONEGOTIATES        : return "Variant Also Negotiates";
        case RTHTTPSTATUS_INSUFFICIENTSTORAGE          : return "Insufficient Storage";
        case RTHTTPSTATUS_LOOPDETECTED                 : return "Loop Detected";
        case RTHTTPSTATUS_NOTEXTENDED                  : return "Not Extended";
        case RTHTTPSTATUS_NETWORKAUTHENTICATIONREQUIRED: return "Network Authentication Required";

        default: break;
    }

    AssertFailed();
    return "<Not implemented>";
}


/*********************************************************************************************************************************
*   HTTP Header List                                                                                                             *
*********************************************************************************************************************************/

RTR3DECL(int) RTHttpHeaderListInit(PRTHTTPHEADERLIST hHdrLst)
{
    PRTHTTPHEADERLISTINTERNAL pThis = (PRTHTTPHEADERLISTINTERNAL)RTMemAllocZ(sizeof(RTHTTPHEADERLISTINTERNAL));
    if (pThis)
    {
        pThis->u32Magic = RTHTTPHEADERLIST_MAGIC;

        RTListInit(&pThis->List);

        *hHdrLst = (RTHTTPHEADERLIST)pThis;

        return VINF_SUCCESS;
    }

    return VERR_NO_MEMORY;
}


/**
 * Destroys the headers associated with this list (w/o telling cURL about it).
 *
 * @param   hHdrLst       The HTTP header list instance.
 */
RTR3DECL(void) RTHttpHeaderListDestroy(RTHTTPHEADERLIST hHdrLst)
{
    PRTHTTPHEADERLISTINTERNAL pThis = hHdrLst;
    RTHTTPHEADERLIST_VALID_RETURN_VOID(pThis);

    rtHttpHeaderListRemoveAll(pThis);
}


static void rtHttpHeaderListRemoveAll(PRTHTTPHEADERLISTINTERNAL pThis)
{
    PRTHTTPHEADERENTRY pEntry, pNext;
    RTListForEachSafe(&pThis->List, pEntry, pNext, RTHTTPHEADERENTRY, Node)
    {
        RTListNodeRemove(&pEntry->Node);
        RTMemFree(pEntry);
    }
}

/**
 * Worker for RTHttpHeaderListSet and RTHttpHeaderListAdd.
 *
 * @returns IPRT status code.
 * @param   pThis       The HTTP header list instance.
 * @param   pchName     The field name.  Does not need to be terminated.
 * @param   cchName     The field name length.
 * @param   pchValue    The field value.  Does not need to be terminated.
 * @param   cchValue    The field value length.
 * @param   fFlags      RTHTTPADDHDR_F_XXX.
 */
static int rtHttpHeaderListAddWorker(PRTHTTPHEADERLISTINTERNAL pThis,
                                     const char *pchName, size_t cchName, const char *pchValue, size_t cchValue, uint32_t fFlags)
{
    /*
     * Create the list entry.
     */
    size_t             cbData = cchName + 2 + cchValue + 1;
    PRTHTTPHEADERENTRY pHdr   = (PRTHTTPHEADERENTRY)RTMemAlloc(RT_UOFFSETOF_DYN(RTHTTPHEADERENTRY, szData[cbData]));
    if (pHdr)
    {
        pHdr->cchName   = (uint32_t)cchName;
        pHdr->offValue  = (uint32_t)(cchName + 2);
        char *psz = pHdr->szData;
        memcpy(psz, pchName, cchName);
        psz += cchName;
        *psz++ = ':';
        *psz++ = ' ';
        memcpy(psz, pchValue, cchValue);
        psz[cchValue] = '\0';

        /*
         * Appending to an existing list requires no cURL interaction.
         */
        AssertCompile(RTHTTPHEADERLISTADD_F_FRONT != 0);
        if (!(fFlags & RTHTTPHEADERLISTADD_F_FRONT))
        {
            RTListAppend(&pThis->List, &pHdr->Node);
            return VINF_SUCCESS;
        }

        /*
         * When prepending or adding the first header we need to inform cURL
         * about the new list head.
         */
        RTListPrepend(&pThis->List, &pHdr->Node);
        return VINF_SUCCESS;
    }
    return VERR_NO_MEMORY;
}


RTR3DECL(int) RTHttpHeaderListSet(RTHTTPHEADERLIST hHdrLst,
                                  size_t cHeaders, const char * const *papszHeaders)
{
    PRTHTTPHEADERLISTINTERNAL pThis = hHdrLst;
    RTHTTPHEADERLIST_VALID_RETURN(pThis);

    /*
     * Drop old headers and reset state.
     */
    rtHttpHeaderListRemoveAll(pThis);

    /*
     * We're done if no headers specified.
     */
    if (!cHeaders)
        return VINF_SUCCESS;

    /*
     * Add the headers, one by one.
     */
    int rc = VINF_SUCCESS;
    for (size_t i = 0; i < cHeaders; i++)
    {
        const char *pszHeader = papszHeaders[i];
        size_t      cchHeader = strlen(pszHeader);
        size_t      cchName   = (const char *)memchr(pszHeader, ':', cchHeader) - pszHeader;
        AssertBreakStmt(cchName < cchHeader, rc = VERR_INVALID_PARAMETER);
        size_t      offValue  = RT_C_IS_BLANK(pszHeader[cchName + 1]) ? cchName + 2 : cchName + 1;
        rc = rtHttpHeaderListAddWorker(pThis, pszHeader, cchName, &pszHeader[offValue], cchHeader - offValue,
                                       RTHTTPHEADERLISTADD_F_BACK);
        AssertRCBreak(rc);
    }
    if (RT_SUCCESS(rc))
        return rc;
    rtHttpHeaderListRemoveAll(pThis);
    return rc;
}


RTR3DECL(int) RTHttpHeaderListAdd(RTHTTPHEADERLIST hHdrLst,
                                  const char *pszField, const char *pszValue, size_t cchValue, uint32_t fFlags)
{
    /*
     * Validate input and calc string lengths.
     */
    PRTHTTPHEADERLISTINTERNAL pThis = hHdrLst;
    RTHTTPHEADERLIST_VALID_RETURN(pThis);
    AssertReturn(!(fFlags & ~RTHTTPHEADERLISTADD_F_BACK), VERR_INVALID_FLAGS);
    AssertPtr(pszField);
    size_t const cchField = strlen(pszField);
    AssertReturn(cchField > 0, VERR_INVALID_PARAMETER);
    AssertReturn(pszField[cchField - 1] != ':', VERR_INVALID_PARAMETER);
    AssertReturn(!RT_C_IS_SPACE(pszField[cchField - 1]), VERR_INVALID_PARAMETER);
#ifdef RT_STRICT
    for (size_t i = 0; i < cchField; i++)
    {
        char const ch = pszField[i];
        Assert(RT_C_IS_PRINT(ch) && ch != ':');
    }
#endif

    AssertPtr(pszValue);
    if (cchValue == RTSTR_MAX)
        cchValue = strlen(pszValue);

    /*
     * Just pass it along to the worker.
     */
    return rtHttpHeaderListAddWorker(pThis, pszField, cchField, pszValue, cchValue, fFlags);
}


RTR3DECL(const char *) RTHttpHeaderListGet(RTHTTPHEADERLIST hHdrLst, const char *pszField, size_t cchField)
{
    PRTHTTPHEADERLISTINTERNAL pThis = hHdrLst;
    RTHTTPHEADERLIST_VALID_RETURN_RC(pThis, NULL);

    if (cchField == RTSTR_MAX)
        cchField = strlen(pszField);

    PRTHTTPHEADERENTRY pEntry;
    RTListForEach(&pThis->List, pEntry, RTHTTPHEADERENTRY, Node)
    {
        if (   pEntry->cchName == cchField
            && RTStrNICmpAscii(pEntry->szData, pszField, cchField) == 0)
            return &pEntry->szData[pEntry->offValue];
    }
    return NULL;
}


RTR3DECL(size_t) RTHttpHeaderListGetCount(RTHTTPHEADERLIST hHdrLst)
{
    PRTHTTPHEADERLISTINTERNAL pThis = hHdrLst;
    RTHTTPHEADERLIST_VALID_RETURN_RC(pThis, 0);

    /* Note! Only for test cases and debugging, so we don't care about performance. */
    size_t cHeaders = 0;
    PRTHTTPHEADERENTRY pEntry;
    RTListForEach(&pThis->List, pEntry, RTHTTPHEADERENTRY, Node)
        cHeaders++;
    return cHeaders;
}


RTR3DECL(const char *) RTHttpHeaderListGetByOrdinal(RTHTTPHEADERLIST hHdrLst, size_t iOrdinal)
{
    PRTHTTPHEADERLISTINTERNAL pThis = hHdrLst;
    RTHTTPHEADERLIST_VALID_RETURN_RC(pThis, NULL);

    /* Note! Only for test cases and debugging, so we don't care about performance. */
    PRTHTTPHEADERENTRY pEntry;
    RTListForEach(&pThis->List, pEntry, RTHTTPHEADERENTRY, Node)
    {
        if (iOrdinal == 0)
            return pEntry->szData;
        iOrdinal--;
    }

    return NULL;
}