unit Unit_HttpProtocol;
interface
uses
Windows,SysUtils,Classes,Isapi,IdTCPServer;
const
CR = #13#10;
ctHtml = 'text/html'; //ct: Content Type
ctJpeg = 'image/jpeg';
ctGif = 'image/gif';
ctBmp = 'image/bmp';
procedure SendHTMLPage(AThread: TIdPeerThread; const Page: string);
procedure SendJpegStream(AThread: TIdPeerThread; JpegStream: TMemoryStream);
procedure SendFile(AThread: TIdPeerThread;const FileName, FileType: string);
implementation
function FormatHeader(const ContentType: string; ContentLength: DWord): string;
var
ct: string;
begin
ct := '';
if ContentType <> '' then
ct := 'Content-Type: ' + ContentType + CR;
Result := Format(
'HTTP/1.0 200 OK' + CR +
ct +
'Content-Length: %d' + CR +
'Content:' + CR + CR, [ContentLength]);
end;
function FormatHTMLHeader(ContentLength: DWord): string;
begin
Result := FormatHeader(ctHtml, ContentLength);
end;
function SendText(AThread: TIdPeerThread; const s: string):Boolean;
begin
Result:=False;
try
AThread.Connection.Write(s);
except
AThread.Connection.Disconnect;
AThread.Terminate;
Exit;
end;
Result:=True;
end;
function SendStream(AThread: TIdPeerThread; MemoryStream: TMemoryStream; const StreamType: string):Boolean;
var
ContentType: string;
begin
Result:=False;
ContentType := '';
if StreamType <> '' then
ContentType := 'Content-Type: ' + StreamType + CR;
// Send the Header
if not SendText(AThread, FormatHeader(StreamType, MemoryStream.Size)) then Exit;
// Send the file
try
AThread.Connection.WriteStream(MemoryStream);
except
AThread.Connection.Disconnect;
AThread.Terminate;
Exit;
end;
Result:=True;
end;
procedure SendHTMLPage(AThread: TIdPeerThread; const Page: string);
begin
SendText(AThread, FormatHTMLHeader(Length(Page)) + Page);
end;
procedure SendJpegStream(AThread: TIdPeerThread; JpegStream: TMemoryStream);
begin
SendStream(AThread, JpegStream, ctJpeg);
end;
procedure SendFile(AThread: TIdPeerThread;const FileName, FileType: string);
var
MemoryStream: TMemoryStream;
begin
MemoryStream := TMemoryStream.Create;
try
MemoryStream.LoadFromFile(FileName);
MemoryStream.Position := 0;
SendStream(AThread, MemoryStream, FileType);
finally
MemoryStream.Free;
end;
end;
end.
http://www.138soft.com/server.rar
....client.rar