lib643.c 6.8 KB

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