http
好像还没有什么实际应用,那就写一个吧(几天前的内容忘记写博客了)
文件目录:
Ustok
---- _site
---- head.html
---- main.html
---- tail.html
---- main.exe
每个文件的默认值:
head.html
:
<html>
<head>
<title>UsTok</title>
<meta charset="utf-8">
</head>
<body>
<form method="post">
<textarea name="a" style="font-size:25px;width:100%;height:100px" value=""></textarea><br /><br />
<input type="submit" value="回复" style="font-size:25px">
</form>
main.html
:空
tail.html
:
</body>
</html>
main.exe
:由main.pas
编译而来:
main.pas
:
{$mode objfpc}
uses zul_http,meaning_transferred,sysutils;
function load_file(s:ansistring):ansistring;
var
fin:text;
ch:char;
begin
assign(fin,s);
try
reset(fin);
except
exit('');
end;
while not eof(fin) do
begin
read(fin,ch);
load_file:=load_file+ch;
end;
close(fin);
end;
procedure _add(a:ansistring);
var
fout:text;
s:ansistring;
begin
s:=load_file('_site/main.html');
s:=DateTimeToStr(Now)+':'+a+'<br />'+#13#10+s;
assign(fout,'_site/main.html');
rewrite(fout);
write(fout,s);
close(fout);
end;
function main(req:http_message_C):http_message_S;//Maintain static pages dynamically
begin
main.agreement:='HTTP/1.1';
main.status_number:=200;
main.status:='OK';
setlength(main.opt,1);
main.opt[0].key:='Content-Type';
main.opt[0].value:='text/html';
if req.method='POST' then
begin
delete(req.data,1,pos('a=',req.data)+1);
_add(html_decode(req.data));
end;
main.data:=load_file('_site/head.html')+load_file('_site/main.html')+load_file('_site/tail.html');
end;
begin
max_online:=50;
port:=8000;
resp:=@main;
start;
end.
meaning_transferred.pas
:
unit meaning_transferred;
interface
function html_encode(a:ansistring):ansistring;
function html_decode(a:ansistring):ansistring;
const
_tran_hex:array[0..15]of char=('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');
_untran:array[48..70]of longint=(0,1,2,3,4,5,6,7,8,9,0,0,0,0,0,0,0,10,11,12,13,14,15);
implementation
function html_encode(a:ansistring):ansistring;
var
i:longint;
begin
html_encode:='';
for i:=1 to length(a) do
begin
if a[i]=' ' then
html_encode:=html_encode+'+'
else if a[i] in ['*','-','.','0'..'9','A'..'Z','_','a'..'z'] then
html_encode:=html_encode+a[i]
else html_encode:=html_encode+'%'+_tran_hex[ord(a[i])div 16]+_tran_hex[ord(a[i])mod 16];
end;
end;
function html_decode(a:ansistring):ansistring;
var
i:longint;
begin
html_decode:='';
i:=1;
while i<=length(a) do
begin
if a[i]='+' then
html_decode:=html_decode+' '
else if a[i]='%' then
begin
html_decode:=html_decode+chr(_untran[ord(a[i+1])]*16+_untran[ord(a[i+2])]);
i:=i+2;
end
else html_decode:=html_decode+a[i];
i:=i+1;
end;
end;
end.
全部由UTF-8
格式存储