In previous article we installed indy 10 in Lazarus.
Lets now build a lightweigth HTTP server.
All it is really is a tcp server listening on port 80, parsing the incoming request and sending back the content of the requested file.
All the « magic » really happens below.
lCmdLine := AContext.Connection.IOHandler.ReadLn;
memResults.Lines.Add (lCmdLine);
if lCmdLine<>'' then
begin
uri := StringReplace (lCmdLine ,'GET ','',[]);
uri := copy(uri,1,pos(' ',uri )-1);
uri :=StringReplace (uri,'/','',[]);;
if FileExists(uri)
then AContext.Connection.IOHandler.WriteFile(uri)
else AContext.Connection.IOHandler.Writeln('file not found');
AContext.Connection.Disconnect;
end;
end;
Source code can be found on github.