| 1 |
/* Collect URLs from CSS source. |
| 2 |
Copyright (C) 1998, 2000-2003, 2009-2011, 2014-2015, 2018-2024 Free |
| 3 |
Software Foundation, Inc. |
| 4 |
|
| 5 |
This file is part of GNU Wget. |
| 6 |
|
| 7 |
GNU Wget is free software; you can redistribute it and/or modify |
| 8 |
it under the terms of the GNU General Public License as published by |
| 9 |
the Free Software Foundation; either version 3 of the License, or (at |
| 10 |
your option) any later version. |
| 11 |
|
| 12 |
GNU Wget 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 General Public License for more details. |
| 16 |
|
| 17 |
You should have received a copy of the GNU General Public License |
| 18 |
along with Wget. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
|
| 20 |
Additional permission under GNU GPL version 3 section 7 |
| 21 |
|
| 22 |
If you modify this program, or any covered work, by linking or |
| 23 |
combining it with the OpenSSL project's OpenSSL library (or a |
| 24 |
modified version of that library), containing parts covered by the |
| 25 |
terms of the OpenSSL or SSLeay licenses, the Free Software Foundation |
| 26 |
grants you additional permission to convey the resulting work. |
| 27 |
Corresponding Source for a non-source form of such a combination |
| 28 |
shall include the source code for the parts of OpenSSL used as well |
| 29 |
as that of the covered work. */ |
| 30 |
|
| 31 |
/* |
| 32 |
Note that this is not an actual CSS parser, but just a lexical |
| 33 |
scanner with a tiny bit more smarts bolted on top. A full parser |
| 34 |
is somewhat overkill for this job. The only things we're interested |
| 35 |
in are @import rules and url() tokens, so it's easy enough to |
| 36 |
grab those without truly understanding the input. The only downside |
| 37 |
to this is that we might be coerced into downloading files that |
| 38 |
a browser would ignore. That might merit some more investigation. |
| 39 |
*/ |
| 40 |
|
| 41 |
#include "wget.h" |
| 42 |
|
| 43 |
#include <stdio.h> |
| 44 |
#include <string.h> |
| 45 |
#include <stdlib.h> |
| 46 |
#include <ctype.h> |
| 47 |
#include <errno.h> |
| 48 |
|
| 49 |
#include "utils.h" |
| 50 |
#include "convert.h" |
| 51 |
#include "html-url.h" |
| 52 |
#include "css-tokens.h" |
| 53 |
#include "css-url.h" |
| 54 |
#include "xstrndup.h" |
| 55 |
|
| 56 |
/* from lex.yy.c */ |
| 57 |
extern char *yytext; |
| 58 |
extern int yyleng; |
| 59 |
typedef struct yy_buffer_state *YY_BUFFER_STATE; |
| 60 |
extern YY_BUFFER_STATE yy_scan_bytes (const char *bytes,int len ); |
| 61 |
extern void yy_delete_buffer (YY_BUFFER_STATE b); |
| 62 |
extern int yylex (void); |
| 63 |
extern void yylex_destroy(void); |
| 64 |
|
| 65 |
/* |
| 66 |
Given a detected URI token, get only the URI specified within. |
| 67 |
Also adjust the starting position and length of the string. |
| 68 |
|
| 69 |
A URI can be specified with or without quotes, and the quotes |
| 70 |
can be single or double quotes. In addition there can be |
| 71 |
whitespace after the opening parenthesis and before the closing |
| 72 |
parenthesis. |
| 73 |
*/ |
| 74 |
static char * |
| 75 |
get_uri_string (const char *at, int *pos, int *length) |
| 76 |
{ |
| 77 |
if (*length < 4) |
| 78 |
return NULL; |
| 79 |
|
| 80 |
if (0 != strncasecmp (at + *pos, "url(", 4)) |
| 81 |
return NULL; |
| 82 |
|
| 83 |
*pos += 4; |
| 84 |
*length -= 5; /* url() */ |
| 85 |
|
| 86 |
/* skip leading space */ |
| 87 |
while (*length > 0 && isspace (at[*pos])) |
| 88 |
{ |
| 89 |
(*pos)++; |
| 90 |
if (--(*length) == 0) |
| 91 |
return NULL; |
| 92 |
} |
| 93 |
|
| 94 |
/* skip trailing space */ |
| 95 |
while (*length > 0 && isspace (at[*pos + *length - 1])) |
| 96 |
{ |
| 97 |
(*length)--; |
| 98 |
} |
| 99 |
|
| 100 |
/* trim off quotes */ |
| 101 |
if (*length >= 2 && (at[*pos] == '\'' || at[*pos] == '"')) |
| 102 |
{ |
| 103 |
(*pos)++; |
| 104 |
*length -= 2; |
| 105 |
} |
| 106 |
|
| 107 |
if (*length <= 0) |
| 108 |
return NULL; |
| 109 |
|
| 110 |
return xstrndup (at + *pos, *length); |
| 111 |
} |
| 112 |
|
| 113 |
void |
| 114 |
get_urls_css (struct map_context *ctx, int offset, int buf_length) |
| 115 |
{ |
| 116 |
int token; |
| 117 |
/*char tmp[2048];*/ |
| 118 |
int buffer_pos = 0; |
| 119 |
int pos, length; |
| 120 |
char *uri; |
| 121 |
YY_BUFFER_STATE b; |
| 122 |
|
| 123 |
/* tell flex to scan from this buffer */ |
| 124 |
b = yy_scan_bytes (ctx->text + offset, buf_length); |
| 125 |
|
| 126 |
while((token = yylex()) != CSSEOF) |
| 127 |
{ |
| 128 |
/*DEBUGP (("%s ", token_names[token]));*/ |
| 129 |
/* @import "foo.css" |
| 130 |
or @import url(foo.css) |
| 131 |
*/ |
| 132 |
if(token == IMPORT_SYM) |
| 133 |
{ |
| 134 |
do { |
| 135 |
buffer_pos += yyleng; |
| 136 |
} while((token = yylex()) == S); |
| 137 |
|
| 138 |
/*DEBUGP (("%s ", token_names[token]));*/ |
| 139 |
|
| 140 |
if (token == STRING || token == URI) |
| 141 |
{ |
| 142 |
/*DEBUGP (("Got URI "));*/ |
| 143 |
pos = buffer_pos + offset; |
| 144 |
length = yyleng; |
| 145 |
|
| 146 |
if (token == URI) |
| 147 |
{ |
| 148 |
uri = get_uri_string (ctx->text, &pos, &length); |
| 149 |
} |
| 150 |
else if (length >= 2) |
| 151 |
{ |
| 152 |
/* cut out quote characters */ |
| 153 |
pos++; |
| 154 |
length -= 2; |
| 155 |
uri = xmalloc (length + 1); |
| 156 |
memcpy (uri, yytext + 1, length); |
| 157 |
uri[length] = '\0'; |
| 158 |
} |
| 159 |
else |
| 160 |
uri = NULL; |
| 161 |
|
| 162 |
if (uri) |
| 163 |
{ |
| 164 |
struct urlpos *up = append_url (uri, pos, length, ctx); |
| 165 |
DEBUGP (("Found @import: [%s] at %d [%s]\n", yytext, buffer_pos, uri)); |
| 166 |
|
| 167 |
if (up) |
| 168 |
{ |
| 169 |
up->link_inline_p = 1; |
| 170 |
up->link_css_p = 1; |
| 171 |
up->link_expect_css = 1; |
| 172 |
} |
| 173 |
|
| 174 |
xfree(uri); |
| 175 |
} |
| 176 |
} |
| 177 |
} |
| 178 |
/* background-image: url(foo.png) |
| 179 |
note that we don't care what |
| 180 |
property this is actually on. |
| 181 |
*/ |
| 182 |
else if(token == URI) |
| 183 |
{ |
| 184 |
pos = buffer_pos + offset; |
| 185 |
length = yyleng; |
| 186 |
uri = get_uri_string (ctx->text, &pos, &length); |
| 187 |
|
| 188 |
if (uri) |
| 189 |
{ |
| 190 |
struct urlpos *up = append_url (uri, pos, length, ctx); |
| 191 |
DEBUGP (("Found URI: [%s] at %d [%s]\n", yytext, buffer_pos, uri)); |
| 192 |
if (up) |
| 193 |
{ |
| 194 |
up->link_inline_p = 1; |
| 195 |
up->link_css_p = 1; |
| 196 |
} |
| 197 |
|
| 198 |
xfree (uri); |
| 199 |
} |
| 200 |
} |
| 201 |
buffer_pos += yyleng; |
| 202 |
} |
| 203 |
|
| 204 |
yy_delete_buffer(b); |
| 205 |
yylex_destroy(); |
| 206 |
|
| 207 |
DEBUGP (("\n")); |
| 208 |
} |
| 209 |
|
| 210 |
struct urlpos * |
| 211 |
get_urls_css_file (const char *file, const char *url) |
| 212 |
{ |
| 213 |
struct file_memory *fm; |
| 214 |
struct map_context ctx; |
| 215 |
|
| 216 |
/* Load the file. */ |
| 217 |
fm = wget_read_file (file); |
| 218 |
if (!fm) |
| 219 |
{ |
| 220 |
logprintf (LOG_NOTQUIET, "%s: %s\n", file, strerror (errno)); |
| 221 |
return NULL; |
| 222 |
} |
| 223 |
DEBUGP (("Loaded %s (size %s).\n", file, number_to_static_string (fm->length))); |
| 224 |
|
| 225 |
ctx.text = fm->content; |
| 226 |
ctx.head = NULL; |
| 227 |
ctx.base = NULL; |
| 228 |
ctx.parent_base = url ? url : opt.base_href; |
| 229 |
ctx.document_file = file; |
| 230 |
ctx.nofollow = 0; |
| 231 |
|
| 232 |
get_urls_css (&ctx, 0, fm->length); |
| 233 |
wget_read_file_free (fm); |
| 234 |
return ctx.head; |
| 235 |
} |