lib508.c 1.8 KB

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