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