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