      *  This is a simple example of a RESTful web service. It
      *  is called via the HTTP protocol, and accepts inputs from
      *  the URL that was used to call it.  For example:
      *
      *        http://example.com/cust/495
      *
      *  This means that you want to retrieve info about customer
      *  495. This program will then look up that customer info,
      *  and return the response in XML format.
      *
      *    <result>
      *      <cust id="495">
      *        <name>ACME FOODS</name>
      *        <street>123 MAIN STREET</street>
      *        <city>BOCA RATON</city>
      *        <state>FL</state>
      *        <postal>12345-6789</postal>
      *      </cust>
      *    </result>
      *
      * To compile:
      *> CHGCURLIB SKWEBSRV
      *> CRTRPGMOD CUSTINFO SRCFILE(QRPGLESRC) DBGVIEW(*LIST)
      *> CRTPGM CUSTINFO BNDSRVPGM(QHTTPSVR/QZHBCGI) ACTGRP(RESTFUL)

     H OPTION(*SRCSTMT: *NODEBUGIO)

     FCUSTFILE  IF   E           K DISK

     D getenv          PR              *   extproc('getenv')
     D   var                           *   value options(*string)

     D QtmhWrStout     PR                  extproc('QtmhWrStout')
     D   DtaVar                   65535a   options(*varsize)
     D   DtaVarLen                   10I 0 const
     D   ErrorCode                 8000A   options(*varsize)

     D err             ds                  qualified
     D   bytesProv                   10i 0 inz(0)
     D   bytesAvail                  10i 0 inz(0)

     D xml             pr          5000a   varying
     D   inp                       5000a   varying const options(*trim)

     D CRLF            C                   x'0d25'
     D pos             s             10i 0
     D uri             s           5000a   varying
     D data            s           5000a
     D ID1             c                   '/cust/'
     D ID2             c                   '/custinfo/'

      /free
          *inlr = *on;
          uri = %str(getenv('REQUEST_URI'));

          monitor;
             pos = %scan('/cust/': uri) + %len('/cust/');
             custno = %int(%subst(uri:pos));
          on-error;
             data = 'Status: 500 Invalid URI' + CRLF
                  + 'Content-type: text/xml' + CRLF
                  + CRLF
                  + '<error>Invalid URI</error>' + CRLF;
             QtmhWrStout(data: %len(%trimr(data)): err);
             return;
          endmon;

          chain custno CUSTFILE;
          if not %found;
             data = 'Status: 500 Unknown Customer' + CRLF
                  + 'Content-type: text/xml' + CRLF
                  + CRLF
                  + '<error>Unknown Customer Number</error>' + CRLF;
             QtmhWrStout(data: %len(%trimr(data)): err);
             return;
          endif;

          data = 'Status: 200 OK' + CRLF
               + 'Content-type: text/xml' + CRLF
               + CRLF
               + '<result>'
               + '<cust id="' + %char(custno) + '">'
               + '<name>'     + xml(name)     + '</name>'
               + '<street>'   + xml(street)   + '</street>'
               + '<city>'     + xml(city)     + '</city>'
               + '<state>'    + xml(state)    + '</state>'
               + '<postal>'   + xml(postal)    + '</postal>'
               + '</cust>'
               + '</result>'  + CRLF;
          QtmhWrStout(data: %len(%trimr(data)): err);
          *inlr = *on;

      /end-free

     P xml             b
     D xml             pi          5000a   varying
     D   inp                       5000a   varying const options(*trim)
     D x               s             10i 0
     D result          s           5000a   varying
      /free
        for x = 1 to %len(inp);
           select;
           when %subst(inp:x:1) = '&';
              result += '&amp;';
           when %subst(inp:x:1) = '<';
              result += '&lt;';
           when %subst(inp:x:1) = '>';
              result += '&gt;';
           when %subst(inp:x:1) = '"';
              result += '&quot;';
           when %subst(inp:x:1) = '''';
              result += '&apos;';
           other;
              result += %subst(inp:x:1);
           endsl;
        endfor;
        return result;
      /end-free
     P                 E
