(* Collection of code snippets by Arne Vajhøj *)
(* (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) *)
[inherit('pxmlrpcdir:common', 'pxmlrpcdir:phttp')]
program diyclient(input,output);

function grab(xmlstr : pstr; var xstart : integer; var xend : integer; tag : pstr; greedy : boolean) : boolean;

var
   starttag, endtag : pstr;
   ixstart, ixend : integer;

begin
   starttag := '<' + tag + '>';
   endtag := '</' + tag + '>';
   ixstart := xstart;
   while (ixstart < (length(xmlstr) - length(starttag))) and (substr(xmlstr, ixstart, length(starttag)) <> starttag) do ixstart := ixstart + 1;
   ixstart := ixstart + length(starttag);
   if(greedy) then begin
      ixend := xend - length(endtag);
      while (ixend > 1) and (substr(xmlstr, ixend, length(endtag)) <> endtag) do ixend := ixend - 1;
   end else begin
      ixend := ixstart;
      while((ixend < (length(xmlstr) - length(starttag))) and (substr(xmlstr, ixend, length(endtag)) <> endtag)) do ixend := ixend + 1;
   end;
   if (ixstart < (length(xmlstr) - length(starttag))) and (ixend >= 1) then begin
       xstart := ixstart;
       xend := ixend;
       grab := true;
   end else begin
       grab := false;
   end;
end;

procedure test(host : pstr; port : integer);

procedure test_getInt(host : pstr; port : integer);

var
   req, resp : pstr;
   ctx : http;
   ixstart, ixend, v : integer;

begin
   req := '<methodCall><methodName>Test.getInt</methodName><params></params></methodCall>';
   ctx := http_post(fix(host), port, '/', 'text/xml', 'text/xml', fix(req));
   http_recv_all(ctx, resp.body, resp.length);
   ixstart := 1;
   ixend := length(resp);
   if grab(resp, ixstart, ixend, 'value', true) then begin
      if grab(resp, ixstart, ixend, 'i4', true) then begin
         readv(substr(resp, ixstart, ixend - ixstart), v);
         writeln(v:1);
      end else begin
         if grab(resp, ixstart, ixend, 'int', true) then begin
            readv(substr(resp, ixstart, ixend - ixstart), v);
            writeln(v:1);
         end;
      end;
   end;
end;

procedure test_getString(host : pstr; port : integer);

var
   req,  resp, s : pstr;
   ctx : http;
   ixstart, ixend : integer;

begin
   req := '<methodCall><methodName>Test.getString</methodName><params></params></methodCall>';
   ctx := http_post(fix(host), port, '/', 'text/xml', 'text/xml', fix(req));
   http_recv_all(ctx, resp.body, resp.length);
   ixstart := 1;
   ixend := length(resp);
   if grab(resp, ixstart, ixend, 'value', true) then begin
      if grab(resp, ixstart, ixend, 'string', true) then begin
         s := substr(resp, ixstart, ixend - ixstart);
         writeln(s);
      end else begin
         s := substr(resp, ixstart, ixend - ixstart);
         writeln(s);
      end;
   end;
end;

procedure test_getListOfInts(host : pstr; port : integer);

var
   req, resp : pstr;
   ctx : http;
   ixstart, ixend, ixstart2, ixend2, ixstart3, ixend3, n, i : integer;
   a : array [1..1000] of integer;

begin
   req := '<methodCall><methodName>Test.getListOfInts</methodName><params></params></methodCall>';
   ctx := http_post(fix(host), port, '/', 'text/xml', 'text/xml', fix(req));
   http_recv_all(ctx, resp.body, resp.length);
   ixstart := 1;
   ixend := length(resp);
   if grab(resp, ixstart, ixend, 'value', true) then begin
      if grab(resp, ixstart, ixend, 'array', true) then begin
         ixstart2 := ixstart;
         ixend2 := 0;
         n := 0;
         while grab(resp, ixstart2, ixend2, 'value', false) do begin
            ixstart3 := ixstart2;
            ixend3 := ixend2;
            if grab(resp, ixstart3, ixend3, 'i4', true) then begin
               n := n + 1;
               readv(substr(resp, ixstart3, ixend3 - ixstart3), a[n]);
            end else begin
               if grab(resp, ixstart3, ixend3, 'int', true) then begin
                  n := n + 1;
                  readv(substr(resp, ixstart3, ixend3 - ixstart3), a[n]);
               end;
            end;
            ixstart2 := ixend2;
            ixend2 := ixend;
         end;
         write('[');
         for i := 1 to n do begin
            if i > 1 then write(',');
            write(a[i]:1);
         end;
         writeln(']');
      end;
   end;
end;

procedure test_getListOfStrings(host : pstr; port : integer);

var
   req, resp : pstr;
   ctx : http;
   ixstart, ixend, ixstart2, ixend2, ixstart3, ixend3, n, i : integer;
   a : array [1..1000] of pstr;

begin
   req := '<methodCall><methodName>Test.getListOfStrings</methodName><params></params></methodCall>';
   ctx := http_post(fix(host), port, '/', 'text/xml', 'text/xml', fix(req));
   http_recv_all(ctx, resp.body, resp.length);
   ixstart := 1;
   ixend := length(resp);
   if grab(resp, ixstart, ixend, 'value', true) then begin
      if grab(resp, ixstart, ixend, 'array', true) then begin
         ixstart2 := ixstart;
         ixend2 := 0;
         n := 0;
         while grab(resp, ixstart2, ixend2, 'value', false) do begin
            ixstart3 := ixstart2;
            ixend3 := ixend2;
            if grab(resp, ixstart3, ixend3, 'string', true) then begin
               n := n + 1;
               a[n] := substr(resp, ixstart3, ixend3 - ixstart3);
            end else begin
               n := n + 1;
               a[n] := substr(resp, ixstart3, ixend3 - ixstart3);
            end;
            ixstart2 := ixend2;
            ixend2 := ixend;
         end;
         write('[');
         for i := 1 to n do begin
            if i > 1 then write(',');
            write(a[i]);
         end;
         writeln(']');
      end;
   end;
end;

procedure test_add(host : pstr; port : integer);

var
   req, resp : pstr;
   ctx : http;
   ixstart, ixend, v1, v2, v3 : integer;

begin
   v1 := 123;
   v2 := 456;
   req := '<methodCall><methodName>Test.add</methodName><params><param><value><i4>' + dec(v1) + '</i4></value></param><param><value><i4>' + dec(v2) + '</i4></value></param></params></methodCall>';
   ctx := http_post(fix(host), port, '/', 'text/xml', 'text/xml', fix(req));
   http_recv_all(ctx, resp.body, resp.length);
   ixstart := 1;
   ixend := length(resp);
   if grab(resp, ixstart, ixend, 'value', true) then begin
      if grab(resp, ixstart, ixend, 'i4', true) then begin
         readv(substr(resp, ixstart, ixend - ixstart), v3);
         writeln(v3:1);
      end else begin
         if grab(resp, ixstart, ixend, 'int', true) then begin
            readv(substr(resp, ixstart, ixend - ixstart), v3);
            writeln(v3:1);
         end;
      end;
   end;
end;

procedure test_concat(host : pstr; port : integer);

var
   req, resp : pstr;
   ctx : http;
   ixstart, ixend : integer;
   s1, s2, s3 : pstr;

begin
   s1 := 'ABC';
   s2 := 'DEF';
   req := '<methodCall><methodName>Test.concat</methodName><params><param><value><string>' + s1 + '</string></value></param><param><value><string>' + s2 + '</string></value></param></params></methodCall>';
   ctx := http_post(fix(host), port, '/', 'text/xml', 'text/xml', fix(req));
   http_recv_all(ctx, resp.body, resp.length);
   ixstart := 1;
   ixend := length(resp);
   if grab(resp, ixstart, ixend, 'value', true) then begin
      if grab(resp, ixstart, ixend, 'string', true) then begin
         s3 := substr(resp, ixstart, ixend - ixstart);
         writeln(s3);
      end else begin
         s3 := substr(resp, ixstart, ixend - ixstart);
         writeln(s3);
      end;
   end;
end;

begin
   writeln('http://', host, ':', port:1, '/ (Pascal)');
   test_getInt(host, port);
   test_getString(host, port);
   test_getListOfInts(host, port);
   test_getListOfStrings(host, port);
   test_add(host, port);
   test_concat(host, port);
end;

begin
   test('localhost', 8001);
   test('localhost', 8002);
end.
