lib508.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*****************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * $Id$
  9. */
  10. #include "test.h"
  11. static char data[]="this is what we post to the silly web server\n";
  12. struct WriteThis {
  13. char *readptr;
  14. size_t sizeleft;
  15. };
  16. static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *userp)
  17. {
  18. struct WriteThis *pooh = (struct WriteThis *)userp;
  19. if(size*nmemb < 1)
  20. return 0;
  21. if(pooh->sizeleft) {
  22. *(char *)ptr = pooh->readptr[0]; /* copy one single byte */
  23. pooh->readptr++; /* advance pointer */
  24. pooh->sizeleft--; /* less data left */
  25. return 1; /* we return 1 byte at a time! */
  26. }
  27. return 0; /* no more data left to deliver */
  28. }
  29. int test(char *URL)
  30. {
  31. CURL *curl;
  32. CURLcode res=CURLE_OK;
  33. struct WriteThis pooh;
  34. pooh.readptr = data;
  35. pooh.sizeleft = strlen(data);
  36. if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
  37. fprintf(stderr, "curl_global_init() failed\n");
  38. return TEST_ERR_MAJOR_BAD;
  39. }
  40. if ((curl = curl_easy_init()) == NULL) {
  41. fprintf(stderr, "curl_easy_init() failed\n");
  42. curl_global_cleanup();
  43. return TEST_ERR_MAJOR_BAD;
  44. }
  45. /* First set the URL that is about to receive our POST. */
  46. curl_easy_setopt(curl, CURLOPT_URL, URL);
  47. /* Now specify we want to POST data */
  48. curl_easy_setopt(curl, CURLOPT_POST, 1L);
  49. /* Set the expected POST size */
  50. curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)pooh.sizeleft);
  51. /* we want to use our own read function */
  52. curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
  53. /* pointer to pass to our read function */
  54. curl_easy_setopt(curl, CURLOPT_INFILE, &pooh);
  55. /* get verbose debug output please */
  56. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  57. /* include headers in the output */
  58. curl_easy_setopt(curl, CURLOPT_HEADER, 1L);
  59. /* Perform the request, res will get the return code */
  60. res = curl_easy_perform(curl);
  61. /* always cleanup */
  62. curl_easy_cleanup(curl);
  63. curl_global_cleanup();
  64. return res;
  65. }