anyauthput.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2019, 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. #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 authentiction 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(void *ptr, size_t size, size_t nmemb, void *stream)
  72. {
  73. ssize_t retcode;
  74. curl_off_t nread;
  75. int *fdp = (int *)stream;
  76. int fd = *fdp;
  77. retcode = read(fd, ptr, (READ_3RD_ARG)(size * nmemb));
  78. nread = (curl_off_t)retcode;
  79. fprintf(stderr, "*** We read %" CURL_FORMAT_CURL_OFF_T
  80. " bytes from file\n", nread);
  81. return retcode;
  82. }
  83. int main(int argc, char **argv)
  84. {
  85. CURL *curl;
  86. CURLcode res;
  87. int hd;
  88. struct stat file_info;
  89. char *file;
  90. char *url;
  91. if(argc < 3)
  92. return 1;
  93. file = argv[1];
  94. url = argv[2];
  95. /* get the file size of the local file */
  96. hd = open(file, O_RDONLY);
  97. fstat(hd, &file_info);
  98. /* In windows, this will init the winsock stuff */
  99. curl_global_init(CURL_GLOBAL_ALL);
  100. /* get a curl handle */
  101. curl = curl_easy_init();
  102. if(curl) {
  103. /* we want to use our own read function */
  104. curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
  105. /* which file to upload */
  106. curl_easy_setopt(curl, CURLOPT_READDATA, (void *)&hd);
  107. /* set the ioctl function */
  108. curl_easy_setopt(curl, CURLOPT_IOCTLFUNCTION, my_ioctl);
  109. /* pass the file descriptor to the ioctl callback as well */
  110. curl_easy_setopt(curl, CURLOPT_IOCTLDATA, (void *)&hd);
  111. /* enable "uploading" (which means PUT when doing HTTP) */
  112. curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
  113. /* specify target URL, and note that this URL should also include a file
  114. name, not only a directory (as you can do with GTP uploads) */
  115. curl_easy_setopt(curl, CURLOPT_URL, url);
  116. /* and give the size of the upload, this supports large file sizes
  117. on systems that have general support for it */
  118. curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
  119. (curl_off_t)file_info.st_size);
  120. /* tell libcurl we can use "any" auth, which lets the lib pick one, but it
  121. also costs one extra round-trip and possibly sending of all the PUT
  122. data twice!!! */
  123. curl_easy_setopt(curl, CURLOPT_HTTPAUTH, (long)CURLAUTH_ANY);
  124. /* set user name and password for the authentication */
  125. curl_easy_setopt(curl, CURLOPT_USERPWD, "user:password");
  126. /* Now run off and do what you've been told! */
  127. res = curl_easy_perform(curl);
  128. /* Check for errors */
  129. if(res != CURLE_OK)
  130. fprintf(stderr, "curl_easy_perform() failed: %s\n",
  131. curl_easy_strerror(res));
  132. /* always cleanup */
  133. curl_easy_cleanup(curl);
  134. }
  135. close(hd); /* close the local file */
  136. curl_global_cleanup();
  137. return 0;
  138. }