httpput.c 3.6 KB

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