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

Annotation of OpenXM/src/kan96xx/Doc/httpd-rpc.sm1, Revision 1.17

1.17    ! takayama    1: %% $OpenXM: OpenXM/src/kan96xx/Doc/httpd-rpc.sm1,v 1.16 2012/12/23 02:33:16 takayama Exp $
1.1       takayama    2: %% http server by sm1. Used for RPC.  Generic example.
                      3:
                      4: [(parse) (httpd.sm1) pushfile] extension pop
1.17    ! takayama    5: /httpd.textmode 1 def  %% It does not work on some browsers.
1.12      takayama    6:
                      7: /httpd_action {
                      8:    httpd_action_allow_file_transfer
                      9:         %% If OXWEB_HTTPD_ROOT is set, file transfer will be allowed.
                     10:         %% Example:  export OXWEB_HTTPD_ROOT=/usr/local/www/data/
                     11:         %% Do not forget the last /
                     12: %  httpd_action_rpc_only    %% old
                     13: } def
1.10      takayama   14: %% keyword for rpc
1.1       takayama   15: /httpd.textarea.name (rpc) def
                     16: /httpd.textarea.name.aaa
                     17:   [(GET /?) httpd.textarea.name] cat (array) dc
                     18: def
1.10      takayama   19:
                     20: [(getenv) (OXWEB_PORT)] extension tag 0 eq {
                     21:   (Default httpd port : ) messagen
                     22:   /httpd.port 8090 def
                     23: } {
                     24:   /httpd.port [(getenv) (OXWEB_PORT)] extension .. (integer) dc def
                     25: } ifelse
                     26: (httpd.port = ) messagen httpd.port message
                     27:
1.1       takayama   28: /httpd.serial 0 def
                     29:
1.7       takayama   30: /httpd.oxasir.timer 1 def  %% 1 to use timer, but it accepts only one command
                     31:                            %% 0 not to use timer. It accepts programs.
                     32:
1.11      takayama   33: /httpd.title
                     34:   (<H1><font color="green">OpenXM/Risa/Asir Online</font></H2><br>)
                     35: def
1.2       takayama   36: %%******* Put initialization codes for ox_asir here.
                     37: /httpd.initialization
1.8       takayama   38:  [(if(1){)
1.2       takayama   39:   (XM_debug=0; ctrl("debug_window",0); Xm_noX=1;)
                     40:   ("Asirweb version 0.80. "+
                     41:    " Risa/Asir oxasir version "+rtostr(version());)
1.8       takayama   42:   (};)
1.2       takayama   43:  ] cat
                     44: def
1.1       takayama   45:
1.7       takayama   46: /httpd.asirman
1.14      ohara      47:  ("http://www.math.sci.kobe-u.ac.jp/OpenXM/1.2.1/doc/asir2000/html-en/man_toc.html")
1.7       takayama   48: def
                     49: /httpd.asirman.ja
1.14      ohara      50:  ("http://www.math.sci.kobe-u.ac.jp/OpenXM/1.2.1/doc/asir2000/html-ja/man_toc.html")
1.7       takayama   51: def
1.1       takayama   52:
                     53: [(parse) (oxasir.sm1) pushfile] extension
                     54: oxNoX
                     55: (oxasir.started) boundp {
                     56: } {
                     57:   %% Initialize oxasir.
                     58:   [(x^2-1) (x)] fctr pop
                     59:   oxasir.ccc oxmathcap
                     60:   oxasir.ccc oxsetmathcap
1.2       takayama   61:   oxasir.ccc httpd.initialization oxexecutestring ;
                     62:   (Initialization returns ...: ) messagen
                     63:   oxasir.ccc oxpopcmo message ;
1.1       takayama   64: } ifelse
                     65:
                     66: /webrpc  {
                     67:  [/rrr ] pushVariables
                     68:  [
                     69:   [(oxGenPass)] extension . (integer) dc  /rrr set
                     70:
                     71:   [(ostype)] extension 0 get
                     72:    (windows) eq {
                     73:      %% On windows.
                     74:       [(forkExec)
                     75:        [
                     76:          ox.win.start.0 aload pop
                     77:          (iexplore)   %% Starting internet explorer (TM).
1.3       takayama   78:          [(http://) [(sm1.socket) (gethostname) []] extension
                     79:           (:) httpd.port toString] cat
1.1       takayama   80:        ]
                     81:        [  ]
                     82:       3] extension
                     83:    }{
                     84:       %% On unix.
1.16      takayama   85:       [(ostype)] extension 1 get (mac) eq {
                     86:         /webrpc.browser (open) def
                     87:       } { /webrpc.browser (firefox) def } ifelse
                     88:       [(sleep 3 ; ) webrpc.browser ( http://)
1.3       takayama   89:                  [(sm1.socket) (gethostname) []] extension
                     90:                 (:) httpd.port toString ( & ) ] cat
1.1       takayama   91:       system
                     92:    } ifelse
                     93:
                     94:    httpd ;
                     95:   ] pop
                     96:   popVariables
                     97: } def
                     98:
                     99:
1.12      takayama  100: /httpd_action_rpc_only {
1.1       takayama  101:   [/in-httpd /ff /httpd.com /httpd.result /sss
                    102:    /sss.engine /sss.web /err
1.4       takayama  103:    /oxserver.vname /scheck
1.1       takayama  104:   ] pushVariables
                    105:   [
                    106:   {
                    107:        [(sm1.socket) (select) [httpd.server.fd 0 get -1]] extension
                    108:        %%     wait for ever
                    109:         [(sm1.socket) (readHTTP) [httpd.server.fd 0 get ]] extension /ff set
1.2       takayama  110:         ff tag 0 eq {
                    111:            (connection is closed.) message exit
1.1       takayama  112:         }
                    113:         {
                    114:           (------------  start ----------------------) message
                    115:              ff message
                    116:           (-----------------------------------------) message
                    117:              ff removeGET webstringToAscii /httpd.com set
                    118:              [(httpd.com=) httpd.com] cat message
                    119:           (------------  end ----------------------) message
                    120:           (   ) message
1.2       takayama  121:
1.1       takayama  122:           httpd.com metaCommand {
                    123:             httpd.textarea.valid {
                    124:
1.4       takayama  125:               %%% Security check
                    126:               [(regionMatches) httpd.com
                    127:                httpd.refusedCommands] extension /scheck set
                    128:               scheck 0 get -1 eq {
                    129:               }{
                    130:                 httpd.refusedCommands scheck 2 get get message
                    131:                 (Command is refused.) message
                    132:                 [
                    133:                  httpd.refusedCommands scheck 2 get get
                    134:                  httpd.com
                    135:                 ]
                    136:                  send-page-refused exit
                    137:               } ifelse
                    138:
1.1       takayama  139:               %%%  Executing command, here.
                    140:               oxasir.ccc
                    141:               httpd.com cookedCommand
                    142:               oxexecutestring ;
                    143:
                    144:             }{
                    145:               send-page-usage  exit
                    146:             } ifelse
                    147:
                    148:             [(oxReq) oxasir.ccc SM_dupErrors ] extension pop
                    149:
                    150:             [(oxReq) oxasir.ccc SM_popCMO ] extension pop
                    151:             [(oxReq) oxasir.ccc SM_popString ] extension pop
                    152:
                    153:             [(flush)] extension pop
                    154:             %% Select inputs for interruption.
                    155:             %% Wait by the spin lock.
                    156:             {
                    157:               [(oxMultiSelect) [oxasir.ccc] 1] extension 1 get 0 get
                    158:               /sss.engine set
                    159:               [(sm1.socket) (mselect)
                    160:                 [[httpd.server.fd 0 get] 1]
                    161:               ] extension 0 get /sss.web set
                    162:               /sss [sss.engine sss.web] def
                    163:               sss.engine { exit } { } ifelse
                    164:               sss.web    { exit } { } ifelse
                    165:             } loop
                    166:             sss message
                    167:
                    168:             sss 0 get {
                    169:                 [(oxGet) oxasir.ccc] extension  /err          set
                    170:                 [(oxGet) oxasir.ccc] extension  /httpd.result set
                    171:             } {
                    172:                 oxasir.ccc oxreset
                    173:                 oxasir.ccc ("computation is interrupted.";) oxexecutestring ;
                    174:                 oxasir.ccc oxpopstring
                    175:                 /httpd.result set
                    176:                 exit
                    177:             } ifelse
                    178:             (------------- result -------------) message
                    179:             httpd.result message
                    180:             (----------------------------------) message
                    181:             (  ) message
1.2       takayama  182:
                    183:             (----------- error -------------) message
1.1       takayama  184:             err message
1.2       takayama  185:             (-------------------------------) message
1.1       takayama  186:             err [ ] eq  {
                    187:             } {
                    188:               oxasir.ccc cleanErrors
                    189:               [httpd.result 10 (string) dc err toString] cat
                    190:               /httpd.result set
                    191:             } ifelse
                    192:
                    193:             httpd.result send-page-result  exit  %% exit the loop LOOP-A
                    194:           } { exit } ifelse  %% metaCommand
                    195:         } ifelse
                    196:   } loop  %% LOOP-A
                    197:   ] pop
                    198:   popVariables
                    199: } def
                    200:
                    201:
1.12      takayama  202: /httpd_action_allow_file_transfer {
                    203:   [/in-httpd /ff /httpd.com /httpd.result /sss
                    204:    /sss.engine /sss.web /err
                    205:    /oxserver.vname /scheck /ff2
                    206:   ] pushVariables
                    207:   [
                    208:    (httpd_action_allow_file_transfer: ) message
                    209:   {
                    210:        [(sm1.socket) (select) [httpd.server.fd 0 get -1]] extension
                    211:        %%     wait for ever
                    212:         [(sm1.socket) (readHTTP) [httpd.server.fd 0 get ]] extension /ff set
                    213:         ff tag 0 eq {
                    214:            (connection is closed.) message exit
                    215:         }
                    216:         {
                    217:           (------------  start ----------------------) message
                    218:              ff message
                    219:           (-----------------------------------------) message
                    220:              ff httpd.parse /ff2 set
                    221:              ff2 message
                    222:              /httpd.textarea.valid 0 def /httpd.com ( ) def
                    223:              ff2 0 get (GET-file) eq {
                    224:                 ff2 length 1 eq httpd.root tag 0 eq {
                    225:                   send-page-usage
                    226:                   exit
                    227:                 } {  } ifelse
                    228:                 ff2 httpd_sendfile exit
                    229:              } {  } ifelse
                    230:              ff2 0 get (GET) eq ff2 0 get (POST) eq or {
                    231:               ff2 1 get tag 6 eq
                    232:               ff2 1 get length 2 eq and
                    233:               {  % list
                    234:                 ff2 1 get 0 get httpd.textarea.name eq {
                    235:                   /httpd.textarea.valid 1 def
                    236:                   /httpd.com ff2 1 get 1 get def
                    237:                 }{ } ifelse
                    238:                 ff2 1 get 0 get (msg) eq { %% meta command
                    239:                   /httpd.textarea.valid 0 def
                    240:                   /httpd.com ff2 1 get 1 get def
                    241:                 }{ } ifelse
                    242:               } { } ifelse
                    243:              }{  } ifelse
                    244:              %% ff removeGET webstringToAscii /httpd.com set
                    245:              [(httpd.com=) httpd.com] cat message
                    246:           (------------  end ----------------------) message
                    247:           (   ) message
                    248:
                    249:           httpd.com metaCommand {
                    250:             httpd.textarea.valid {
                    251:
                    252:               %%% Security check
                    253:               [(regionMatches) httpd.com
                    254:                httpd.refusedCommands] extension /scheck set
                    255:               scheck 0 get -1 eq {
                    256:               }{
                    257:                 httpd.refusedCommands scheck 2 get get message
                    258:                 (Command is refused.) message
                    259:                 [
                    260:                  httpd.refusedCommands scheck 2 get get
                    261:                  httpd.com
                    262:                 ]
                    263:                  send-page-refused exit
                    264:               } ifelse
                    265:
                    266:               %%%  Executing command, here.
                    267:               oxasir.ccc
                    268:               httpd.com cookedCommand
                    269:               oxexecutestring ;
                    270:
                    271:             }{
                    272:               send-page-usage  exit
                    273:             } ifelse
                    274:
                    275:             [(oxReq) oxasir.ccc SM_dupErrors ] extension pop
                    276:
                    277:             [(oxReq) oxasir.ccc SM_popCMO ] extension pop
                    278:             [(oxReq) oxasir.ccc SM_popString ] extension pop
                    279:
                    280:             [(flush)] extension pop
                    281:             %% Select inputs for interruption.
                    282:             %% Wait by the spin lock.
                    283:             {
                    284:               [(oxMultiSelect) [oxasir.ccc] 1] extension 1 get 0 get
                    285:               /sss.engine set
                    286:               [(sm1.socket) (mselect)
                    287:                 [[httpd.server.fd 0 get] 1]
                    288:               ] extension 0 get /sss.web set
                    289:               /sss [sss.engine sss.web] def
                    290:               sss.engine { exit } { } ifelse
                    291:               sss.web    { exit } { } ifelse
                    292:             } loop
                    293:             sss message
                    294:
                    295:             sss 0 get {
                    296:                 [(oxGet) oxasir.ccc] extension  /err          set
                    297:                 [(oxGet) oxasir.ccc] extension  /httpd.result set
                    298:             } {
                    299:                 oxasir.ccc oxreset
                    300:                 oxasir.ccc ("computation is interrupted.";) oxexecutestring ;
                    301:                 oxasir.ccc oxpopstring
                    302:                 /httpd.result set
                    303:                 exit
                    304:             } ifelse
                    305:             (------------- result -------------) message
                    306:             httpd.result message
                    307:             (----------------------------------) message
                    308:             (  ) message
                    309:
                    310:             (----------- error -------------) message
                    311:             err message
                    312:             (-------------------------------) message
                    313:             err [ ] eq  {
                    314:             } {
                    315:               oxasir.ccc cleanErrors
                    316:               [httpd.result 10 (string) dc err toString] cat
                    317:               /httpd.result set
                    318:             } ifelse
                    319:
                    320:             httpd.result send-page-result  exit  %% exit the loop LOOP-A
                    321:           } { exit } ifelse  %% metaCommand
                    322:         } ifelse
                    323:   } loop  %% LOOP-A
                    324:   ] pop
                    325:   popVariables
                    326: } def
1.1       takayama  327:
                    328:
                    329: /metaCommand {
                    330:   /arg1 set
                    331:   [/in-metaCommand /msg /result /msg2 /nn
                    332:    /err /fn
                    333:   ] pushVariables
                    334:   [
                    335:     /msg arg1 def
                    336:     /result 1 def
                    337:     msg (httpdAsirMeta quit) eq {
                    338:        oxasir.ccc oxshutdown
                    339:        send-page-bye
                    340:        quit
                    341:        /result 0 def
                    342:     } { } ifelse
                    343:     msg (httpdAsirMeta interrupt) eq {
                    344:        oxasir.ccc oxreset
                    345:        (Interrupted! <br>) send-page-3
                    346:        /result 0 def
                    347:     } { } ifelse
                    348:     /arg1 result def
                    349:   ] pop
                    350:   popVariables
                    351:   arg1
                    352: } def
                    353:
1.11      takayama  354:  [
                    355:   (Input example---factorization:<font color="blue"> fctr(x^3-1)</font>)
                    356:   (<br>)
                    357:   [(<font color="blue">
                    358:     <a href=) httpd.asirman ( > AsirManual (En) </a> </font>, )] cat
                    359:   [(<font color="blue">
                    360:     <a href=) httpd.asirman.ja ( > AsirManual (Ja) </a> </font>)] cat
                    361:    httpd.oxasir.timer {
                    362:     (<br> It accepts only one command. The limit of CPU time is 30 seconds.
                    363:           <font color="red"> Do not put <font color="blue"> ; </font>
                    364:                              (semi-colon) after the command.
                    365:           </font>)
                    366:    }{ } ifelse
                    367:    (<br>)
                    368:   [(You can also retrieve the result by GET /?) httpd.textarea.name (=) ( encoded_codes  HTTP/1.0)]
                    369:    (<br>)
                    370:  ] cat /httpd.msg1 set
                    371:
1.1       takayama  372: /send-page-usage {
                    373:   [/in-send-page-usage ] pushVariables
                    374:   [
                    375:    (HTTP/0.9 200 OK) sendln
                    376:    (Connection: close) sendln
                    377:    (Content-Type: text/html) sendln
                    378:    0 sendln
                    379:
1.11      takayama  380:   httpd.title sendln
1.1       takayama  381:   [(getenv) (OXWEB_POST)] extension tag 0 eq {
                    382:     (<FORM NAME="myFORM">) sendln  % use get
                    383:   }{
                    384:     (<FORM NAME="myFORM" METHOD="POST">) sendln
                    385:   } ifelse
                    386:    (<INPUT TYPE=submit VALUE="submit">) sendln
                    387:    [(<textarea name=) httpd.textarea.name
1.8       takayama  388:     ( rows=7 cols="80" wrap="soft"></textarea>)] cat sendln
1.1       takayama  389:    (</FORM>) sendln
1.7       takayama  390:
1.11      takayama  391:    httpd.msg1 sendln
                    392:
1.9       takayama  393:    [$<a href="http://$ [(sm1.socket) (gethostname) []] extension
                    394:     $:$ httpd.port toString
1.7       takayama  395:     $/?msg=httpdAsirMeta+quit"> Shutdown the oxserver (and restart it). </a>$
1.1       takayama  396:    ] cat sendln
1.7       takayama  397:    (<h3 align="right"><font color="gree">Powered by OpenXM and httpd on Kan/sm1 </font> </h3>_) sendln
1.1       takayama  398:    0 sendln
                    399:    [(flush)] extension
                    400:   ] pop
                    401:   popVariables
                    402: } def
                    403:
                    404:
                    405:
                    406: /send-page-result {
                    407:   /arg1 set
1.17    ! takayama  408:   [/in-send-page-result /result /hlen /msg1 /msg2] pushVariables
1.1       takayama  409:   [
                    410:    /result arg1 def
1.17    ! takayama  411:    /msg1 (<html> <body> <pre>)  def
        !           412:    /msg2  (</pre> </body> </html>) def
        !           413:    httpd.textmode { 0 /hlen set }
        !           414:    { msg1 length msg2 length add 3 add /hlen set } ifelse
1.1       takayama  415:    (HTTP/0.9 200 OK) sendln
                    416:    (Connection: close) sendln
1.17    ! takayama  417:    [(Content-length: ) result length hlen add toString ] cat sendln
        !           418:    httpd.textmode
        !           419:    { (Content-Type: text/plain) sendln }  % It does not work on some browsers.
        !           420:    { (Content-Type: text/html) sendln } ifelse
1.1       takayama  421:    0 sendln
                    422:
1.17    ! takayama  423:    httpd.textmode { } { msg1 sendln } ifelse
1.1       takayama  424:    result sendln
1.17    ! takayama  425:    httpd.textmode { } { msg2 sendln } ifelse
1.1       takayama  426:    0 sendln
                    427:    [(flush)] extension
                    428:   ] pop
                    429:   popVariables
                    430: } def
                    431:
1.4       takayama  432: /send-page-refused {
                    433:   /arg1 set
                    434:   [/in-send-page-refused /reason] pushVariables
                    435:   [
                    436:    /reason arg1 def
                    437:    (HTTP/0.9 200 OK) sendln
                    438:    (Connection: close) sendln
                    439:    (Content-Type: text/html) sendln
                    440:    0 sendln
                    441:
                    442:   (<H2> Your request is refused. </H2> <br> ) sendln
                    443:   (Because your input contains the key word <font color="red">) sendln
                    444:   reason 0 get sendln
                    445:   ( </font> ) sendln
                    446:   (<br> <br>) sendln
                    447:   (Your input is <br> ) sendln
                    448:   (<pre> ) sendln
                    449:   reason 1 get sendln
                    450:   (</pre> ) sendln
                    451:
                    452:    0 sendln
                    453:    [(flush)] extension
                    454:   ] pop
                    455:   popVariables
                    456: } def
                    457:
1.2       takayama  458: %% **** Overwrites the definition in httpd.sm1
                    459: /httpd_startserver {
                    460:    (httpd server accessible outside localhost.) message
                    461:    %%  get the hostname of this machine.
                    462:    [(sm1.socket) (open) [httpd.port
                    463:                          [(sm1.socket) (gethostname) [ ] ]extension ] ]
                    464:     extension
                    465:    /httpd.server.fdAndPort set
                    466:    (sm1.socket.open returns  ) messagen httpd.server.fdAndPort message
                    467:    [(sm1.socket) (accept) [httpd.server.fdAndPort 0 get]] extension
                    468:    /httpd.server.fd set
                    469:    (connected.) message
                    470:    (sm1.socket.accept returns <httpd.server.fd> ) messagen
                    471:    httpd.server.fd message
                    472:
                    473: } def
1.1       takayama  474:
                    475: %% ******* sample of cooked command
1.7       takayama  476: /cookedCommand.simplest {
1.1       takayama  477:   /arg1 set
                    478:   [/in-cookedCommand /httpd.com] pushVariables
                    479:   [
                    480:     /httpd.com arg1 def
                    481:     [(if (1) {)  httpd.com  (; };)] cat
                    482:     /arg1 set
1.7       takayama  483:   ] pop
                    484:   popVariables
                    485:   arg1
                    486: } def
                    487: /cookedCommand {
                    488:   /arg1 set
                    489:   [/in-cookedCommand /httpd.com /fff] pushVariables
                    490:   [
                    491:     /httpd.com arg1 def
                    492:     httpd.oxasir.timer not {
                    493:       [(if (1) {)  httpd.com  (; };)] cat
                    494:       /fff set
                    495:     }{
                    496: %%  It does not work.
                    497: %%      [$timer(30,eval_str("$
                    498: %%          (if (1) {)  httpd.com  (; };)
                    499: %%       $"),"Computation is aborted with the resource limit ( 30 seconds)");$
                    500: %%      ] cat
                    501:      [$timer(30,$   httpd.com
                    502:         $,"Computation is aborted with the resource limit ( 30 seconds) or there was a syntax error.");$
                    503:      ] cat
                    504:       /fff set
                    505:     } ifelse
                    506:     (cooked command is ) messagen fff message
                    507:     /arg1 fff def
1.1       takayama  508:   ] pop
                    509:   popVariables
                    510:   arg1
                    511: } def
1.4       takayama  512: /httpd.refusedCommands
1.8       takayama  513:    [(shell) (eval_str) (ox_) (sm1_)
                    514:     (m_start) (m_N_) (m_Inverse) (m_TexForm) %(m_)
1.5       takayama  515:     (connect) (load)  (bload) (bsave)
                    516:     (end) (quit) (output) (bload27) (open)
                    517:     (plot) (ctrl) (debug) (error) (port) (bind) (accept)
1.6       takayama  518:     (draw)  (peek) (poke)
1.13      takayama  519:     (write_string_to_a_file) (_filter)
1.4       takayama  520:    ]
                    521: def

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