Annotation of OpenXM/src/kan96xx/Doc/cgi.sm1, Revision 1.3
1.3 ! takayama 1: % $OpenXM: OpenXM/src/kan96xx/Doc/cgi.sm1,v 1.2 2004/09/24 13:15:27 takayama Exp $
! 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:
! 6: [(plugin-cgi)
! 7: [(Key words: )
! 8: ( cgiUrlEncodingToKeyValuePair)
! 9: ( cgiKeyValuePairToHttpString)
! 10: ( cgiKeyValuePairToUrlEncodingString)
! 11: ( cgiStringToUrlEncoding)
! 12: ( cgiUrlEncodedStringToObj)
! 13: ( cgiHttpToKeyValuePair)
! 14: ]] putUsages
1.1 takayama 15:
1.2 takayama 16: % A sample code
17: % (OpenXM) cgiQueryGoogle --> it does not work well.
18: /cgiQueryGoogle {
1.1 takayama 19: /key set
20: [[(URL),(/search)],
21: [(hl),(ja)],
22: [(ie),(UTF-8)],
23: [(q),key]] /kv set
24: [(cgiKeyValuePairToUrlEncodingString) kv] extension /sss set
25: [(GET ) , sss , ( HTTP/0.9) , nl , nl ] cat /sss2 set
26:
27: [(sm1.socket) (connect) [80 (www.google.co.jp)]] extension 0 get /goo set
28: [(sm1.socket) (write) [goo sss2]] extension
1.2 takayama 29: goo readHTTP0 /pp set
1.1 takayama 30: pp message
31: [(sm1.socket) (close) [goo]] extension
32: [(cgiHttpToKeyValuePair) pp] extension /pp2 set
33: } def
34:
1.2 takayama 35: % A sample
36: % (www.math.kobe-u.ac.jp) cgiGetIndex
37: /cgiGetIndex {
1.1 takayama 38: /url set
1.2 takayama 39: [[(URL),(/index.html)]
1.1 takayama 40: ] /kv set
41: [(cgiKeyValuePairToUrlEncodingString) kv] extension /sss set
42: [(GET ) , sss , ( HTTP/0.9) , nl , nl ] cat /sss2 set
43:
44: sss2 message
45: [(sm1.socket) (connect) [80 url]] extension 0 get /goo set
46: [(sm1.socket) (write) [goo sss2]] extension
1.2 takayama 47: goo readHTTP0 /pp set
1.1 takayama 48: pp message
49: [(sm1.socket) (close) [goo]] extension
50: [(cgiHttpToKeyValuePair) pp] extension /pp2 set
1.2 takayama 51: } def
52:
1.3 ! takayama 53: [(readHTTP0)
! 54: [(fd readHTTP0 result-str)
! 55: (Read data from fd until the connection is closed.)
! 56: (Example:)
! 57: $[(sm1.socket) (connect) [80 (www.math.kobe-u.ac.jp)]] extension $
! 58: $ 0 get /goo set $
! 59: $goo readHTTP0 /pp set pp message$
! 60: $[(sm1.socket) (close) [goo]] extension $
! 61: ]] putUsages
1.2 takayama 62: /readHTTP0 {
63: /arg1 set
64: [/fd /sss /pp /pp2 /nn] pushVariables
65: [
66: /fd arg1 def
67: /sss [ ] def
68: {
69: [(sm1.socket) (select) [fd -1]] extension {
70: } { (select error) error } ifelse
71:
72: [(sm1.socket) (read) [fd]] extension /pp set
73: pp length 0 eq { exit } { } ifelse
74: sss pp append /sss set
75: } loop
76: sss cat /arg1 set
77: ] pop
78: popVariables
79: arg1
80: } def
81:
1.3 ! takayama 82: % ----------------------- server side -----------------------
! 83: % command filename action
! 84: /cgiPolymake.polymakeName (polymake) def
! 85: %/cgiPolymake.polymakeName (polymake_dummy) def
! 86: %/cgiPolymake.polymakeName (hoge) def
! 87:
! 88: [(cgiPolymake)
! 89: [(Make sm1 to a polymake server based on CGI/OoHG)
! 90: $ sm1 -q -s "[(parse) (cgi.sm1) pushfile] extension cgiPolymake quit " $
! 91: ]] putUsages
! 92: % Server-side-script
! 93: % oxMessageBody=action input-data-for-polymake
! 94: % sm1 -q -s "[(parse) (cgi.sm1) pushfile] extension cgiPolymake quit "
! 95: % env QUERY_STRING="t?oxMessageBody=FACETS+POINTS+1+0+0%0A" sm1 -q -s "[(parse) (cgi.sm1) pushfile] extension cgiPolymake quit "
1.2 takayama 96: /cgiPolymake {
1.3 ! takayama 97: {
! 98: [(getenv) (QUERY_STRING)] extension /cgi.query_string set
! 99: cgi.query_string isString { }
! 100: {
! 101: cgiPolymake.errorMessage message exit
! 102: } ifelse
! 103: cgi.body.http0 message
! 104: cgi.query_string cgiPolymake.0
! 105: cgi.body cgiReplyInHttp0 /cgi.body.http set
! 106: cgi.body.http message
! 107: exit
! 108: } loop
1.2 takayama 109: } def
1.3 ! takayama 110: /cgiPolymake.errorMessage
! 111: [ (Content-Type: text/html) nl
! 112: nl
! 113: (<html><body>) nl
! 114: (Input <br> action polymake-data <br> ) nl
! 115: (Example: <pre> ) nl
! 116: (FACETS POINTS) nl
! 117: (1 0 0 ) nl
! 118: (1 1 0 ) nl
! 119: (1 0 1 ) nl
! 120: (</pre>) nl
! 121: (<form>) nl
! 122: (<input type=submit>) nl
! 123: (<textarea name="oxMesassageBody") nl
! 124: ( rows=10 cols="80" wrap="soft"></textarea>) nl
! 125: (</form>) nl
! 126: (</body></html>) nl
! 127: ] cat
! 128: def
! 129:
! 130: /cgi.body.http0
! 131: (Content-Type: text/plain)
! 132: def
1.2 takayama 133: /cgiPolymake.0 {
134: /arg1 set
135: [/sss /kv /comm /i /acti0 /pError] pushVariables
136: [
137: /sss arg1 def
1.3 ! takayama 138: % Step 1. analyze the query string in URL encoding.
1.2 takayama 139: [(cgiUrlEncodingToKeyValuePair) sss] extension /kv set
140: kv (oxMessageBody) getNode /comm set
1.3 ! takayama 141: % Step 2. Extract action part and file part for polymake.
1.2 takayama 142: comm (array) dc /comm set
143: /i 0 def
144: [
145: 0 1, comm length 1 sub {
146: /i set
147: comm,i,get 33 lt {
148: exit
149: } { comm,i,get } ifelse
150: } for
151: ] /acti0 set
152: acti0 { (string) dc } map cat /acti0 set
153:
154: /cgi.body comm def
155: 0 1 i {
156: cgi.body rest /cgi.body set
157: } for
158: cgi.body { (string) dc } map cat /cgi.body set
159:
1.3 ! takayama 160: % Step 3. Calling polymake
1.2 takayama 161: % acti0 cgi.body
162: /pError [ ] def
1.3 ! takayama 163: % cgi.body --> oxsVarToFile(), value is not a string object ...
1.2 takayama 164: % [(polymake) (stringInOut://cgi.body) acti0] addStdoutStderr
1.3 ! takayama 165: % It is not a bug; .body is removed from the variable name.
! 166: /cgi_body cgi.body def
! 167: [cgiPolymake.polymakeName (stringInOut://cgi_body.poly) acti0] addStdoutStderr
1.2 takayama 168: oxshell pop
169: pError [@@@stdout @@@stderr] append /pError set
1.3 ! takayama 170: /cgi.body cgi_body def
1.2 takayama 171: ] pop
172: popVariables
173: } def
174:
175: /cgi.test0 {
176: [[(URL) (hoge)]
177: [(oxMessageBody)
178: [(FACETS )
179: (POINTS) nl
180: (1 0 0) nl
181: (1 1 0) nl
182: (1 0 1) nl
183: (1 1 1) nl ] cat
184: ]] /ff set
185: [(cgiKeyValuePairToUrlEncodingString) ff] extension /ff1 set
186: ff1 message
187: ff1 cgiPolymake.0
188: cgi.body message
1.3 ! takayama 189: } def
! 190:
! 191: /cgiReplyInHttp0 {
! 192: /arg1 set
! 193: [/ss /sskv] pushVariables
! 194: [
! 195: /ss arg1 def
! 196: [[(Content-Body) ss]
! 197: % [(Content-Type) (text/plain)]
! 198: [(oxshell-stdout) @@@stdout]
! 199: [(oxshell-stderr) @@@stderr]
! 200: ] /sskv set
! 201: [(cgiKeyValuePairToHttpString) sskv] extension /arg1 set
! 202: ] pop
! 203: arg1
! 204: } def
! 205:
! 206: % ------------ client side script ---------------
! 207: /cgiQueryPolymake.hostname (polymake.math.kobe-u.ac.jp) def
! 208: /cgiQueryPolymake.cginame (cgi-bin/cgi-polymake.sh) def
! 209: /cgiQueryPolymake {
! 210: /arg2 set /arg1 set
! 211: [/saction /sfile /ff /ff1 /sss2 /goo /pp /pp2] pushVariables
! 212: [
! 213: /saction arg1 def /sfile arg2 def
! 214: % step1. Generate query in URL encoding.
! 215: [[(URL) cgiQueryPolymake.cginame]
! 216: [(oxMessageBody)
! 217: [saction ( )
! 218: sfile nl ] cat
! 219: ]] /ff set
! 220: [(cgiKeyValuePairToUrlEncodingString) ff] extension /ff1 set
! 221: [(GET ) , ff1 , ( HTTP/0.9) , nl , nl ] cat /sss2 set
! 222: % step 2. Call the server and get the response in pp
! 223: [(sm1.socket) (connect)
! 224: [80 cgiQueryPolymake.hostname]] extension 0 get /goo set
! 225: [(sm1.socket) (write) [goo sss2]] extension
! 226: goo readHTTP0 /pp set
! 227: pp message
! 228: [(sm1.socket) (close) [goo]] extension
! 229: [(cgiHttpToKeyValuePair) pp] extension /pp2 set
! 230:
! 231: % step 3. Analyze the response.
! 232: [pp2 (Content-Body) getNode
! 233: pp2 (oxshell-stdout) getNode
! 234: pp2 (oxshell-stderr) getNode
! 235: ] /arg1 set
! 236: ] pop
! 237: popVariables
! 238: arg1
! 239: } def
! 240:
! 241: % Overrides doPolymake
! 242: %/doPolymake { doPolymake.OoHG } def
! 243:
! 244: [(doPolymake.OoHG)
! 245: [(It first looks for local polymake. If there is, it calls the local polymake.)
! 246: (If not, it calls http://polymake.math.kobe-u.ac.jp/cgi-bin/cgi-polymake.sh)
! 247: (to make a computation in polymake.)
! 248: (See doPolymake for the syntax. doPolymake.OoHG may overrides doPolymake.)
! 249: ]] putUsages
! 250: /doPolymake.OoHG {
! 251: /arg1 set
! 252: [/in-doPolymake.OoHG /pAction /pData /pNative /ptree
! 253: /pResult /pError
! 254: ] pushVariables
! 255: [
! 256: arg1 0 get /pAction set
! 257: arg1 1 get /pData set
! 258: polymake.start
! 259:
! 260: /pError [ ] def
! 261: %% step 1: polymake tfb ===> polymake native data
! 262: @@@polymake.k0.ccc ( polymake=Object; ) oxexecutestring
! 263: @@@polymake.k0.ccc ( QuoteMode(1); ) oxexecutestring
! 264: @@@polymake.k0.ccc [pData ( ;)] cat oxexecutestring
! 265: @@@polymake.k0.ccc oxpopcmo /ptree set
! 266: @@@polymake.k0.ccc ( QuoteMode(0); ) oxexecutestring
! 267: [(treeToPolymake) ptree] extension /pNative set
! 268: %% step 2: calling the polymake
! 269: [(which) (polymake)] oxshell tag 0 eq
! 270: {
! 271: % cgi-polymake
! 272: (Trying web service.) message
! 273: pAction pNative cgiQueryPolymake /doPolymake.OoHG.result set
! 274:
! 275: doPolymake.OoHG.result 0 get /pNative set
! 276: pError doPolymake.OoHG.result rest append /pError set
! 277: } {
! 278: % local polymake
! 279: [(polymake) (stringInOut://pNative.poly) pAction] addStdoutStderr
! 280: oxshell pop
! 281: pError [@@@stdout @@@stderr] append /pError set
! 282: } ifelse
! 283: %% step 3: polymake native data to polymake tfb
! 284: [(polymake2tfb) (<) (stringIn://pNative) (>) (stringOut://pResult)
! 285: (2>) (stringOut://@@@stderr)] oxshell pop
! 286: pError [@@@stderr] append /pError set
! 287: %% step 4: get also tree style data.
! 288: @@@polymake.k0.ccc ( polymake=Object; ) oxexecutestring
! 289: @@@polymake.k0.ccc ( QuoteMode(1); ) oxexecutestring
! 290: @@@polymake.k0.ccc [pResult ( ;)] cat oxexecutestring
! 291: @@@polymake.k0.ccc oxpopcmo /ptree set
! 292: @@@polymake.k0.ccc ( QuoteMode(0); ) oxexecutestring
! 293: [pResult ptree pError] /arg1 set
! 294: ] pop
! 295: popVariables
! 296: arg1
! 297: } def
! 298:
! 299:
! 300:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>