lib643.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2022, 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. ***************************************************************************/
  22. #include "test.h"
  23. #include "memdebug.h"
  24. static char data[]=
  25. "dummy\n";
  26. struct WriteThis {
  27. char *readptr;
  28. curl_off_t sizeleft;
  29. };
  30. static size_t read_callback(char *ptr, size_t size, size_t nmemb, void *userp)
  31. {
  32. #ifdef LIB644
  33. static int count = 0;
  34. (void)ptr;
  35. (void)size;
  36. (void)nmemb;
  37. (void)userp;
  38. switch(count++) {
  39. case 0: /* Return a single byte. */
  40. *ptr = '\n';
  41. return 1;
  42. case 1: /* Request abort. */
  43. return CURL_READFUNC_ABORT;
  44. }
  45. printf("Wrongly called >2 times\n");
  46. exit(1); /* trigger major failure */
  47. #else
  48. struct WriteThis *pooh = (struct WriteThis *)userp;
  49. int eof = !*pooh->readptr;
  50. if(size*nmemb < 1)
  51. return 0;
  52. #ifndef LIB645
  53. eof = pooh->sizeleft <= 0;
  54. if(!eof)
  55. pooh->sizeleft--;
  56. #endif
  57. if(!eof) {
  58. *ptr = *pooh->readptr; /* copy one single byte */
  59. pooh->readptr++; /* advance pointer */
  60. return 1; /* we return 1 byte at a time! */
  61. }
  62. return 0; /* no more data left to deliver */
  63. #endif
  64. }
  65. static int once(char *URL, bool oldstyle)
  66. {
  67. CURL *curl;
  68. CURLcode res = CURLE_OK;
  69. curl_mime *mime = NULL;
  70. curl_mimepart *part = NULL;
  71. struct WriteThis pooh;
  72. struct WriteThis pooh2;
  73. curl_off_t datasize = -1;
  74. pooh.readptr = data;
  75. #ifndef LIB645
  76. datasize = (curl_off_t)strlen(data);
  77. #endif
  78. pooh.sizeleft = datasize;
  79. curl = curl_easy_init();
  80. if(!curl) {
  81. fprintf(stderr, "curl_easy_init() failed\n");
  82. curl_global_cleanup();
  83. return TEST_ERR_MAJOR_BAD;
  84. }
  85. mime = curl_mime_init(curl);
  86. if(!mime) {
  87. fprintf(stderr, "curl_mime_init() failed\n");
  88. curl_easy_cleanup(curl);
  89. curl_global_cleanup();
  90. return TEST_ERR_MAJOR_BAD;
  91. }
  92. part = curl_mime_addpart(mime);
  93. if(!part) {
  94. fprintf(stderr, "curl_mime_addpart(1) failed\n");
  95. curl_mime_free(mime);
  96. curl_easy_cleanup(curl);
  97. curl_global_cleanup();
  98. return TEST_ERR_MAJOR_BAD;
  99. }
  100. /* Fill in the file upload part */
  101. if(oldstyle) {
  102. res = curl_mime_name(part, "sendfile");
  103. if(!res)
  104. res = curl_mime_data_cb(part, datasize, read_callback,
  105. NULL, NULL, &pooh);
  106. if(!res)
  107. res = curl_mime_filename(part, "postit2.c");
  108. }
  109. else {
  110. /* new style */
  111. res = curl_mime_name(part, "sendfile alternative");
  112. if(!res)
  113. res = curl_mime_data_cb(part, datasize, read_callback,
  114. NULL, NULL, &pooh);
  115. if(!res)
  116. res = curl_mime_filename(part, "file name 2");
  117. }
  118. if(res)
  119. printf("curl_mime_xxx(1) = %s\n", curl_easy_strerror(res));
  120. /* Now add the same data with another name and make it not look like
  121. a file upload but still using the callback */
  122. pooh2.readptr = data;
  123. #ifndef LIB645
  124. datasize = (curl_off_t)strlen(data);
  125. #endif
  126. pooh2.sizeleft = datasize;
  127. part = curl_mime_addpart(mime);
  128. if(!part) {
  129. fprintf(stderr, "curl_mime_addpart(2) failed\n");
  130. curl_mime_free(mime);
  131. curl_easy_cleanup(curl);
  132. curl_global_cleanup();
  133. return TEST_ERR_MAJOR_BAD;
  134. }
  135. /* Fill in the file upload part */
  136. res = curl_mime_name(part, "callbackdata");
  137. if(!res)
  138. res = curl_mime_data_cb(part, datasize, read_callback,
  139. NULL, NULL, &pooh2);
  140. if(res)
  141. printf("curl_mime_xxx(2) = %s\n", curl_easy_strerror(res));
  142. part = curl_mime_addpart(mime);
  143. if(!part) {
  144. fprintf(stderr, "curl_mime_addpart(3) failed\n");
  145. curl_mime_free(mime);
  146. curl_easy_cleanup(curl);
  147. curl_global_cleanup();
  148. return TEST_ERR_MAJOR_BAD;
  149. }
  150. /* Fill in the filename field */
  151. res = curl_mime_name(part, "filename");
  152. if(!res)
  153. res = curl_mime_data(part, "postit2.c",
  154. CURL_ZERO_TERMINATED);
  155. if(res)
  156. printf("curl_mime_xxx(3) = %s\n", curl_easy_strerror(res));
  157. /* Fill in a submit field too */
  158. part = curl_mime_addpart(mime);
  159. if(!part) {
  160. fprintf(stderr, "curl_mime_addpart(4) failed\n");
  161. curl_mime_free(mime);
  162. curl_easy_cleanup(curl);
  163. curl_global_cleanup();
  164. return TEST_ERR_MAJOR_BAD;
  165. }
  166. res = curl_mime_name(part, "submit");
  167. if(!res)
  168. res = curl_mime_data(part, "send",
  169. CURL_ZERO_TERMINATED);
  170. if(res)
  171. printf("curl_mime_xxx(4) = %s\n", curl_easy_strerror(res));
  172. part = curl_mime_addpart(mime);
  173. if(!part) {
  174. fprintf(stderr, "curl_mime_addpart(5) failed\n");
  175. curl_mime_free(mime);
  176. curl_easy_cleanup(curl);
  177. curl_global_cleanup();
  178. return TEST_ERR_MAJOR_BAD;
  179. }
  180. res = curl_mime_name(part, "somename");
  181. if(!res)
  182. res = curl_mime_filename(part, "somefile.txt");
  183. if(!res)
  184. res = curl_mime_data(part, "blah blah", 9);
  185. if(res)
  186. printf("curl_mime_xxx(5) = %s\n", curl_easy_strerror(res));
  187. /* First set the URL that is about to receive our POST. */
  188. test_setopt(curl, CURLOPT_URL, URL);
  189. /* send a multi-part mimepost */
  190. test_setopt(curl, CURLOPT_MIMEPOST, mime);
  191. /* get verbose debug output please */
  192. test_setopt(curl, CURLOPT_VERBOSE, 1L);
  193. /* include headers in the output */
  194. test_setopt(curl, CURLOPT_HEADER, 1L);
  195. /* Perform the request, res will get the return code */
  196. res = curl_easy_perform(curl);
  197. test_cleanup:
  198. /* always cleanup */
  199. curl_easy_cleanup(curl);
  200. /* now cleanup the mimepost structure */
  201. curl_mime_free(mime);
  202. return res;
  203. }
  204. static int cyclic_add(void)
  205. {
  206. CURL *easy = curl_easy_init();
  207. curl_mime *mime = curl_mime_init(easy);
  208. curl_mimepart *part = curl_mime_addpart(mime);
  209. CURLcode a1 = curl_mime_subparts(part, mime);
  210. if(a1 == CURLE_BAD_FUNCTION_ARGUMENT) {
  211. curl_mime *submime = curl_mime_init(easy);
  212. curl_mimepart *subpart = curl_mime_addpart(submime);
  213. curl_mime_subparts(part, submime);
  214. a1 = curl_mime_subparts(subpart, mime);
  215. }
  216. curl_mime_free(mime);
  217. curl_easy_cleanup(easy);
  218. if(a1 != CURLE_BAD_FUNCTION_ARGUMENT)
  219. /* that should have failed */
  220. return 1;
  221. return 0;
  222. }
  223. int test(char *URL)
  224. {
  225. int res;
  226. if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
  227. fprintf(stderr, "curl_global_init() failed\n");
  228. return TEST_ERR_MAJOR_BAD;
  229. }
  230. res = once(URL, TRUE); /* old */
  231. if(!res)
  232. res = once(URL, FALSE); /* new */
  233. if(!res)
  234. res = cyclic_add();
  235. curl_global_cleanup();
  236. return res;
  237. }