lib554.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 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 https://curl.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. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "test.h"
  25. #include "memdebug.h"
  26. static char testdata[]=
  27. "this is what we post to the silly web server\n";
  28. struct WriteThis {
  29. char *readptr;
  30. size_t sizeleft;
  31. };
  32. static size_t read_callback(char *ptr, size_t size, size_t nmemb, void *userp)
  33. {
  34. #ifdef LIB587
  35. (void)ptr;
  36. (void)size;
  37. (void)nmemb;
  38. (void)userp;
  39. return CURL_READFUNC_ABORT;
  40. #else
  41. struct WriteThis *pooh = (struct WriteThis *)userp;
  42. if(size*nmemb < 1)
  43. return 0;
  44. if(pooh->sizeleft) {
  45. *ptr = pooh->readptr[0]; /* copy one single byte */
  46. pooh->readptr++; /* advance pointer */
  47. pooh->sizeleft--; /* less data left */
  48. return 1; /* we return 1 byte at a time! */
  49. }
  50. return 0; /* no more data left to deliver */
  51. #endif
  52. }
  53. static CURLcode test_once(char *URL, bool oldstyle)
  54. {
  55. CURL *curl;
  56. CURLcode res = CURLE_OK;
  57. CURLFORMcode formrc;
  58. struct curl_httppost *formpost = NULL;
  59. struct curl_httppost *lastptr = NULL;
  60. struct WriteThis pooh;
  61. struct WriteThis pooh2;
  62. pooh.readptr = testdata;
  63. pooh.sizeleft = strlen(testdata);
  64. /* Fill in the file upload field */
  65. if(oldstyle) {
  66. CURL_IGNORE_DEPRECATION(
  67. formrc = curl_formadd(&formpost,
  68. &lastptr,
  69. CURLFORM_COPYNAME, "sendfile",
  70. CURLFORM_STREAM, &pooh,
  71. CURLFORM_CONTENTSLENGTH, (long)pooh.sizeleft,
  72. CURLFORM_FILENAME, "postit2.c",
  73. CURLFORM_END);
  74. )
  75. }
  76. else {
  77. CURL_IGNORE_DEPRECATION(
  78. /* new style */
  79. formrc = curl_formadd(&formpost,
  80. &lastptr,
  81. CURLFORM_COPYNAME, "sendfile alternative",
  82. CURLFORM_STREAM, &pooh,
  83. CURLFORM_CONTENTLEN, (curl_off_t)pooh.sizeleft,
  84. CURLFORM_FILENAME, "file name 2",
  85. CURLFORM_END);
  86. )
  87. }
  88. if(formrc)
  89. printf("curl_formadd(1) = %d\n", (int)formrc);
  90. /* Now add the same data with another name and make it not look like
  91. a file upload but still using the callback */
  92. pooh2.readptr = testdata;
  93. pooh2.sizeleft = strlen(testdata);
  94. CURL_IGNORE_DEPRECATION(
  95. /* Fill in the file upload field */
  96. formrc = curl_formadd(&formpost,
  97. &lastptr,
  98. CURLFORM_COPYNAME, "callbackdata",
  99. CURLFORM_STREAM, &pooh2,
  100. CURLFORM_CONTENTSLENGTH, (long)pooh2.sizeleft,
  101. CURLFORM_END);
  102. )
  103. if(formrc)
  104. printf("curl_formadd(2) = %d\n", (int)formrc);
  105. CURL_IGNORE_DEPRECATION(
  106. /* Fill in the filename field */
  107. formrc = curl_formadd(&formpost,
  108. &lastptr,
  109. CURLFORM_COPYNAME, "filename",
  110. CURLFORM_COPYCONTENTS, "postit2.c",
  111. CURLFORM_END);
  112. )
  113. if(formrc)
  114. printf("curl_formadd(3) = %d\n", (int)formrc);
  115. CURL_IGNORE_DEPRECATION(
  116. /* Fill in a submit field too */
  117. formrc = curl_formadd(&formpost,
  118. &lastptr,
  119. CURLFORM_COPYNAME, "submit",
  120. CURLFORM_COPYCONTENTS, "send",
  121. CURLFORM_CONTENTTYPE, "text/plain",
  122. CURLFORM_END);
  123. )
  124. if(formrc)
  125. printf("curl_formadd(4) = %d\n", (int)formrc);
  126. CURL_IGNORE_DEPRECATION(
  127. formrc = curl_formadd(&formpost, &lastptr,
  128. CURLFORM_COPYNAME, "somename",
  129. CURLFORM_BUFFER, "somefile.txt",
  130. CURLFORM_BUFFERPTR, "blah blah",
  131. CURLFORM_BUFFERLENGTH, (long)9,
  132. CURLFORM_END);
  133. )
  134. if(formrc)
  135. printf("curl_formadd(5) = %d\n", (int)formrc);
  136. curl = curl_easy_init();
  137. if(!curl) {
  138. fprintf(stderr, "curl_easy_init() failed\n");
  139. CURL_IGNORE_DEPRECATION(
  140. curl_formfree(formpost);
  141. )
  142. curl_global_cleanup();
  143. return TEST_ERR_MAJOR_BAD;
  144. }
  145. /* First set the URL that is about to receive our POST. */
  146. test_setopt(curl, CURLOPT_URL, URL);
  147. /* Now specify we want to POST data */
  148. test_setopt(curl, CURLOPT_POST, 1L);
  149. /* Set the expected POST size */
  150. test_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)pooh.sizeleft);
  151. /* we want to use our own read function */
  152. test_setopt(curl, CURLOPT_READFUNCTION, read_callback);
  153. CURL_IGNORE_DEPRECATION(
  154. /* send a multi-part formpost */
  155. test_setopt(curl, CURLOPT_HTTPPOST, formpost);
  156. )
  157. /* get verbose debug output please */
  158. test_setopt(curl, CURLOPT_VERBOSE, 1L);
  159. /* include headers in the output */
  160. test_setopt(curl, CURLOPT_HEADER, 1L);
  161. /* Perform the request, res will get the return code */
  162. res = curl_easy_perform(curl);
  163. test_cleanup:
  164. CURL_IGNORE_DEPRECATION(
  165. /* always cleanup */
  166. curl_easy_cleanup(curl);
  167. )
  168. CURL_IGNORE_DEPRECATION(
  169. /* now cleanup the formpost chain */
  170. curl_formfree(formpost);
  171. )
  172. return res;
  173. }
  174. CURLcode test(char *URL)
  175. {
  176. CURLcode res;
  177. if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
  178. fprintf(stderr, "curl_global_init() failed\n");
  179. return TEST_ERR_MAJOR_BAD;
  180. }
  181. res = test_once(URL, TRUE); /* old */
  182. if(!res)
  183. res = test_once(URL, FALSE); /* new */
  184. curl_global_cleanup();
  185. return res;
  186. }