anyauthput.c 5.0 KB

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