#include "tool_setup.h"
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <curlx.h>
#include "tool_cfgable.h"
#include "tool_doswin.h"
#include "tool_msgs.h"
#include "tool_cb_hdr.h"
#include "tool_cb_wrt.h"
#include "tool_operate.h"
#include "tool_libinfo.h"
#include <memdebug.h>
static char *parse_filename(const char *ptr, size_t len);
#ifdef _WIN32
#define BOLD "\x1b[1m"
#define BOLDOFF "\x1b[22m"
#else
#define BOLD "\x1b[1m"
#define BOLDOFF "\x1b[0m"
#define LINK "\x1b]8;;"
#define LINKST "\x1b\\"
#define LINKOFF LINK LINKST
#endif
#ifdef LINK
static void write_linked_location(CURL *curl, const char *location,
size_t loclen, FILE *stream);
#endif
int tool_write_headers(struct HdrCbData *hdrcbdata, FILE *stream)
{
struct curl_slist *h = hdrcbdata->headlist;
int rc = 1;
while(h) {
size_t len = strlen(h->data);
if(len != fwrite(h->data, 1, len, stream))
goto fail;
h = h->next;
}
rc = 0;
fail:
curl_slist_free_all(hdrcbdata->headlist);
hdrcbdata->headlist = NULL;
return rc;
}
size_t tool_header_cb(char *ptr, size_t size, size_t nmemb, void *userdata)
{
struct per_transfer *per = userdata;
struct HdrCbData *hdrcbdata = &per->hdrcbdata;
struct OutStruct *outs = &per->outs;
struct OutStruct *heads = &per->heads;
struct OutStruct *etag_save = &per->etag_save;
const char *str = ptr;
const size_t cb = size * nmemb;
const char *end = (char *)ptr + cb;
const char *scheme = NULL;
if(!per->config)
return CURL_WRITEFUNC_ERROR;
#ifdef DEBUGBUILD
if(size * nmemb > (size_t)CURL_MAX_HTTP_HEADER) {
warnf(per->config->global, "Header data exceeds single call write limit");
return CURL_WRITEFUNC_ERROR;
}
#endif
#ifdef _WIN32
if(outs->utf8seq[0])
memset(outs->utf8seq, 0, sizeof(outs->utf8seq));
#endif
if(per->config->headerfile && heads->stream) {
size_t rc = fwrite(ptr, size, nmemb, heads->stream);
if(rc != cb)
return rc;
if(fflush(heads->stream)) {
errorf(per->config->global, "Failed writing headers to %s",
per->config->headerfile);
return CURL_WRITEFUNC_ERROR;
}
}
curl_easy_getinfo(per->curl, CURLINFO_SCHEME, &scheme);
scheme = proto_token(scheme);
if((scheme == proto_http || scheme == proto_https)) {
long response = 0;
curl_easy_getinfo(per->curl, CURLINFO_RESPONSE_CODE, &response);
if((response/100 != 2) && (response/100 != 3))
;
else if(per->config->etag_save_file && etag_save->stream &&
checkprefix("etag:", str)) {
const char *etag_h = &str[5];
const char *eot = end - 1;
if(*eot == '\n') {
while(ISBLANK(*etag_h) && (etag_h < eot))
etag_h++;
while(ISSPACE(*eot))
eot--;
if(eot >= etag_h) {
size_t etag_length = eot - etag_h + 1;
#if defined(HAVE_FTRUNCATE) && !defined(__MINGW32CE__)
if(ftruncate(fileno(etag_save->stream), 0)) {
return CURL_WRITEFUNC_ERROR;
}
#else
if(fseek(etag_save->stream, 0, SEEK_SET)) {
return CURL_WRITEFUNC_ERROR;
}
#endif
fwrite(etag_h, size, etag_length, etag_save->stream);
fputc('\n', etag_save->stream);
(void)fflush(etag_save->stream);
}
}
}
else if(hdrcbdata->honor_cd_filename) {
if((cb > 20) && checkprefix("Content-disposition:", str)) {
const char *p = str + 20;
for(;;) {
char *filename;
size_t len;
while((p < end) && *p && !ISALPHA(*p))
p++;
if(p > end - 9)
break;
if(memcmp(p, "filename=", 9)) {
while((p < end) && *p && (*p != ';'))
p++;
if((p < end) && *p)
continue;
else
break;
}
p += 9;
len = cb - (size_t)(p - str);
filename = parse_filename(p, len);
if(filename) {
if(outs->stream) {
free(filename);
return CURL_WRITEFUNC_ERROR;
}
if(per->config->output_dir) {
outs->filename = aprintf("%s/%s", per->config->output_dir,
filename);
free(filename);
if(!outs->filename)
return CURL_WRITEFUNC_ERROR;
}
else
outs->filename = filename;
outs->is_cd_filename = TRUE;
outs->s_isreg = TRUE;
outs->fopened = FALSE;
outs->alloc_filename = TRUE;
hdrcbdata->honor_cd_filename = FALSE;
if(!tool_create_output_file(outs, per->config))
return CURL_WRITEFUNC_ERROR;
if(tool_write_headers(&per->hdrcbdata, outs->stream))
return CURL_WRITEFUNC_ERROR;
}
break;
}
if(!outs->stream && !tool_create_output_file(outs, per->config))
return CURL_WRITEFUNC_ERROR;
if(tool_write_headers(&per->hdrcbdata, outs->stream))
return CURL_WRITEFUNC_ERROR;
}
if(hdrcbdata->honor_cd_filename &&
hdrcbdata->config->show_headers) {
char *clone = aprintf("%.*s", (int)cb, str);
if(clone) {
struct curl_slist *old = hdrcbdata->headlist;
hdrcbdata->headlist = curl_slist_append(old, clone);
free(clone);
if(!hdrcbdata->headlist) {
curl_slist_free_all(old);
return CURL_WRITEFUNC_ERROR;
}
}
else {
curl_slist_free_all(hdrcbdata->headlist);
hdrcbdata->headlist = NULL;
return CURL_WRITEFUNC_ERROR;
}
return cb;
}
}
}
if(hdrcbdata->config->writeout) {
char *value = memchr(ptr, ':', cb);
if(value) {
if(per->was_last_header_empty)
per->num_headers = 0;
per->was_last_header_empty = FALSE;
per->num_headers++;
}
else if(ptr[0] == '\r' || ptr[0] == '\n')
per->was_last_header_empty = TRUE;
}
if(hdrcbdata->config->show_headers &&
(scheme == proto_http || scheme == proto_https ||
scheme == proto_rtsp || scheme == proto_file)) {
char *value = NULL;
if(!outs->stream && !tool_create_output_file(outs, per->config))
return CURL_WRITEFUNC_ERROR;
if(hdrcbdata->global->isatty &&
#ifdef _WIN32
tool_term_has_bold &&
#endif
hdrcbdata->global->styled_output)
value = memchr(ptr, ':', cb);
if(value) {
size_t namelen = value - ptr;
fprintf(outs->stream, BOLD "%.*s" BOLDOFF ":", (int)namelen, ptr);
#ifndef LINK
fwrite(&value[1], cb - namelen - 1, 1, outs->stream);
#else
if(curl_strnequal("Location", ptr, namelen)) {
write_linked_location(per->curl, &value[1], cb - namelen - 1,
outs->stream);
}
else
fwrite(&value[1], cb - namelen - 1, 1, outs->stream);
#endif
}
else
fwrite(ptr, cb, 1, outs->stream);
}
return cb;
}
static char *parse_filename(const char *ptr, size_t len)
{
char *copy;
char *p;
char *q;
char stop = '\0';
copy = malloc(len + 1);
if(!copy)
return NULL;
memcpy(copy, ptr, len);
copy[len] = '\0';
p = copy;
if(*p == '\'' || *p == '"') {
stop = *p;
p++;
}
else
stop = ';';
q = strchr(p, stop);
if(q)
*q = '\0';
q = strrchr(p, '/');
if(q) {
p = q + 1;
if(!*p) {
tool_safefree(copy);
return NULL;
}
}
q = strrchr(p, '\\');
if(q) {
p = q + 1;
if(!*p) {
tool_safefree(copy);
return NULL;
}
}
q = strchr(p, '\r');
if(q)
*q = '\0';
q = strchr(p, '\n');
if(q)
*q = '\0';
if(copy != p)
memmove(copy, p, strlen(p) + 1);
#if defined(_WIN32) || defined(MSDOS)
{
char *sanitized;
SANITIZEcode sc = sanitize_file_name(&sanitized, copy, 0);
tool_safefree(copy);
if(sc)
return NULL;
copy = sanitized;
}
#endif
return copy;
}
#ifdef LINK
static
void write_linked_location(CURL *curl, const char *location, size_t loclen,
FILE *stream) {
CURLU *u = NULL;
char *copyloc = NULL, *locurl = NULL, *scheme = NULL, *finalurl = NULL;
const char *loc = location;
size_t llen = loclen;
int space_skipped = 0;
const char *vver = getenv("VTE_VERSION");
if(vver) {
curl_off_t num;
if(curlx_str_number(&vver, &num, CURL_OFF_T_MAX) ||
(num <= 4801))
goto locout;
}
while(llen && (*loc == ' ' || *loc == '\t')) {
++loc;
--llen;
++space_skipped;
}
while(llen && (loc[llen-1] == '\n' || loc[llen-1] == '\r'))
--llen;
u = curl_url();
if(!u)
goto locout;
copyloc = malloc(llen + 1);
if(!copyloc)
goto locout;
memcpy(copyloc, loc, llen);
copyloc[llen] = 0;
if(curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &locurl))
goto locout;
if(curl_url_set(u, CURLUPART_URL, locurl, 0))
goto locout;
if(curl_url_set(u, CURLUPART_URL, copyloc, 0))
goto locout;
if(curl_url_get(u, CURLUPART_URL, &finalurl, CURLU_NO_DEFAULT_PORT))
goto locout;
if(curl_url_get(u, CURLUPART_SCHEME, &scheme, 0))
goto locout;
if(!strcmp("http", scheme) ||
!strcmp("https", scheme) ||
!strcmp("ftp", scheme) ||
!strcmp("ftps", scheme)) {
fprintf(stream, "%.*s" LINK "%s" LINKST "%.*s" LINKOFF,
space_skipped, location,
finalurl,
(int)loclen - space_skipped, loc);
goto locdone;
}
locout:
fwrite(location, loclen, 1, stream);
locdone:
if(u) {
curl_free(finalurl);
curl_free(scheme);
curl_url_cleanup(u);
free(copyloc);
}
}
#endif