lib1940.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2022, 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 *show[]={
  25. "daTE",
  26. "Server",
  27. "content-type",
  28. "content-length",
  29. "location",
  30. "set-cookie",
  31. "silly-thing",
  32. NULL
  33. };
  34. #ifdef LIB1946
  35. #define HEADER_REQUEST 0
  36. #else
  37. #define HEADER_REQUEST -1
  38. #endif
  39. static void showem(CURL *easy, unsigned int type)
  40. {
  41. int i;
  42. struct curl_header *header;
  43. for(i = 0; show[i]; i++) {
  44. if(CURLHE_OK == curl_easy_header(easy, show[i], 0, type, HEADER_REQUEST,
  45. &header)) {
  46. if(header->amount > 1) {
  47. /* more than one, iterate over them */
  48. size_t index = 0;
  49. size_t amount = header->amount;
  50. do {
  51. printf("- %s == %s (%u/%u)\n", header->name, header->value,
  52. (int)index, (int)amount);
  53. if(++index == amount)
  54. break;
  55. if(CURLHE_OK != curl_easy_header(easy, show[i], index, type,
  56. HEADER_REQUEST, &header))
  57. break;
  58. } while(1);
  59. }
  60. else {
  61. /* only one of this */
  62. printf(" %s == %s\n", header->name, header->value);
  63. }
  64. }
  65. }
  66. }
  67. static size_t write_cb(char *data, size_t n, size_t l, void *userp)
  68. {
  69. /* take care of the data here, ignored in this example */
  70. (void)data;
  71. (void)userp;
  72. return n*l;
  73. }
  74. int test(char *URL)
  75. {
  76. CURL *easy;
  77. curl_global_init(CURL_GLOBAL_DEFAULT);
  78. easy = curl_easy_init();
  79. if(easy) {
  80. CURLcode res;
  81. curl_easy_setopt(easy, CURLOPT_URL, URL);
  82. curl_easy_setopt(easy, CURLOPT_VERBOSE, 1L);
  83. curl_easy_setopt(easy, CURLOPT_FOLLOWLOCATION, 1L);
  84. /* ignores any content */
  85. curl_easy_setopt(easy, CURLOPT_WRITEFUNCTION, write_cb);
  86. /* if there's a proxy set, use it */
  87. if(libtest_arg2 && *libtest_arg2) {
  88. curl_easy_setopt(easy, CURLOPT_PROXY, libtest_arg2);
  89. curl_easy_setopt(easy, CURLOPT_HTTPPROXYTUNNEL, 1L);
  90. }
  91. res = curl_easy_perform(easy);
  92. if(res) {
  93. printf("badness: %d\n", (int)res);
  94. }
  95. showem(easy, CURLH_HEADER);
  96. if(libtest_arg2 && *libtest_arg2) {
  97. /* now show connect headers only */
  98. showem(easy, CURLH_CONNECT);
  99. }
  100. showem(easy, CURLH_1XX);
  101. showem(easy, CURLH_TRAILER);
  102. curl_easy_cleanup(easy);
  103. }
  104. curl_global_cleanup();
  105. return 0;
  106. }