lib1517.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. #include "test.h"
  25. #include "memdebug.h"
  26. static char data[]="this is what we post to the silly web server\n";
  27. struct WriteThis {
  28. char *readptr;
  29. size_t sizeleft;
  30. };
  31. static size_t read_callback(char *ptr, size_t size, size_t nmemb, void *userp)
  32. {
  33. struct WriteThis *pooh = (struct WriteThis *)userp;
  34. size_t tocopy = size * nmemb;
  35. /* Wait one second before return POST data *
  36. * so libcurl will wait before sending request body */
  37. wait_ms(1000);
  38. if(tocopy < 1 || !pooh->sizeleft)
  39. return 0;
  40. if(pooh->sizeleft < tocopy)
  41. tocopy = pooh->sizeleft;
  42. memcpy(ptr, pooh->readptr, tocopy);/* copy requested data */
  43. pooh->readptr += tocopy; /* advance pointer */
  44. pooh->sizeleft -= tocopy; /* less data left */
  45. return tocopy;
  46. }
  47. int test(char *URL)
  48. {
  49. CURL *curl;
  50. CURLcode res = CURLE_OK;
  51. struct WriteThis pooh;
  52. if(!strcmp(URL, "check")) {
  53. #if (defined(_WIN32) || defined(__CYGWIN__))
  54. printf("Windows TCP does not deliver response data but reports "
  55. "CONNABORTED\n");
  56. return 1; /* skip since test will fail on Windows without workaround */
  57. #else
  58. return 0; /* sure, run this! */
  59. #endif
  60. }
  61. pooh.readptr = data;
  62. pooh.sizeleft = strlen(data);
  63. if(curl_global_init(CURL_GLOBAL_ALL)) {
  64. fprintf(stderr, "curl_global_init() failed\n");
  65. return TEST_ERR_MAJOR_BAD;
  66. }
  67. curl = curl_easy_init();
  68. if(!curl) {
  69. fprintf(stderr, "curl_easy_init() failed\n");
  70. curl_global_cleanup();
  71. return TEST_ERR_MAJOR_BAD;
  72. }
  73. /* First set the URL that is about to receive our POST. */
  74. test_setopt(curl, CURLOPT_URL, URL);
  75. /* Now specify we want to POST data */
  76. test_setopt(curl, CURLOPT_POST, 1L);
  77. /* Set the expected POST size */
  78. test_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)pooh.sizeleft);
  79. /* we want to use our own read function */
  80. test_setopt(curl, CURLOPT_READFUNCTION, read_callback);
  81. /* pointer to pass to our read function */
  82. test_setopt(curl, CURLOPT_READDATA, &pooh);
  83. /* get verbose debug output please */
  84. test_setopt(curl, CURLOPT_VERBOSE, 1L);
  85. /* include headers in the output */
  86. test_setopt(curl, CURLOPT_HEADER, 1L);
  87. /* detect HTTP error codes >= 400 */
  88. /* test_setopt(curl, CURLOPT_FAILONERROR, 1L); */
  89. /* Perform the request, res will get the return code */
  90. res = curl_easy_perform(curl);
  91. test_cleanup:
  92. /* always cleanup */
  93. curl_easy_cleanup(curl);
  94. curl_global_cleanup();
  95. return res;
  96. }