lib650.c 6.4 KB

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