[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.5

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

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