lib650.c 6.3 KB

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