lib554.c 5.9 KB

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