message.c 19.7 KB
Newer Older
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
1 2 3 4 5 6 7 8 9 10 11 12 13
/*****************************************************************************
 * message.c: HTTP request/response
 *****************************************************************************
 * Copyright (C) 2015 Rémi Denis-Courmont
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * 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
14
 * GNU Lesser General Public License for more details.
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <assert.h>
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
31
#include <time.h>
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48

#include <vlc_common.h>
#include "message.h"
#include "h2frame.h"

struct vlc_http_msg
{
    short status;
    char *method;
    char *scheme;
    char *authority;
    char *path;
    char *(*headers)[2];
    unsigned count;
    struct vlc_http_stream *payload;
};

49 50
static bool vlc_http_is_token(const char *);

51 52 53 54
static ssize_t vlc_http_msg_find_header(const struct vlc_http_msg *m,
                                        const char *name)
{
    for (unsigned i = 0; i < m->count; i++)
55
        if (!strcasecmp(m->headers[i][0], name))
56 57 58 59
            return i;
    return -1;
}

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
60 61 62
static int vlc_http_msg_vadd_header(struct vlc_http_msg *m, const char *name,
                                    const char *fmt, va_list ap)
{
63 64 65 66 67 68
    if (!vlc_http_is_token(name))
    {   /* Not a valid field name, i.e. not an HTTP token */
        errno = EINVAL;
        return -1;
    }

69 70 71 72 73 74 75 76 77 78 79 80
    char *value;
    if (unlikely(vasprintf(&value, fmt, ap) < 0))
        return -1;

    /* IETF RFC7230 §3.2.4 */
    for (char *p = value; *p; p++)
        if (*p == '\r' || *p == '\n')
            *p = ' ';

    /* Fold identically named header field values. This is unfortunately not
     * possible for Set-Cookie, while Cookie requires a special separator. */
    ssize_t idx = vlc_http_msg_find_header(m, name);
81
    if (idx >= 0 && strcasecmp(name, "Set-Cookie"))
82 83
    {
        char *merged;
84
        char sep = strcasecmp(name, "Cookie") ? ',' : ';';
85 86 87 88 89 90 91 92 93 94 95 96 97

        int val = asprintf(&merged, "%s%c %s", m->headers[idx][1], sep, value);

        free(value);

        if (unlikely(val == -1))
            return -1;

        free(m->headers[idx][1]);
        m->headers[idx][1] = merged;
        return 0;
    }

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
98 99
    char *(*h)[2] = realloc(m->headers, sizeof (char *[2]) * (m->count + 1));
    if (unlikely(h == NULL))
100 101
    {
        free(value);
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
102
        return -1;
103
    }
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
104 105 106 107 108 109 110

    m->headers = h;
    h += m->count;

    h[0][0] = strdup(name);
    if (unlikely(h[0][0] == NULL))
    {
111
        free(value);
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
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
        return -1;
    }

    h[0][1] = value;
    m->count++;
    return 0;
}

int vlc_http_msg_add_header(struct vlc_http_msg *m, const char *name,
                            const char *fmt, ...)
{
    va_list ap;
    int ret;

    va_start(ap, fmt);
    ret = vlc_http_msg_vadd_header(m, name, fmt, ap);
    va_end(ap);
    return ret;
}

/* TODO: merge identically named headers (not really needed yet) */

const char *vlc_http_msg_get_header(const struct vlc_http_msg *m,
                                    const char *name)
{
137 138 139 140 141 142 143
    ssize_t idx = vlc_http_msg_find_header(m, name);
    if (idx < 0)
    {
        errno = ENOENT;
        return NULL;
    }
    return m->headers[idx][1];
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
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
}

int vlc_http_msg_get_status(const struct vlc_http_msg *m)
{
    return m->status;
}

const char *vlc_http_msg_get_method(const struct vlc_http_msg *m)
{
    return m->method;
}

const char *vlc_http_msg_get_scheme(const struct vlc_http_msg *m)
{
    return m->scheme;
}

const char *vlc_http_msg_get_authority(const struct vlc_http_msg *m)
{
    return m->authority;
}

const char *vlc_http_msg_get_path(const struct vlc_http_msg *m)
{
    return m->path;
}

void vlc_http_msg_destroy(struct vlc_http_msg *m)
{
    if (m->payload != NULL)
174
        vlc_http_stream_close(m->payload, false);
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
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

    for (unsigned i = 0; i < m->count; i++)
    {
        free(m->headers[i][0]);
        free(m->headers[i][1]);
    }

    free(m->headers);
    free(m->path);
    free(m->authority);
    free(m->scheme);
    free(m->method);
    free(m);
}

struct vlc_http_msg *
vlc_http_req_create(const char *method, const char *scheme,
                    const char *authority, const char *path)
{
    struct vlc_http_msg *m = malloc(sizeof (*m));
    if (unlikely(m == NULL))
        return NULL;

    assert(method != NULL);
    m->status = -1;
    m->method = strdup(method);
    m->scheme = (scheme != NULL) ? strdup(scheme) : NULL;
    m->authority = (authority != NULL) ? strdup(authority) : NULL;
    m->path = (path != NULL) ? strdup(path) : NULL;
    m->count = 0;
    m->headers = NULL;
    m->payload = NULL;

    if (unlikely(m->method == NULL
              || (scheme != NULL && m->scheme == NULL)
              || (authority != NULL && m->authority == NULL)
              || (path != NULL && m->path == NULL)))
    {
        vlc_http_msg_destroy(m);
        m = NULL;
    }
    return m;
}

struct vlc_http_msg *vlc_http_resp_create(unsigned status)
{
    struct vlc_http_msg *m = malloc(sizeof (*m));
    if (unlikely(m == NULL))
        return NULL;

    assert(status < 1000);
    m->status = status;
    m->method = NULL;
    m->scheme = NULL;
    m->authority = NULL;
    m->path = NULL;
    m->count = 0;
    m->headers = NULL;
    m->payload = NULL;
    return m;
}

void vlc_http_msg_attach(struct vlc_http_msg *m, struct vlc_http_stream *s)
{
    assert(m->payload == NULL);
    m->payload = s;
}

struct vlc_http_msg *vlc_http_msg_iterate(struct vlc_http_msg *m)
{
    struct vlc_http_msg *next = vlc_http_stream_read_headers(m->payload);

    m->payload = NULL;
    vlc_http_msg_destroy(m);
    return next;
}

struct vlc_http_msg *vlc_http_msg_get_final(struct vlc_http_msg *m)
{
    while (m != NULL && (vlc_http_msg_get_status(m) / 100) == 1)
        m = vlc_http_msg_iterate(m);
    return m;
}

block_t *vlc_http_msg_read(struct vlc_http_msg *m)
{
    if (m->payload == NULL)
        return NULL;

    return vlc_http_stream_read(m->payload);
}

/* Serialization and deserialization */

char *vlc_http_msg_format(const struct vlc_http_msg *m, size_t *restrict lenp)
{
    size_t len;

    if (m->status < 0)
    {
275
        len = sizeof ("  HTTP/1.1\r\nHost: \r\n\r\n");
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
276 277 278 279 280
        len += strlen(m->method);
        len += strlen(m->path ? m->path : m->authority);
        len += strlen(m->authority);
    }
    else
281
        len = sizeof ("HTTP/1.1 123 .\r\n\r\n");
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301

    for (unsigned i = 0; i < m->count; i++)
        len += 4 + strlen(m->headers[i][0]) + strlen(m->headers[i][1]);

    char *buf = malloc(len + 1);
    if (unlikely(buf == NULL))
        return NULL;

    len = 0;

    if (m->status < 0)
        len += sprintf(buf, "%s %s HTTP/1.1\r\nHost: %s\r\n", m->method,
                       m->path ? m->path : m->authority, m->authority);
    else
        len += sprintf(buf, "HTTP/1.1 %03hd .\r\n", m->status);

    for (unsigned i = 0; i < m->count; i++)
        len += sprintf(buf + len, "%s: %s\r\n",
                       m->headers[i][0], m->headers[i][1]);

302
    len += sprintf(buf + len, "\r\n");
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
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 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
    if (lenp != NULL)
        *lenp = len;
    return buf;
}

struct vlc_http_msg *vlc_http_msg_headers(const char *msg)
{
    struct vlc_http_msg *m;
    unsigned short code;

    /* TODO: handle HTTP/1.0 differently */
    if (sscanf(msg, "HTTP/1.%*1u %3hu %*s", &code) == 1)
    {
        m = vlc_http_resp_create(code);
        if (unlikely(m == NULL))
            return NULL;
    }
    else
        return NULL; /* TODO: request support */

    msg = strstr(msg, "\r\n");
    if (msg == NULL)
        goto error;

    while (strcmp(msg + 2, "\r\n"))
    {
        const char *eol = msg;

        do
        {
            eol = strstr(eol + 2, "\r\n");
            if (eol == NULL)
                goto error;
        }   /* Deal with legacy obs-fold (i.e. multi-line header) */
        while (eol[2] == ' ' || eol[2] == '\t');

        msg += 2; /* skip CRLF */

        const char *colon = memchr(msg, ':', eol - msg);
        if (colon == NULL || colon == msg)
            goto error;

        char *name = strndup(msg, colon - msg);
        if (unlikely(name == NULL))
            goto error;

        colon++;
        colon += strspn(colon, " \t");

        if (unlikely(vlc_http_msg_add_header(m, name, "%.*s",
                                             (int)(eol - colon), colon)))
        {
            free(name);
            goto error;
        }
        free(name);
        msg = eol;
    }

    return m;
error:
    vlc_http_msg_destroy(m);
    return NULL;
}

struct vlc_h2_frame *vlc_http_msg_h2_frame(const struct vlc_http_msg *m,
                                           uint_fast32_t stream_id, bool eos)
{
    for (unsigned j = 0; j < m->count; j++)
    {   /* No HTTP 1 specific headers */
        assert(strcasecmp(m->headers[j][0], "Connection"));
        assert(strcasecmp(m->headers[j][0], "Upgrade"));
        assert(strcasecmp(m->headers[j][0], "HTTP2-Settings"));
    }

    const char *(*headers)[2] = malloc((m->count + 5) * sizeof (char *[2]));
    if (unlikely(headers == NULL))
        return NULL;

    struct vlc_h2_frame *f;
    unsigned i = 0;
    char status[4];

    if (m->status >= 0)
    {
        assert(m->status < 1000);
        sprintf(status, "%hd", m->status);
        headers[i][0] = ":status";
        headers[i][1] = status;
        i++;
    }
    if (m->method != NULL)
    {
        headers[i][0] = ":method";
        headers[i][1] = m->method;
        i++;
    }
    if (m->scheme != NULL)
    {
        headers[i][0] = ":scheme";
        headers[i][1] = m->scheme;
        i++;
    }
    if (m->authority != NULL)
    {
        headers[i][0] = ":authority";
        headers[i][1] = m->authority;
        i++;
    }
    if (m->path != NULL)
    {
        headers[i][0] = ":path";
        headers[i][1] = m->path;
        i++;
    }
    if (m->count > 0)
    {
        memcpy(headers + i, m->headers, m->count * sizeof (*headers));
        i += m->count;
    }

    f = vlc_h2_frame_headers(stream_id, VLC_H2_DEFAULT_MAX_FRAME, eos,
                             i, headers);
    free(headers);
    return f;
}

430 431
struct vlc_http_msg *vlc_http_msg_h2_headers(unsigned n,
                                             const char *const hdrs[][2])
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
432 433 434 435 436 437 438
{
    struct vlc_http_msg *m = vlc_http_resp_create(0);
    if (unlikely(m == NULL))
        return NULL;

    for (unsigned i = 0; i < n; i++)
    {
439 440
        const char *name = hdrs[i][0];
        const char *value = hdrs[i][1];
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
441

442 443
        /* NOTE: HPACK always returns lower-case names, so strcmp() is fine. */
        if (!strcmp(name, ":status"))
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
444
        {
445 446
            char *end;
            unsigned long status = strtoul(value, &end, 10);
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
447

448 449 450 451
            if (m->status != 0 || status > 999 || *end != '\0')
                goto error; /* Not a three decimal digits status! */

            m->status = status;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
452 453 454
            continue;
        }

455 456 457 458
        if (!strcmp(name, ":method"))
        {
            if (m->method != NULL)
                goto error;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
459

460 461 462
            m->method = strdup(value);
            if (unlikely(m->method == NULL))
                goto error;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
463

464 465 466
            m->status = -1; /* this is a request */
            continue;
        }
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
467

468 469 470 471
        if (!strcmp(name, ":scheme"))
        {
            if (m->scheme != NULL)
                goto error;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
472

473 474 475 476 477
            m->scheme = strdup(value);
            if (unlikely(m->scheme == NULL))
                goto error;
            continue;
        }
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
478

479 480 481 482
        if (!strcmp(name, ":authority"))
        {
            if (m->authority != NULL)
                goto error;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
483

484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
            m->authority = strdup(value);
            if (unlikely(m->authority == NULL))
                goto error;
            continue;
        }

        if (!strcmp(name, ":path"))
        {
            if (m->path != NULL)
                goto error;

            m->path = strdup(value);
            if (unlikely(m->path == NULL))
                goto error;
            continue;
        }
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
500

501 502 503 504 505 506
        if (vlc_http_msg_add_header(m, name, "%s", value))
            goto error;
    }

    if ((m->status < 0) == (m->method == NULL))
    {   /* Must be either a request or response. Not both, not neither. */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
507
error:
508 509 510 511
        vlc_http_msg_destroy(m);
        m = NULL;
    }
    return m;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
}

/* Header helpers */

static int vlc_http_istoken(int c)
{   /* IETF RFC7230 §3.2.6 */
    return (c >= '0' && c <= '9')
        || (c >= 'a' && c <= 'z')
        || (c >= 'A' && c <= 'Z')
        || (c && strchr("!#$%&'*+-.^_`|~", c) != NULL);
}

static int vlc_http_isctext(int c)
{   /* IETF RFC7230 §3.2.6 */
    return (c == '\t') || (c == ' ') || (c >= 0x21 && c <= 0x27)
        || (c >= 0x2A && c <= 0x5B) || (c >= 0x5D && c <= 0x7E)
        || (c >= 0x80);
}

static size_t vlc_http_token_length(const char *str)
{
    size_t i = 0;

    while (vlc_http_istoken(str[i]))
        i++;
    return i;
}

540 541 542 543 544 545
static bool vlc_http_is_token(const char *str)
{
    size_t len = vlc_http_token_length(str);
    return len > 0 && str[len] == '\0';
}

546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
const char *vlc_http_first_token(const char *value)
{
    return value + strspn(value, "\t ");
}

const char *vlc_http_next_token(const char *value)
{
    value = strchr(value, ',');
    if (value == NULL)
        return NULL;

    return value + strspn(value, "\t ,");
}

const char *vlc_http_msg_get_token(const struct vlc_http_msg *msg,
                                   const char *field, const char *token)
{
    const char *value = vlc_http_msg_get_header(msg, field);
    if (value == NULL)
        return NULL;

    const size_t length = strlen(token);

    for (value = vlc_http_first_token(value);
         value != NULL;
         value = vlc_http_next_token(value))
    {
        if (vlc_http_token_length(value) == length
         && !strncasecmp(token, value, length))
            return value;
    }

    return NULL;
}

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
static size_t vlc_http_comment_length(const char *str)
{   /* IETF RFC7230 §3.2.6 */
    if (*str != '(')
        return 0;

    size_t i = 1;

    for (size_t nested = 1; nested > 0; i++)
    {
        unsigned char c = str[i];

        if (c == ')')
            nested--;
        else
        if (c == '(') /* Nested comment */
            nested++;
        else
        if (c == '\\') /* Quoted pair */
        {
            i++;
            if (str[i] < 32)
                return 0;
        }
        else
        if (!vlc_http_isctext(c))
            return 0;
    }
    return i;
}

static bool vlc_http_is_agent(const char *s)
{   /* IETF RFC7231 §5.5.3 and §7.4.2 */
    if (!vlc_http_istoken(*s))
        return false;

    for (;;)
    {
        size_t l = vlc_http_token_length(s);
        if (l != 0) /* product */
        {
            if (s[l] == '/') /* product version */
            {
                s += l + 1;
                l = vlc_http_token_length(s);
            }
        }
        else
            l = vlc_http_comment_length(s);

        if (l == 0)
            break;

        s += l;

        if (*s == '\0')
            return true;

        l = strspn(s, "\t "); /* RWS */

        if (l == 0)
            break;

        s += l;
    }

    return false;
}

int vlc_http_msg_add_agent(struct vlc_http_msg *m, const char *str)
{
    const char *hname = (m->status < 0) ? "User-Agent" : "Server";

    if (!vlc_http_is_agent(str))
    {
        errno = EINVAL;
        return -1;
    }
    return vlc_http_msg_add_header(m, hname, "%s", str);
}

const char *vlc_http_msg_get_agent(const struct vlc_http_msg *m)
{
    const char *hname = (m->status < 0) ? "User-Agent" : "Server";
    const char *str = vlc_http_msg_get_header(m, hname);

    return (str != NULL && vlc_http_is_agent(str)) ? str : NULL;
}

669 670 671 672 673 674 675 676
static const char vlc_http_days[7][4] = {
    "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
};
static const char vlc_http_months[12][4] = {
    "Jan", "Feb", "Mar", "Apr", "May", "Jun",
    "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};

677 678
int vlc_http_msg_add_time(struct vlc_http_msg *m, const char *hname,
                          const time_t *t)
679
{
680 681 682 683
    struct tm tm;

    if (gmtime_r(t, &tm) == NULL)
        return -1;
684 685
    return vlc_http_msg_add_header(m, hname,
                                   "%s, %02d %s %04d %02d:%02d:%02d GMT",
686 687 688 689
                                   vlc_http_days[tm.tm_wday], tm.tm_mday,
                                   vlc_http_months[tm.tm_mon],
                                   1900 + tm.tm_year,
                                   tm.tm_hour, tm.tm_min, tm.tm_sec);
690 691 692 693 694 695 696
}

int vlc_http_msg_add_atime(struct vlc_http_msg *m)
{
    time_t now;

    time(&now);
697
    return vlc_http_msg_add_time(m, "Date", &now);
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729
}

static time_t vlc_http_mktime(const char *str)
{   /* IETF RFC7231 §7.1.1.1 */
    struct tm tm;
    char mon[4];

    /* Internet Message Format date */
    if (sscanf(str, "%*c%*c%*c, %2d %3s %4d %2d:%2d:%2d", &tm.tm_mday, mon,
               &tm.tm_year, &tm.tm_hour, &tm.tm_min, &tm.tm_sec) == 6
    /* ANSI C format */
     || sscanf(str, "%*3s %3s %2d %2d:%2d:%2d %4d", mon, &tm.tm_mday,
               &tm.tm_hour, &tm.tm_min, &tm.tm_sec, &tm.tm_year) == 6)
        tm.tm_year -= 1900;
    /* RFC850 date */
    else if (sscanf(str, "%*[^,], %2d-%3s-%2d %2d:%2d:%2d", &tm.tm_mday, mon,
                    &tm.tm_year, &tm.tm_hour, &tm.tm_min, &tm.tm_sec) == 6)
    {
        if (tm.tm_year <= 75)
            tm.tm_year += 100; /* Y2K compat, sort of */
    }
    else /* Unknown format */
        goto error;

    for (tm.tm_mon = 0; tm.tm_mon < 12; tm.tm_mon++)
        if (!strcmp(mon, vlc_http_months[tm.tm_mon])) /* found month */
            return timegm(&tm);
error:
    errno = EINVAL;
    return -1; /* invalid month */
}

730 731 732 733 734 735 736 737
time_t vlc_http_msg_get_time(const struct vlc_http_msg *m, const char *name)
{
    const char *str = vlc_http_msg_get_header(m, name);
    if (str == NULL)
        return -1;
    return vlc_http_mktime(str);
}

738 739
time_t vlc_http_msg_get_atime(const struct vlc_http_msg *m)
{
740
    return vlc_http_msg_get_time(m, "Date");
741 742 743 744
}

time_t vlc_http_msg_get_mtime(const struct vlc_http_msg *m)
{
745
    return vlc_http_msg_get_time(m, "Last-Modified");
746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768
}

unsigned vlc_http_msg_get_retry_after(const struct vlc_http_msg *m)
{
    const char *str = vlc_http_msg_get_header(m, "Retry-After");
    char *end;

    unsigned long delay = strtoul(str, &end, 10);
    if (end != str && *end == '\0')
        return delay;

    time_t t = vlc_http_mktime(str);
    if (t != (time_t)-1)
    {
        time_t now;

        time(&now);
        if (t >= now)
            return t - now;
    }
    return 0;
}

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
769
uintmax_t vlc_http_msg_get_size(const struct vlc_http_msg *m)
770
{   /* IETF RFC7230 §3.3.3 */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
771 772
    if ((m->status / 100) == 1 /* Informational 1xx (implicitly void) */
     || m->status == 204 /* No Content (implicitly void) */
773 774
     || m->status == 205 /* Reset Content (must be explicitly void) */
     || m->status == 304 /* Not Modified */)
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
775 776
        return 0;

777 778 779 780 781
    const char *str = vlc_http_msg_get_header(m, "Transfer-Encoding");
    if (str != NULL) /* Transfer-Encoding preempts Content-Length */
        return -1;

    str = vlc_http_msg_get_header(m, "Content-Length");
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
782 783 784 785 786 787 788 789 790 791 792 793 794 795 796
    if (str == NULL)
    {
        if (m->status < 0)
            return 0; /* Requests are void by default */
        return -1; /* Response of unknown size (e.g. chunked) */
    }

    uintmax_t length;

    if (sscanf(str, "%ju", &length) == 1)
        return length;

    errno = EINVAL;
    return -1;
}