lib510.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2011, 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 http://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. memcpy(ptr, data, len);
  44. pooh->counter++; /* advance pointer */
  45. return len;
  46. }
  47. return 0; /* no more data left to deliver */
  48. }
  49. int test(char *URL)
  50. {
  51. CURL *curl;
  52. CURLcode res=CURLE_OK;
  53. struct curl_slist *slist = NULL;
  54. struct WriteThis pooh;
  55. pooh.counter = 0;
  56. if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
  57. fprintf(stderr, "curl_global_init() failed\n");
  58. return TEST_ERR_MAJOR_BAD;
  59. }
  60. if ((curl = curl_easy_init()) == NULL) {
  61. fprintf(stderr, "curl_easy_init() failed\n");
  62. curl_global_cleanup();
  63. return TEST_ERR_MAJOR_BAD;
  64. }
  65. slist = curl_slist_append(slist, "Transfer-Encoding: chunked");
  66. if (slist == NULL) {
  67. fprintf(stderr, "curl_slist_append() failed\n");
  68. curl_easy_cleanup(curl);
  69. curl_global_cleanup();
  70. return TEST_ERR_MAJOR_BAD;
  71. }
  72. /* First set the URL that is about to receive our POST. */
  73. test_setopt(curl, CURLOPT_URL, URL);
  74. /* Now specify we want to POST data */
  75. test_setopt(curl, CURLOPT_POST, 1L);
  76. #ifdef CURL_DOES_CONVERSIONS
  77. /* Convert the POST data to ASCII */
  78. test_setopt(curl, CURLOPT_TRANSFERTEXT, 1L);
  79. #endif
  80. /* we want to use our own read function */
  81. test_setopt(curl, CURLOPT_READFUNCTION, read_callback);
  82. /* pointer to pass to our read function */
  83. test_setopt(curl, CURLOPT_INFILE, &pooh);
  84. /* get verbose debug output please */
  85. test_setopt(curl, CURLOPT_VERBOSE, 1L);
  86. /* include headers in the output */
  87. test_setopt(curl, CURLOPT_HEADER, 1L);
  88. /* enforce chunked transfer by setting the header */
  89. test_setopt(curl, CURLOPT_HTTPHEADER, slist);
  90. #ifdef LIB565
  91. test_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
  92. test_setopt(curl, CURLOPT_USERPWD, "foo:bar");
  93. #endif
  94. /* Perform the request, res will get the return code */
  95. res = curl_easy_perform(curl);
  96. test_cleanup:
  97. /* clean up the headers list */
  98. if(slist)
  99. curl_slist_free_all(slist);
  100. /* always cleanup */
  101. curl_easy_cleanup(curl);
  102. curl_global_cleanup();
  103. return res;
  104. }