anyauthput.c 4.6 KB

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