lib554.c 5.9 KB

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