lib554.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2017, 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\x0a";
  30. #else
  31. "this is what we post to the silly web server\n";
  32. #endif
  33. struct WriteThis {
  34. char *readptr;
  35. size_t sizeleft;
  36. };
  37. static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *userp)
  38. {
  39. #ifdef LIB587
  40. (void)ptr;
  41. (void)size;
  42. (void)nmemb;
  43. (void)userp;
  44. return CURL_READFUNC_ABORT;
  45. #else
  46. struct WriteThis *pooh = (struct WriteThis *)userp;
  47. if(size*nmemb < 1)
  48. return 0;
  49. if(pooh->sizeleft) {
  50. *(char *)ptr = pooh->readptr[0]; /* copy one single byte */
  51. pooh->readptr++; /* advance pointer */
  52. pooh->sizeleft--; /* less data left */
  53. return 1; /* we return 1 byte at a time! */
  54. }
  55. return 0; /* no more data left to deliver */
  56. #endif
  57. }
  58. static int once(char *URL, bool oldstyle)
  59. {
  60. CURL *curl;
  61. CURLcode res = CURLE_OK;
  62. CURLFORMcode formrc;
  63. struct curl_httppost *formpost = NULL;
  64. struct curl_httppost *lastptr = NULL;
  65. struct WriteThis pooh;
  66. struct WriteThis pooh2;
  67. pooh.readptr = data;
  68. pooh.sizeleft = strlen(data);
  69. /* Fill in the file upload field */
  70. if(oldstyle) {
  71. formrc = curl_formadd(&formpost,
  72. &lastptr,
  73. CURLFORM_COPYNAME, "sendfile",
  74. CURLFORM_STREAM, &pooh,
  75. CURLFORM_CONTENTSLENGTH, (long)pooh.sizeleft,
  76. CURLFORM_FILENAME, "postit2.c",
  77. CURLFORM_END);
  78. }
  79. else {
  80. /* new style */
  81. formrc = curl_formadd(&formpost,
  82. &lastptr,
  83. CURLFORM_COPYNAME, "sendfile alternative",
  84. CURLFORM_STREAM, &pooh,
  85. CURLFORM_CONTENTLEN, (curl_off_t)pooh.sizeleft,
  86. CURLFORM_FILENAME, "file name 2",
  87. CURLFORM_END);
  88. }
  89. if(formrc)
  90. printf("curl_formadd(1) = %d\n", (int)formrc);
  91. /* Now add the same data with another name and make it not look like
  92. a file upload but still using the callback */
  93. pooh2.readptr = data;
  94. pooh2.sizeleft = strlen(data);
  95. /* Fill in the file upload field */
  96. formrc = curl_formadd(&formpost,
  97. &lastptr,
  98. CURLFORM_COPYNAME, "callbackdata",
  99. CURLFORM_STREAM, &pooh2,
  100. CURLFORM_CONTENTSLENGTH, (long)pooh2.sizeleft,
  101. CURLFORM_END);
  102. if(formrc)
  103. printf("curl_formadd(2) = %d\n", (int)formrc);
  104. /* Fill in the filename field */
  105. formrc = curl_formadd(&formpost,
  106. &lastptr,
  107. CURLFORM_COPYNAME, "filename",
  108. #ifdef CURL_DOES_CONVERSIONS
  109. /* ASCII representation with escape
  110. sequences for non-ASCII platforms */
  111. CURLFORM_COPYCONTENTS,
  112. "\x70\x6f\x73\x74\x69\x74\x32\x2e\x63",
  113. #else
  114. CURLFORM_COPYCONTENTS, "postit2.c",
  115. #endif
  116. CURLFORM_END);
  117. if(formrc)
  118. printf("curl_formadd(3) = %d\n", (int)formrc);
  119. /* Fill in a submit field too */
  120. formrc = curl_formadd(&formpost,
  121. &lastptr,
  122. CURLFORM_COPYNAME, "submit",
  123. #ifdef CURL_DOES_CONVERSIONS
  124. /* ASCII representation with escape
  125. sequences for non-ASCII platforms */
  126. CURLFORM_COPYCONTENTS, "\x73\x65\x6e\x64",
  127. #else
  128. CURLFORM_COPYCONTENTS, "send",
  129. #endif
  130. CURLFORM_CONTENTTYPE, "text/plain",
  131. CURLFORM_END);
  132. if(formrc)
  133. printf("curl_formadd(4) = %d\n", (int)formrc);
  134. formrc = curl_formadd(&formpost, &lastptr,
  135. CURLFORM_COPYNAME, "somename",
  136. CURLFORM_BUFFER, "somefile.txt",
  137. CURLFORM_BUFFERPTR, "blah blah",
  138. CURLFORM_BUFFERLENGTH, (long)9,
  139. CURLFORM_END);
  140. if(formrc)
  141. printf("curl_formadd(5) = %d\n", (int)formrc);
  142. curl = curl_easy_init();
  143. if(!curl) {
  144. fprintf(stderr, "curl_easy_init() failed\n");
  145. curl_formfree(formpost);
  146. curl_global_cleanup();
  147. return TEST_ERR_MAJOR_BAD;
  148. }
  149. /* First set the URL that is about to receive our POST. */
  150. test_setopt(curl, CURLOPT_URL, URL);
  151. /* Now specify we want to POST data */
  152. test_setopt(curl, CURLOPT_POST, 1L);
  153. /* Set the expected POST size */
  154. test_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)pooh.sizeleft);
  155. /* we want to use our own read function */
  156. test_setopt(curl, CURLOPT_READFUNCTION, read_callback);
  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. return res;
  171. }
  172. int test(char *URL)
  173. {
  174. int res;
  175. if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
  176. fprintf(stderr, "curl_global_init() failed\n");
  177. return TEST_ERR_MAJOR_BAD;
  178. }
  179. res = once(URL, TRUE); /* old */
  180. if(!res)
  181. res = once(URL, FALSE); /* new */
  182. curl_global_cleanup();
  183. return res;
  184. }