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