blob: 6b05c4cec72e17ab40af2b368260d27c4ba520e8 [file] [log] [blame]
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001/***************************************************************************
Lucas Eckels9bd90e62012-08-06 15:07:02 -07002 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
Elliott Hughescee03382017-06-23 12:17:18 -07008 * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07009 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
Alex Deymo8f1a2142016-06-28 14:49:26 -070012 * are also available at https://curl.haxx.se/docs/copyright.html.
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070013 *
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 ***************************************************************************/
Alex Deymo8f1a2142016-06-28 14:49:26 -070022/* <DESC>
23 * Upload to a file:// URL
24 * </DESC>
25 */
Lucas Eckels9bd90e62012-08-06 15:07:02 -070026#include <stdio.h>
27#include <curl/curl.h>
28#include <sys/stat.h>
29#include <fcntl.h>
30
31int main(void)
32{
33 CURL *curl;
34 CURLcode res;
35 struct stat file_info;
36 double speed_upload, total_time;
37 FILE *fd;
38
39 fd = fopen("debugit", "rb"); /* open file to upload */
Elliott Hughescee03382017-06-23 12:17:18 -070040 if(!fd)
Lucas Eckels9bd90e62012-08-06 15:07:02 -070041 return 1; /* can't continue */
Lucas Eckels9bd90e62012-08-06 15:07:02 -070042
43 /* to get the file size */
Elliott Hughescee03382017-06-23 12:17:18 -070044 if(fstat(fileno(fd), &file_info) != 0)
Lucas Eckels9bd90e62012-08-06 15:07:02 -070045 return 1; /* can't continue */
Lucas Eckels9bd90e62012-08-06 15:07:02 -070046
47 curl = curl_easy_init();
48 if(curl) {
49 /* upload to this place */
50 curl_easy_setopt(curl, CURLOPT_URL,
51 "file:///home/dast/src/curl/debug/new");
52
53 /* tell it to "upload" to the URL */
54 curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
55
56 /* set where to read from (on Windows you need to use READFUNCTION too) */
57 curl_easy_setopt(curl, CURLOPT_READDATA, fd);
58
59 /* and give the size of the upload (optional) */
60 curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
61 (curl_off_t)file_info.st_size);
62
63 /* enable verbose for easier tracing */
64 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
65
66 res = curl_easy_perform(curl);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070067 /* Check for errors */
68 if(res != CURLE_OK) {
69 fprintf(stderr, "curl_easy_perform() failed: %s\n",
70 curl_easy_strerror(res));
Lucas Eckels9bd90e62012-08-06 15:07:02 -070071
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070072 }
73 else {
74 /* now extract transfer info */
75 curl_easy_getinfo(curl, CURLINFO_SPEED_UPLOAD, &speed_upload);
76 curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &total_time);
Lucas Eckels9bd90e62012-08-06 15:07:02 -070077
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070078 fprintf(stderr, "Speed: %.3f bytes/sec during %.3f seconds\n",
79 speed_upload, total_time);
Lucas Eckels9bd90e62012-08-06 15:07:02 -070080
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070081 }
Lucas Eckels9bd90e62012-08-06 15:07:02 -070082 /* always cleanup */
83 curl_easy_cleanup(curl);
84 }
Elliott Hughescee03382017-06-23 12:17:18 -070085 fclose(fd);
Lucas Eckels9bd90e62012-08-06 15:07:02 -070086 return 0;
87}