lib510.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 const char *post[]={
  25. "one",
  26. "two",
  27. "three",
  28. "and a final longer crap: four",
  29. NULL
  30. };
  31. struct WriteThis {
  32. int counter;
  33. };
  34. static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *userp)
  35. {
  36. struct WriteThis *pooh = (struct WriteThis *)userp;
  37. const char *data;
  38. if(size*nmemb < 1)
  39. return 0;
  40. data = post[pooh->counter];
  41. if(data) {
  42. size_t len = strlen(data);
  43. if(size*nmemb < len) {
  44. fprintf(stderr, "read buffer is too small to run test\n");
  45. return 0;
  46. }
  47. memcpy(ptr, data, len);
  48. pooh->counter++; /* advance pointer */
  49. return len;
  50. }
  51. return 0; /* no more data left to deliver */
  52. }
  53. int test(char *URL)
  54. {
  55. CURL *curl;
  56. CURLcode res = CURLE_OK;
  57. struct curl_slist *slist = NULL;
  58. struct WriteThis pooh;
  59. pooh.counter = 0;
  60. if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
  61. fprintf(stderr, "curl_global_init() failed\n");
  62. return TEST_ERR_MAJOR_BAD;
  63. }
  64. curl = curl_easy_init();
  65. if(!curl) {
  66. fprintf(stderr, "curl_easy_init() failed\n");
  67. curl_global_cleanup();
  68. return TEST_ERR_MAJOR_BAD;
  69. }
  70. slist = curl_slist_append(slist, "Transfer-Encoding: chunked");
  71. if(slist == NULL) {
  72. fprintf(stderr, "curl_slist_append() failed\n");
  73. curl_easy_cleanup(curl);
  74. curl_global_cleanup();
  75. return TEST_ERR_MAJOR_BAD;
  76. }
  77. /* First set the URL that is about to receive our POST. */
  78. test_setopt(curl, CURLOPT_URL, URL);
  79. /* Now specify we want to POST data */
  80. test_setopt(curl, CURLOPT_POST, 1L);
  81. #ifdef CURL_DOES_CONVERSIONS
  82. /* Convert the POST data to ASCII */
  83. test_setopt(curl, CURLOPT_TRANSFERTEXT, 1L);
  84. #endif
  85. /* we want to use our own read function */
  86. test_setopt(curl, CURLOPT_READFUNCTION, read_callback);
  87. /* pointer to pass to our read function */
  88. test_setopt(curl, CURLOPT_READDATA, &pooh);
  89. /* get verbose debug output please */
  90. test_setopt(curl, CURLOPT_VERBOSE, 1L);
  91. /* include headers in the output */
  92. test_setopt(curl, CURLOPT_HEADER, 1L);
  93. /* enforce chunked transfer by setting the header */
  94. test_setopt(curl, CURLOPT_HTTPHEADER, slist);
  95. #ifdef LIB565
  96. test_setopt(curl, CURLOPT_HTTPAUTH, (long)CURLAUTH_DIGEST);
  97. test_setopt(curl, CURLOPT_USERPWD, "foo:bar");
  98. #endif
  99. /* Perform the request, res will get the return code */
  100. res = curl_easy_perform(curl);
  101. test_cleanup:
  102. /* clean up the headers list */
  103. if(slist)
  104. curl_slist_free_all(slist);
  105. /* always cleanup */
  106. curl_easy_cleanup(curl);
  107. curl_global_cleanup();
  108. return res;
  109. }