anyauthput.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 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. * 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. # define FILENO(fp) _fileno(fp)
  36. #else
  37. # define FILENO(fp) fileno(fp)
  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 an 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 seek
  48. * function.
  49. *
  50. * This example also uses its own read callback.
  51. */
  52. /* seek callback function */
  53. static int my_seek(void *userp, curl_off_t offset, int origin)
  54. {
  55. FILE *fp = (FILE *) userp;
  56. if(-1 == fseek(fp, (long) offset, origin))
  57. /* couldn't seek */
  58. return CURL_SEEKFUNC_CANTSEEK;
  59. return CURL_SEEKFUNC_OK; /* success! */
  60. }
  61. /* read callback function, fread() look alike */
  62. static size_t read_callback(char *ptr, size_t size, size_t nmemb, void *stream)
  63. {
  64. ssize_t retcode;
  65. unsigned long nread;
  66. retcode = fread(ptr, size, nmemb, stream);
  67. if(retcode > 0) {
  68. nread = (unsigned long)retcode;
  69. fprintf(stderr, "*** We read %lu bytes from file\n", nread);
  70. }
  71. return retcode;
  72. }
  73. int main(int argc, char **argv)
  74. {
  75. CURL *curl;
  76. CURLcode res;
  77. FILE *fp;
  78. struct stat file_info;
  79. char *file;
  80. char *url;
  81. if(argc < 3)
  82. return 1;
  83. file = argv[1];
  84. url = argv[2];
  85. /* get the file size of the local file */
  86. fp = fopen(file, "rb");
  87. fstat(FILENO(fp), &file_info);
  88. /* In windows, this will init the winsock stuff */
  89. curl_global_init(CURL_GLOBAL_ALL);
  90. /* get a curl handle */
  91. curl = curl_easy_init();
  92. if(curl) {
  93. /* we want to use our own read function */
  94. curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
  95. /* which file to upload */
  96. curl_easy_setopt(curl, CURLOPT_READDATA, (void *) fp);
  97. /* set the seek function */
  98. curl_easy_setopt(curl, CURLOPT_SEEKFUNCTION, my_seek);
  99. /* pass the file descriptor to the seek callback as well */
  100. curl_easy_setopt(curl, CURLOPT_SEEKDATA, (void *) fp);
  101. /* enable "uploading" (which means PUT when doing HTTP) */
  102. curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
  103. /* specify target URL, and note that this URL should also include a file
  104. name, not only a directory (as you can do with GTP uploads) */
  105. curl_easy_setopt(curl, CURLOPT_URL, url);
  106. /* and give the size of the upload, this supports large file sizes
  107. on systems that have general support for it */
  108. curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
  109. (curl_off_t)file_info.st_size);
  110. /* tell libcurl we can use "any" auth, which lets the lib pick one, but it
  111. also costs one extra round-trip and possibly sending of all the PUT
  112. data twice!!! */
  113. curl_easy_setopt(curl, CURLOPT_HTTPAUTH, (long)CURLAUTH_ANY);
  114. /* set user name and password for the authentication */
  115. curl_easy_setopt(curl, CURLOPT_USERPWD, "user:password");
  116. /* Now run off and do what you have been told! */
  117. res = curl_easy_perform(curl);
  118. /* Check for errors */
  119. if(res != CURLE_OK)
  120. fprintf(stderr, "curl_easy_perform() failed: %s\n",
  121. curl_easy_strerror(res));
  122. /* always cleanup */
  123. curl_easy_cleanup(curl);
  124. }
  125. fclose(fp); /* close the local file */
  126. curl_global_cleanup();
  127. return 0;
  128. }