anyauthput.c 4.8 KB

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