httpput.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 <curl/curl.h>
  14. /*
  15. * This example shows a HTTP PUT operation. PUTs a file given as a command
  16. * line argument to the URL also given on the command line.
  17. *
  18. * This example also uses its own read callback.
  19. */
  20. size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream)
  21. {
  22. size_t retcode;
  23. /* in real-world cases, this would probably get this data differently
  24. as this fread() stuff is exactly what the library already would do
  25. by default internally */
  26. retcode = fread(ptr, size, nmemb, stream);
  27. fprintf(stderr, "*** We read %d bytes from file\n", retcode);
  28. return retcode;
  29. }
  30. int main(int argc, char **argv)
  31. {
  32. CURL *curl;
  33. CURLcode res;
  34. FILE *ftpfile;
  35. FILE * hd_src ;
  36. int hd ;
  37. struct stat file_info;
  38. char *file;
  39. char *url;
  40. if(argc < 3)
  41. return 1;
  42. file= argv[1];
  43. url = argv[2];
  44. /* get the file size of the local file */
  45. hd = open(file, O_RDONLY) ;
  46. fstat(hd, &file_info);
  47. close(hd) ;
  48. /* get a FILE * of the same file, could also be made with
  49. fdopen() from the previous descriptor, but hey this is just
  50. an example! */
  51. hd_src = fopen(file, "rb");
  52. /* In windows, this will init the winsock stuff */
  53. curl_global_init(CURL_GLOBAL_ALL);
  54. /* get a curl handle */
  55. curl = curl_easy_init();
  56. if(curl) {
  57. /* we want to use our own read function */
  58. curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
  59. /* enable uploading */
  60. curl_easy_setopt(curl, CURLOPT_UPLOAD, TRUE) ;
  61. /* HTTP PUT please */
  62. curl_easy_setopt(curl, CURLOPT_PUT, TRUE);
  63. /* specify target */
  64. curl_easy_setopt(curl,CURLOPT_URL, url);
  65. /* now specify which file to upload */
  66. curl_easy_setopt(curl, CURLOPT_INFILE, hd_src);
  67. /* and give the size of the upload (optional) */
  68. curl_easy_setopt(curl, CURLOPT_INFILESIZE, file_info.st_size);
  69. /* Now run off and do what you've been told! */
  70. res = curl_easy_perform(curl);
  71. /* always cleanup */
  72. curl_easy_cleanup(curl);
  73. }
  74. fclose(hd_src); /* close the local file */
  75. curl_global_cleanup();
  76. return 0;
  77. }