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

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

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