lib510.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #include "test.h"
  2. static const char *post[]={
  3. "one",
  4. "two",
  5. "three",
  6. "and a final longer crap: four",
  7. NULL
  8. };
  9. struct WriteThis {
  10. int counter;
  11. };
  12. static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *userp)
  13. {
  14. struct WriteThis *pooh = (struct WriteThis *)userp;
  15. const char *data;
  16. if(size*nmemb < 1)
  17. return 0;
  18. data = post[pooh->counter];
  19. if(data) {
  20. size_t len = strlen(data);
  21. memcpy(ptr, data, len);
  22. pooh->counter++; /* advance pointer */
  23. return len;
  24. }
  25. return 0; /* no more data left to deliver */
  26. }
  27. int test(char *URL)
  28. {
  29. CURL *curl;
  30. CURLcode res=CURLE_OK;
  31. struct curl_slist *slist = NULL;
  32. struct WriteThis pooh;
  33. pooh.counter = 0;
  34. slist = curl_slist_append(slist, "Transfer-Encoding: chunked");
  35. curl = curl_easy_init();
  36. if(curl) {
  37. /* First set the URL that is about to receive our POST. */
  38. curl_easy_setopt(curl, CURLOPT_URL, URL);
  39. /* Now specify we want to POST data */
  40. curl_easy_setopt(curl, CURLOPT_POST, TRUE);
  41. /* we want to use our own read function */
  42. curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
  43. /* pointer to pass to our read function */
  44. curl_easy_setopt(curl, CURLOPT_INFILE, &pooh);
  45. /* get verbose debug output please */
  46. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  47. /* include headers in the output */
  48. curl_easy_setopt(curl, CURLOPT_HEADER, TRUE);
  49. /* enforce chunked transfer by setting the header */
  50. curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist);
  51. /* Perform the request, res will get the return code */
  52. res = curl_easy_perform(curl);
  53. /* always cleanup */
  54. curl_easy_cleanup(curl);
  55. /* clean up the headers list */
  56. curl_slist_free_all(slist);
  57. }
  58. return res;
  59. }