Coverage Report

Created: 2026-03-31 07:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/wget2/libwget/css_url.c
Line
Count
Source
1
/*
2
 * Copyright (c) 2013 Tim Ruehsen
3
 * Copyright (c) 2015-2026 Free Software Foundation, Inc.
4
 *
5
 * This file is part of libwget.
6
 *
7
 * Libwget is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Lesser General Public License as published by
9
 * the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * Libwget is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public License
18
 * along with libwget.  If not, see <https://www.gnu.org/licenses/>.
19
 *
20
 *
21
 * Higher level CSS parsing routines
22
 *
23
 * Changelog
24
 * 15.01.2013  Tim Ruehsen  created
25
 *
26
 */
27
28
#include <config.h>
29
30
#include <unistd.h>
31
#include <stdlib.h>
32
#include <string.h>
33
#include <assert.h>
34
35
#include <wget.h>
36
#include "private.h"
37
38
typedef struct {
39
  const char
40
    **encoding;
41
  wget_vector
42
    *uris;
43
} css_context;
44
45
static void url_free(void *url)
46
16.4k
{
47
16.4k
  wget_css_parsed_url *u = url;
48
49
16.4k
  xfree(u->url);
50
16.4k
  xfree(u->abs_url);
51
16.4k
  xfree(u);
52
16.4k
}
53
54
// Callback function, called from CSS parser for each @charset found.
55
static void get_encoding(void *context, const char *encoding, size_t len)
56
839
{
57
839
  css_context *ctx = context;
58
59
  // take only the first @charset rule
60
839
  if (!*ctx->encoding) {
61
34
    *ctx->encoding = wget_strmemdup(encoding, len);
62
34
    debug_printf("URI content encoding = '%s'\n", *ctx->encoding);
63
34
  }
64
839
}
65
66
// Callback function, called from CSS parser for each URI found.
67
static void get_url(void *context, const char *url, size_t len, size_t pos)
68
16.4k
{
69
16.4k
  css_context *ctx = context;
70
16.4k
  wget_css_parsed_url *parsed_url;
71
72
16.4k
  if (!(parsed_url = wget_calloc(1, sizeof(wget_css_parsed_url))))
73
0
    return;
74
75
16.4k
  if (!(parsed_url->url = wget_strmemdup(url, len))) {
76
0
    xfree(parsed_url);
77
0
    return;
78
0
  }
79
80
16.4k
  parsed_url->len = len;
81
16.4k
  parsed_url->pos = pos;
82
83
16.4k
  if (!ctx->uris) {
84
565
    ctx->uris = wget_vector_create(16, NULL);
85
565
    wget_vector_set_destructor(ctx->uris, url_free);
86
565
  }
87
88
16.4k
  wget_vector_add(ctx->uris, parsed_url);
89
16.4k
}
90
91
static void urls_to_absolute(wget_vector *urls, wget_iri *base)
92
1.25k
{
93
1.25k
  if (base && urls) {
94
565
    wget_buffer buf;
95
565
    wget_buffer_init(&buf, NULL, 1024);
96
97
16.9k
    for (int it = 0; it < wget_vector_size(urls); it++) {
98
16.4k
      wget_css_parsed_url *url = wget_vector_get(urls, it);
99
16.4k
      assert(url != NULL);
100
101
16.4k
      if (wget_iri_relative_to_abs(base, url->url, url->len, &buf))
102
16.4k
        url->abs_url = wget_strmemdup(buf.data, buf.length);
103
0
      else
104
0
        error_printf(_("Cannot resolve relative URI '%s'\n"), url->url);
105
16.4k
    }
106
107
565
    wget_buffer_deinit(&buf);
108
565
  }
109
1.25k
}
110
111
wget_vector *wget_css_get_urls(const char *css, size_t len, wget_iri *base, const char **encoding)
112
1.25k
{
113
1.25k
  css_context context = { .encoding = encoding };
114
115
1.25k
  wget_css_parse_buffer(css, len, get_url, encoding ? get_encoding : NULL, &context);
116
1.25k
  urls_to_absolute(context.uris, base);
117
118
1.25k
  return context.uris;
119
1.25k
}
120
121
wget_vector *wget_css_get_urls_from_localfile(const char *fname, wget_iri *base, const char **encoding)
122
0
{
123
0
  css_context context = { .encoding = encoding };
124
125
0
  wget_css_parse_file(fname, get_url, encoding ? get_encoding : NULL, &context);
126
0
  urls_to_absolute(context.uris, base);
127
128
0
  return context.uris;
129
0
}