lib554.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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\n";
  29. struct WriteThis {
  30. char *readptr;
  31. size_t sizeleft;
  32. };
  33. static size_t read_callback(char *ptr, size_t size, size_t nmemb, void *userp)
  34. {
  35. #ifdef LIB587
  36. (void)ptr;
  37. (void)size;
  38. (void)nmemb;
  39. (void)userp;
  40. return CURL_READFUNC_ABORT;
  41. #else
  42. struct WriteThis *pooh = (struct WriteThis *)userp;
  43. if(size*nmemb < 1)
  44. return 0;
  45. if(pooh->sizeleft) {
  46. *ptr = pooh->readptr[0]; /* copy one single byte */
  47. pooh->readptr++; /* advance pointer */
  48. pooh->sizeleft--; /* less data left */
  49. return 1; /* we return 1 byte at a time! */
  50. }
  51. return 0; /* no more data left to deliver */
  52. #endif
  53. }
  54. static int once(char *URL, bool oldstyle)
  55. {
  56. CURL *curl;
  57. CURLcode res = CURLE_OK;
  58. CURLFORMcode formrc;
  59. struct curl_httppost *formpost = NULL;
  60. struct curl_httppost *lastptr = NULL;
  61. struct WriteThis pooh;
  62. struct WriteThis pooh2;
  63. pooh.readptr = data;
  64. pooh.sizeleft = strlen(data);
  65. /* Fill in the file upload field */
  66. if(oldstyle) {
  67. formrc = curl_formadd(&formpost,
  68. &lastptr,
  69. CURLFORM_COPYNAME, "sendfile",
  70. CURLFORM_STREAM, &pooh,
  71. CURLFORM_CONTENTSLENGTH, (long)pooh.sizeleft,
  72. CURLFORM_FILENAME, "postit2.c",
  73. CURLFORM_END);
  74. }
  75. else {
  76. /* new style */
  77. formrc = curl_formadd(&formpost,
  78. &lastptr,
  79. CURLFORM_COPYNAME, "sendfile alternative",
  80. CURLFORM_STREAM, &pooh,
  81. CURLFORM_CONTENTLEN, (curl_off_t)pooh.sizeleft,
  82. CURLFORM_FILENAME, "file name 2",
  83. CURLFORM_END);
  84. }
  85. if(formrc)
  86. printf("curl_formadd(1) = %d\n", (int)formrc);
  87. /* Now add the same data with another name and make it not look like
  88. a file upload but still using the callback */
  89. pooh2.readptr = data;
  90. pooh2.sizeleft = strlen(data);
  91. /* Fill in the file upload field */
  92. formrc = curl_formadd(&formpost,
  93. &lastptr,
  94. CURLFORM_COPYNAME, "callbackdata",
  95. CURLFORM_STREAM, &pooh2,
  96. CURLFORM_CONTENTSLENGTH, (long)pooh2.sizeleft,
  97. CURLFORM_END);
  98. if(formrc)
  99. printf("curl_formadd(2) = %d\n", (int)formrc);
  100. /* Fill in the filename field */
  101. formrc = curl_formadd(&formpost,
  102. &lastptr,
  103. CURLFORM_COPYNAME, "filename",
  104. CURLFORM_COPYCONTENTS, "postit2.c",
  105. CURLFORM_END);
  106. if(formrc)
  107. printf("curl_formadd(3) = %d\n", (int)formrc);
  108. /* Fill in a submit field too */
  109. formrc = curl_formadd(&formpost,
  110. &lastptr,
  111. CURLFORM_COPYNAME, "submit",
  112. CURLFORM_COPYCONTENTS, "send",
  113. CURLFORM_CONTENTTYPE, "text/plain",
  114. CURLFORM_END);
  115. if(formrc)
  116. printf("curl_formadd(4) = %d\n", (int)formrc);
  117. formrc = curl_formadd(&formpost, &lastptr,
  118. CURLFORM_COPYNAME, "somename",
  119. CURLFORM_BUFFER, "somefile.txt",
  120. CURLFORM_BUFFERPTR, "blah blah",
  121. CURLFORM_BUFFERLENGTH, (long)9,
  122. CURLFORM_END);
  123. if(formrc)
  124. printf("curl_formadd(5) = %d\n", (int)formrc);
  125. curl = curl_easy_init();
  126. if(!curl) {
  127. fprintf(stderr, "curl_easy_init() failed\n");
  128. curl_formfree(formpost);
  129. curl_global_cleanup();
  130. return TEST_ERR_MAJOR_BAD;
  131. }
  132. /* First set the URL that is about to receive our POST. */
  133. test_setopt(curl, CURLOPT_URL, URL);
  134. /* Now specify we want to POST data */
  135. test_setopt(curl, CURLOPT_POST, 1L);
  136. /* Set the expected POST size */
  137. test_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)pooh.sizeleft);
  138. /* we want to use our own read function */
  139. test_setopt(curl, CURLOPT_READFUNCTION, read_callback);
  140. /* send a multi-part formpost */
  141. test_setopt(curl, CURLOPT_HTTPPOST, formpost);
  142. /* get verbose debug output please */
  143. test_setopt(curl, CURLOPT_VERBOSE, 1L);
  144. /* include headers in the output */
  145. test_setopt(curl, CURLOPT_HEADER, 1L);
  146. /* Perform the request, res will get the return code */
  147. res = curl_easy_perform(curl);
  148. test_cleanup:
  149. /* always cleanup */
  150. curl_easy_cleanup(curl);
  151. /* now cleanup the formpost chain */
  152. curl_formfree(formpost);
  153. return res;
  154. }
  155. int test(char *URL)
  156. {
  157. int res;
  158. if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
  159. fprintf(stderr, "curl_global_init() failed\n");
  160. return TEST_ERR_MAJOR_BAD;
  161. }
  162. res = once(URL, TRUE); /* old */
  163. if(!res)
  164. res = once(URL, FALSE); /* new */
  165. curl_global_cleanup();
  166. return res;
  167. }