lib554.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. CURLFORMcode formrc;
  34. struct curl_httppost *formpost=NULL;
  35. struct curl_httppost *lastptr=NULL;
  36. struct WriteThis pooh;
  37. if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
  38. fprintf(stderr, "curl_global_init() failed\n");
  39. return TEST_ERR_MAJOR_BAD;
  40. }
  41. pooh.readptr = data;
  42. pooh.sizeleft = strlen(data);
  43. /* Fill in the file upload field */
  44. formrc = curl_formadd(&formpost,
  45. &lastptr,
  46. CURLFORM_COPYNAME, "sendfile",
  47. CURLFORM_STREAM, &pooh,
  48. CURLFORM_CONTENTSLENGTH, pooh.sizeleft,
  49. CURLFORM_FILENAME, "postit2.c",
  50. CURLFORM_END);
  51. if(formrc)
  52. printf("curl_formadd(1) = %d\n", (int)formrc);
  53. /* Fill in the filename field */
  54. formrc = curl_formadd(&formpost,
  55. &lastptr,
  56. CURLFORM_COPYNAME, "filename",
  57. CURLFORM_COPYCONTENTS, "postit2.c",
  58. CURLFORM_END);
  59. if(formrc)
  60. printf("curl_formadd(2) = %d\n", (int)formrc);
  61. /* Fill in a submit field too */
  62. formrc = curl_formadd(&formpost,
  63. &lastptr,
  64. CURLFORM_COPYNAME, "submit",
  65. CURLFORM_COPYCONTENTS, "send",
  66. CURLFORM_END);
  67. if(formrc)
  68. printf("curl_formadd(3) = %d\n", (int)formrc);
  69. if ((curl = curl_easy_init()) == NULL) {
  70. fprintf(stderr, "curl_easy_init() failed\n");
  71. curl_formfree(formpost);
  72. curl_global_cleanup();
  73. return TEST_ERR_MAJOR_BAD;
  74. }
  75. /* First set the URL that is about to receive our POST. */
  76. curl_easy_setopt(curl, CURLOPT_URL, URL);
  77. /* Now specify we want to POST data */
  78. curl_easy_setopt(curl, CURLOPT_POST, 1L);
  79. /* Set the expected POST size */
  80. curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)pooh.sizeleft);
  81. /* we want to use our own read function */
  82. curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
  83. /* pointer to pass to our read function */
  84. curl_easy_setopt(curl, CURLOPT_READDATA, &pooh);
  85. /* send a multi-part formpost */
  86. curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
  87. /* get verbose debug output please */
  88. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  89. /* include headers in the output */
  90. curl_easy_setopt(curl, CURLOPT_HEADER, 1L);
  91. /* Perform the request, res will get the return code */
  92. res = curl_easy_perform(curl);
  93. /* always cleanup */
  94. curl_easy_cleanup(curl);
  95. curl_global_cleanup();
  96. /* now cleanup the formpost chain */
  97. curl_formfree(formpost);
  98. return res;
  99. }