lib554.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at http://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. ***************************************************************************/
  22. #include "test.h"
  23. #include "memdebug.h"
  24. static char data[]=
  25. #ifdef CURL_DOES_CONVERSIONS
  26. /* ASCII representation with escape sequences for non-ASCII platforms */
  27. "\x74\x68\x69\x73\x20\x69\x73\x20\x77\x68\x61\x74\x20\x77\x65\x20\x70"
  28. "\x6f\x73\x74\x20\x74\x6f\x20\x74\x68\x65\x20\x73\x69\x6c\x6c\x79\x20"
  29. "\x77\x65\x62\x20\x73\x65\x72\x76\x65\x72\x0a";
  30. #else
  31. "this is what we post to the silly web server\n";
  32. #endif
  33. struct WriteThis {
  34. char *readptr;
  35. size_t sizeleft;
  36. };
  37. static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *userp)
  38. {
  39. #ifdef LIB587
  40. (void)ptr;
  41. (void)size;
  42. (void)nmemb;
  43. (void)userp;
  44. return CURL_READFUNC_ABORT;
  45. #else
  46. struct WriteThis *pooh = (struct WriteThis *)userp;
  47. if(size*nmemb < 1)
  48. return 0;
  49. if(pooh->sizeleft) {
  50. *(char *)ptr = pooh->readptr[0]; /* copy one single byte */
  51. pooh->readptr++; /* advance pointer */
  52. pooh->sizeleft--; /* less data left */
  53. return 1; /* we return 1 byte at a time! */
  54. }
  55. return 0; /* no more data left to deliver */
  56. #endif
  57. }
  58. int test(char *URL)
  59. {
  60. CURL *curl;
  61. CURLcode res=CURLE_OK;
  62. CURLFORMcode formrc;
  63. struct curl_httppost *formpost=NULL;
  64. struct curl_httppost *lastptr=NULL;
  65. struct WriteThis pooh;
  66. struct WriteThis pooh2;
  67. if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
  68. fprintf(stderr, "curl_global_init() failed\n");
  69. return TEST_ERR_MAJOR_BAD;
  70. }
  71. pooh.readptr = data;
  72. pooh.sizeleft = strlen(data);
  73. /* Fill in the file upload field */
  74. formrc = curl_formadd(&formpost,
  75. &lastptr,
  76. CURLFORM_COPYNAME, "sendfile",
  77. CURLFORM_STREAM, &pooh,
  78. CURLFORM_CONTENTSLENGTH, pooh.sizeleft,
  79. CURLFORM_FILENAME, "postit2.c",
  80. CURLFORM_END);
  81. if(formrc)
  82. printf("curl_formadd(1) = %d\n", (int)formrc);
  83. /* Now add the same data with another name and make it not look like
  84. a file upload but still using the callback */
  85. pooh2.readptr = data;
  86. pooh2.sizeleft = strlen(data);
  87. /* Fill in the file upload field */
  88. formrc = curl_formadd(&formpost,
  89. &lastptr,
  90. CURLFORM_COPYNAME, "callbackdata",
  91. CURLFORM_STREAM, &pooh2,
  92. CURLFORM_CONTENTSLENGTH, pooh2.sizeleft,
  93. CURLFORM_END);
  94. if(formrc)
  95. printf("curl_formadd(1) = %d\n", (int)formrc);
  96. /* Fill in the filename field */
  97. formrc = curl_formadd(&formpost,
  98. &lastptr,
  99. CURLFORM_COPYNAME, "filename",
  100. #ifdef CURL_DOES_CONVERSIONS
  101. /* ASCII representation with escape
  102. sequences for non-ASCII platforms */
  103. CURLFORM_COPYCONTENTS,
  104. "\x70\x6f\x73\x74\x69\x74\x32\x2e\x63",
  105. #else
  106. CURLFORM_COPYCONTENTS, "postit2.c",
  107. #endif
  108. CURLFORM_END);
  109. if(formrc)
  110. printf("curl_formadd(2) = %d\n", (int)formrc);
  111. /* Fill in a submit field too */
  112. formrc = curl_formadd(&formpost,
  113. &lastptr,
  114. CURLFORM_COPYNAME, "submit",
  115. #ifdef CURL_DOES_CONVERSIONS
  116. /* ASCII representation with escape
  117. sequences for non-ASCII platforms */
  118. CURLFORM_COPYCONTENTS, "\x73\x65\x6e\x64",
  119. #else
  120. CURLFORM_COPYCONTENTS, "send",
  121. #endif
  122. CURLFORM_END);
  123. if(formrc)
  124. printf("curl_formadd(3) = %d\n", (int)formrc);
  125. formrc = curl_formadd(&formpost, &lastptr,
  126. CURLFORM_COPYNAME, "somename",
  127. CURLFORM_BUFFER, "somefile.txt",
  128. CURLFORM_BUFFERPTR, "blah blah",
  129. CURLFORM_BUFFERLENGTH, 9,
  130. CURLFORM_END);
  131. if(formrc)
  132. printf("curl_formadd(4) = %d\n", (int)formrc);
  133. if ((curl = curl_easy_init()) == NULL) {
  134. fprintf(stderr, "curl_easy_init() failed\n");
  135. curl_formfree(formpost);
  136. curl_global_cleanup();
  137. return TEST_ERR_MAJOR_BAD;
  138. }
  139. /* First set the URL that is about to receive our POST. */
  140. test_setopt(curl, CURLOPT_URL, URL);
  141. /* Now specify we want to POST data */
  142. test_setopt(curl, CURLOPT_POST, 1L);
  143. /* Set the expected POST size */
  144. test_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)pooh.sizeleft);
  145. /* we want to use our own read function */
  146. test_setopt(curl, CURLOPT_READFUNCTION, read_callback);
  147. /* send a multi-part formpost */
  148. test_setopt(curl, CURLOPT_HTTPPOST, formpost);
  149. /* get verbose debug output please */
  150. test_setopt(curl, CURLOPT_VERBOSE, 1L);
  151. /* include headers in the output */
  152. test_setopt(curl, CURLOPT_HEADER, 1L);
  153. /* Perform the request, res will get the return code */
  154. res = curl_easy_perform(curl);
  155. test_cleanup:
  156. /* always cleanup */
  157. curl_easy_cleanup(curl);
  158. curl_global_cleanup();
  159. /* now cleanup the formpost chain */
  160. curl_formfree(formpost);
  161. return res;
  162. }