anyauthput.c 4.7 KB

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