[BACK]Return to cgi.sm1 CVS log [TXT][DIR] Up to [local] / OpenXM / src / kan96xx / Doc

Annotation of OpenXM/src/kan96xx/Doc/cgi.sm1, Revision 1.4

1.4     ! takayama    1: % $OpenXM: OpenXM/src/kan96xx/Doc/cgi.sm1,v 1.3 2004/09/27 01:20:46 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:
                      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
1.4     ! takayama   99:    cgi.query_string isString {
        !           100:      cgi.query_string length 0 eq  {
        !           101:        cgiPolymake.errorMessage message  exit
        !           102:      } { } ifelse
        !           103:    }
1.3       takayama  104:    {
                    105:      cgiPolymake.errorMessage message  exit
                    106:    } ifelse
1.4     ! takayama  107:    cgi.body.http0 message
1.3       takayama  108:    cgi.query_string cgiPolymake.0
                    109:    cgi.body cgiReplyInHttp0 /cgi.body.http set
                    110:    cgi.body.http message
                    111:    exit
                    112:   } loop
1.4     ! takayama  113:   [(flush)] extension
1.2       takayama  114: } def
1.3       takayama  115: /cgiPolymake.errorMessage
                    116:   [ (Content-Type: text/html) nl
                    117:     nl
                    118:     (<html><body>) nl
                    119:     (Input <br> action polymake-data <br> ) nl
                    120:     (Example: <pre> ) nl
                    121:     (FACETS  POINTS) nl
                    122:     (1 0 0 ) nl
                    123:     (1 1 0 ) nl
                    124:     (1 0 1 ) nl
                    125:     (</pre>) nl
                    126:     (<form>) nl
                    127:     (<input type=submit>) nl
1.4     ! takayama  128:     (<textarea name="oxMessageBody") nl
1.3       takayama  129:     ( rows=10 cols="80" wrap="soft"></textarea>) nl
                    130:     (</form>) nl
                    131:     (</body></html>) nl
                    132:   ] cat
                    133: def
                    134:
                    135: /cgi.body.http0
                    136:   (Content-Type: text/plain)
                    137: def
1.2       takayama  138: /cgiPolymake.0 {
                    139:   /arg1 set
                    140:   [/sss /kv /comm /i /acti0 /pError] pushVariables
                    141:   [
                    142:     /sss arg1 def
1.3       takayama  143: % Step 1.  analyze the query string in URL encoding.
1.4     ! takayama  144:     [(cgiUrlEncodingToKeyValuePair) [(?) sss] cat] extension /kv set
1.2       takayama  145:     kv (oxMessageBody) getNode /comm set
1.4     ! takayama  146: %   [nl nl] cat message sss message kv message  % for debug.
1.3       takayama  147: % Step 2. Extract action part and file part for polymake.
1.2       takayama  148:     comm (array) dc /comm set
                    149:     /i 0 def
                    150:     [
                    151:        0 1, comm length 1 sub {
                    152:          /i set
                    153:          comm,i,get 33 lt {
                    154:            exit
                    155:          } { comm,i,get } ifelse
                    156:        } for
                    157:     ] /acti0 set
                    158:     acti0 { (string) dc } map cat /acti0 set
                    159:
                    160:     /cgi.body comm def
                    161:     0 1 i {
                    162:       cgi.body rest /cgi.body set
                    163:     } for
                    164:     cgi.body { (string) dc } map cat /cgi.body set
                    165:
1.3       takayama  166: % Step 3. Calling polymake
1.2       takayama  167: % acti0  cgi.body
                    168:     /pError [ ] def
1.3       takayama  169: %  cgi.body --> oxsVarToFile(), value is not a string object ...
1.2       takayama  170: %    [(polymake) (stringInOut://cgi.body) acti0] addStdoutStderr
1.3       takayama  171: %  It is not a bug; .body is removed from the variable name.
                    172:     /cgi_body cgi.body def
                    173:     [cgiPolymake.polymakeName (stringInOut://cgi_body.poly) acti0] addStdoutStderr
1.2       takayama  174:     oxshell pop
                    175:     pError [@@@stdout @@@stderr] append /pError set
1.4     ! takayama  176:     /cgi.body cgi_body def
1.2       takayama  177:   ] pop
                    178:   popVariables
                    179: } def
                    180:
                    181: /cgi.test0 {
                    182:   [[(URL) (hoge)]
                    183:    [(oxMessageBody)
                    184:    [(FACETS )
                    185:     (POINTS) nl
                    186:     (1 0 0) nl
                    187:     (1 1 0) nl
                    188:     (1 0 1) nl
                    189:     (1 1 1) nl ] cat
                    190:    ]] /ff set
                    191:    [(cgiKeyValuePairToUrlEncodingString) ff] extension /ff1 set
                    192:    ff1 message
                    193:    ff1 cgiPolymake.0
                    194:    cgi.body message
1.3       takayama  195: } def
                    196:
                    197: /cgiReplyInHttp0 {
                    198:   /arg1 set
                    199:   [/ss /sskv] pushVariables
                    200:   [
                    201:     /ss arg1 def
                    202:     [[(Content-Body) ss]
                    203: %     [(Content-Type) (text/plain)]
1.4     ! takayama  204:      [(oxshell-stdout) [(cgiStringToUrlEncoding) @@@stdout] extension]
        !           205:      [(oxshell-stderr) [(cgiStringToUrlEncoding) @@@stderr] extension]
1.3       takayama  206:     ] /sskv set
                    207:     [(cgiKeyValuePairToHttpString) sskv] extension /arg1 set
                    208:   ] pop
                    209:   arg1
                    210: } def
                    211:
                    212: % ------------  client side script ---------------
                    213: /cgiQueryPolymake.hostname (polymake.math.kobe-u.ac.jp) def
1.4     ! takayama  214: /cgiQueryPolymake.cginame (/cgi-bin/cgi-polymake.sh) def
1.3       takayama  215: /cgiQueryPolymake {
                    216:   /arg2 set /arg1 set
                    217:   [/saction /sfile /ff /ff1 /sss2 /goo /pp /pp2] pushVariables
                    218:   [
                    219:     /saction arg1 def /sfile arg2 def
                    220: % step1. Generate query in URL encoding.
                    221:     [[(URL) cgiQueryPolymake.cginame]
                    222:      [(oxMessageBody)
                    223:      [saction ( )
                    224:       sfile nl ] cat
                    225:      ]] /ff set
                    226:      [(cgiKeyValuePairToUrlEncodingString) ff] extension /ff1 set
                    227:      [(GET ) , ff1 , ( HTTP/0.9) , nl , nl ] cat /sss2 set
                    228: % step 2. Call the server and get the response in pp
                    229:      [(sm1.socket) (connect)
                    230:         [80 cgiQueryPolymake.hostname]] extension 0 get /goo set
                    231:      [(sm1.socket) (write) [goo sss2]]  extension
                    232:      goo readHTTP0 /pp set
                    233:      pp message
                    234:      [(sm1.socket) (close) [goo]] extension
                    235:      [(cgiHttpToKeyValuePair) pp] extension /pp2 set
                    236:
                    237: % step 3. Analyze the response.
                    238:      [pp2 (Content-Body) getNode
                    239:       pp2 (oxshell-stdout) getNode
                    240:       pp2 (oxshell-stderr) getNode
                    241:      ] /arg1 set
                    242:   ] pop
                    243:   popVariables
                    244:   arg1
                    245: } def
                    246:
                    247: % Overrides doPolymake
                    248: %/doPolymake { doPolymake.OoHG } def
                    249:
                    250: [(doPolymake.OoHG)
                    251: [(It first looks for local polymake. If there is, it calls the local polymake.)
                    252:  (If not, it calls http://polymake.math.kobe-u.ac.jp/cgi-bin/cgi-polymake.sh)
                    253:  (to make a computation in polymake.)
                    254:  (See doPolymake for the syntax. doPolymake.OoHG may overrides doPolymake.)
                    255: ]] putUsages
                    256: /doPolymake.OoHG  {
                    257:   /arg1 set
                    258:   [/in-doPolymake.OoHG  /pAction /pData /pNative /ptree
                    259:    /pResult  /pError
                    260:   ] pushVariables
                    261:   [
                    262:     arg1 0 get /pAction set
                    263:     arg1 1 get /pData set
                    264:     polymake.start
                    265:
                    266:     /pError [ ] def
                    267: %% step 1:  polymake tfb ===> polymake native data
                    268:     @@@polymake.k0.ccc ( polymake=Object; ) oxexecutestring
                    269:     @@@polymake.k0.ccc ( QuoteMode(1); ) oxexecutestring
                    270:     @@@polymake.k0.ccc [pData ( ;)] cat oxexecutestring
                    271:     @@@polymake.k0.ccc oxpopcmo /ptree set
                    272:     @@@polymake.k0.ccc ( QuoteMode(0); ) oxexecutestring
                    273:     [(treeToPolymake) ptree] extension /pNative set
                    274: %% step 2: calling the polymake
1.4     ! takayama  275: %    [(which) (polymake)] oxshell tag 0 eq
        !           276:     1  % always call web service
1.3       takayama  277:     {
                    278: % cgi-polymake
                    279:        (Trying web service.) message
                    280:        pAction pNative  cgiQueryPolymake /doPolymake.OoHG.result set
                    281:
                    282:       doPolymake.OoHG.result 0 get /pNative set
                    283:       pError doPolymake.OoHG.result rest append /pError set
                    284:     } {
                    285: % local polymake
                    286:       [(polymake) (stringInOut://pNative.poly) pAction] addStdoutStderr
                    287:       oxshell pop
                    288:       pError [@@@stdout @@@stderr] append /pError set
                    289:     } ifelse
                    290: %% step 3: polymake native data to polymake tfb
                    291:     [(polymake2tfb) (<) (stringIn://pNative) (>) (stringOut://pResult)
                    292:      (2>) (stringOut://@@@stderr)] oxshell pop
                    293:     pError [@@@stderr] append /pError set
                    294: %% step 4: get also tree style data.
                    295:     @@@polymake.k0.ccc ( polymake=Object; ) oxexecutestring
                    296:     @@@polymake.k0.ccc ( QuoteMode(1); ) oxexecutestring
                    297:     @@@polymake.k0.ccc [pResult ( ;)] cat oxexecutestring
                    298:     @@@polymake.k0.ccc oxpopcmo /ptree set
                    299:     @@@polymake.k0.ccc ( QuoteMode(0); ) oxexecutestring
                    300:     [pResult ptree pError] /arg1 set
                    301:   ] pop
                    302:   popVariables
                    303:   arg1
                    304: } def
                    305:
                    306:
                    307:

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>