text
stringlengths
0
14.1k
/**
* download the data to a local file instead of
* to memory
*
* the parser chain will receive the file name in
* this.data instead of the real buffer.
**/
M_CODE
lm_handler_writefile(worker_t *w, iohandle_t *h,
url_t *url)
{
int r;
char *name;
char *ext;
char *s;
int x;
int ext_offs;
int a_sz;
int sz;
struct stat st;
/**
* create a filename to download to
**/
if (url->ext_o) {
for (x = url->ext_o; *(url->str+x) && *(url->str+x) != '?'; x++)
;
if (!(ext = malloc(x-url->ext_o+1)))
return M_OUT_OF_MEM;
memcpy(ext, url->str+url->ext_o, x-url->ext_o);
ext[x-url->ext_o] = '\0';
ext_offs = url->ext_o-(url->file_o+1);
} else {
ext = strdup("""");
for (x = url->file_o+1; *(url->str+x) && *(url->str+x) != '?'; x++)
;
ext_offs = x-(url->file_o+1);
}
if (url->file_o+1 == url->sz) {
if (!(name = malloc(a_sz = sizeof(""index.html"")+32)))
return M_OUT_OF_MEM;
memcpy(name, ""index.html"", sizeof(""index.html""));
ext_offs = strlen(""index"");
ext = strdup("".html"");
} else {
if (!(name = malloc(a_sz = ext_offs+strlen(ext)+1+32)))
return M_OUT_OF_MEM;
memcpy(name, url->str+url->file_o+1, ext_offs);
strcpy(name+ext_offs, ext);
}
x=0;
if (stat(name, &st) == 0) {
do {
x++;
sz = sprintf(name+ext_offs, ""-%d%s"", x, ext);
} while (stat(name, &st) == 0);
}
r = lm_io_save(h, url, name);
if (r == M_OK) {
/* set the I/O buffer to the name of the file */
free(h->buf.ptr);
h->buf.ptr = name;
h->buf.sz = strlen(name);
h->buf.cap = a_sz;
} else
free(name);
free(ext);
return M_OK;
}
/**
* Parse the given string as CSS and add the found URLs to
* the uehandle.
**/
M_CODE
lm_extract_css_urls(uehandle_t *ue_h, char *p, size_t sz)
{
char *e = p+sz;
char *t, *s;
while ((p = memmem(p, e-p, ""url"", 3))) {
p += 3;
while (isspace(*p)) p++;
if (*p == '(') {
do p++; while (isspace(*p));
t = (*p == '""' ? ""\"")""
: (*p == '\'' ? ""')"" : "")""));
if (*t != ')')
p++;
} else
t = (*p == '""' ? ""\""""
: (*p == '\'' ? ""'"" : "";""));