113498266Sopenharmony_ci      * Curl header API: extract headers post transfer
213498266Sopenharmony_ci      *
313498266Sopenharmony_ci     h DFTACTGRP(*NO) ACTGRP(*NEW)
413498266Sopenharmony_ci     h OPTION(*NOSHOWCPY)
513498266Sopenharmony_ci     h BNDDIR('CURL')
613498266Sopenharmony_ci      *
713498266Sopenharmony_ci      **************************************************************************
813498266Sopenharmony_ci      *                                  _   _ ____  _
913498266Sopenharmony_ci      *  Project                     ___| | | |  _ \| |
1013498266Sopenharmony_ci      *                             / __| | | | |_) | |
1113498266Sopenharmony_ci      *                            | (__| |_| |  _ <| |___
1213498266Sopenharmony_ci      *                             \___|\___/|_| \_\_____|
1313498266Sopenharmony_ci      *
1413498266Sopenharmony_ci      * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
1513498266Sopenharmony_ci      *
1613498266Sopenharmony_ci      * This software is licensed as described in the file COPYING, which
1713498266Sopenharmony_ci      * you should have received as part of this distribution. The terms
1813498266Sopenharmony_ci      * are also available at https://curl.se/docs/copyright.html.
1913498266Sopenharmony_ci      *
2013498266Sopenharmony_ci      * You may opt to use, copy, modify, merge, publish, distribute and/or sell
2113498266Sopenharmony_ci      * copies of the Software, and permit persons to whom the Software is
2213498266Sopenharmony_ci      * furnished to do so, under the terms of the COPYING file.
2313498266Sopenharmony_ci      *
2413498266Sopenharmony_ci      * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF
2513498266Sopenharmony_ci      * ANY KIND, either express or implied.
2613498266Sopenharmony_ci      *
2713498266Sopenharmony_ci      * SPDX-License-Identifier: curl
2813498266Sopenharmony_ci      *
2913498266Sopenharmony_ci      **************************************************************************
3013498266Sopenharmony_ci      *
3113498266Sopenharmony_ci      /include H,CURL.INC
3213498266Sopenharmony_ci      *
3313498266Sopenharmony_ci      * Extract headers post transfer with the header API.
3413498266Sopenharmony_ci      *
3513498266Sopenharmony_ci     d                 pi
3613498266Sopenharmony_ci     d url                          120
3713498266Sopenharmony_ci      *
3813498266Sopenharmony_ci     d urllen          s             10u 0                                      URL length
3913498266Sopenharmony_ci      *
4013498266Sopenharmony_ci      **************************************************************************
4113498266Sopenharmony_ci
4213498266Sopenharmony_ci        urllen = trimmed_length(url: %len(url));
4313498266Sopenharmony_ci
4413498266Sopenharmony_ci        // Do the curl stuff.
4513498266Sopenharmony_ci
4613498266Sopenharmony_ci        curl_global_init(CURL_GLOBAL_ALL);
4713498266Sopenharmony_ci        main();
4813498266Sopenharmony_ci        curl_global_cleanup();
4913498266Sopenharmony_ci        *inlr = *on;            // Exit
5013498266Sopenharmony_ci      *
5113498266Sopenharmony_ci      **************************************************************************
5213498266Sopenharmony_ci      * Main procedure: do the curl job.
5313498266Sopenharmony_ci      **************************************************************************
5413498266Sopenharmony_ci      *
5513498266Sopenharmony_ci     p main            b
5613498266Sopenharmony_ci     d main            pi
5713498266Sopenharmony_ci      *
5813498266Sopenharmony_ci     d h               s               *                                        Easy handle
5913498266Sopenharmony_ci     d result          s                   like(CURLcode)                       Curl return code
6013498266Sopenharmony_ci     d                                     inz(CURLE_OUT_OF_MEMORY)
6113498266Sopenharmony_ci     d header          ds                  likeds(curl_header) based(hp)
6213498266Sopenharmony_ci     d strp1           s               *                                        Work string pointer
6313498266Sopenharmony_ci     d strp2           s               *                                        Work string pointer
6413498266Sopenharmony_ci     d inout           s             52                                         For error display
6513498266Sopenharmony_ci
6613498266Sopenharmony_ci        // Create and fill curl handle.
6713498266Sopenharmony_ci
6813498266Sopenharmony_ci        h = curl_easy_init();
6913498266Sopenharmony_ci        if h <> *NULL;
7013498266Sopenharmony_ci            curl_easy_setopt_ccsid(h: CURLOPT_URL: %subst(url: 1: urllen): 0);
7113498266Sopenharmony_ci            curl_easy_setopt(h: CURLOPT_FOLLOWLOCATION: 1);
7213498266Sopenharmony_ci            curl_easy_setopt(h: CURLOPT_WRITEFUNCTION: %paddr(in_data_cb));     // Ignore input data
7313498266Sopenharmony_ci
7413498266Sopenharmony_ci            // Perform the request.
7513498266Sopenharmony_ci
7613498266Sopenharmony_ci            result = curl_easy_perform(h);
7713498266Sopenharmony_ci        endif;
7813498266Sopenharmony_ci
7913498266Sopenharmony_ci        // Check for error and report if some.
8013498266Sopenharmony_ci
8113498266Sopenharmony_ci        if result <> CURLE_OK;
8213498266Sopenharmony_ci            inout = %str(curl_easy_strerror_ccsid(result: 0));
8313498266Sopenharmony_ci            dsply '' '*EXT' inout;
8413498266Sopenharmony_ci        else;
8513498266Sopenharmony_ci            if curl_easy_header_ccsid(h: 'Content-Type': 0: CURLH_HEADER: -1:
8613498266Sopenharmony_ci                                      hp: 0) = CURLHE_OK;
8713498266Sopenharmony_ci                strp2 = curl_to_ccsid(header.value: 0);
8813498266Sopenharmony_ci                inout = 'Content-Type: ' + %str(strp2);
8913498266Sopenharmony_ci                dsply inout;
9013498266Sopenharmony_ci                curl_free(strp2);
9113498266Sopenharmony_ci            endif;
9213498266Sopenharmony_ci            dsply '    All server headers:';
9313498266Sopenharmony_ci            hp = *NULL;
9413498266Sopenharmony_ci            dow *on;
9513498266Sopenharmony_ci                hp = curl_easy_nextheader(h: CURLH_HEADER: -1: hp);
9613498266Sopenharmony_ci                if hp = *NULL;
9713498266Sopenharmony_ci                    leave;
9813498266Sopenharmony_ci                endif;
9913498266Sopenharmony_ci                strp1 = curl_to_ccsid(header.name: 0);
10013498266Sopenharmony_ci                strp2 = curl_to_ccsid(header.value: 0);
10113498266Sopenharmony_ci                inout = %str(strp1) + ': ' + %str(strp2) +
10213498266Sopenharmony_ci                        ' (' + %char(header.amount) + ')';
10313498266Sopenharmony_ci                curl_free(strp2);
10413498266Sopenharmony_ci                curl_free(strp1);
10513498266Sopenharmony_ci                dsply inout;
10613498266Sopenharmony_ci            enddo;
10713498266Sopenharmony_ci            inout = 'Done';
10813498266Sopenharmony_ci            dsply '' '*EXT' inout;
10913498266Sopenharmony_ci            curl_easy_cleanup(h);       // Release handle
11013498266Sopenharmony_ci        endif;
11113498266Sopenharmony_ci     p main            e
11213498266Sopenharmony_ci      *
11313498266Sopenharmony_ci      **************************************************************************
11413498266Sopenharmony_ci      * Dummy data input callback procedure.
11513498266Sopenharmony_ci      **************************************************************************
11613498266Sopenharmony_ci      *
11713498266Sopenharmony_ci     p in_data_cb      b
11813498266Sopenharmony_ci     d in_data_cb      pi            10u 0
11913498266Sopenharmony_ci     d  ptr                            *   value                                Input data pointer
12013498266Sopenharmony_ci     d  size                         10u 0 value                                Data element size
12113498266Sopenharmony_ci     d  nmemb                        10u 0 value                                Data element count
12213498266Sopenharmony_ci     d  userdata                       *   value                                User data pointer
12313498266Sopenharmony_ci      *
12413498266Sopenharmony_ci        return size * nmemb;
12513498266Sopenharmony_ci     p in_data_cb      e
12613498266Sopenharmony_ci      *
12713498266Sopenharmony_ci      **************************************************************************
12813498266Sopenharmony_ci      * Get the length of right-trimmed string
12913498266Sopenharmony_ci      **************************************************************************
13013498266Sopenharmony_ci      *
13113498266Sopenharmony_ci     p trimmed_length  b
13213498266Sopenharmony_ci     d trimmed_length  pi            10u 0
13313498266Sopenharmony_ci     d  string                   999999    const options(*varsize)
13413498266Sopenharmony_ci     d  length                       10u 0 value
13513498266Sopenharmony_ci      *
13613498266Sopenharmony_ci     d len             s             10u 0
13713498266Sopenharmony_ci      *
13813498266Sopenharmony_ci        len = %scan(X'00': string: 1: length); // Limit to zero-terminated string
13913498266Sopenharmony_ci        if len = 0;
14013498266Sopenharmony_ci            len = length + 1;
14113498266Sopenharmony_ci        endif;
14213498266Sopenharmony_ci        if len <= 1;
14313498266Sopenharmony_ci            return 0;
14413498266Sopenharmony_ci        endif;
14513498266Sopenharmony_ci        return %checkr(' ': string: len - 1);  // Trim right
14613498266Sopenharmony_ci     p trimmed_length  e
147