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 <stdio.h> 23#include <fcntl.h> 24#ifdef WIN32 25# include <io.h> 26#else 27# ifdef __VMS 28 typedef int intptr_t; 29# endif 30# include <unistd.h> 31#endif 32#include <sys/types.h> 33#include <sys/stat.h> 34 35#ifdef _MSC_VER 36# ifdef _WIN64 37 typedef __int64 intptr_t; 38# else 39 typedef int intptr_t; 40# endif 41#endif 42 43#include <curl/curl.h> 44#include "printf_macro.h" 45 46#if LIBCURL_VERSION_NUM < 0x070c03 47#error "upgrade your libcurl to no less than 7.12.3" 48#endif 49 50#ifndef TRUE 51#define TRUE 1 52#endif 53 54/* 55 * This example shows a HTTP PUT operation with authentiction using "any" 56 * type. It PUTs a file given as a command line argument to the URL also given 57 * on the command line. 58 * 59 * Since libcurl 7.12.3, using "any" auth and POST/PUT requires a set ioctl 60 * function. 61 * 62 * This example also uses its own read callback. 63 */ 64 65/* ioctl callback function */ 66static curlioerr my_ioctl(CURL *handle, curliocmd cmd, void *userp) 67{ 68 intptr_t fd = (intptr_t)userp; 69 70 (void)handle; /* not used in here */ 71 72 switch(cmd) { 73 case CURLIOCMD_RESTARTREAD: 74 /* mr libcurl kindly asks as to rewind the read data stream to start */ 75 if(-1 == lseek(fd, 0, SEEK_SET)) 76 /* couldn't rewind */ 77 return CURLIOE_FAILRESTART; 78 79 break; 80 81 default: /* ignore unknown commands */ 82 return CURLIOE_UNKNOWNCMD; 83 } 84 return CURLIOE_OK; /* success! */ 85} 86 87/* read callback function, fread() look alike */ 88static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream) 89{ 90 size_t retcode; 91 92 intptr_t fd = (intptr_t)stream; 93 94 retcode = read(fd, ptr, size * nmemb); 95 96 fprintf(stderr, "*** We read %" _FMT_SIZE_T " bytes from file\n", retcode); 97 98 return retcode; 99} 100 101int main(int argc, char **argv) 102{ 103 CURL *curl; 104 CURLcode res; 105 intptr_t hd ; 106 struct stat file_info; 107 108 char *file; 109 char *url; 110 111 if(argc < 3) 112 return 1; 113 114 file= argv[1]; 115 url = argv[2]; 116 117 /* get the file size of the local file */ 118 hd = open(file, O_RDONLY) ; 119 fstat(hd, &file_info); 120 121 /* In windows, this will init the winsock stuff */ 122 curl_global_init(CURL_GLOBAL_ALL); 123 124 /* get a curl handle */ 125 curl = curl_easy_init(); 126 if(curl) { 127 /* we want to use our own read function */ 128 curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback); 129 130 /* which file to upload */ 131 curl_easy_setopt(curl, CURLOPT_READDATA, (void*)hd); 132 133 /* set the ioctl function */ 134 curl_easy_setopt(curl, CURLOPT_IOCTLFUNCTION, my_ioctl); 135 136 /* pass the file descriptor to the ioctl callback as well */ 137 curl_easy_setopt(curl, CURLOPT_IOCTLDATA, (void*)hd); 138 139 /* enable "uploading" (which means PUT when doing HTTP) */ 140 curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L) ; 141 142 /* specify target URL, and note that this URL should also include a file 143 name, not only a directory (as you can do with GTP uploads) */ 144 curl_easy_setopt(curl,CURLOPT_URL, url); 145 146 /* and give the size of the upload, this supports large file sizes 147 on systems that have general support for it */ 148 curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, 149 (curl_off_t)file_info.st_size); 150 151 /* tell libcurl we can use "any" auth, which lets the lib pick one, but it 152 also costs one extra round-trip and possibly sending of all the PUT 153 data twice!!! */ 154 curl_easy_setopt(curl, CURLOPT_HTTPAUTH, (long)CURLAUTH_ANY); 155 156 /* set user name and password for the authentication */ 157 curl_easy_setopt(curl, CURLOPT_USERPWD, "user:password"); 158 159 /* Now run off and do what you've been told! */ 160 res = curl_easy_perform(curl); 161 162 /* always cleanup */ 163 curl_easy_cleanup(curl); 164 } 165 close(hd); /* close the local file */ 166 167 curl_global_cleanup(); 168 return 0; 169} 170