113498266Sopenharmony_ci      * Curl simple URL request (free-format RPG)
213498266Sopenharmony_ci      *
313498266Sopenharmony_ci        ctl-opt dftactgrp(*NO) actgrp(*NEW)
413498266Sopenharmony_ci                option(*NOSHOWCPY)
513498266Sopenharmony_ci                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      * Simple free-format RPG program to request the URL given as command line
3413498266Sopenharmony_ci      * parameter and output its response.
3513498266Sopenharmony_ci
3613498266Sopenharmony_ci        dcl-pi *N;
3713498266Sopenharmony_ci            url char(120);
3813498266Sopenharmony_ci        end-pi;
3913498266Sopenharmony_ci
4013498266Sopenharmony_ci        dcl-s urllen int(10);           // URL length
4113498266Sopenharmony_ci
4213498266Sopenharmony_ci      **************************************************************************
4313498266Sopenharmony_ci
4413498266Sopenharmony_ci        urllen = trimmed_length(url: %len(url));
4513498266Sopenharmony_ci
4613498266Sopenharmony_ci        // Do the curl stuff.
4713498266Sopenharmony_ci
4813498266Sopenharmony_ci        curl_global_init(CURL_GLOBAL_ALL);
4913498266Sopenharmony_ci        main();
5013498266Sopenharmony_ci        curl_global_cleanup();
5113498266Sopenharmony_ci        *inlr = *on;            // Exit
5213498266Sopenharmony_ci
5313498266Sopenharmony_ci      **************************************************************************
5413498266Sopenharmony_ci      * Main procedure: do the curl job.
5513498266Sopenharmony_ci      **************************************************************************
5613498266Sopenharmony_ci
5713498266Sopenharmony_ci        dcl-proc main;
5813498266Sopenharmony_ci            dcl-pi *N end-pi;
5913498266Sopenharmony_ci
6013498266Sopenharmony_ci            dcl-s h pointer;                                            // Easy handle
6113498266Sopenharmony_ci            dcl-s result like(CURLcode) inz(CURLE_OUT_OF_MEMORY);       // Curl return code
6213498266Sopenharmony_ci            dcl-s errmsgp pointer;                                      // Error string pointer
6313498266Sopenharmony_ci            dcl-s response char(52);                                    // For error display
6413498266Sopenharmony_ci
6513498266Sopenharmony_ci            // Create and fill curl handle.
6613498266Sopenharmony_ci
6713498266Sopenharmony_ci            h = curl_easy_init();
6813498266Sopenharmony_ci            if h <> *NULL;
6913498266Sopenharmony_ci                curl_easy_setopt_ccsid(h: CURLOPT_URL: %subst(url: 1: urllen):
7013498266Sopenharmony_ci                                       0);
7113498266Sopenharmony_ci                curl_easy_setopt(h: CURLOPT_FOLLOWLOCATION: 1);
7213498266Sopenharmony_ci
7313498266Sopenharmony_ci                // Perform the request.
7413498266Sopenharmony_ci
7513498266Sopenharmony_ci                result = curl_easy_perform(h);
7613498266Sopenharmony_ci                curl_easy_cleanup(h);       // Release handle
7713498266Sopenharmony_ci            endif;
7813498266Sopenharmony_ci
7913498266Sopenharmony_ci            // Check for error and report if some.
8013498266Sopenharmony_ci
8113498266Sopenharmony_ci            if result <> CURLE_OK;
8213498266Sopenharmony_ci                errmsgp = curl_easy_strerror_ccsid(result: 0);
8313498266Sopenharmony_ci                response = %str(errmsgp);
8413498266Sopenharmony_ci                dsply '' '*EXT' response;
8513498266Sopenharmony_ci            endif;
8613498266Sopenharmony_ci        end-proc;
8713498266Sopenharmony_ci      *
8813498266Sopenharmony_ci      **************************************************************************
8913498266Sopenharmony_ci      * Get the length of right-trimmed string
9013498266Sopenharmony_ci      **************************************************************************
9113498266Sopenharmony_ci      *
9213498266Sopenharmony_ci        dcl-proc trimmed_length;
9313498266Sopenharmony_ci            dcl-pi *N uns(10);
9413498266Sopenharmony_ci                string char(9999999) const options(*varsize);
9513498266Sopenharmony_ci                length uns(10) value;
9613498266Sopenharmony_ci            end-pi;
9713498266Sopenharmony_ci
9813498266Sopenharmony_ci            dcl-s len uns(10);
9913498266Sopenharmony_ci
10013498266Sopenharmony_ci            len = %scan(X'00': string: 1: length); // Limit to zero-terminated string
10113498266Sopenharmony_ci            if len = 0;
10213498266Sopenharmony_ci                len = length + 1;
10313498266Sopenharmony_ci            endif;
10413498266Sopenharmony_ci            if len <= 1;
10513498266Sopenharmony_ci                return 0;
10613498266Sopenharmony_ci            endif;
10713498266Sopenharmony_ci            return %checkr(' ': string: len - 1);  // Trim right
10813498266Sopenharmony_ci        end-proc;
109