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

1.7     ! takayama    1: % $OpenXM: OpenXM/src/kan96xx/Doc/cgi.sm1,v 1.6 2004/09/28 12:20:39 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.5       takayama  236:      cgi.verbose { pp message } {  } ifelse
1.3       takayama  237:      [(sm1.socket) (close) [goo]] extension
                    238:      [(cgiHttpToKeyValuePair) pp] extension /pp2 set
                    239:
                    240: % step 3. Analyze the response.
                    241:      [pp2 (Content-Body) getNode
                    242:       pp2 (oxshell-stdout) getNode
                    243:       pp2 (oxshell-stderr) getNode
                    244:      ] /arg1 set
                    245:   ] pop
                    246:   popVariables
                    247:   arg1
                    248: } def
                    249:
                    250: % Overrides doPolymake
                    251: %/doPolymake { doPolymake.OoHG } def
1.7     ! takayama  252: %/polymake.start { polymake.start.OoHG } def
1.3       takayama  253:
                    254: [(doPolymake.OoHG)
                    255: [(It first looks for local polymake. If there is, it calls the local polymake.)
                    256:  (If not, it calls http://polymake.math.kobe-u.ac.jp/cgi-bin/cgi-polymake.sh)
                    257:  (to make a computation in polymake.)
                    258:  (See doPolymake for the syntax. doPolymake.OoHG may overrides doPolymake.)
                    259: ]] putUsages
                    260: /doPolymake.OoHG  {
                    261:   /arg1 set
                    262:   [/in-doPolymake.OoHG  /pAction /pData /pNative /ptree
                    263:    /pResult  /pError
                    264:   ] pushVariables
                    265:   [
                    266:     arg1 0 get /pAction set
                    267:     arg1 1 get /pData set
                    268:     polymake.start
                    269:
                    270:     /pError [ ] def
                    271: %% step 1:  polymake tfb ===> polymake native data
                    272:     @@@polymake.k0.ccc ( polymake=Object; ) oxexecutestring
                    273:     @@@polymake.k0.ccc ( QuoteMode(1); ) oxexecutestring
                    274:     @@@polymake.k0.ccc [pData ( ;)] cat oxexecutestring
                    275:     @@@polymake.k0.ccc oxpopcmo /ptree set
                    276:     @@@polymake.k0.ccc ( QuoteMode(0); ) oxexecutestring
                    277:     [(treeToPolymake) ptree] extension /pNative set
                    278: %% step 2: calling the polymake
1.4       takayama  279: %    [(which) (polymake)] oxshell tag 0 eq
                    280:     1  % always call web service
1.3       takayama  281:     {
                    282: % cgi-polymake
                    283:        (Trying web service.) message
                    284:        pAction pNative  cgiQueryPolymake /doPolymake.OoHG.result set
                    285:
1.7     ! takayama  286:       [(regexec) (Not Found) [doPolymake.OoHG.result 0 get ]]
        !           287:       extension length 0 eq not {
        !           288:          doPolymake.OoHG.result message
        !           289:          (The polymake cgi seems to be out of service.) error
        !           290:       } {  } ifelse
        !           291:
1.3       takayama  292:       doPolymake.OoHG.result 0 get /pNative set
1.7     ! takayama  293:
        !           294:
1.3       takayama  295:       pError doPolymake.OoHG.result rest append /pError set
                    296:     } {
                    297: % local polymake
                    298:       [(polymake) (stringInOut://pNative.poly) pAction] addStdoutStderr
                    299:       oxshell pop
                    300:       pError [@@@stdout @@@stderr] append /pError set
                    301:     } ifelse
                    302: %% step 3: polymake native data to polymake tfb
                    303:     [(polymake2tfb) (<) (stringIn://pNative) (>) (stringOut://pResult)
                    304:      (2>) (stringOut://@@@stderr)] oxshell pop
                    305:     pError [@@@stderr] append /pError set
                    306: %% step 4: get also tree style data.
                    307:     @@@polymake.k0.ccc ( polymake=Object; ) oxexecutestring
                    308:     @@@polymake.k0.ccc ( QuoteMode(1); ) oxexecutestring
                    309:     @@@polymake.k0.ccc [pResult ( ;)] cat oxexecutestring
                    310:     @@@polymake.k0.ccc oxpopcmo /ptree set
                    311:     @@@polymake.k0.ccc ( QuoteMode(0); ) oxexecutestring
                    312:     [pResult ptree pError] /arg1 set
                    313:   ] pop
                    314:   popVariables
                    315:   arg1
1.7     ! takayama  316: } def
        !           317:
        !           318: /polymake.start.OoHG {
        !           319:   (ox.k0.loaded) boundp {  }
        !           320:   { [(parse) (ox.sm1) pushfile] extension
        !           321:     /@@@polymake.k0.ccc [ ] def
        !           322:   } ifelse
        !           323:   @@@polymake.k0.ccc [ ] eq {
        !           324:      k0connectr /@@@polymake.k0.ccc oxk0.ccc def /oxk0.ccc [ ] def
        !           325:      @@@polymake.k0.ccc oxsetmathcap @@@polymake.k0.ccc oxmathcap
        !           326:   } {  } ifelse
        !           327:   @@@polymake.k0.ccc 0 get (closed) eq {
        !           328:      k0connectr /@@@polymake.k0.ccc oxk0.ccc def /oxk0.ccc [ ] def
        !           329:      @@@polymake.k0.ccc oxsetmathcap @@@polymake.k0.ccc oxmathcap
        !           330:   } {  } ifelse
        !           331:   [(which) (polymake2tfb)] oxshell tag 0 eq {
        !           332:      (polymake2tfb is not installed in this system.) error
        !           333:   } {  } ifelse
1.3       takayama  334: } def
                    335:
                    336:
                    337:

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