Annotation of OpenXM/src/kan96xx/Doc/cgi.sm1, Revision 1.17
1.17 ! takayama 1: % $OpenXM: OpenXM/src/kan96xx/Doc/cgi.sm1,v 1.16 2013/09/25 07:30:52 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.17 ! takayama 234: %This host and cgi are version 2.0 polymake. dim, ... are different with new versions.
! 235: /cgiQueryPolymake { cgiQueryPolymake.curl } def
! 236: %/cgiQueryPolymake { cgiQueryPolymake.native } def
1.14 takayama 237: /cgiQueryPolymake.curl {
238: /arg2 set /arg1 set
239: [/saction /sfile /ff /ff1 /sss2 /goo /pp /pp2 /key /body /fd] pushVariables
240: [
241: /saction arg1 def /sfile arg2 def
242: % step1. Generate query in URL encoding.
243: [(http://) cgiQueryPolymake.hostname cgiQueryPolymake.cginame] cat
244: /ff1 set
245: % full URL necessary for virtual host of apache.
246: [[(oxMessageBody)
247: [saction ( )
248: sfile nl ] cat
249: ]
250: ] /ff set
251:
252: ff 0 get 0 get /key set
253: ff 0 get 1 get /body set
254: % step 2. Call the server and get the response in pp
255: /cgi.verbose 1 def
256: (/tmp/sm1-q-cgi.txt) (w) file /fd set
257: fd body writestring
258: fd closefile
1.15 takayama 259: % [(curl) (--form) [key (=@/tmp/sm1-q-cgi.txt)] cat ff1] /ff set
260: % ff addStdoutStderr oxshell @@@stdout /pp set
261: [(curl --form ) [key (=@/tmp/sm1-q-cgi.txt )] cat ff1 (>/tmp/sm1-q-cgi-out.txt)] cat /ff set
262: ff system
263: [nl (/tmp/sm1-q-cgi-out.txt) pushfile] cat /pp set
1.14 takayama 264:
265: cgi.verbose { pp message } { } ifelse
266: [(cgiHttpToKeyValuePair) pp] extension /pp2 set
1.15 takayama 267: pp2 message
1.14 takayama 268: % step 3. Analyze the response.
269: [pp2 (Content-Body) getNode
270: pp2 (oxshell-stdout) getNode
271: pp2 (oxshell-stderr) getNode
272: ] /arg1 set
273: ] pop
274: popVariables
275: arg1
276: } def
277:
278: /cgiQueryPolymake.native {
1.3 takayama 279: /arg2 set /arg1 set
280: [/saction /sfile /ff /ff1 /sss2 /goo /pp /pp2] pushVariables
281: [
282: /saction arg1 def /sfile arg2 def
283: % step1. Generate query in URL encoding.
1.9 takayama 284: [(http://) cgiQueryPolymake.hostname cgiQueryPolymake.cginame] cat
285: /ff1 set
1.7 takayama 286: % full URL necessary for virtual host of apache.
1.9 takayama 287: [[(oxMessageBody)
288: [saction ( )
289: sfile nl ] cat
290: ]
291: ] /ff set
292: [(cgiKeyValuePairToUrlEncodingString) ff] extension /ff set
293: [(POST ) , ff1 , ( HTTP/0.9) , nl ,
1.11 takayama 294: % (Connection: Keep-Alive) , nl ,
1.12 takayama 295: (HOST: ) cgiQueryPolymake.hostname , nl ,
1.11 takayama 296: (Content-length: ) , ff length (dollar) dc , nl , nl
297: ff nl] cat /sss2 set
1.3 takayama 298: % step 2. Call the server and get the response in pp
299: [(sm1.socket) (connect)
300: [80 cgiQueryPolymake.hostname]] extension 0 get /goo set
301: [(sm1.socket) (write) [goo sss2]] extension
302: goo readHTTP0 /pp set
1.8 takayama 303:
304: % For the case of error.
305: [(regexec) (200 OK) [pp]] extension length 0 eq {
306: (sss2=) message
307: sss2 message
308: (pp=) message
309: pp message
310: (cgi.sm1: there seems to be an error in the HTTP connection.)
311: } { } ifelse
312:
1.5 takayama 313: cgi.verbose { pp message } { } ifelse
1.3 takayama 314: [(sm1.socket) (close) [goo]] extension
315: [(cgiHttpToKeyValuePair) pp] extension /pp2 set
316:
317: % step 3. Analyze the response.
318: [pp2 (Content-Body) getNode
319: pp2 (oxshell-stdout) getNode
320: pp2 (oxshell-stderr) getNode
321: ] /arg1 set
322: ] pop
323: popVariables
324: arg1
325: } def
326:
1.11 takayama 327: /cgi.test1 {
328: (FACETS)
329: [(POINTS) nl
330: (1 0 0 ) nl
331: (1 1 0 ) nl
332: (1 0 1 ) nl
333: ] cat
334: cgiQueryPolymake message
335: } def
1.3 takayama 336: % Overrides doPolymake
337: %/doPolymake { doPolymake.OoHG } def
1.7 takayama 338: %/polymake.start { polymake.start.OoHG } def
1.3 takayama 339:
340: [(doPolymake.OoHG)
1.17 ! takayama 341: [(doPolymake.local calls the local polymake.)
! 342: (This function calls http://polymake.math.kobe-u.ac.jp/cgi-bin/cgi-polymake.sh)
1.3 takayama 343: (to make a computation in polymake.)
1.17 ! takayama 344: (The host name and the cgi name are set to the variable)
! 345: ( cgiQueryPolymake.hostname and cgiQueryPolymake.cginame)
1.3 takayama 346: (See doPolymake for the syntax. doPolymake.OoHG may overrides doPolymake.)
1.16 takayama 347: (If you use curl to call the server execute)
348: ( usePolymake.OoHG.curl)
349: (after loading cgi.sm1)
1.3 takayama 350: ]] putUsages
351: /doPolymake.OoHG {
352: /arg1 set
353: [/in-doPolymake.OoHG /pAction /pData /pNative /ptree
354: /pResult /pError
355: ] pushVariables
356: [
357: arg1 0 get /pAction set
358: arg1 1 get /pData set
1.14 takayama 359: polymake.start.OoHG
1.3 takayama 360:
361: /pError [ ] def
362: %% step 1: polymake tfb ===> polymake native data
363: @@@polymake.k0.ccc ( polymake=Object; ) oxexecutestring
364: @@@polymake.k0.ccc ( QuoteMode(1); ) oxexecutestring
365: @@@polymake.k0.ccc [pData ( ;)] cat oxexecutestring
366: @@@polymake.k0.ccc oxpopcmo /ptree set
367: @@@polymake.k0.ccc ( QuoteMode(0); ) oxexecutestring
368: [(treeToPolymake) ptree] extension /pNative set
369: %% step 2: calling the polymake
1.4 takayama 370: % [(which) (polymake)] oxshell tag 0 eq
371: 1 % always call web service
1.3 takayama 372: {
373: % cgi-polymake
374: (Trying web service.) message
375: pAction pNative cgiQueryPolymake /doPolymake.OoHG.result set
1.16 takayama 376: doPolymake.OoHG.result /@@@doPolymake.vars set
1.7 takayama 377: [(regexec) (Not Found) [doPolymake.OoHG.result 0 get ]]
378: extension length 0 eq not {
379: doPolymake.OoHG.result message
380: (The polymake cgi seems to be out of service.) error
381: } { } ifelse
382:
1.3 takayama 383: doPolymake.OoHG.result 0 get /pNative set
1.7 takayama 384:
385:
1.3 takayama 386: pError doPolymake.OoHG.result rest append /pError set
387: } {
388: % local polymake
389: [(polymake) (stringInOut://pNative.poly) pAction] addStdoutStderr
390: oxshell pop
391: pError [@@@stdout @@@stderr] append /pError set
392: } ifelse
393: %% step 3: polymake native data to polymake tfb
394: [(polymake2tfb) (<) (stringIn://pNative) (>) (stringOut://pResult)
395: (2>) (stringOut://@@@stderr)] oxshell pop
396: pError [@@@stderr] append /pError set
397: %% step 4: get also tree style data.
398: @@@polymake.k0.ccc ( polymake=Object; ) oxexecutestring
399: @@@polymake.k0.ccc ( QuoteMode(1); ) oxexecutestring
400: @@@polymake.k0.ccc [pResult ( ;)] cat oxexecutestring
401: @@@polymake.k0.ccc oxpopcmo /ptree set
402: @@@polymake.k0.ccc ( QuoteMode(0); ) oxexecutestring
403: [pResult ptree pError] /arg1 set
404: ] pop
405: popVariables
406: arg1
1.7 takayama 407: } def
408:
409: /polymake.start.OoHG {
410: (ox.k0.loaded) boundp { }
411: { [(parse) (ox.sm1) pushfile] extension
412: /@@@polymake.k0.ccc [ ] def
413: } ifelse
414: @@@polymake.k0.ccc [ ] eq {
415: k0connectr /@@@polymake.k0.ccc oxk0.ccc def /oxk0.ccc [ ] def
416: @@@polymake.k0.ccc oxsetmathcap @@@polymake.k0.ccc oxmathcap
417: } { } ifelse
418: @@@polymake.k0.ccc 0 get (closed) eq {
419: k0connectr /@@@polymake.k0.ccc oxk0.ccc def /oxk0.ccc [ ] def
420: @@@polymake.k0.ccc oxsetmathcap @@@polymake.k0.ccc oxmathcap
421: } { } ifelse
422: [(which) (polymake2tfb)] oxshell tag 0 eq {
423: (polymake2tfb is not installed in this system.) error
424: } { } ifelse
1.3 takayama 425: } def
426:
427:
1.16 takayama 428: /usePolymake.local {
429: /doPolymake { doPolymake.local } def
430: /polymake.start { polymake.start.local } def
431: } def
432:
433: /usePolymake.OoHG.native {
434: /doPolymake { doPolymake.OoHG } def
435: /polymake.start { polymake.start.OoHG } def
436: /cgiQueryPolymake { cgiQueryPolymake.native } def
437: } def
438:
439: /usePolymake.OoHG.curl {
1.17 ! takayama 440: [(which) (curl)] oxshell tag 0 eq {
! 441: (Error in usePolymake.OoHG.curl: curl is not found.) error
! 442: } { } ifelse
1.16 takayama 443: /doPolymake { doPolymake.OoHG } def
444: /polymake.start { polymake.start.OoHG } def
445: /cgiQueryPolymake { cgiQueryPolymake.curl } def
446: } def
447:
1.17 ! takayama 448: [(usePolymake.local)
! 449: [(doPolymake calls local polymake)
! 450: ]] putUsages
! 451:
1.16 takayama 452: [(usePolymake.OoHG.native)
453: [(doPolymake = doPolymake.OoHG with cgiQueryPolymake.native)
454: (cf. doPolymake.OoHG)
1.17 ! takayama 455: (hostname is set in cgiQueryPolymake.hostname)
! 456: (cginame is set in cgiQueryPolymake.cginame)
1.16 takayama 457: ]] putUsages
458:
459: [(usePolymake.OoHG.curl)
460: [(doPolymake = doPolymake.OoHG with cgiQueryPolymake.curl)
461: (It should be used when cgiQueryPolymake.native does not work)
462: (over, e.g., a reverse proxy.)
463: (The command curl is required. cf. cgi.verbose, doPolymake.OoHG)
1.17 ! takayama 464: (hostname is set in cgiQueryPolymake.hostname)
! 465: (cginame is set in cgiQueryPolymake.cginame)
1.16 takayama 466: ]] putUsages
467:
468:
469:
470:
471:
1.3 takayama 472:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>