WebserverPlugin.pas
上传用户:hndmjx
上传日期:2014-09-16
资源大小:3369k
文件大小:3k
源码类别:

Delphi控件源码

开发平台:

Delphi

  1. {
  2.   Webserver LITE 1.0
  3.   Version: 1.0
  4.   Written By: ~LOM~
  5.   Website: www.evileyesoftware.com
  6.   OverView
  7.   --------
  8.   This is a basic webserver I did for learning sake -
  9.   really wasn't that hard - I've commented the most part ;)
  10.   if you use any of the source please credit ~LOM~ and
  11.   evileyesoftware.com
  12.   I've kept everything pretty lite - using basic API ;)
  13.   NOTE FROM APHEX: I tidied up a few things hope you
  14.   don't mind :P
  15. }
  16. unit WebserverPlugin;
  17. interface
  18. uses
  19. Windows, SocketUnit, ThreadUnit;
  20. const
  21.   CRLF = #13#10;
  22.   PACKETLEN = 5012;
  23.   DOT = '.';
  24. var
  25.   WebServer: TServerSocket;
  26.   Port: integer;
  27.   Directory: string;
  28.   CloseServer: boolean = False;
  29. procedure StartWebServer(pPort: Integer; pDirectory: string);
  30. implementation
  31. function IntToStr(X: integer): string;
  32. begin
  33.   str(X, Result);
  34. end;
  35. function StrToInt(S: string): integer;
  36. var
  37.   X: integer;
  38. begin
  39.   val(S, Result, X);
  40. end;
  41. function ExtractFileExt(Delimiter, Input: string): string;
  42. begin
  43.   while Pos(Delimiter, Input) <> 0 do Delete(Input, 1, Pos(Delimiter, Input));
  44.   Result := Input;
  45. end;
  46. function LowerCase(const S: string): string;
  47. var
  48.   Ch: Char;
  49.   L: Integer;
  50.   Source, Dest: PChar;
  51. begin
  52.   L := Length(S);
  53.   SetLength(Result, L);
  54.   Source := Pointer(S);
  55.   Dest := Pointer(Result);
  56.   while L <> 0 do
  57.   begin
  58.     Ch := Source^;
  59.     if (Ch >= 'A') and (Ch <= 'Z') then Inc(Ch, 32);
  60.     Dest^ := Ch;
  61.     Inc(Source);
  62.     Inc(Dest);
  63.     Dec(L);
  64.   end;
  65. end;
  66. procedure ServeFile(Filename: string; Connection: TClientSocket);
  67. var
  68.   FH, BR: cardinal;
  69.   FS: integer;
  70.   OS: string;
  71.   FStrm: array [1..5012] of Char;
  72. begin
  73.   if (ExtractFileExt(DOT, Filename) = Filename) then Filename := Filename + '/index.htm';
  74.   FH := CreateFile(Pchar(Directory + Filename), LongWord($80000000), 0, nil, 3, $00000080, 0);
  75.   FS := GetFileSize(FH, nil);
  76.   OS := 'HTTP/1.1 200 OK' + CRLF
  77.         + 'Accept-Ranges: bytes' + CRLF
  78.         + 'Content-Length: ' + IntToStr(FS) + CRLF
  79.         + 'Keep-Alive: timeout=15, max=100' + CRLF
  80.         + 'Connection: Keep-Alive' + CRLF
  81.         + 'Content-Type: ' + CRLF + CRLF;
  82.   while Connection.SendBuffer(OS[1], Length(OS)) = -1 do
  83.   begin
  84.     Sleep(1);
  85.   end;
  86.   repeat
  87.     ReadFile(FH, FStrm, PACKETLEN, BR, nil);
  88.     while Connection.SendBuffer(FStrm, BR) = -1 do
  89.     begin
  90.       Sleep(1);
  91.     end;
  92.   until BR < PACKETLEN;
  93.   CloseHandle(FH);
  94. end;
  95. procedure ServePages(Params: TThread);
  96. var
  97.   BI: array [0..PACKETLEN] of Char;
  98.   TB: string;
  99.   Ln: integer;
  100.   Client: TClientSocket;
  101. begin
  102.   Params.Lock;
  103.   Client := WebServer.Accept;
  104.   Params.Unlock;
  105.   Client.Idle(10000);
  106.   Ln := Client.ReceiveBuffer(BI, PACKETLEN);
  107.   TB := Copy(BI, 1, Ln);
  108.   If Pos('GET', BI) > 0 then
  109.   begin
  110.     Delete(TB, 1, 4);
  111.     ServeFile(LowerCase(Copy(TB, 1, Pos('HTTP/1.', TB) - 2)), Client);
  112.   end;
  113.   Client.Free;
  114.   Params.Free;
  115. end;
  116. procedure ListenThread;
  117. begin
  118.   WebServer := TServerSocket.Create;
  119.   WebServer.Listen(Port);
  120.   while CloseServer = False do
  121.   begin
  122.     Webserver.Idle;
  123.     TThread.Create (ServePages,0);
  124.   end;
  125.   WebServer.Destroy;
  126. end;
  127. procedure StartWebServer(pPort: Integer; pDirectory: String);
  128. begin
  129.   CloseServer := False;
  130.   Port := pPort;
  131.   Directory := pDirectory;
  132.   ListenThread;
  133. end;
  134. procedure StopWebServer;
  135. begin
  136.   CloseServer := True;
  137. end;
  138. end.