1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2012, Daniel Stenberg, <daniel@haxx.se>, et al.
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at http://curl.haxx.se/docs/copyright.html.
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
22 #include "tool_setup.h"
26 #define ENABLE_CURLX_PRINTF
27 /* use our own printf() functions */
30 #include "tool_cfgable.h"
31 #include "tool_msgs.h"
32 #include "tool_cb_hdr.h"
34 #include "memdebug.h" /* keep this as LAST include */
36 static char *parse_filename(const char *ptr, size_t len);
39 ** callback for CURLOPT_HEADERFUNCTION
42 size_t tool_header_cb(void *ptr, size_t size, size_t nmemb, void *userdata)
44 HeaderData *hdrdata = userdata;
45 struct getout *urlnode = hdrdata->urlnode;
46 struct OutStruct *outs = hdrdata->outs;
47 struct OutStruct *heads = hdrdata->heads;
48 const char *str = ptr;
49 const size_t cb = size * nmemb;
50 const char *end = (char*)ptr + cb;
53 * Once that libcurl has called back tool_header_cb() the returned value
54 * is checked against the amount that was intended to be written, if
55 * it does not match then it fails with CURLE_WRITE_ERROR. So at this
56 * point returning a value different from sz*nmemb indicates failure.
58 size_t failure = (size * nmemb) ? 0 : 1;
64 if(size * nmemb > (size_t)CURL_MAX_HTTP_HEADER) {
65 warnf(outs->config, "Header data exceeds single call write limit!\n");
69 /* --dump-header option */
70 if(outs->config->headerfile) {
71 fwrite(ptr, size, nmemb, heads->stream);
74 if((urlnode->flags & GETOUT_USEREMOTE) && outs->config->content_disposition
75 && (cb > 20) && checkprefix("Content-disposition:", str)) {
76 const char *p = str + 20;
78 /* look for the 'filename=' parameter
79 (encoded filenames (*=) are not supported) */
84 while(*p && (p < end) && !ISALPHA(*p))
89 if(memcmp(p, "filename=", 9)) {
90 /* no match, find next parameter */
91 while((p < end) && (*p != ';'))
97 /* this expression below typecasts 'cb' only to avoid
98 warning: signed and unsigned type in conditional expression
100 len = (ssize_t)cb - (p - str);
101 filename = parse_filename(p, len);
103 outs->filename = filename;
104 outs->alloc_filename = TRUE;
105 outs->s_isreg = TRUE;
106 outs->fopened = FALSE;
119 * Copies a file name part and returns an ALLOCATED data buffer.
121 static char *parse_filename(const char *ptr, size_t len)
128 /* simple implementation of strndup() */
129 copy = malloc(len+1);
132 memcpy(copy, ptr, len);
136 if(*p == '\'' || *p == '"') {
137 /* store the starting quote */
144 /* if the filename contains a path, only use filename portion */
145 q = strrchr(copy, '/');
154 /* If the filename contains a backslash, only use filename portion. The idea
155 is that even systems that don't handle backslashes as path separators
156 probably want the path removed for convenience. */
157 q = strrchr(p, '\\');
166 /* scan for the end letter and stop there */
169 if(q[1] && (q[0] == '\\'))
171 else if(q[0] == stop)
177 /* make sure the file name doesn't end in \r or \n */
187 memmove(copy, p, strlen(p) + 1);
189 /* in case we built debug enabled, we allow an evironment variable
190 * named CURL_TESTDIR to prefix the given file name to put it into a
195 char *tdir = curlx_getenv("CURL_TESTDIR");
197 char buffer[512]; /* suitably large */
198 snprintf(buffer, sizeof(buffer), "%s/%s", tdir, copy);
200 copy = strdup(buffer); /* clone the buffer, we don't use the libcurl
201 aprintf() or similar since we want to use the
202 same memory code as the "real" parse_filename