httpput.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 with easy interface and read callback
  26. * </DESC>
  27. */
  28. #include <stdio.h>
  29. #include <fcntl.h>
  30. #include <sys/stat.h>
  31. #include <curl/curl.h>
  32. #ifdef _WIN32
  33. #undef stat
  34. #define stat _stat
  35. #endif
  36. /*
  37. * This example shows an HTTP PUT operation. PUTs a file given as a command
  38. * line argument to the URL also given on the command line.
  39. *
  40. * This example also uses its own read callback.
  41. *
  42. * Here's an article on how to setup a PUT handler for Apache:
  43. * http://www.apacheweek.com/features/put
  44. */
  45. static size_t read_callback(char *ptr, size_t size, size_t nmemb, void *stream)
  46. {
  47. size_t retcode;
  48. unsigned long nread;
  49. /* in real-world cases, this would probably get this data differently
  50. as this fread() stuff is exactly what the library already would do
  51. by default internally */
  52. retcode = fread(ptr, size, nmemb, stream);
  53. if(retcode > 0) {
  54. nread = (unsigned long)retcode;
  55. fprintf(stderr, "*** We read %lu bytes from file\n", nread);
  56. }
  57. return retcode;
  58. }
  59. int main(int argc, char **argv)
  60. {
  61. CURL *curl;
  62. CURLcode res;
  63. FILE * hd_src;
  64. struct stat file_info;
  65. char *file;
  66. char *url;
  67. if(argc < 3)
  68. return 1;
  69. file = argv[1];
  70. url = argv[2];
  71. /* get the file size of the local file */
  72. stat(file, &file_info);
  73. /* get a FILE * of the same file, could also be made with
  74. fdopen() from the previous descriptor, but hey this is just
  75. an example! */
  76. hd_src = fopen(file, "rb");
  77. /* In Windows, this inits the Winsock stuff */
  78. curl_global_init(CURL_GLOBAL_ALL);
  79. /* get a curl handle */
  80. curl = curl_easy_init();
  81. if(curl) {
  82. /* we want to use our own read function */
  83. curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
  84. /* enable uploading (implies PUT over HTTP) */
  85. curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
  86. /* specify target URL, and note that this URL should include a file
  87. name, not only a directory */
  88. curl_easy_setopt(curl, CURLOPT_URL, url);
  89. /* now specify which file to upload */
  90. curl_easy_setopt(curl, CURLOPT_READDATA, hd_src);
  91. /* provide the size of the upload, we typecast the value to curl_off_t
  92. since we must be sure to use the correct data size */
  93. curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
  94. (curl_off_t)file_info.st_size);
  95. /* Now run off and do what you have been told! */
  96. res = curl_easy_perform(curl);
  97. /* Check for errors */
  98. if(res != CURLE_OK)
  99. fprintf(stderr, "curl_easy_perform() failed: %s\n",
  100. curl_easy_strerror(res));
  101. /* always cleanup */
  102. curl_easy_cleanup(curl);
  103. }
  104. fclose(hd_src); /* close the local file */
  105. curl_global_cleanup();
  106. return 0;
  107. }