#!/local/bin/hs -gif
puts "Content-type: text/html\n\n" ;

puts "<html><body>" ;

puts "<h1>SOAP Demo</h1>" ;
puts "<p>Opening port 80 to hyperscript.net, requesting soap_server.cgi</p>" ;
h = http_open ( "hyperscript.net", 80 ) ;
if ( !h ) exit ;
http_assign ( 1, h ) ;

xmlText = {} ;
isXML = 0 ;
timesSent = 0 ;

list GET_REPLY()
{
  puts "RECEIVED DATA" ;
  puts _http_data_ ;
} ;

http_enable ( GET_REPLY, 1 ) ;

SEND_SOAP()
{
  puts {"<h2>Sending SOAP message at "+time()+"...</h2>"} ;

  puts "<h3>(SOAP message in HyperScript format)</h3>" ;

  list SOAP:Envelope = {
    attr xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/",
    list SOAP:Header = {
      "something"
    },
    list SOAP:Body = {
      list m:DoLogin = {
        attr xmlns:m="urn:soapserver/soap:AuthorizationModule",
        list UserName = {
          "bergsma"
        },
        list Password = {
          "XML"
        }
      }
    }
  } ;
  xmlHeader = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" ;
  str payload = xmlHeader + xsdescribe ( SOAP:Envelope ) ;

  puts "<pre>" ;
  describe SOAP:Envelope ;
  puts "</pre>" ;

  puts "<h2>Waiting for response...</h2>" ;

  on_hangup return 1 ;
  on_message return 1 ;
  on_timeout { puts "Failed to receive response"; exit;} ;

  list http_attributes = {
    str 'Content-Type' = "text/xml; application/soap; charset=iso-8859-1;",
    str 'SOAPAction' = "http://hyperscript.net/soap"
  } ;

  timeout 5 ;
  ret = http_query ( 1, "POST", "/cgi-bin/soap_server.cgi", http_attributes, payload ) ;
  if ( exists ( html.body.div ) ) xdescribe html.body.div ;
  timesSent++ ;

} ;


SEND_SOAP() ;

puts "</body></html>" ;
exit;