fd208e862ceab7a4c4b130d8b25d400448164165
[platform/upstream/curl.git] / src / tool_cb_hdr.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2015, Daniel Stenberg, <daniel@haxx.se>, et al.
9  *
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.
13  *
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.
17  *
18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19  * KIND, either express or implied.
20  *
21  ***************************************************************************/
22 #include "tool_setup.h"
23
24 #include "rawstr.h"
25
26 #define ENABLE_CURLX_PRINTF
27 /* use our own printf() functions */
28 #include "curlx.h"
29
30 #include "tool_cfgable.h"
31 #include "tool_msgs.h"
32 #include "tool_cb_hdr.h"
33
34 #include "memdebug.h" /* keep this as LAST include */
35
36 static char *parse_filename(const char *ptr, size_t len);
37
38 /*
39 ** callback for CURLOPT_HEADERFUNCTION
40 */
41
42 size_t tool_header_cb(void *ptr, size_t size, size_t nmemb, void *userdata)
43 {
44   struct HdrCbData *hdrcbdata = userdata;
45   struct OutStruct *outs = hdrcbdata->outs;
46   struct OutStruct *heads = hdrcbdata->heads;
47   const char *str = ptr;
48   const size_t cb = size * nmemb;
49   const char *end = (char*)ptr + cb;
50
51   /*
52    * Once that libcurl has called back tool_header_cb() the returned value
53    * is checked against the amount that was intended to be written, if
54    * it does not match then it fails with CURLE_WRITE_ERROR. So at this
55    * point returning a value different from sz*nmemb indicates failure.
56    */
57   size_t failure = (size * nmemb) ? 0 : 1;
58
59   if(!heads->config)
60     return failure;
61
62 #ifdef DEBUGBUILD
63   if(size * nmemb > (size_t)CURL_MAX_HTTP_HEADER) {
64     warnf(heads->config->global, "Header data exceeds single call write "
65           "limit!\n");
66     return failure;
67   }
68 #endif
69
70   /*
71    * Write header data when curl option --dump-header (-D) is given.
72    */
73
74   if(heads->config->headerfile && heads->stream) {
75     size_t rc = fwrite(ptr, size, nmemb, heads->stream);
76     if(rc != cb)
77       return rc;
78     /* flush the stream to send off what we got earlier */
79     (void)fflush(heads->stream);
80   }
81
82   /*
83    * This callback sets the filename where output shall be written when
84    * curl options --remote-name (-O) and --remote-header-name (-J) have
85    * been simultaneously given and additionally server returns an HTTP
86    * Content-Disposition header specifying a filename property.
87    */
88
89   if(hdrcbdata->honor_cd_filename &&
90      (cb > 20) && checkprefix("Content-disposition:", str)) {
91     const char *p = str + 20;
92
93     /* look for the 'filename=' parameter
94        (encoded filenames (*=) are not supported) */
95     for(;;) {
96       char *filename;
97       size_t len;
98
99       while(*p && (p < end) && !ISALPHA(*p))
100         p++;
101       if(p > end - 9)
102         break;
103
104       if(memcmp(p, "filename=", 9)) {
105         /* no match, find next parameter */
106         while((p < end) && (*p != ';'))
107           p++;
108         continue;
109       }
110       p += 9;
111
112       /* this expression below typecasts 'cb' only to avoid
113          warning: signed and unsigned type in conditional expression
114       */
115       len = (ssize_t)cb - (p - str);
116       filename = parse_filename(p, len);
117       if(filename) {
118         outs->filename = filename;
119         outs->alloc_filename = TRUE;
120         outs->is_cd_filename = TRUE;
121         outs->s_isreg = TRUE;
122         outs->fopened = FALSE;
123         outs->stream = NULL;
124         hdrcbdata->honor_cd_filename = FALSE;
125         break;
126       }
127       else
128         return failure;
129     }
130   }
131
132   return cb;
133 }
134
135 /*
136  * Copies a file name part and returns an ALLOCATED data buffer.
137  */
138 static char *parse_filename(const char *ptr, size_t len)
139 {
140   char *copy;
141   char *p;
142   char *q;
143   char  stop = '\0';
144
145   /* simple implementation of strndup() */
146   copy = malloc(len+1);
147   if(!copy)
148     return NULL;
149   memcpy(copy, ptr, len);
150   copy[len] = '\0';
151
152   p = copy;
153   if(*p == '\'' || *p == '"') {
154     /* store the starting quote */
155     stop = *p;
156     p++;
157   }
158   else
159     stop = ';';
160
161   /* if the filename contains a path, only use filename portion */
162   q = strrchr(copy, '/');
163   if(q) {
164     p = q + 1;
165     if(!*p) {
166       Curl_safefree(copy);
167       return NULL;
168     }
169   }
170
171   /* If the filename contains a backslash, only use filename portion. The idea
172      is that even systems that don't handle backslashes as path separators
173      probably want the path removed for convenience. */
174   q = strrchr(p, '\\');
175   if(q) {
176     p = q + 1;
177     if(!*p) {
178       Curl_safefree(copy);
179       return NULL;
180     }
181   }
182
183   /* scan for the end letter and stop there */
184   q = p;
185   while(*q) {
186     if(q[1] && (q[0] == '\\'))
187       q++;
188     else if(q[0] == stop)
189       break;
190     q++;
191   }
192   *q = '\0';
193
194   /* make sure the file name doesn't end in \r or \n */
195   q = strchr(p, '\r');
196   if(q)
197     *q = '\0';
198
199   q = strchr(p, '\n');
200   if(q)
201     *q = '\0';
202
203   if(copy != p)
204     memmove(copy, p, strlen(p) + 1);
205
206   /* in case we built debug enabled, we allow an evironment variable
207    * named CURL_TESTDIR to prefix the given file name to put it into a
208    * specific directory
209    */
210 #ifdef DEBUGBUILD
211   {
212     char *tdir = curlx_getenv("CURL_TESTDIR");
213     if(tdir) {
214       char buffer[512]; /* suitably large */
215       snprintf(buffer, sizeof(buffer), "%s/%s", tdir, copy);
216       Curl_safefree(copy);
217       copy = strdup(buffer); /* clone the buffer, we don't use the libcurl
218                                 aprintf() or similar since we want to use the
219                                 same memory code as the "real" parse_filename
220                                 function */
221       curl_free(tdir);
222     }
223   }
224 #endif
225
226   return copy;
227 }
228