2
0

lib650.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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";
  28. static const char testname[] = "fieldname";
  29. /* This test attempts to use all form API features that are not
  30. * used elsewhere.
  31. */
  32. /* curl_formget callback to count characters. */
  33. static size_t count_chars(void *userp, const char *buf, size_t len)
  34. {
  35. size_t *pcounter = (size_t *) userp;
  36. (void) buf;
  37. *pcounter += len;
  38. return len;
  39. }
  40. CURLcode test(char *URL)
  41. {
  42. CURL *curl = NULL;
  43. CURLcode res = TEST_ERR_MAJOR_BAD;
  44. CURLFORMcode formrc;
  45. struct curl_slist *headers, *headers2 = NULL;
  46. struct curl_httppost *formpost = NULL;
  47. struct curl_httppost *lastptr = NULL;
  48. struct curl_forms formarray[3];
  49. size_t formlength = 0;
  50. char flbuf[32];
  51. long contentlength = 0;
  52. if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
  53. fprintf(stderr, "curl_global_init() failed\n");
  54. return TEST_ERR_MAJOR_BAD;
  55. }
  56. /* Check proper name and data copying, as well as headers. */
  57. headers = curl_slist_append(NULL, "X-customheader-1: Header 1 data");
  58. if(!headers) {
  59. goto test_cleanup;
  60. }
  61. headers2 = curl_slist_append(headers, "X-customheader-2: Header 2 data");
  62. if(!headers2) {
  63. goto test_cleanup;
  64. }
  65. headers = headers2;
  66. headers2 = curl_slist_append(headers, "Content-Type: text/plain");
  67. if(!headers2) {
  68. goto test_cleanup;
  69. }
  70. headers = headers2;
  71. CURL_IGNORE_DEPRECATION(
  72. formrc = curl_formadd(&formpost, &lastptr,
  73. CURLFORM_COPYNAME, &testname,
  74. CURLFORM_COPYCONTENTS, &testdata,
  75. CURLFORM_CONTENTHEADER, headers,
  76. CURLFORM_END);
  77. )
  78. if(formrc) {
  79. printf("curl_formadd(1) = %d\n", (int) formrc);
  80. goto test_cleanup;
  81. }
  82. contentlength = (long)(strlen(testdata) - 1);
  83. CURL_IGNORE_DEPRECATION(
  84. /* Use a form array for the non-copy test. */
  85. formarray[0].option = CURLFORM_PTRCONTENTS;
  86. formarray[0].value = testdata;
  87. formarray[1].option = CURLFORM_CONTENTSLENGTH;
  88. formarray[1].value = (char *)(size_t)contentlength;
  89. formarray[2].option = CURLFORM_END;
  90. formarray[2].value = NULL;
  91. formrc = curl_formadd(&formpost,
  92. &lastptr,
  93. CURLFORM_PTRNAME, testname,
  94. CURLFORM_NAMELENGTH, strlen(testname) - 1,
  95. CURLFORM_ARRAY, formarray,
  96. CURLFORM_FILENAME, "remotefile.txt",
  97. CURLFORM_END);
  98. )
  99. if(formrc) {
  100. printf("curl_formadd(2) = %d\n", (int) formrc);
  101. goto test_cleanup;
  102. }
  103. /* Now change in-memory data to affect CURLOPT_PTRCONTENTS value.
  104. Copied values (first field) must not be affected.
  105. CURLOPT_PTRNAME actually copies the name thus we do not test this here. */
  106. testdata[0]++;
  107. CURL_IGNORE_DEPRECATION(
  108. /* Check multi-files and content type propagation. */
  109. formrc = curl_formadd(&formpost,
  110. &lastptr,
  111. CURLFORM_COPYNAME, "multifile",
  112. CURLFORM_FILE, libtest_arg2, /* Set in first.c. */
  113. CURLFORM_FILE, libtest_arg2,
  114. CURLFORM_CONTENTTYPE, "text/whatever",
  115. CURLFORM_FILE, libtest_arg2,
  116. CURLFORM_END);
  117. )
  118. if(formrc) {
  119. printf("curl_formadd(3) = %d\n", (int) formrc);
  120. goto test_cleanup;
  121. }
  122. CURL_IGNORE_DEPRECATION(
  123. /* Check data from file content. */
  124. formrc = curl_formadd(&formpost,
  125. &lastptr,
  126. CURLFORM_COPYNAME, "filecontents",
  127. CURLFORM_FILECONTENT, libtest_arg2,
  128. CURLFORM_END);
  129. )
  130. if(formrc) {
  131. printf("curl_formadd(4) = %d\n", (int) formrc);
  132. goto test_cleanup;
  133. }
  134. CURL_IGNORE_DEPRECATION(
  135. /* Measure the current form length.
  136. * This is done before including stdin data because we want to reuse it
  137. * and stdin cannot be rewound.
  138. */
  139. curl_formget(formpost, (void *) &formlength, count_chars);
  140. )
  141. /* Include length in data for external check. */
  142. curl_msnprintf(flbuf, sizeof(flbuf), "%lu", (unsigned long) formlength);
  143. CURL_IGNORE_DEPRECATION(
  144. formrc = curl_formadd(&formpost,
  145. &lastptr,
  146. CURLFORM_COPYNAME, "formlength",
  147. CURLFORM_COPYCONTENTS, &flbuf,
  148. CURLFORM_END);
  149. )
  150. if(formrc) {
  151. printf("curl_formadd(5) = %d\n", (int) formrc);
  152. goto test_cleanup;
  153. }
  154. CURL_IGNORE_DEPRECATION(
  155. /* Check stdin (may be problematic on some platforms). */
  156. formrc = curl_formadd(&formpost,
  157. &lastptr,
  158. CURLFORM_COPYNAME, "standardinput",
  159. CURLFORM_FILE, "-",
  160. CURLFORM_END);
  161. )
  162. if(formrc) {
  163. printf("curl_formadd(6) = %d\n", (int) formrc);
  164. goto test_cleanup;
  165. }
  166. curl = curl_easy_init();
  167. if(!curl) {
  168. fprintf(stderr, "curl_easy_init() failed\n");
  169. goto test_cleanup;
  170. }
  171. /* First set the URL that is about to receive our POST. */
  172. test_setopt(curl, CURLOPT_URL, URL);
  173. CURL_IGNORE_DEPRECATION(
  174. /* send a multi-part formpost */
  175. test_setopt(curl, CURLOPT_HTTPPOST, formpost);
  176. )
  177. /* get verbose debug output please */
  178. test_setopt(curl, CURLOPT_VERBOSE, 1L);
  179. test_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
  180. test_setopt(curl, CURLOPT_POSTREDIR, (long)CURL_REDIR_POST_301);
  181. /* include headers in the output */
  182. test_setopt(curl, CURLOPT_HEADER, 1L);
  183. /* Perform the request, res will get the return code */
  184. res = curl_easy_perform(curl);
  185. test_cleanup:
  186. /* always cleanup */
  187. curl_easy_cleanup(curl);
  188. CURL_IGNORE_DEPRECATION(
  189. /* now cleanup the formpost chain */
  190. curl_formfree(formpost);
  191. )
  192. curl_slist_free_all(headers);
  193. curl_global_cleanup();
  194. return res;
  195. }