%% $OpenXM: OpenXM/src/kan96xx/Doc/httpd.sm1,v 1.13 2002/11/03 12:43:03 takayama Exp $
%% common modules for httpd on sm1.
/Oxserver_history_variable (Oxserver_history_variable_) def
/httpd.image.type
[(getenv) (OpenXM_PSTOIMG_TYPE)] extension
def
[(getenv) (OXWEB_DEBUG)] extension tag 0 eq {
/httpd.debug 0 def
} {
/httpd.debug 1 def
} ifelse
[(getenv) (OXWEB_TAKE_LOG)] extension tag 0 eq {
/httpd.take.log 0 def
} {
/httpd.take.log 1 def
} ifelse
/httpd_startserver {
[(sm1.socket) (open) [httpd.port (localhost)]] extension
/httpd.server.fdAndPort set
(sm1.socket.open returns ) messagen httpd.server.fdAndPort message
[(sm1.socket) (accept) [httpd.server.fdAndPort 0 get]] extension
/httpd.server.fd set
(connected.) message
(sm1.socket.accept returns ) messagen httpd.server.fd message
} def
/httpd_stopserver {
[(sm1.socket) (close) httpd.server.fd ] extension message
} def
/send_packet {
/arg1 set
[(sm1.socket) (write) [httpd.server.fd 0 get arg1]] extension message
} def
/sendln {
/arg1 set
[/in-sendln /mmm /i] pushVariables
[ arg1 /mmm set
mmm tag 5 eq {
[mmm 10 (string) dc] cat /mmm set
}{
10 (string) dc /mmm set
} ifelse
[(sm1.socket) (select) [httpd.server.fd 0 get 0]] extension {
(Warning (sendln): your peer closed the connection. Do not send the data.) message
} {
[(sm1.socket) (write) [httpd.server.fd 0 get mmm]] extension /i set
httpd.debug { i message } { } ifelse
} ifelse
] pop
popVariables
} def
/sendBinaryFile {
/arg1 set
[/in-sendln /fname /fd /c /cdata] pushVariables
[ arg1 /fname set
[(sendBinaryFile: sending data) ] cat message
[(fp2openForRead) fname] extension /fd set fd message
fd 0 lt {
[(Error: sendBinaryFile: file ) fname ( is not found.)] cat message
/aaaa goto
} { } ifelse
[(fp2pushfile) fname] extension /cdata set
[(sm1.socket) (select) [httpd.server.fd 0 get 0]] extension {
(Warning (sendBinaryFile): your peer closed the connection. Do not send the data.)
message
exit
} {
[(sm1.socket) (writeByte) [httpd.server.fd 0 get cdata]] extension pop
} ifelse
/aaaa
] pop
popVariables
} def
/httpd {
/httpd.serial 0 def
/httpd.history [ ] def
/httpd.result.history [ 0 ] def
{
httpd_startserver ;
httpd_action ;
httpd_stopserver ;
httpd.take.log { (date) system } { } ifelse
% (sleep 2) system
httpd.serial 1 add /httpd.serial set
} loop
} def
/send-page-bye {
(HTTP/0.9 200 OK) sendln
%% (Date: Sun, 18 Mar 2001 02:54:50 GMT) sendln
%% (Server: sm1/0.1 (Unix)) sendln
%% (Last-Modified: Wed, 23 Aug 2000 11:19:27 GMT) sendln
%% (ETag: "1f8f-5df-39a3b33f") sendln
%% (Accept-Ranges: bytes) sendln
%% (Content-Length: 10) sendln
(Connection: close) sendln
% (Content-Type: text/plain) sendln
(Content-Type: text/html) sendln
0 sendln
() sendln
(Shutdown the engine.
) sendln
(See you! Web/asir, Web/sm1 ) sendln
() sendln
0 sendln
[(flush)] extension
} def
/send-page-2 {
(HTTP/0.9 200 OK) sendln
%% (Content-Length: 10) sendln
(Connection: close) sendln
(Content-Type: text/html) sendln
0 sendln
(
) sss ()] cat /arg1 set ] pop popVariables arg1 } def /executeStringAndSelectInputFromBrowserAndOxserver { /arg3 set /arg2 set /arg1 set [/in-executeStringAndSelectInputFromBrowserAndOxserver /oxserver.ccc /command.to.oxserver /sss.engine /sss.web /sss /err /httpd.result /stringOrCmo ] pushVariables % Global var: httpd.server.fd [ /oxserver.ccc arg1 def /command.to.oxserver arg2 def /stringOrCmo arg3 def oxserver.ccc command.to.oxserver oxexecutestring ; [(oxReq) oxserver.ccc SM_dupErrors ] extension pop [(oxReq) oxserver.ccc SM_popCMO ] extension pop stringOrCmo (string) eq { [(oxReq) oxserver.ccc SM_popString ] extension pop }{ [(oxReq) oxserver.ccc SM_popCMO ] extension pop } ifelse [(flush)] extension pop %% Select inputs for interruption. %% Wait by the spin lock. { [(oxMultiSelect) [oxserver.ccc] 1] extension 1 get 0 get /sss.engine set [(sm1.socket) (mselect) [[httpd.server.fd 0 get] 1] ] extension 0 get /sss.web set /sss [sss.engine sss.web] def sss.engine { exit } { } ifelse sss.web { exit } { } ifelse } loop sss message sss 0 get { [(oxGet) oxserver.ccc] extension /err set [(oxGet) oxserver.ccc] extension /httpd.result set } { oxserver.ccc oxreset oxserver.ccc ("computation is interrupted.";) oxexecutestring ; oxserver.ccc oxpopstring /httpd.result set exit } ifelse (------------- result -------------) message httpd.result message (----------------------------------) message ( ) message err message err [ ] eq { } { oxserver.ccc cleanErrors [httpd.result 10 (string) dc err toString] cat /httpd.result set } ifelse /arg1 [err httpd.result] def ] pop popVariables arg1 } def % This function will be written in C in a future. % [(httpd) (parseHTTP) string] extension % [(GET) key-value-pair-1 key-value-pair-2 ...] % [(POST) key-value-pair-1 key-value-pair-2 ...] % [(GET-file) file-1 file2 ...] % ex. (GET / HTT..) (GET /?rpc=1-2) /httpd.parse { /arg1 set [/in-httpd.parse /s /s2 /sta /i0 /ans /getKeyword /j /tname /tvalue ] pushVariables [ /s arg1 def s 1 copy /s2 set s (array) dc /s set /sta 0 def /getKeyword 0 def s length 7 lt { /ans [(GET-file)] def /httpd.exit goto }{ } ifelse /ans [(GET)] def [s 0 get s 1 get s 2 get s 3 get s 4 get s 5 get] (GET /?) (array) dc eq { /sta 6 def /getKeyword 1 def }{ [s 0 get s 1 get s 2 get s 3 get s 4 get] (GET /) (array) dc eq { /sta 5 def }{ [s 0 get s 1 get s 2 get s 3 get] (GET ) (array) dc eq { /ans [(GET-file)] def /httpd.exit goto } { /ans [ ] def /httpd.exit.goto } ifelse } ifelse }ifelse %% In case of POST, remove the HTTP header. cf. trouble for NS 6.0 [s 0 get s 1 get s 2 get s 3 get] (POST) (array) dc eq { /ans [(POST) ] def /getKeyword 1 def 0 1 s length 4 sub { /i0 set /sta i0 def [ s i0 get s i0 1 add get ] [ 10 10 ] eq [ s i0 get s i0 1 add get s i0 2 add get s i0 3 add get] [ 13 10 13 10] eq or { exit } { } ifelse } for }{ } ifelse (sta=) messagen sta message %% get file name getKeyword not { /tname [ ] def sta 1 << s length 1 sub >> { /i0 set s i0 get 32 le { %% 32 is " " exit } { } ifelse tname s i0 get append /tname set } for httpd.debug { (Filename is ) messagen tname {(string) dc } map message} { } ifelse /ans [(GET-file) tname { (string) dc } map cat ] def /httpd.exit goto } { } ifelse /tname [ ] def sta 1 << s length 1 sub >> { /i0 set s i0 get 61 eq { %% 61 is = httpd.debug { tname message tname {(string) dc } map cat message} { } ifelse i0 1 add /i0 set exit } { } ifelse tname s i0 get append /tname set } for /j 0 def i0 1 s length 1 sub { /i set s2 j << s i get (string) dc >> put j s2 length 1 sub lt { s2 j 1 add ( ) put } { } ifelse %% might cause a BUG. It should be improved. j 1 add /j set } for ans [tname {(string) dc} map cat s2 webstringToAscii ] append /ans set /httpd.exit /arg1 ans def ] pop popVariables arg1 } def