Annotation of OpenXM/src/kan96xx/Doc/cgi.sm1, Revision 1.11
1.11 ! takayama 1: % $OpenXM: OpenXM/src/kan96xx/Doc/cgi.sm1,v 1.10 2004/11/23 02:19:16 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 ,
1.11 ! takayama 243: % (Connection: Keep-Alive) , nl ,
! 244: (Content-length: ) , ff length (dollar) dc , nl , nl
! 245: ff nl] 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:
1.11 ! takayama 275: /cgi.test1 {
! 276: (FACETS)
! 277: [(POINTS) nl
! 278: (1 0 0 ) nl
! 279: (1 1 0 ) nl
! 280: (1 0 1 ) nl
! 281: ] cat
! 282: cgiQueryPolymake message
! 283: } def
1.3 takayama 284: % Overrides doPolymake
285: %/doPolymake { doPolymake.OoHG } def
1.7 takayama 286: %/polymake.start { polymake.start.OoHG } def
1.3 takayama 287:
288: [(doPolymake.OoHG)
289: [(It first looks for local polymake. If there is, it calls the local polymake.)
290: (If not, it calls http://polymake.math.kobe-u.ac.jp/cgi-bin/cgi-polymake.sh)
291: (to make a computation in polymake.)
292: (See doPolymake for the syntax. doPolymake.OoHG may overrides doPolymake.)
293: ]] putUsages
294: /doPolymake.OoHG {
295: /arg1 set
296: [/in-doPolymake.OoHG /pAction /pData /pNative /ptree
297: /pResult /pError
298: ] pushVariables
299: [
300: arg1 0 get /pAction set
301: arg1 1 get /pData set
302: polymake.start
303:
304: /pError [ ] def
305: %% step 1: polymake tfb ===> polymake native data
306: @@@polymake.k0.ccc ( polymake=Object; ) oxexecutestring
307: @@@polymake.k0.ccc ( QuoteMode(1); ) oxexecutestring
308: @@@polymake.k0.ccc [pData ( ;)] cat oxexecutestring
309: @@@polymake.k0.ccc oxpopcmo /ptree set
310: @@@polymake.k0.ccc ( QuoteMode(0); ) oxexecutestring
311: [(treeToPolymake) ptree] extension /pNative set
312: %% step 2: calling the polymake
1.4 takayama 313: % [(which) (polymake)] oxshell tag 0 eq
314: 1 % always call web service
1.3 takayama 315: {
316: % cgi-polymake
317: (Trying web service.) message
318: pAction pNative cgiQueryPolymake /doPolymake.OoHG.result set
319:
1.7 takayama 320: [(regexec) (Not Found) [doPolymake.OoHG.result 0 get ]]
321: extension length 0 eq not {
322: doPolymake.OoHG.result message
323: (The polymake cgi seems to be out of service.) error
324: } { } ifelse
325:
1.3 takayama 326: doPolymake.OoHG.result 0 get /pNative set
1.7 takayama 327:
328:
1.3 takayama 329: pError doPolymake.OoHG.result rest append /pError set
330: } {
331: % local polymake
332: [(polymake) (stringInOut://pNative.poly) pAction] addStdoutStderr
333: oxshell pop
334: pError [@@@stdout @@@stderr] append /pError set
335: } ifelse
336: %% step 3: polymake native data to polymake tfb
337: [(polymake2tfb) (<) (stringIn://pNative) (>) (stringOut://pResult)
338: (2>) (stringOut://@@@stderr)] oxshell pop
339: pError [@@@stderr] append /pError set
340: %% step 4: get also tree style data.
341: @@@polymake.k0.ccc ( polymake=Object; ) oxexecutestring
342: @@@polymake.k0.ccc ( QuoteMode(1); ) oxexecutestring
343: @@@polymake.k0.ccc [pResult ( ;)] cat oxexecutestring
344: @@@polymake.k0.ccc oxpopcmo /ptree set
345: @@@polymake.k0.ccc ( QuoteMode(0); ) oxexecutestring
346: [pResult ptree pError] /arg1 set
347: ] pop
348: popVariables
349: arg1
1.7 takayama 350: } def
351:
352: /polymake.start.OoHG {
353: (ox.k0.loaded) boundp { }
354: { [(parse) (ox.sm1) pushfile] extension
355: /@@@polymake.k0.ccc [ ] def
356: } ifelse
357: @@@polymake.k0.ccc [ ] eq {
358: k0connectr /@@@polymake.k0.ccc oxk0.ccc def /oxk0.ccc [ ] def
359: @@@polymake.k0.ccc oxsetmathcap @@@polymake.k0.ccc oxmathcap
360: } { } ifelse
361: @@@polymake.k0.ccc 0 get (closed) eq {
362: k0connectr /@@@polymake.k0.ccc oxk0.ccc def /oxk0.ccc [ ] def
363: @@@polymake.k0.ccc oxsetmathcap @@@polymake.k0.ccc oxmathcap
364: } { } ifelse
365: [(which) (polymake2tfb)] oxshell tag 0 eq {
366: (polymake2tfb is not installed in this system.) error
367: } { } ifelse
1.3 takayama 368: } def
369:
370:
371:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>