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