~ubuntu-branches/ubuntu/natty/curl/natty-security

« back to all changes in this revision

Viewing changes to docs/examples/ftpuploadresume.c

  • Committer: Bazaar Package Importer
  • Author(s): Bhavani Shankar
  • Date: 2010-06-20 13:56:28 UTC
  • mfrom: (3.4.7 sid)
  • Revision ID: james.westby@ubuntu.com-20100620135628-e30tp9jldq6hq985
Tags: 7.21.0-1ubuntu1
* Merge from debian unstable.  Remaining changes: LP: #596334
  - Keep build deps in main:
    - Drop build dependencies: stunnel, libssh2-1-dev
    - Add build-dependency on openssh-server
    - Drop libssh2-1-dev from libcurl4-openssl-dev's Depends.

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
 *                            | (__| |_| |  _ <| |___
6
6
 *                             \___|\___/|_| \_\_____|
7
7
 *
8
 
 * $Id: ftpuploadresume.c,v 1.5 2008-08-31 12:12:35 yangtse Exp $
9
8
 *
10
9
 * Upload to FTP, resuming failed transfers
11
10
 *
34
33
 
35
34
/* parse headers for Content-Length */
36
35
size_t getcontentlengthfunc(void *ptr, size_t size, size_t nmemb, void *stream) {
37
 
        int r;
38
 
        long len = 0;
39
 
 
40
 
        /* _snscanf() is Win32 specific */
41
 
        r = _snscanf(ptr, size * nmemb, "Content-Length: %ld\n", &len);
42
 
 
43
 
        if (r) /* Microsoft: we don't read the specs */
44
 
                *((long *) stream) = len;
45
 
 
46
 
        return size * nmemb;
 
36
  int r;
 
37
  long len = 0;
 
38
 
 
39
  /* _snscanf() is Win32 specific */
 
40
  r = _snscanf(ptr, size * nmemb, "Content-Length: %ld\n", &len);
 
41
 
 
42
  if (r) /* Microsoft: we don't read the specs */
 
43
    *((long *) stream) = len;
 
44
 
 
45
  return size * nmemb;
47
46
}
48
47
 
49
48
/* discard downloaded data */
50
49
size_t discardfunc(void *ptr, size_t size, size_t nmemb, void *stream) {
51
 
        return size * nmemb;
 
50
  return size * nmemb;
52
51
}
53
52
 
54
53
/* read data to upload */
55
54
size_t readfunc(void *ptr, size_t size, size_t nmemb, void *stream)
56
55
{
57
 
        FILE *f = stream;
58
 
        size_t n;
59
 
 
60
 
        if (ferror(f))
61
 
                return CURL_READFUNC_ABORT;
62
 
 
63
 
        n = fread(ptr, size, nmemb, f) * size;
64
 
 
65
 
        return n;
 
56
  FILE *f = stream;
 
57
  size_t n;
 
58
 
 
59
  if (ferror(f))
 
60
    return CURL_READFUNC_ABORT;
 
61
 
 
62
  n = fread(ptr, size, nmemb, f) * size;
 
63
 
 
64
  return n;
66
65
}
67
66
 
68
67
 
69
68
int upload(CURL *curlhandle, const char * remotepath, const char * localpath,
70
69
           long timeout, long tries)
71
70
{
72
 
        FILE *f;
73
 
        long uploaded_len = 0;
74
 
        CURLcode r = CURLE_GOT_NOTHING;
75
 
        int c;
76
 
 
77
 
        f = fopen(localpath, "rb");
78
 
        if (f == NULL) {
79
 
                perror(NULL);
80
 
                return 0;
81
 
        }
82
 
 
83
 
        curl_easy_setopt(curlhandle, CURLOPT_UPLOAD, 1L);
84
 
 
85
 
        curl_easy_setopt(curlhandle, CURLOPT_URL, remotepath);
86
 
 
87
 
        if (timeout)
88
 
                curl_easy_setopt(curlhandle, CURLOPT_FTP_RESPONSE_TIMEOUT, timeout);
89
 
 
90
 
        curl_easy_setopt(curlhandle, CURLOPT_HEADERFUNCTION, getcontentlengthfunc);
91
 
        curl_easy_setopt(curlhandle, CURLOPT_HEADERDATA, &uploaded_len);
92
 
 
93
 
        curl_easy_setopt(curlhandle, CURLOPT_WRITEFUNCTION, discardfunc);
94
 
 
95
 
        curl_easy_setopt(curlhandle, CURLOPT_READFUNCTION, readfunc);
96
 
        curl_easy_setopt(curlhandle, CURLOPT_READDATA, f);
97
 
 
98
 
        curl_easy_setopt(curlhandle, CURLOPT_FTPPORT, "-"); /* disable passive mode */
99
 
        curl_easy_setopt(curlhandle, CURLOPT_FTP_CREATE_MISSING_DIRS, 1L);
100
 
 
101
 
        curl_easy_setopt(curlhandle, CURLOPT_VERBOSE, 1L);
102
 
 
103
 
        for (c = 0; (r != CURLE_OK) && (c < tries); c++) {
104
 
                /* are we resuming? */
105
 
                if (c) { /* yes */
106
 
                        /* determine the length of the file already written */
107
 
 
108
 
                        /*
109
 
                         * With NOBODY and NOHEADER, libcurl will issue a SIZE
110
 
                         * command, but the only way to retrieve the result is
111
 
                         * to parse the returned Content-Length header. Thus,
112
 
                         * getcontentlengthfunc(). We need discardfunc() above
113
 
                         * because HEADER will dump the headers to stdout
114
 
                         * without it.
115
 
                         */
116
 
                        curl_easy_setopt(curlhandle, CURLOPT_NOBODY, 1L);
117
 
                        curl_easy_setopt(curlhandle, CURLOPT_HEADER, 1L);
118
 
 
119
 
                        r = curl_easy_perform(curlhandle);
120
 
                        if (r != CURLE_OK)
121
 
                                continue;
122
 
 
123
 
                        curl_easy_setopt(curlhandle, CURLOPT_NOBODY, 0L);
124
 
                        curl_easy_setopt(curlhandle, CURLOPT_HEADER, 0L);
125
 
 
126
 
                        fseek(f, uploaded_len, SEEK_SET);
127
 
 
128
 
                        curl_easy_setopt(curlhandle, CURLOPT_APPEND, 1L);
129
 
                }
130
 
                else { /* no */
131
 
                        curl_easy_setopt(curlhandle, CURLOPT_APPEND, 0L);
132
 
                }
133
 
 
134
 
                r = curl_easy_perform(curlhandle);
135
 
        }
136
 
 
137
 
        fclose(f);
138
 
 
139
 
        if (r == CURLE_OK)
140
 
                return 1;
141
 
        else {
142
 
                fprintf(stderr, "%s\n", curl_easy_strerror(r));
143
 
                return 0;
144
 
        }
 
71
  FILE *f;
 
72
  long uploaded_len = 0;
 
73
  CURLcode r = CURLE_GOT_NOTHING;
 
74
  int c;
 
75
 
 
76
  f = fopen(localpath, "rb");
 
77
  if (f == NULL) {
 
78
    perror(NULL);
 
79
    return 0;
 
80
  }
 
81
 
 
82
  curl_easy_setopt(curlhandle, CURLOPT_UPLOAD, 1L);
 
83
 
 
84
  curl_easy_setopt(curlhandle, CURLOPT_URL, remotepath);
 
85
 
 
86
  if (timeout)
 
87
    curl_easy_setopt(curlhandle, CURLOPT_FTP_RESPONSE_TIMEOUT, timeout);
 
88
 
 
89
  curl_easy_setopt(curlhandle, CURLOPT_HEADERFUNCTION, getcontentlengthfunc);
 
90
  curl_easy_setopt(curlhandle, CURLOPT_HEADERDATA, &uploaded_len);
 
91
 
 
92
  curl_easy_setopt(curlhandle, CURLOPT_WRITEFUNCTION, discardfunc);
 
93
 
 
94
  curl_easy_setopt(curlhandle, CURLOPT_READFUNCTION, readfunc);
 
95
  curl_easy_setopt(curlhandle, CURLOPT_READDATA, f);
 
96
 
 
97
  curl_easy_setopt(curlhandle, CURLOPT_FTPPORT, "-"); /* disable passive mode */
 
98
  curl_easy_setopt(curlhandle, CURLOPT_FTP_CREATE_MISSING_DIRS, 1L);
 
99
 
 
100
  curl_easy_setopt(curlhandle, CURLOPT_VERBOSE, 1L);
 
101
 
 
102
  for (c = 0; (r != CURLE_OK) && (c < tries); c++) {
 
103
    /* are we resuming? */
 
104
    if (c) { /* yes */
 
105
      /* determine the length of the file already written */
 
106
 
 
107
      /*
 
108
       * With NOBODY and NOHEADER, libcurl will issue a SIZE
 
109
       * command, but the only way to retrieve the result is
 
110
       * to parse the returned Content-Length header. Thus,
 
111
       * getcontentlengthfunc(). We need discardfunc() above
 
112
       * because HEADER will dump the headers to stdout
 
113
       * without it.
 
114
       */
 
115
      curl_easy_setopt(curlhandle, CURLOPT_NOBODY, 1L);
 
116
      curl_easy_setopt(curlhandle, CURLOPT_HEADER, 1L);
 
117
 
 
118
      r = curl_easy_perform(curlhandle);
 
119
      if (r != CURLE_OK)
 
120
        continue;
 
121
 
 
122
      curl_easy_setopt(curlhandle, CURLOPT_NOBODY, 0L);
 
123
      curl_easy_setopt(curlhandle, CURLOPT_HEADER, 0L);
 
124
 
 
125
      fseek(f, uploaded_len, SEEK_SET);
 
126
 
 
127
      curl_easy_setopt(curlhandle, CURLOPT_APPEND, 1L);
 
128
    }
 
129
    else { /* no */
 
130
      curl_easy_setopt(curlhandle, CURLOPT_APPEND, 0L);
 
131
    }
 
132
 
 
133
    r = curl_easy_perform(curlhandle);
 
134
  }
 
135
 
 
136
  fclose(f);
 
137
 
 
138
  if (r == CURLE_OK)
 
139
    return 1;
 
140
  else {
 
141
    fprintf(stderr, "%s\n", curl_easy_strerror(r));
 
142
    return 0;
 
143
  }
145
144
}
146
145
 
147
146
int main(int c, char **argv) {
148
 
        CURL *curlhandle = NULL;
149
 
 
150
 
        curl_global_init(CURL_GLOBAL_ALL);
151
 
        curlhandle = curl_easy_init();
152
 
 
153
 
        upload(curlhandle, "ftp://user:pass@host/path/file", "C:\\file", 0, 3);
154
 
 
155
 
        curl_easy_cleanup(curlhandle);
156
 
        curl_global_cleanup();
157
 
 
158
 
        return 0;
 
147
  CURL *curlhandle = NULL;
 
148
 
 
149
  curl_global_init(CURL_GLOBAL_ALL);
 
150
  curlhandle = curl_easy_init();
 
151
 
 
152
  upload(curlhandle, "ftp://user:pass@host/path/file", "C:\\file", 0, 3);
 
153
 
 
154
  curl_easy_cleanup(curlhandle);
 
155
  curl_global_cleanup();
 
156
 
 
157
  return 0;
159
158
}