lib508.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 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. if(size*nmemb < 1)
  33. return 0;
  34. if(pooh->sizeleft) {
  35. *(char *)ptr = pooh->readptr[0]; /* copy one single byte */
  36. pooh->readptr++; /* advance pointer */
  37. pooh->sizeleft--; /* less data left */
  38. return 1; /* we return 1 byte at a time! */
  39. }
  40. return 0; /* no more data left to deliver */
  41. }
  42. int test(char *URL)
  43. {
  44. CURL *curl;
  45. CURLcode res=CURLE_OK;
  46. struct WriteThis pooh;
  47. pooh.readptr = data;
  48. pooh.sizeleft = strlen(data);
  49. if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
  50. fprintf(stderr, "curl_global_init() failed\n");
  51. return TEST_ERR_MAJOR_BAD;
  52. }
  53. if ((curl = curl_easy_init()) == NULL) {
  54. fprintf(stderr, "curl_easy_init() failed\n");
  55. curl_global_cleanup();
  56. return TEST_ERR_MAJOR_BAD;
  57. }
  58. /* First set the URL that is about to receive our POST. */
  59. test_setopt(curl, CURLOPT_URL, URL);
  60. /* Now specify we want to POST data */
  61. test_setopt(curl, CURLOPT_POST, 1L);
  62. #ifdef CURL_DOES_CONVERSIONS
  63. /* Convert the POST data to ASCII */
  64. test_setopt(curl, CURLOPT_TRANSFERTEXT, 1L);
  65. #endif
  66. /* Set the expected POST size */
  67. test_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)pooh.sizeleft);
  68. /* we want to use our own read function */
  69. test_setopt(curl, CURLOPT_READFUNCTION, read_callback);
  70. /* pointer to pass to our read function */
  71. test_setopt(curl, CURLOPT_INFILE, &pooh);
  72. /* get verbose debug output please */
  73. test_setopt(curl, CURLOPT_VERBOSE, 1L);
  74. /* include headers in the output */
  75. test_setopt(curl, CURLOPT_HEADER, 1L);
  76. /* Perform the request, res will get the return code */
  77. res = curl_easy_perform(curl);
  78. test_cleanup:
  79. /* always cleanup */
  80. curl_easy_cleanup(curl);
  81. curl_global_cleanup();
  82. return res;
  83. }