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