~ubuntu-branches/ubuntu/utopic/curl/utopic-updates

« back to all changes in this revision

Viewing changes to src/tool_operhlp.c

  • Committer: Package Import Robot
  • Author(s): Andres Rodriguez
  • Date: 2012-05-22 14:53:29 UTC
  • mfrom: (3.4.28 sid)
  • Revision ID: package-import@ubuntu.com-20120522145329-hbf1n3zr7qh08qab
Tags: 7.25.0-1ubuntu1
* Merge from Debian testing (LP: #1003049).  Remaining changes:
  - Drop dependencies not in main:
    + Build-Depends: Drop stunnel4 and libssh2-1-dev.
    + Drop libssh2-1-dev from libcurl4-openssl-dev's Depends.
  - Add new libcurl3-udeb package.
  - Add new curl-udeb package.
  - Also closes (LP: #855291)
* debian/patches/CVE-2012-0036.patch: Dropped. CVE resolved upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *                                  _   _ ____  _
 
3
 *  Project                     ___| | | |  _ \| |
 
4
 *                             / __| | | | |_) | |
 
5
 *                            | (__| |_| |  _ <| |___
 
6
 *                             \___|\___/|_| \_\_____|
 
7
 *
 
8
 * Copyright (C) 1998 - 2011, 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 "setup.h"
 
23
 
 
24
#include <curl/curl.h>
 
25
 
 
26
#include "rawstr.h"
 
27
 
 
28
#define ENABLE_CURLX_PRINTF
 
29
/* use our own printf() functions */
 
30
#include "curlx.h"
 
31
 
 
32
#include "tool_cfgable.h"
 
33
#include "tool_convert.h"
 
34
#include "tool_operhlp.h"
 
35
#include "tool_version.h"
 
36
 
 
37
#include "memdebug.h" /* keep this as LAST include */
 
38
 
 
39
/*
 
40
 * my_useragent: returns allocated string with default user agent
 
41
 */
 
42
char *my_useragent(void)
 
43
{
 
44
  char useragent[256]; /* we don't want a larger default user agent */
 
45
 
 
46
  snprintf(useragent, sizeof(useragent),
 
47
           CURL_NAME "/" CURL_VERSION " (" OS ") " "%s", curl_version());
 
48
 
 
49
  return strdup(useragent);
 
50
}
 
51
 
 
52
/*
 
53
 * Print list of OpenSSL supported engines
 
54
 */
 
55
void list_engines(const struct curl_slist *engines)
 
56
{
 
57
  puts("Build-time engines:");
 
58
  if(!engines) {
 
59
    puts("  <none>");
 
60
    return;
 
61
  }
 
62
  for(; engines; engines = engines->next)
 
63
    printf("  %s\n", engines->data);
 
64
}
 
65
 
 
66
void clean_getout(struct Configurable *config)
 
67
{
 
68
  struct getout *next;
 
69
  struct getout *node = config->url_list;
 
70
 
 
71
  while(node) {
 
72
    next = node->next;
 
73
    Curl_safefree(node->url);
 
74
    Curl_safefree(node->outfile);
 
75
    Curl_safefree(node->infile);
 
76
    Curl_safefree(node);
 
77
    node = next;
 
78
  }
 
79
  config->url_list = NULL;
 
80
}
 
81
 
 
82
bool output_expected(const char *url, const char *uploadfile)
 
83
{
 
84
  if(!uploadfile)
 
85
    return TRUE;  /* download */
 
86
  if(checkprefix("http://", url) || checkprefix("https://", url))
 
87
    return TRUE;   /* HTTP(S) upload */
 
88
 
 
89
  return FALSE; /* non-HTTP upload, probably no output should be expected */
 
90
}
 
91
 
 
92
bool stdin_upload(const char *uploadfile)
 
93
{
 
94
  return (curlx_strequal(uploadfile, "-") ||
 
95
          curlx_strequal(uploadfile, ".")) ? TRUE : FALSE;
 
96
}
 
97
 
 
98
/*
 
99
 * Adds the file name to the URL if it doesn't already have one.
 
100
 * url will be freed before return if the returned pointer is different
 
101
 */
 
102
char *add_file_name_to_url(CURL *curl, char *url, const char *filename)
 
103
{
 
104
  /* If no file name part is given in the URL, we add this file name */
 
105
  char *ptr = strstr(url, "://");
 
106
  if(ptr)
 
107
    ptr += 3;
 
108
  else
 
109
    ptr = url;
 
110
  ptr = strrchr(ptr, '/');
 
111
  if(!ptr || !strlen(++ptr)) {
 
112
    /* The URL has no file name part, add the local file name. In order
 
113
       to be able to do so, we have to create a new URL in another
 
114
       buffer.*/
 
115
 
 
116
    /* We only want the part of the local path that is on the right
 
117
       side of the rightmost slash and backslash. */
 
118
    const char *filep = strrchr(filename, '/');
 
119
    char *file2 = strrchr(filep?filep:filename, '\\');
 
120
    char *encfile;
 
121
 
 
122
    if(file2)
 
123
      filep = file2 + 1;
 
124
    else if(filep)
 
125
      filep++;
 
126
    else
 
127
      filep = filename;
 
128
 
 
129
    /* URL encode the file name */
 
130
    encfile = curl_easy_escape(curl, filep, 0 /* use strlen */);
 
131
    if(encfile) {
 
132
      char *urlbuffer = malloc(strlen(url) + strlen(encfile) + 3);
 
133
      if(!urlbuffer) {
 
134
        curl_free(encfile);
 
135
        Curl_safefree(url);
 
136
        return NULL;
 
137
      }
 
138
      if(ptr)
 
139
        /* there is a trailing slash on the URL */
 
140
        sprintf(urlbuffer, "%s%s", url, encfile);
 
141
      else
 
142
        /* there is no trailing slash on the URL */
 
143
        sprintf(urlbuffer, "%s/%s", url, encfile);
 
144
 
 
145
      curl_free(encfile);
 
146
      Curl_safefree(url);
 
147
 
 
148
      url = urlbuffer; /* use our new URL instead! */
 
149
    }
 
150
  }
 
151
  return url;
 
152
}
 
153
 
 
154
/* Extracts the name portion of the URL.
 
155
 * Returns a pointer to a heap-allocated string or NULL if
 
156
 * no name part, at location indicated by first argument.
 
157
 */
 
158
CURLcode get_url_file_name(char **filename, const char *url)
 
159
{
 
160
  const char *pc;
 
161
 
 
162
  *filename = NULL;
 
163
 
 
164
  /* Find and get the remote file name */
 
165
  pc = strstr(url, "://");
 
166
  if(pc)
 
167
    pc += 3;
 
168
  else
 
169
    pc = url;
 
170
  pc = strrchr(pc, '/');
 
171
 
 
172
  if(pc) {
 
173
    /* duplicate the string beyond the slash */
 
174
    pc++;
 
175
    if(*pc) {
 
176
      *filename = strdup(pc);
 
177
      if(!*filename)
 
178
        return CURLE_OUT_OF_MEMORY;
 
179
    }
 
180
  }
 
181
 
 
182
  /* in case we built debug enabled, we allow an environment variable
 
183
   * named CURL_TESTDIR to prefix the given file name to put it into a
 
184
   * specific directory
 
185
   */
 
186
#ifdef DEBUGBUILD
 
187
  {
 
188
    char *tdir = curlx_getenv("CURL_TESTDIR");
 
189
    if(tdir) {
 
190
      char buffer[512]; /* suitably large */
 
191
      snprintf(buffer, sizeof(buffer), "%s/%s", tdir, *filename);
 
192
      Curl_safefree(*filename);
 
193
      *filename = strdup(buffer); /* clone the buffer */
 
194
      curl_free(tdir);
 
195
    }
 
196
  }
 
197
#endif
 
198
 
 
199
  return CURLE_OK;
 
200
}
 
201
 
 
202
/*
 
203
 * This is the main global constructor for the app. Call this before
 
204
 * _any_ libcurl usage. If this fails, *NO* libcurl functions may be
 
205
 * used, or havoc may be the result.
 
206
 */
 
207
CURLcode main_init(void)
 
208
{
 
209
#if defined(__DJGPP__) || defined(__GO32__)
 
210
  /* stop stat() wasting time */
 
211
  _djstat_flags |= _STAT_INODE | _STAT_EXEC_MAGIC | _STAT_DIRSIZE;
 
212
#endif
 
213
 
 
214
  return curl_global_init(CURL_GLOBAL_DEFAULT);
 
215
}
 
216
 
 
217
/*
 
218
 * This is the main global destructor for the app. Call this after
 
219
 * _all_ libcurl usage is done.
 
220
 */
 
221
void main_free(void)
 
222
{
 
223
  curl_global_cleanup();
 
224
  convert_cleanup();
 
225
}
 
226
 
 
227
#ifdef CURLDEBUG
 
228
void memory_tracking_init(void)
 
229
{
 
230
  char *env;
 
231
  /* if CURL_MEMDEBUG is set, this starts memory tracking message logging */
 
232
  env = curlx_getenv("CURL_MEMDEBUG");
 
233
  if(env) {
 
234
    /* use the value as file name */
 
235
    char fname[CURL_MT_LOGFNAME_BUFSIZE];
 
236
    if(strlen(env) >= CURL_MT_LOGFNAME_BUFSIZE)
 
237
      env[CURL_MT_LOGFNAME_BUFSIZE-1] = '\0';
 
238
    strcpy(fname, env);
 
239
    curl_free(env);
 
240
    curl_memdebug(fname);
 
241
    /* this weird stuff here is to make curl_free() get called
 
242
       before curl_memdebug() as otherwise memory tracking will
 
243
       log a free() without an alloc! */
 
244
  }
 
245
  /* if CURL_MEMLIMIT is set, this enables fail-on-alloc-number-N feature */
 
246
  env = curlx_getenv("CURL_MEMLIMIT");
 
247
  if(env) {
 
248
    char *endptr;
 
249
    long num = strtol(env, &endptr, 10);
 
250
    if((endptr != env) && (endptr == env + strlen(env)) && (num > 0))
 
251
      curl_memlimit(num);
 
252
    curl_free(env);
 
253
  }
 
254
}
 
255
#endif
 
256