lib650.c 6.6 KB

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