lib1517.c 3.2 KB

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