Hi, thanks, indeed, it works with the output size corrected =)

But I am getting some question marks in my HTML. Any ideas why? Here
is the new code:

program upserver;

{$mode objfpc}{$H+}

uses
  Classes, blcksock, sockets, Synautil, SysUtils;

var
  ListenerSocket, ConnectionSocket: TTCPBlockSocket;

procedure AttendConnection(ASocket: TTCPBlockSocket);
var
  timeout: integer;
  s: string;
  method, uri, protocol: string;
  ResultCode: integer;
  OutputData: TMemoryStream;
begin
  OutputData := TMemoryStream.Create;

  try
    timeout := 120000;

    WriteLn('Received headers+document from browser:');

    //read request line
    s := ASocket.RecvString(timeout);
    WriteLn(s);
    method := fetch(s, ' ');
    uri := fetch(s, ' ');
    protocol := fetch(s, ' ');

    //read request headers
    repeat
      s := ASocket.RecvString(Timeout);
      WriteLn(s);
    until s = '';

    // Now write the document to the output stream

    if uri = '/' then
    begin
      // Write the output document to the stream
      OutputData.WriteAnsiString('<html><h1>Teste</h1></html>' + CRLF);

      // Write the headers back to the client
      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: Servidor do Felipe usando Synapse' + CRLF);
      ASocket.SendString('' + CRLF);

    //  if ASocket.lasterror <> 0 then HandleError;

      // Write the document back to the browser
      ASocket.SendBuffer(OutputData.Memory, OutputData.Size);
    end
    else
      ASocket.SendString('HTTP/1.0 504' + CRLF);
  finally
    OutputData.Free;
  end;
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

Reply via email to