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