lib643.c 7.7 KB

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