lib1527.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2020, Vijay Panghal, <vpanghal@maginatics.com>, 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. ***************************************************************************/
  22. /*
  23. * This unit test PUT http data over proxy. Same http header will be generated
  24. * for server and proxy
  25. */
  26. #include "test.h"
  27. #include "memdebug.h"
  28. static char data [] = "Hello Cloud!\n";
  29. static size_t read_callback(char *ptr, size_t size, size_t nmemb, void *stream)
  30. {
  31. size_t amount = nmemb * size; /* Total bytes curl wants */
  32. if(amount < strlen(data)) {
  33. return strlen(data);
  34. }
  35. (void)stream;
  36. memcpy(ptr, data, strlen(data));
  37. return strlen(data);
  38. }
  39. int test(char *URL)
  40. {
  41. CURL *curl = NULL;
  42. CURLcode res = CURLE_FAILED_INIT;
  43. /* http header list*/
  44. struct curl_slist *hhl = NULL, *tmp = NULL;
  45. if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
  46. fprintf(stderr, "curl_global_init() failed\n");
  47. return TEST_ERR_MAJOR_BAD;
  48. }
  49. curl = curl_easy_init();
  50. if(!curl) {
  51. fprintf(stderr, "curl_easy_init() failed\n");
  52. curl_global_cleanup();
  53. return TEST_ERR_MAJOR_BAD;
  54. }
  55. hhl = curl_slist_append(hhl, "User-Agent: Http Agent");
  56. if(!hhl) {
  57. goto test_cleanup;
  58. }
  59. tmp = curl_slist_append(hhl, "Expect: 100-continue");
  60. if(!tmp) {
  61. goto test_cleanup;
  62. }
  63. hhl = tmp;
  64. test_setopt(curl, CURLOPT_URL, URL);
  65. test_setopt(curl, CURLOPT_PROXY, libtest_arg2);
  66. test_setopt(curl, CURLOPT_HTTPHEADER, hhl);
  67. test_setopt(curl, CURLOPT_POST, 0L);
  68. test_setopt(curl, CURLOPT_UPLOAD, 1L);
  69. test_setopt(curl, CURLOPT_VERBOSE, 1L);
  70. test_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
  71. test_setopt(curl, CURLOPT_HEADER, 1L);
  72. test_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite);
  73. test_setopt(curl, CURLOPT_READFUNCTION, read_callback);
  74. test_setopt(curl, CURLOPT_HTTPPROXYTUNNEL, 1L);
  75. test_setopt(curl, CURLOPT_INFILESIZE, (long)strlen(data));
  76. test_setopt(curl, CURLOPT_HEADEROPT, CURLHEADER_UNIFIED);
  77. res = curl_easy_perform(curl);
  78. test_cleanup:
  79. curl_easy_cleanup(curl);
  80. curl_slist_free_all(hhl);
  81. curl_global_cleanup();
  82. return (int)res;
  83. }