1 #!/usr/home/nuevatec/bin/newlisp
3 # xmlrpc.cgi - CGI script to handle XML-RPC requests
5 # This is similar to xmlrpc-server, but stateless as a new
6 # newLISP process is invoked everytime this script is executed.
7 # For a XML-RPC server maintaining state run xmlrpc-server.
9 # v.1.0 - 2005-01-14 Lutz Mueller
12 # method name for newLISP.evalString was listed wrong
14 # supports the following methods:
16 # Method Return Parameter
17 # ------ ------ ---------
18 # system.listMethods string n/a
19 # system.methodHelp string string
20 # system.methodSignature array string
21 # newLISP.evalString base64 base64
27 # formatting templates for responses
30 [text]<?xml version="1.0"?>
41 [text]<?xml version="1.0"?>
47 <name>faultCode</name>
48 <value><int>%d</int></value>
51 <name>faultString</name>
52 <value><string>%s</string></value>
61 # event handler called when newLISP receives a request
63 (define (process-post request)
64 (if (not (catch (handle request) 'page))
65 (set 'page (format fault-response 0 page)))
67 "Content-type: text/xml\r\n"
68 "Content-length: " (length page) "\r\n\r\n"
72 (define (handle input, XML contentlength methodName params)
74 (xml-type-tags nil nil nil nil)
75 (if (not (set 'XML (xml-parse input (+ 1 2 4 8 16))))
78 (error 3 "No XML or XML is empty")
79 (error 4 (append "XML error: "
80 (first (xml-error))))))
82 (set 'XML (first XML)))
84 ; get methodName and parameter section
85 (set 'm (match '(methodCall (methodName *) *) XML))
87 (error 5 "Invalid XML-RPC format"))
89 (set 'methodName (first (first m)))
90 (set 'params (last m))
93 ("newLISP.evalString" (newLISP.evalString params))
94 ("system.listMethods" (system.listMethods))
95 ("system.methodHelp" (system.methodHelp params))
96 ("system.methodSignature" (system.methodSignature params))
97 (true (error 6 "Method name not known")))
100 (define (error no msg)
101 (throw (format fault-response no
102 (append "newLISP XML-RPC v." version " - " msg))))
105 ######################### remote callable methods ##############################
107 (define (system.listMethods)
108 [text]<?xml version="1.0"?>
111 <param><value><array><data>
112 <value><string>system.listMethods</string></value>
113 <value><string>system.methodHelp</string></value>
114 <value><string>system.methodSignature</string></value>
115 <value><string>newLISP.evalString</string></value>
116 </data></array></value></param>
122 (define (system.methodHelp params, methodName)
123 (set 'methodName (nth 0 1 1 1 1 params))
125 ("system.listMethods" (format normal-response "Lists all methods implemented."))
126 ("system.methodHelp" (format normal-response "Documents a method."))
127 ("system.methodSignature" (format normal-response "Shows the signatures of a method."))
128 ("newLISP.evalString" (format normal-response "Evaluate a base64 encoded string."))
129 (true (error 7 "Method name in system.methodHelp not known")))
132 (define (system.methodSignature params)
133 (set 'methodName (nth 0 1 1 1 1 params))
135 ("system.listMethods" (format normal-response
148 ("system.methodHelp" (format normal-response
154 <value>string</value>
155 <value>string</value>
162 ("system.methodSignature" (format normal-response
169 <value>string</value>
176 ("newLISP.evalString" (format normal-response
182 <value>base64</value>
183 <value>base64</value>
190 (true (error 7 "Method name in system.methodSignature not known")))
193 (define (newLISP.evalString params, m, result)
194 (set 'm (match '((params (param (value (base64 *))))) params))
196 (error 8 "Invalid format for method newLISP.evalString")
198 (string (eval-string (base64-dec (first (first m))) (error-text))))
199 ;; "This function has been disabled in this demo for security reasons.")
200 (format normal-response
201 (append "<base64>" (base64-enc result) "</base64>")) )
205 ########################### MAIN ENTRY POINT #######################
207 (set 'input (read-line))
211 "Content-type: text/html\r\n\r\n"
212 "<h2>newLISP XML-RPC v." version
213 ": not a valid XML-RPC request</h2>")
215 (while (read-line) (write-buffer input (current-line)))
216 (process-post input))