Пример #1
0
void wget_css_parse_file(
	const char *fname,
	void(*callback_uri)(void *user_ctx, const char *url, size_t len, size_t pos),
	void(*callback_encoding)(void *user_ctx, const char *url, size_t len),
	void *user_ctx)
{
	int fd;

	if (strcmp(fname,"-")) {
		if ((fd = open(fname, O_RDONLY)) != -1) {
			struct stat st;
			if (fstat(fd, &st) == 0) {
#ifdef HAVE_MMAP
				size_t nread = st.st_size;
				char *buf = mmap(NULL, nread + 1, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
#else
				char *buf=xmalloc(st.st_size+1);
				size_t nread=read(fd,buf,st.st_size);
#endif

				if (nread > 0) {
					buf[nread] = 0; // PROT_WRITE allows this write, MAP_PRIVATE prevents changes in underlying file system
					wget_css_parse_buffer(buf, callback_uri, callback_encoding, user_ctx);
				}

#ifdef HAVE_MMAP
				munmap(buf, nread);
#else
				xfree(buf);
#endif
			}
			close(fd);
		} else
			error_printf(_("Failed to open %s\n"), fname);
	} else {
		// read data from STDIN.
		// maybe should use yy_scan_bytes instead of buffering into memory.
		char tmp[4096];
		ssize_t nbytes;
		wget_buffer_t *buf = wget_buffer_alloc(4096);

		while ((nbytes = read(STDIN_FILENO, tmp, sizeof(tmp))) > 0) {
			wget_buffer_memcat(buf, tmp, nbytes);
		}

		if (buf->length)
			wget_css_parse_buffer(buf->data, callback_uri, callback_encoding, user_ctx);

		wget_buffer_free(&buf);
	}
}
Пример #2
0
int main(void)
{
	char *empty = (char *)"";

	wget_info_printf("%d\n", wget_base64_is_string("")); // base64.c
	wget_buffer_alloc(0); // buffer.c
	wget_buffer_printf((wget_buffer_t *)1, "%s", ""); // buffer_printf.c
	strlcpy((char *)"", "", 0); // strlcpy.c
	wget_css_parse_buffer((const char *)1, 0, NULL, NULL, NULL); // css.c
	wget_decompress_close(NULL); // decompressor.c
	wget_hashmap_create(0, 0, NULL, NULL); // hashmap.c
	wget_fdgetline(&empty, (size_t *)1, 0); // io.c
	wget_iri_parse("", NULL); // iri.c
	wget_list_free((wget_list_t **)1); // list.c
	wget_debug_write("", 0); // log.c
	wget_logger_set_file(NULL, ""); // logger.c
	wget_tcp_set_connect_timeout(NULL, 0); // net.c
	wget_netrc_deinit(NULL); // netrc.c
	wget_strdup(""); // mem.c
//	wget_popenf("r", "%s", ""); // pipe.c
//	wget_bsprintf(NULL, NULL, "%s", ""); // printf.c
	wget_ssl_set_config_int(0, 0); // ssl_[gnutls].c
	wget_stringmap_create(0); // stringmap.c
	if (wget_strcmp("", "")) {}; // utils.c
	wget_vector_set_destructor(NULL, NULL); // vector.c
	wget_malloc(1); // xalloc.c
	wget_xml_parse_buffer("", NULL, NULL, 0); // xml.c
}
Пример #3
0
// Callback function, called from CSS parser for each URI found.
static void _css_get_url(void *context, const char *url, size_t len, size_t pos)
{
	_CSS_CONTEXT *ctx = context;
	WGET_PARSED_URL parsed_url = { .len = len, .pos = pos, .url = wget_strmemdup(url, len), .abs_url = NULL };

	if (!ctx->uris) {
		ctx->uris = wget_vector_create(16, -2, NULL);
		wget_vector_set_destructor(ctx->uris, (wget_vector_destructor_t)_free_url);
	}

	wget_vector_add(ctx->uris, &parsed_url, sizeof(parsed_url));
}

static void _urls_to_absolute(wget_vector_t *urls, wget_iri_t *base)
{
	if (base && urls) {
		wget_buffer_t buf;
		wget_buffer_init(&buf, NULL, 1024);

		for (int it = 0; it < wget_vector_size(urls); it++) {
			WGET_PARSED_URL *url = wget_vector_get(urls, it);

			if (wget_iri_relative_to_abs(base, url->url, url->len, &buf))
				url->abs_url = wget_strmemdup(buf.data, buf.length);
			else
				error_printf("Cannot resolve relative URI '%s'\n", url->url);
		}

		wget_buffer_deinit(&buf);
	}
}

wget_vector_t *wget_css_get_urls(const char *css, size_t len, wget_iri_t *base, const char **encoding)
{
	_CSS_CONTEXT context = { .encoding = encoding };

	wget_css_parse_buffer(css, len, _css_get_url, encoding ? _css_get_encoding : NULL, &context);
	_urls_to_absolute(context.uris, base);

	return context.uris;
}