Annotation of OpenXM/src/kan96xx/Doc/cgi.sm1, Revision 1.10
1.10 ! takayama 1: % $OpenXM: OpenXM/src/kan96xx/Doc/cgi.sm1,v 1.9 2004/11/23 01:37:47 takayama Exp $
1.3 takayama 2: % OpenXM/src/polymake/cgi/cgi-polymake.sh
3: % is a shell script to call sm1/polymake as a CGI script.
4: % Override as /doPolymake {doPolymake.OoHG} def if you need.
5:
1.5 takayama 6: /cgi.verbose 0 def
7:
1.3 takayama 8: [(plugin-cgi)
9: [(Key words: )
10: ( cgiUrlEncodingToKeyValuePair)
11: ( cgiKeyValuePairToHttpString)
12: ( cgiKeyValuePairToUrlEncodingString)
13: ( cgiStringToUrlEncoding)
14: ( cgiUrlEncodedStringToObj)
15: ( cgiHttpToKeyValuePair)
16: ]] putUsages
1.1 takayama 17:
1.2 takayama 18: % A sample code
19: % (OpenXM) cgiQueryGoogle --> it does not work well.
20: /cgiQueryGoogle {
1.1 takayama 21: /key set
22: [[(URL),(/search)],
23: [(hl),(ja)],
24: [(ie),(UTF-8)],
25: [(q),key]] /kv set
26: [(cgiKeyValuePairToUrlEncodingString) kv] extension /sss set
27: [(GET ) , sss , ( HTTP/0.9) , nl , nl ] cat /sss2 set
28:
29: [(sm1.socket) (connect) [80 (www.google.co.jp)]] extension 0 get /goo set
30: [(sm1.socket) (write) [goo sss2]] extension
1.2 takayama 31: goo readHTTP0 /pp set
1.1 takayama 32: pp message
33: [(sm1.socket) (close) [goo]] extension
34: [(cgiHttpToKeyValuePair) pp] extension /pp2 set
35: } def
36:
1.2 takayama 37: % A sample
38: % (www.math.kobe-u.ac.jp) cgiGetIndex
39: /cgiGetIndex {
1.1 takayama 40: /url set
1.2 takayama 41: [[(URL),(/index.html)]
1.1 takayama 42: ] /kv set
43: [(cgiKeyValuePairToUrlEncodingString) kv] extension /sss set
44: [(GET ) , sss , ( HTTP/0.9) , nl , nl ] cat /sss2 set
45:
46: sss2 message
47: [(sm1.socket) (connect) [80 url]] extension 0 get /goo set
48: [(sm1.socket) (write) [goo sss2]] extension
1.2 takayama 49: goo readHTTP0 /pp set
1.1 takayama 50: pp message
51: [(sm1.socket) (close) [goo]] extension
52: [(cgiHttpToKeyValuePair) pp] extension /pp2 set
1.2 takayama 53: } def
54:
1.3 takayama 55: [(readHTTP0)
56: [(fd readHTTP0 result-str)
57: (Read data from fd until the connection is closed.)
58: (Example:)
59: $[(sm1.socket) (connect) [80 (www.math.kobe-u.ac.jp)]] extension $
60: $ 0 get /goo set $
61: $goo readHTTP0 /pp set pp message$
62: $[(sm1.socket) (close) [goo]] extension $
63: ]] putUsages
1.2 takayama 64: /readHTTP0 {
65: /arg1 set
66: [/fd /sss /pp /pp2 /nn] pushVariables
67: [
68: /fd arg1 def
69: /sss [ ] def
70: {
71: [(sm1.socket) (select) [fd -1]] extension {
72: } { (select error) error } ifelse
73:
74: [(sm1.socket) (read) [fd]] extension /pp set
75: pp length 0 eq { exit } { } ifelse
76: sss pp append /sss set
77: } loop
78: sss cat /arg1 set
79: ] pop
80: popVariables
81: arg1
82: } def
83:
1.3 takayama 84: % ----------------------- server side -----------------------
85: % command filename action
86: /cgiPolymake.polymakeName (polymake) def
87: %/cgiPolymake.polymakeName (polymake_dummy) def
88: %/cgiPolymake.polymakeName (hoge) def
89:
90: [(cgiPolymake)
91: [(Make sm1 to a polymake server based on CGI/OoHG)
92: $ sm1 -q -s "[(parse) (cgi.sm1) pushfile] extension cgiPolymake quit " $
93: ]] putUsages
94: % Server-side-script
95: % oxMessageBody=action input-data-for-polymake
1.9 takayama 96: % t.t : oxMessageBody=FACETS++POINTS%0A1+0+0+%0A1+1+0+%0A1+0+1%0A
97: % t?oxMe...
98: % env CONTENT_LENGTH=57 sm1 -q -s "[(parse) (cgi.sm1) pushfile] extension cgiPolymake quit " <t.t
1.2 takayama 99: /cgiPolymake {
1.3 takayama 100: {
1.9 takayama 101: [(getenv) (CONTENT_LENGTH)] extension /cgi.content_length set
1.10 ! takayama 102: cgi.content_length isString {
! 103: } {
! 104: cgiPolymake.errorMessage message exit
! 105: } ifelse
1.9 takayama 106: cgi.content_length .. (integer) dc /cgi.content_length set
1.10 ! takayama 107: cgi.content_length 1 lt {
! 108: cgiPolymake.errorMessage message exit
! 109: } { } ifelse
1.9 takayama 110: [(read) 0 cgi.content_length] extension /cgi.query_string set
1.4 takayama 111: cgi.query_string isString {
112: cgi.query_string length 0 eq {
113: cgiPolymake.errorMessage message exit
114: } { } ifelse
115: }
1.3 takayama 116: {
117: cgiPolymake.errorMessage message exit
118: } ifelse
1.4 takayama 119: cgi.body.http0 message
1.3 takayama 120: cgi.query_string cgiPolymake.0
121: cgi.body cgiReplyInHttp0 /cgi.body.http set
122: cgi.body.http message
123: exit
124: } loop
1.4 takayama 125: [(flush)] extension
1.2 takayama 126: } def
1.3 takayama 127: /cgiPolymake.errorMessage
128: [ (Content-Type: text/html) nl
129: nl
130: (<html><body>) nl
131: (Input <br> action polymake-data <br> ) nl
132: (Example: <pre> ) nl
133: (FACETS POINTS) nl
134: (1 0 0 ) nl
135: (1 1 0 ) nl
136: (1 0 1 ) nl
137: (</pre>) nl
1.9 takayama 138: (<form method="POST">) nl
1.3 takayama 139: (<input type=submit>) nl
1.4 takayama 140: (<textarea name="oxMessageBody") nl
1.3 takayama 141: ( rows=10 cols="80" wrap="soft"></textarea>) nl
142: (</form>) nl
143: (</body></html>) nl
144: ] cat
145: def
146:
147: /cgi.body.http0
148: (Content-Type: text/plain)
149: def
1.2 takayama 150: /cgiPolymake.0 {
151: /arg1 set
152: [/sss /kv /comm /i /acti0 /pError] pushVariables
153: [
154: /sss arg1 def
1.3 takayama 155: % Step 1. analyze the query string in URL encoding.
1.6 takayama 156: [(cgiUrlEncodingToKeyValuePair) sss] extension /kv set
1.2 takayama 157: kv (oxMessageBody) getNode /comm set
1.4 takayama 158: % [nl nl] cat message sss message kv message % for debug.
1.3 takayama 159: % Step 2. Extract action part and file part for polymake.
1.2 takayama 160: comm (array) dc /comm set
161: /i 0 def
162: [
163: 0 1, comm length 1 sub {
164: /i set
165: comm,i,get 33 lt {
166: exit
167: } { comm,i,get } ifelse
168: } for
169: ] /acti0 set
170: acti0 { (string) dc } map cat /acti0 set
171:
172: /cgi.body comm def
173: 0 1 i {
174: cgi.body rest /cgi.body set
175: } for
176: cgi.body { (string) dc } map cat /cgi.body set
177:
1.3 takayama 178: % Step 3. Calling polymake
1.2 takayama 179: % acti0 cgi.body
180: /pError [ ] def
1.3 takayama 181: % cgi.body --> oxsVarToFile(), value is not a string object ...
1.2 takayama 182: % [(polymake) (stringInOut://cgi.body) acti0] addStdoutStderr
1.3 takayama 183: % It is not a bug; .body is removed from the variable name.
184: /cgi_body cgi.body def
185: [cgiPolymake.polymakeName (stringInOut://cgi_body.poly) acti0] addStdoutStderr
1.2 takayama 186: oxshell pop
187: pError [@@@stdout @@@stderr] append /pError set
1.4 takayama 188: /cgi.body cgi_body def
1.2 takayama 189: ] pop
190: popVariables
191: } def
192:
193: /cgi.test0 {
194: [[(URL) (hoge)]
195: [(oxMessageBody)
196: [(FACETS )
197: (POINTS) nl
198: (1 0 0) nl
199: (1 1 0) nl
200: (1 0 1) nl
201: (1 1 1) nl ] cat
202: ]] /ff set
203: [(cgiKeyValuePairToUrlEncodingString) ff] extension /ff1 set
204: ff1 message
205: ff1 cgiPolymake.0
206: cgi.body message
1.3 takayama 207: } def
208:
209: /cgiReplyInHttp0 {
210: /arg1 set
211: [/ss /sskv] pushVariables
212: [
213: /ss arg1 def
214: [[(Content-Body) ss]
215: % [(Content-Type) (text/plain)]
1.4 takayama 216: [(oxshell-stdout) [(cgiStringToUrlEncoding) @@@stdout] extension]
217: [(oxshell-stderr) [(cgiStringToUrlEncoding) @@@stderr] extension]
1.3 takayama 218: ] /sskv set
219: [(cgiKeyValuePairToHttpString) sskv] extension /arg1 set
220: ] pop
221: arg1
222: } def
223:
224: % ------------ client side script ---------------
225: /cgiQueryPolymake.hostname (polymake.math.kobe-u.ac.jp) def
1.4 takayama 226: /cgiQueryPolymake.cginame (/cgi-bin/cgi-polymake.sh) def
1.3 takayama 227: /cgiQueryPolymake {
228: /arg2 set /arg1 set
229: [/saction /sfile /ff /ff1 /sss2 /goo /pp /pp2] pushVariables
230: [
231: /saction arg1 def /sfile arg2 def
232: % step1. Generate query in URL encoding.
1.9 takayama 233: [(http://) cgiQueryPolymake.hostname cgiQueryPolymake.cginame] cat
234: /ff1 set
1.7 takayama 235: % full URL necessary for virtual host of apache.
1.9 takayama 236: [[(oxMessageBody)
237: [saction ( )
238: sfile nl ] cat
239: ]
240: ] /ff set
241: [(cgiKeyValuePairToUrlEncodingString) ff] extension /ff set
242: [(POST ) , ff1 , ( HTTP/0.9) , nl ,
243: (Connection: Keep-Alive) , nl ,
244: (Content-length: ) , ff length (string) dc , nl ,
245: ff ] cat /sss2 set
1.3 takayama 246: % step 2. Call the server and get the response in pp
247: [(sm1.socket) (connect)
248: [80 cgiQueryPolymake.hostname]] extension 0 get /goo set
249: [(sm1.socket) (write) [goo sss2]] extension
250: goo readHTTP0 /pp set
1.8 takayama 251:
252: % For the case of error.
253: [(regexec) (200 OK) [pp]] extension length 0 eq {
254: (sss2=) message
255: sss2 message
256: (pp=) message
257: pp message
258: (cgi.sm1: there seems to be an error in the HTTP connection.)
259: } { } ifelse
260:
1.5 takayama 261: cgi.verbose { pp message } { } ifelse
1.3 takayama 262: [(sm1.socket) (close) [goo]] extension
263: [(cgiHttpToKeyValuePair) pp] extension /pp2 set
264:
265: % step 3. Analyze the response.
266: [pp2 (Content-Body) getNode
267: pp2 (oxshell-stdout) getNode
268: pp2 (oxshell-stderr) getNode
269: ] /arg1 set
270: ] pop
271: popVariables
272: arg1
273: } def
274:
275: % Overrides doPolymake
276: %/doPolymake { doPolymake.OoHG } def
1.7 takayama 277: %/polymake.start { polymake.start.OoHG } def
1.3 takayama 278:
279: [(doPolymake.OoHG)
280: [(It first looks for local polymake. If there is, it calls the local polymake.)
281: (If not, it calls http://polymake.math.kobe-u.ac.jp/cgi-bin/cgi-polymake.sh)
282: (to make a computation in polymake.)
283: (See doPolymake for the syntax. doPolymake.OoHG may overrides doPolymake.)
284: ]] putUsages
285: /doPolymake.OoHG {
286: /arg1 set
287: [/in-doPolymake.OoHG /pAction /pData /pNative /ptree
288: /pResult /pError
289: ] pushVariables
290: [
291: arg1 0 get /pAction set
292: arg1 1 get /pData set
293: polymake.start
294:
295: /pError [ ] def
296: %% step 1: polymake tfb ===> polymake native data
297: @@@polymake.k0.ccc ( polymake=Object; ) oxexecutestring
298: @@@polymake.k0.ccc ( QuoteMode(1); ) oxexecutestring
299: @@@polymake.k0.ccc [pData ( ;)] cat oxexecutestring
300: @@@polymake.k0.ccc oxpopcmo /ptree set
301: @@@polymake.k0.ccc ( QuoteMode(0); ) oxexecutestring
302: [(treeToPolymake) ptree] extension /pNative set
303: %% step 2: calling the polymake
1.4 takayama 304: % [(which) (polymake)] oxshell tag 0 eq
305: 1 % always call web service
1.3 takayama 306: {
307: % cgi-polymake
308: (Trying web service.) message
309: pAction pNative cgiQueryPolymake /doPolymake.OoHG.result set
310:
1.7 takayama 311: [(regexec) (Not Found) [doPolymake.OoHG.result 0 get ]]
312: extension length 0 eq not {
313: doPolymake.OoHG.result message
314: (The polymake cgi seems to be out of service.) error
315: } { } ifelse
316:
1.3 takayama 317: doPolymake.OoHG.result 0 get /pNative set
1.7 takayama 318:
319:
1.3 takayama 320: pError doPolymake.OoHG.result rest append /pError set
321: } {
322: % local polymake
323: [(polymake) (stringInOut://pNative.poly) pAction] addStdoutStderr
324: oxshell pop
325: pError [@@@stdout @@@stderr] append /pError set
326: } ifelse
327: %% step 3: polymake native data to polymake tfb
328: [(polymake2tfb) (<) (stringIn://pNative) (>) (stringOut://pResult)
329: (2>) (stringOut://@@@stderr)] oxshell pop
330: pError [@@@stderr] append /pError set
331: %% step 4: get also tree style data.
332: @@@polymake.k0.ccc ( polymake=Object; ) oxexecutestring
333: @@@polymake.k0.ccc ( QuoteMode(1); ) oxexecutestring
334: @@@polymake.k0.ccc [pResult ( ;)] cat oxexecutestring
335: @@@polymake.k0.ccc oxpopcmo /ptree set
336: @@@polymake.k0.ccc ( QuoteMode(0); ) oxexecutestring
337: [pResult ptree pError] /arg1 set
338: ] pop
339: popVariables
340: arg1
1.7 takayama 341: } def
342:
343: /polymake.start.OoHG {
344: (ox.k0.loaded) boundp { }
345: { [(parse) (ox.sm1) pushfile] extension
346: /@@@polymake.k0.ccc [ ] def
347: } ifelse
348: @@@polymake.k0.ccc [ ] eq {
349: k0connectr /@@@polymake.k0.ccc oxk0.ccc def /oxk0.ccc [ ] def
350: @@@polymake.k0.ccc oxsetmathcap @@@polymake.k0.ccc oxmathcap
351: } { } ifelse
352: @@@polymake.k0.ccc 0 get (closed) eq {
353: k0connectr /@@@polymake.k0.ccc oxk0.ccc def /oxk0.ccc [ ] def
354: @@@polymake.k0.ccc oxsetmathcap @@@polymake.k0.ccc oxmathcap
355: } { } ifelse
356: [(which) (polymake2tfb)] oxshell tag 0 eq {
357: (polymake2tfb is not installed in this system.) error
358: } { } ifelse
1.3 takayama 359: } def
360:
361:
362:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>