2
0

lib650.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "test.h"
  25. #include "memdebug.h"
  26. static char data[] =
  27. "this is what we post to the silly web server";
  28. static const char name[] = "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. int 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. formrc = curl_formadd(&formpost, &lastptr,
  72. CURLFORM_COPYNAME, &name,
  73. CURLFORM_COPYCONTENTS, &data,
  74. CURLFORM_CONTENTHEADER, headers,
  75. CURLFORM_END);
  76. if(formrc) {
  77. printf("curl_formadd(1) = %d\n", (int) formrc);
  78. goto test_cleanup;
  79. }
  80. contentlength = (long)(strlen(data) - 1);
  81. /* Use a form array for the non-copy test. */
  82. formarray[0].option = CURLFORM_PTRCONTENTS;
  83. formarray[0].value = data;
  84. formarray[1].option = CURLFORM_CONTENTSLENGTH;
  85. formarray[1].value = (char *)(size_t)contentlength;
  86. formarray[2].option = CURLFORM_END;
  87. formarray[2].value = NULL;
  88. formrc = curl_formadd(&formpost,
  89. &lastptr,
  90. CURLFORM_PTRNAME, name,
  91. CURLFORM_NAMELENGTH, strlen(name) - 1,
  92. CURLFORM_ARRAY, formarray,
  93. CURLFORM_FILENAME, "remotefile.txt",
  94. CURLFORM_END);
  95. if(formrc) {
  96. printf("curl_formadd(2) = %d\n", (int) formrc);
  97. goto test_cleanup;
  98. }
  99. /* Now change in-memory data to affect CURLOPT_PTRCONTENTS value.
  100. Copied values (first field) must not be affected.
  101. CURLOPT_PTRNAME actually copies the name thus we do not test this here. */
  102. data[0]++;
  103. /* Check multi-files and content type propagation. */
  104. formrc = curl_formadd(&formpost,
  105. &lastptr,
  106. CURLFORM_COPYNAME, "multifile",
  107. CURLFORM_FILE, libtest_arg2, /* Set in first.c. */
  108. CURLFORM_FILE, libtest_arg2,
  109. CURLFORM_CONTENTTYPE, "text/whatever",
  110. CURLFORM_FILE, libtest_arg2,
  111. CURLFORM_END);
  112. if(formrc) {
  113. printf("curl_formadd(3) = %d\n", (int) formrc);
  114. goto test_cleanup;
  115. }
  116. /* Check data from file content. */
  117. formrc = curl_formadd(&formpost,
  118. &lastptr,
  119. CURLFORM_COPYNAME, "filecontents",
  120. CURLFORM_FILECONTENT, libtest_arg2,
  121. CURLFORM_END);
  122. if(formrc) {
  123. printf("curl_formadd(4) = %d\n", (int) formrc);
  124. goto test_cleanup;
  125. }
  126. /* Measure the current form length.
  127. * This is done before including stdin data because we want to reuse it
  128. * and stdin cannot be rewound.
  129. */
  130. curl_formget(formpost, (void *) &formlength, count_chars);
  131. /* Include length in data for external check. */
  132. curl_msnprintf(flbuf, sizeof(flbuf), "%lu", (unsigned long) formlength);
  133. formrc = curl_formadd(&formpost,
  134. &lastptr,
  135. CURLFORM_COPYNAME, "formlength",
  136. CURLFORM_COPYCONTENTS, &flbuf,
  137. CURLFORM_END);
  138. if(formrc) {
  139. printf("curl_formadd(5) = %d\n", (int) formrc);
  140. goto test_cleanup;
  141. }
  142. /* Check stdin (may be problematic on some platforms). */
  143. formrc = curl_formadd(&formpost,
  144. &lastptr,
  145. CURLFORM_COPYNAME, "standardinput",
  146. CURLFORM_FILE, "-",
  147. CURLFORM_END);
  148. if(formrc) {
  149. printf("curl_formadd(6) = %d\n", (int) formrc);
  150. goto test_cleanup;
  151. }
  152. curl = curl_easy_init();
  153. if(!curl) {
  154. fprintf(stderr, "curl_easy_init() failed\n");
  155. goto test_cleanup;
  156. }
  157. /* First set the URL that is about to receive our POST. */
  158. test_setopt(curl, CURLOPT_URL, URL);
  159. /* send a multi-part formpost */
  160. test_setopt(curl, CURLOPT_HTTPPOST, formpost);
  161. /* get verbose debug output please */
  162. test_setopt(curl, CURLOPT_VERBOSE, 1L);
  163. /* include headers in the output */
  164. test_setopt(curl, CURLOPT_HEADER, 1L);
  165. /* Perform the request, res will get the return code */
  166. res = curl_easy_perform(curl);
  167. test_cleanup:
  168. /* always cleanup */
  169. curl_easy_cleanup(curl);
  170. /* now cleanup the formpost chain */
  171. curl_formfree(formpost);
  172. curl_slist_free_all(headers);
  173. curl_global_cleanup();
  174. return res;
  175. }