lib1526.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. Proxy header will be different
  24. * from server http header
  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 and proxy header list*/
  44. struct curl_slist *hhl = NULL, *phl = 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. phl = curl_slist_append(phl, "User-Agent: Proxy Agent");
  57. if(!hhl || !phl) {
  58. goto test_cleanup;
  59. }
  60. tmp = curl_slist_append(phl, "Expect:");
  61. if(!tmp) {
  62. goto test_cleanup;
  63. }
  64. phl = tmp;
  65. test_setopt(curl, CURLOPT_URL, URL);
  66. test_setopt(curl, CURLOPT_PROXY, libtest_arg2);
  67. test_setopt(curl, CURLOPT_HTTPHEADER, hhl);
  68. test_setopt(curl, CURLOPT_PROXYHEADER, phl);
  69. test_setopt(curl, CURLOPT_HEADEROPT, CURLHEADER_SEPARATE);
  70. test_setopt(curl, CURLOPT_POST, 0L);
  71. test_setopt(curl, CURLOPT_UPLOAD, 1L);
  72. test_setopt(curl, CURLOPT_VERBOSE, 1L);
  73. test_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
  74. test_setopt(curl, CURLOPT_HEADER, 1L);
  75. test_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite);
  76. test_setopt(curl, CURLOPT_READFUNCTION, read_callback);
  77. test_setopt(curl, CURLOPT_HTTPPROXYTUNNEL, 1L);
  78. test_setopt(curl, CURLOPT_INFILESIZE, (long)strlen(data));
  79. res = curl_easy_perform(curl);
  80. test_cleanup:
  81. curl_easy_cleanup(curl);
  82. curl_slist_free_all(hhl);
  83. curl_slist_free_all(phl);
  84. curl_global_cleanup();
  85. return (int)res;
  86. }