httpput.c 2.8 KB

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