anyauthput.c 3.9 KB

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