mormot THttpApiServer使用例子

THttpApiServer封装了WINDOWS的HTTPS.SYS。

unit Unit1;

interface

uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs
, SynCommons, SynZip, SynCrtSock
;

type
TTestServer = class
protected
fPath: TFileName;
fServer: THttpApiServer;
function Process(Ctxt: THttpServerRequest): cardinal;
public
constructor Create(const Path: TFileName);
destructor Destroy; override;
end;

type
TForm1 = class(TForm)
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

{ TTestServer }

constructor TTestServer.Create(const Path: TFileName);
begin
fServer := THttpApiServer.Create(false);
fServer.AddUrl('root','888',false,'+',true);
fServer.RegisterCompress(CompressDeflate); // our server will deflate html :)
fServer.OnRequest := Process;
fPath := IncludeTrailingPathDelimiter(Path);
end;

destructor TTestServer.Destroy;
begin
fServer.Free;
inherited;
end;

function TTestServer.Process(Ctxt: THttpServerRequest): cardinal;
var W: TTextWriter;
FileName: TFileName;
FN, SRName, href: RawUTF8;
i: integer;
SR: TSearchRec;

procedure hrefCompute;
begin
SRName := StringToUTF8(SR.Name);
href := FN+StringReplaceChars(SRName,'\','/');
end;

begin
writeln(Ctxt.Method,' ',Ctxt.URL);
if not IdemPChar(pointer(Ctxt.URL),'/ROOT') then begin
result := 404;
exit;
end;
FN := StringReplaceChars(UrlDecode(copy(Ctxt.URL,7,maxInt)),'/','\');
if PosEx('..',FN)>0 then begin
result := 404; // circumvent obvious potential security leak
exit;
end;
while (FN<>'') and (FN[1]='\') do
delete(FN,1,1);
while (FN<>'') and (FN[length(FN)]='\') do
delete(FN,length(FN),1);
FileName := fPath+UTF8ToString(FN);
if DirectoryExists(FileName) then begin
// reply directory listing as html
W := TTextWriter.CreateOwnedStream;
try
W.Add('<html><body style="font-family: Arial">'+
'<h3>%</h3><p><table>',[FN]);
FN := StringReplaceChars(FN,'\','/');
if FN<>'' then
FN := FN+'/';
if FindFirst(FileName+'\*.*',faDirectory,SR)=0 then begin
repeat
if (SR.Attr and faDirectory<>0) and (SR.Name<>'.') then begin
hrefCompute;
if SRName='..' then begin
i := length(FN);
while (i>0) and (FN[i]='/') do dec(i);
while (i>0) and (FN[i]<>'/') do dec(i);
href := copy(FN,1,i);
end;
W.Add('<tr><td><b><a href="/root/%">[%]</a></b></td></tr>',[href,SRName]);
end;
until FindNext(SR)<>0;
FindClose(SR);
end;
if FindFirst(FileName+'\*.*',faAnyFile-faDirectory-faHidden,SR)=0 then begin
repeat
hrefCompute;
if SR.Attr and faDirectory=0 then
W.Add('<tr><td><b><a href="/root/%">%</a></b></td><td>%</td><td>%</td></td></tr>',
[href,SRName,KB(SR.Size),DateTimeToStr(
{$ifdef ISDELPHIXE2}SR.TimeStamp{$else}FileDateToDateTime(SR.Time){$endif})]);
until FindNext(SR)<>0;
FindClose(SR);
end;
W.AddShort('</table></p><p><i>Powered by mORMot''s <strong>');

W.AddClassName(Ctxt.Server.ClassType);

W.AddShort('</strong></i> - '+

'see <a href=http://synopse.info>http://synopse.info</a></p></body></html>');
Ctxt.OutContent := W.Text;
Ctxt.OutContentType := HTML_CONTENT_TYPE;
result := 200;
finally
W.Free;
end;
end else begin
// http.sys will send the specified file from kernel mode
Ctxt.OutContent := StringToUTF8(FileName);
Ctxt.OutContentType := HTTP_RESP_STATICFILE;
result := 200; // THttpApiServer.Execute will return 404 if not found
end;
end;

end.

调用:

with TTestServer.Create('d:\Documents\Smart Mobile Projects\Featured Demos\Binary Data Import\www\') do
try
write('Server is now running on http://localhost:888/root'#13#10#13#10+
'Press [Enter] to quit');
readln;
finally
Free;
end;

最新文章

  1. Entity Framework 6 Recipes 2nd Edition(13-1)译 -&gt; 优化TPT继承模型的查询
  2. 用PHP实现浏览器点击下载各种格式文档的方法详解【txt apk等等】
  3. php emoji处理微信表情
  4. Django1.8教程——从零开始搭建一个完整django博客(一)
  5. PHP 加密 和 解密 方法
  6. NAND Flash【转】
  7. iso中自动伸缩属性
  8. charAt(i) 函数
  9. 字典 -- 数据结构与算法的javascript描述 第七章
  10. Dockerfile
  11. 关于使用scrapy框架编写爬虫以及Ajax动态加载问题、反爬问题解决方案
  12. c/c++ 网络编程 read,write函数深入理解
  13. OpenCV3编程入门读书笔记5-边缘检测
  14. 潭州课堂25班:Ph201805201 tornado 项目 第七课 界面美化和静态文件处理(课堂笔记)
  15. 20164310Exp6 信息搜索和漏洞扫描
  16. Windows 命令行
  17. docker 应用-4(swarm模式搭建集群)
  18. Java Timer, TimerTask, Timer.Schedule
  19. Ubuntu GNOME 13.04将关闭窗口的按钮放在最右边
  20. war包内更新文件

热门文章

  1. SparkContext源码阅读
  2. windows下python3.4安装scikit-learn
  3. tinyxml学习一
  4. php数组转换成json格式。
  5. js方法之间的调用之——传参方法
  6. Android IOS WebRTC 音视频开发总结(七五)-- WebRTC视频通信中的错误恢复机制
  7. 阿里巴巴分布式服务框架Dubbo介绍(1)主要特色
  8. 删除svn文件的批处理文件
  9. WebClient异步下载文件
  10. Spring 4集成 Quartz2(转)