2
0

lib643.c 7.7 KB

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