HEADERAPI 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. * Curl header API: extract headers post transfer
  2. *
  3. h DFTACTGRP(*NO) ACTGRP(*NEW)
  4. h OPTION(*NOSHOWCPY)
  5. h BNDDIR('CURL')
  6. *
  7. **************************************************************************
  8. * _ _ ____ _
  9. * Project ___| | | | _ \| |
  10. * / __| | | | |_) | |
  11. * | (__| |_| | _ <| |___
  12. * \___|\___/|_| \_\_____|
  13. *
  14. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  15. *
  16. * This software is licensed as described in the file COPYING, which
  17. * you should have received as part of this distribution. The terms
  18. * are also available at https://curl.se/docs/copyright.html.
  19. *
  20. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  21. * copies of the Software, and permit persons to whom the Software is
  22. * furnished to do so, under the terms of the COPYING file.
  23. *
  24. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF
  25. * ANY KIND, either express or implied.
  26. *
  27. * SPDX-License-Identifier: curl
  28. *
  29. **************************************************************************
  30. *
  31. /include H,CURL.INC
  32. *
  33. * Extract headers post transfer with the header API.
  34. *
  35. d pi
  36. d url 120
  37. *
  38. d urllen s 10u 0 URL length
  39. *
  40. **************************************************************************
  41. urllen = trimmed_length(url: %len(url));
  42. // Do the curl stuff.
  43. curl_global_init(CURL_GLOBAL_ALL);
  44. main();
  45. curl_global_cleanup();
  46. *inlr = *on; // Exit
  47. *
  48. **************************************************************************
  49. * Main procedure: do the curl job.
  50. **************************************************************************
  51. *
  52. p main b
  53. d main pi
  54. *
  55. d h s * Easy handle
  56. d result s like(CURLcode) Curl return code
  57. d inz(CURLE_OUT_OF_MEMORY)
  58. d header ds likeds(curl_header) based(hp)
  59. d strp1 s * Work string pointer
  60. d strp2 s * Work string pointer
  61. d inout s 52 For error display
  62. // Create and fill curl handle.
  63. h = curl_easy_init();
  64. if h <> *NULL;
  65. curl_easy_setopt_ccsid(h: CURLOPT_URL: %subst(url: 1: urllen): 0);
  66. curl_easy_setopt(h: CURLOPT_FOLLOWLOCATION: 1);
  67. curl_easy_setopt(h: CURLOPT_WRITEFUNCTION: %paddr(in_data_cb)); // Ignore input data
  68. // Perform the request.
  69. result = curl_easy_perform(h);
  70. endif;
  71. // Check for error and report if some.
  72. if result <> CURLE_OK;
  73. inout = %str(curl_easy_strerror_ccsid(result: 0));
  74. dsply '' '*EXT' inout;
  75. else;
  76. if curl_easy_header_ccsid(h: 'Content-Type': 0: CURLH_HEADER: -1:
  77. hp: 0) = CURLHE_OK;
  78. strp2 = curl_to_ccsid(header.value: 0);
  79. inout = 'Content-Type: ' + %str(strp2);
  80. dsply inout;
  81. curl_free(strp2);
  82. endif;
  83. dsply ' All server headers:';
  84. hp = *NULL;
  85. dow *on;
  86. hp = curl_easy_nextheader(h: CURLH_HEADER: -1: hp);
  87. if hp = *NULL;
  88. leave;
  89. endif;
  90. strp1 = curl_to_ccsid(header.name: 0);
  91. strp2 = curl_to_ccsid(header.value: 0);
  92. inout = %str(strp1) + ': ' + %str(strp2) +
  93. ' (' + %char(header.amount) + ')';
  94. curl_free(strp2);
  95. curl_free(strp1);
  96. dsply inout;
  97. enddo;
  98. inout = 'Done';
  99. dsply '' '*EXT' inout;
  100. curl_easy_cleanup(h); // Release handle
  101. endif;
  102. p main e
  103. *
  104. **************************************************************************
  105. * Dummy data input callback procedure.
  106. **************************************************************************
  107. *
  108. p in_data_cb b
  109. d in_data_cb pi 10u 0
  110. d ptr * value Input data pointer
  111. d size 10u 0 value Data element size
  112. d nmemb 10u 0 value Data element count
  113. d userdata * value User data pointer
  114. *
  115. return size * nmemb;
  116. p in_data_cb e
  117. *
  118. **************************************************************************
  119. * Get the length of right-trimmed string
  120. **************************************************************************
  121. *
  122. p trimmed_length b
  123. d trimmed_length pi 10u 0
  124. d string 999999 const options(*varsize)
  125. d length 10u 0 value
  126. *
  127. d len s 10u 0
  128. *
  129. len = %scan(X'00': string: 1: length); // Limit to zero-terminated string
  130. if len = 0;
  131. len = length + 1;
  132. endif;
  133. if len <= 1;
  134. return 0;
  135. endif;
  136. return %checkr(' ': string: len - 1); // Trim right
  137. p trimmed_length e