anyauthput.c 4.9 KB

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