Hello,
I am trying to create a very simple http server, using the latest
stable Synapse on Mac OS X 10.4 and Free Pascal
Here is my code, I adapted it from the demo which comes with Synapse.
It does not work. The server runs, I can access the website from
Firefox at port 1500, but nothing is shown =( It writes on screen that
the request was correctly handled. I am curious about reading data
from the browser. I don't want to read any data, just send! Do I
really need to read the header from the browser?
program upserver;
{$mode objfpc}{$H+}
uses
Classes, blcksock, sockets, Synautil, SysUtils;
var
ListenerSocket, ConnectionSocket: TTCPBlockSocket;
InputData, OutputData: TMemoryStream;
procedure AttendConnection(ASocket: TTCPBlockSocket);
var
b: byte;
timeout: integer;
s: string;
method, uri, protocol: string;
size: integer;
x, n: integer;
resultcode: integer;
begin
InputData := TMemoryStream.Create;
OutputData := TMemoryStream.Create;
timeout := 120000;
//read request line
s := ASocket.RecvString(timeout);
if ASocket.lasterror <> 0 then Exit;
if s = '' then Exit;
method := fetch(s, ' ');
if (s = '') or (method = '') then Exit;
uri := fetch(s, ' ');
if uri = '' then Exit;
protocol := fetch(s, ' ');
size := -1;
//read request headers
if protocol <> '' then
begin
if pos('HTTP/', protocol) <> 1 then Exit;
repeat
s := ASocket.RecvString(Timeout);
if ASocket.lasterror <> 0 then Exit;
// if s <> '' then Headers.add(s);
if Pos('CONTENT-LENGTH:', Uppercase(s)) = 1 then
Size := StrToIntDef(SeparateRight(s, ' '), -1);
until s = '';
end;
//recv document...
InputData.Clear;
if size >= 0 then
begin
InputData.SetSize(Size);
x := ASocket.RecvBufferEx(InputData.Memory, Size, Timeout);
InputData.SetSize(x);
if ASocket.lasterror <> 0 then Exit;
end;
ASocket.SendString('HTTP/1.0 200' + CRLF);
ASocket.SendString('Content-type: Text/Html' + CRLF);
ASocket.SendString('Content-length: ' + IntTostr(OutputData.Size) + CRLF);
ASocket.SendString('Connection: close' + CRLF);
ASocket.SendString('Date: ' + Rfc822DateTime(now) + CRLF);
ASocket.SendString('Server: Synapse HTTP server demo' + CRLF);
ASocket.SendString('' + CRLF);
if ASocket.lasterror <> 0 then Exit;
OutputData.WriteAnsiString('<html><h1>Teste</h1></html>');
ASocket.SendBuffer(OutputData.Memory, OutputData.Size);
OutputData.Free;
InputData.Free;
end;
begin
ListenerSocket := TTCPBlockSocket.Create;
ConnectionSocket := TTCPBlockSocket.Create;
ListenerSocket.CreateSocket;
ListenerSocket.setLinger(true,10);
ListenerSocket.bind('0.0.0.0','1500');
ListenerSocket.listen;
repeat
if ListenerSocket.canread(1000) then
begin
ConnectionSocket.Socket := ListenerSocket.accept;
WriteLn('Attending Connection. Error code (0=Success): ',
ConnectionSocket.lasterror);
AttendConnection(ConnectionSocket);
end;
until false;
ListenerSocket.Free;
ConnectionSocket.Free;
end.
-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
synalist-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/synalist-public