xref: /curl/packages/OS400/rpg-examples/SIMPLE2 (revision 7c142d05)
1      * Curl simple URL request (free-format RPG)
2      *
3        ctl-opt dftactgrp(*NO) actgrp(*NEW)
4                option(*NOSHOWCPY)
5                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      * Simple free-format RPG program to request the URL given as command line
34      * parameter and output its response.
35
36        dcl-pi *N;
37            url char(120);
38        end-pi;
39
40        dcl-s urllen int(10);           // URL length
41
42      **************************************************************************
43
44        urllen = trimmed_length(url: %len(url));
45
46        // Do the curl stuff.
47
48        curl_global_init(CURL_GLOBAL_ALL);
49        main();
50        curl_global_cleanup();
51        *inlr = *on;            // Exit
52
53      **************************************************************************
54      * Main procedure: do the curl job.
55      **************************************************************************
56
57        dcl-proc main;
58            dcl-pi *N end-pi;
59
60            dcl-s h pointer;                                            // Easy handle
61            dcl-s result like(CURLcode) inz(CURLE_OUT_OF_MEMORY);       // Curl return code
62            dcl-s errmsgp pointer;                                      // Error string pointer
63            dcl-s response char(52);                                    // For error display
64
65            // Create and fill curl handle.
66
67            h = curl_easy_init();
68            if h <> *NULL;
69                curl_easy_setopt_ccsid(h: CURLOPT_URL: %subst(url: 1: urllen):
70                                       0);
71                curl_easy_setopt(h: CURLOPT_FOLLOWLOCATION: 1);
72
73                // Perform the request.
74
75                result = curl_easy_perform(h);
76                curl_easy_cleanup(h);       // Release handle
77            endif;
78
79            // Check for error and report if some.
80
81            if result <> CURLE_OK;
82                errmsgp = curl_easy_strerror_ccsid(result: 0);
83                response = %str(errmsgp);
84                dsply '' '*EXT' response;
85            endif;
86        end-proc;
87      *
88      **************************************************************************
89      * Get the length of right-trimmed string
90      **************************************************************************
91      *
92        dcl-proc trimmed_length;
93            dcl-pi *N uns(10);
94                string char(9999999) const options(*varsize);
95                length uns(10) value;
96            end-pi;
97
98            dcl-s len uns(10);
99
100            len = %scan(X'00': string: 1: length); // Limit to zero-terminated string
101            if len = 0;
102                len = length + 1;
103            endif;
104            if len <= 1;
105                return 0;
106            endif;
107            return %checkr(' ': string: len - 1);  // Trim right
108        end-proc;
109