1 /* Collect URLs from CSS source.
2 Copyright (C) 1998, 2000, 2001, 2002, 2003, 2009 Free Software
5 This file is part of GNU Wget.
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.
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.
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/>.
20 Additional permission under GNU GPL version 3 section 7
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. */
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.
58 #include "css-tokens.h"
63 typedef struct yy_buffer_state *YY_BUFFER_STATE;
64 extern YY_BUFFER_STATE yy_scan_bytes (const char *bytes,int len );
65 extern int yylex (void);
68 const char *token_names[] = {
103 Given a detected URI token, get only the URI specified within.
104 Also adjust the starting position and length of the string.
106 A URI can be specified with or without quotes, and the quotes
107 can be single or double quotes. In addition there can be
108 whitespace after the opening parenthesis and before the closing
112 get_uri_string (const char *at, int *pos, int *length)
116 strncpy(buf,at + *pos, *length);
118 DEBUGP (("get_uri_string: \"%s\"\n", buf));*/
120 if (0 != strncasecmp (at + *pos, "url(", 4))
124 *length -= 5; /* url() */
125 /* skip leading space */
126 while (isspace (at[*pos]))
131 /* skip trailing space */
132 while (isspace (at[*pos + *length - 1]))
136 /* trim off quotes */
137 if (at[*pos] == '\'' || at[*pos] == '"')
143 uri = xmalloc (*length + 1);
146 strncpy (uri, at + *pos, *length);
154 get_urls_css (struct map_context *ctx, int offset, int buf_length)
163 strncpy(tmp,ctx->text + offset, buf_length);
164 tmp[buf_length] = '\0';
165 DEBUGP (("get_urls_css: \"%s\"\n", tmp));
168 /* tell flex to scan from this buffer */
169 yy_scan_bytes (ctx->text + offset, buf_length);
171 while((token = yylex()) != CSSEOF)
173 /*DEBUGP (("%s ", token_names[token]));*/
175 or @import url(foo.css)
177 if(token == IMPORT_SYM)
180 buffer_pos += yyleng;
181 } while((token = yylex()) == S);
183 /*DEBUGP (("%s ", token_names[token]));*/
185 if (token == STRING || token == URI)
187 /*DEBUGP (("Got URI "));*/
188 pos = buffer_pos + offset;
193 uri = get_uri_string (ctx->text, &pos, &length);
197 /* cut out quote characters */
200 uri = xmalloc (length + 1);
201 strncpy (uri, yytext + 1, length);
207 struct urlpos *up = append_url (uri, pos, length, ctx);
208 DEBUGP (("Found @import: [%s] at %d [%s]\n", yytext, buffer_pos, uri));
212 up->link_inline_p = 1;
214 up->link_expect_css = 1;
221 /* background-image: url(foo.png)
222 note that we don't care what
223 property this is actually on.
225 else if(token == URI)
227 pos = buffer_pos + offset;
229 uri = get_uri_string (ctx->text, &pos, &length);
233 struct urlpos *up = append_url (uri, pos, length, ctx);
234 DEBUGP (("Found URI: [%s] at %d [%s]\n", yytext, buffer_pos, uri));
237 up->link_inline_p = 1;
244 buffer_pos += yyleng;
250 get_urls_css_file (const char *file, const char *url)
252 struct file_memory *fm;
253 struct map_context ctx;
256 fm = read_file (file);
259 logprintf (LOG_NOTQUIET, "%s: %s\n", file, strerror (errno));
262 DEBUGP (("Loaded %s (size %s).\n", file, number_to_static_string (fm->length)));
264 ctx.text = fm->content;
265 ctx.head = ctx.tail = NULL;
267 ctx.parent_base = url ? url : opt.base_href;
268 ctx.document_file = file;
271 get_urls_css (&ctx, 0, fm->length);