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

1.8     ! takayama    1: % $OpenXM: OpenXM/src/kan96xx/Doc/cgi.sm1,v 1.7 2004/10/13 12:11:31 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.6       takayama  146:     [(cgiUrlEncodingToKeyValuePair) sss] 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.
1.7       takayama  223:     [[(URL) [(http://) cgiQueryPolymake.hostname cgiQueryPolymake.cginame] cat]
                    224: % full URL necessary for virtual host of apache.
1.3       takayama  225:      [(oxMessageBody)
                    226:      [saction ( )
                    227:       sfile nl ] cat
                    228:      ]] /ff set
                    229:      [(cgiKeyValuePairToUrlEncodingString) ff] extension /ff1 set
                    230:      [(GET ) , ff1 , ( HTTP/0.9) , nl , nl ] cat /sss2 set
                    231: % step 2. Call the server and get the response in pp
                    232:      [(sm1.socket) (connect)
                    233:         [80 cgiQueryPolymake.hostname]] extension 0 get /goo set
                    234:      [(sm1.socket) (write) [goo sss2]]  extension
                    235:      goo readHTTP0 /pp set
1.8     ! takayama  236:
        !           237:      % For the case of error.
        !           238:      [(regexec) (200 OK) [pp]] extension length 0 eq {
        !           239:         (sss2=) message
        !           240:         sss2 message
        !           241:         (pp=) message
        !           242:         pp message
        !           243:         (cgi.sm1: there seems to be an error in the HTTP connection.)
        !           244:      } { } ifelse
        !           245:
1.5       takayama  246:      cgi.verbose { pp message } {  } ifelse
1.3       takayama  247:      [(sm1.socket) (close) [goo]] extension
                    248:      [(cgiHttpToKeyValuePair) pp] extension /pp2 set
                    249:
                    250: % step 3. Analyze the response.
                    251:      [pp2 (Content-Body) getNode
                    252:       pp2 (oxshell-stdout) getNode
                    253:       pp2 (oxshell-stderr) getNode
                    254:      ] /arg1 set
                    255:   ] pop
                    256:   popVariables
                    257:   arg1
                    258: } def
                    259:
                    260: % Overrides doPolymake
                    261: %/doPolymake { doPolymake.OoHG } def
1.7       takayama  262: %/polymake.start { polymake.start.OoHG } def
1.3       takayama  263:
                    264: [(doPolymake.OoHG)
                    265: [(It first looks for local polymake. If there is, it calls the local polymake.)
                    266:  (If not, it calls http://polymake.math.kobe-u.ac.jp/cgi-bin/cgi-polymake.sh)
                    267:  (to make a computation in polymake.)
                    268:  (See doPolymake for the syntax. doPolymake.OoHG may overrides doPolymake.)
                    269: ]] putUsages
                    270: /doPolymake.OoHG  {
                    271:   /arg1 set
                    272:   [/in-doPolymake.OoHG  /pAction /pData /pNative /ptree
                    273:    /pResult  /pError
                    274:   ] pushVariables
                    275:   [
                    276:     arg1 0 get /pAction set
                    277:     arg1 1 get /pData set
                    278:     polymake.start
                    279:
                    280:     /pError [ ] def
                    281: %% step 1:  polymake tfb ===> polymake native data
                    282:     @@@polymake.k0.ccc ( polymake=Object; ) oxexecutestring
                    283:     @@@polymake.k0.ccc ( QuoteMode(1); ) oxexecutestring
                    284:     @@@polymake.k0.ccc [pData ( ;)] cat oxexecutestring
                    285:     @@@polymake.k0.ccc oxpopcmo /ptree set
                    286:     @@@polymake.k0.ccc ( QuoteMode(0); ) oxexecutestring
                    287:     [(treeToPolymake) ptree] extension /pNative set
                    288: %% step 2: calling the polymake
1.4       takayama  289: %    [(which) (polymake)] oxshell tag 0 eq
                    290:     1  % always call web service
1.3       takayama  291:     {
                    292: % cgi-polymake
                    293:        (Trying web service.) message
                    294:        pAction pNative  cgiQueryPolymake /doPolymake.OoHG.result set
                    295:
1.7       takayama  296:       [(regexec) (Not Found) [doPolymake.OoHG.result 0 get ]]
                    297:       extension length 0 eq not {
                    298:          doPolymake.OoHG.result message
                    299:          (The polymake cgi seems to be out of service.) error
                    300:       } {  } ifelse
                    301:
1.3       takayama  302:       doPolymake.OoHG.result 0 get /pNative set
1.7       takayama  303:
                    304:
1.3       takayama  305:       pError doPolymake.OoHG.result rest append /pError set
                    306:     } {
                    307: % local polymake
                    308:       [(polymake) (stringInOut://pNative.poly) pAction] addStdoutStderr
                    309:       oxshell pop
                    310:       pError [@@@stdout @@@stderr] append /pError set
                    311:     } ifelse
                    312: %% step 3: polymake native data to polymake tfb
                    313:     [(polymake2tfb) (<) (stringIn://pNative) (>) (stringOut://pResult)
                    314:      (2>) (stringOut://@@@stderr)] oxshell pop
                    315:     pError [@@@stderr] append /pError set
                    316: %% step 4: get also tree style data.
                    317:     @@@polymake.k0.ccc ( polymake=Object; ) oxexecutestring
                    318:     @@@polymake.k0.ccc ( QuoteMode(1); ) oxexecutestring
                    319:     @@@polymake.k0.ccc [pResult ( ;)] cat oxexecutestring
                    320:     @@@polymake.k0.ccc oxpopcmo /ptree set
                    321:     @@@polymake.k0.ccc ( QuoteMode(0); ) oxexecutestring
                    322:     [pResult ptree pError] /arg1 set
                    323:   ] pop
                    324:   popVariables
                    325:   arg1
1.7       takayama  326: } def
                    327:
                    328: /polymake.start.OoHG {
                    329:   (ox.k0.loaded) boundp {  }
                    330:   { [(parse) (ox.sm1) pushfile] extension
                    331:     /@@@polymake.k0.ccc [ ] def
                    332:   } ifelse
                    333:   @@@polymake.k0.ccc [ ] eq {
                    334:      k0connectr /@@@polymake.k0.ccc oxk0.ccc def /oxk0.ccc [ ] def
                    335:      @@@polymake.k0.ccc oxsetmathcap @@@polymake.k0.ccc oxmathcap
                    336:   } {  } ifelse
                    337:   @@@polymake.k0.ccc 0 get (closed) eq {
                    338:      k0connectr /@@@polymake.k0.ccc oxk0.ccc def /oxk0.ccc [ ] def
                    339:      @@@polymake.k0.ccc oxsetmathcap @@@polymake.k0.ccc oxmathcap
                    340:   } {  } ifelse
                    341:   [(which) (polymake2tfb)] oxshell tag 0 eq {
                    342:      (polymake2tfb is not installed in this system.) error
                    343:   } {  } ifelse
1.3       takayama  344: } def
                    345:
                    346:
                    347:

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