How to create https server using synapse in lazarus

I am trying to create an https server in lazarus using synapse, but I am failing. I want the myserver server to receive data from other https clients. I am sending requests using my browser using https: // localhost: 1500 and the myserver server is receiving signals. But when I try to read the sent data, I get nothing. When I tested a simple HTTP server, everything worked fine. But now in the case of https it does not work. I am using ubuntu 15.04 as my OS

s: = ASocket.RecvString (timeout); // returns noething

My sample code is:

unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

uses
  blcksock, sockets, Synautil, ssl_openssl, ssl_openssl_lib;

procedure AttendConnection(ASocket: TTCPBlockSocket);
var
  timeout: integer;
  s: string;
  method, uri, protocol: string;
  OutputDataString: string;
  ResultCode: integer;
begin
  timeout := 1000;

  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
    OutputDataString :=
      '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'
      + ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">' + CRLF
      + '<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(Length(OutputDataString)) + 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.SendString(OutputDataString);
  end
  else
    ASocket.SendString('HTTP/1.0 404' + CRLF);
end;

{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
var
  ListenerSocket, ConnectionSocket: TTCPBlockSocket;
begin
  ListenerSocket := TTCPBlockSocket.Create;
  ConnectionSocket := TTCPBlockSocket.Create;

  ListenerSocket.CreateSocket;
  ListenerSocket.SSL.CertificateFile := '/home/imants/projects/apps/medieval/bin/40669199_localhost_8080.cert';
  ListenerSocket.SSL.PrivateKeyFile := '/home/imants/projects/apps/medieval/bin/40669199_localhost_8080.key';
  ListenerSocket.SSLDoConnect;
  ListenerSocket.setLinger(true,10);
  ListenerSocket.bind('localhost','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);
      ConnectionSocket.CloseSocket;
    end;
  until false;

  ListenerSocket.Free;
  ConnectionSocket.Free;
end;

end.
+4
source share
2 answers

, , HTTP (s) Synapse.

Synapse ( 40). SVN ( ), " 40".

synapse40\source\demo\httpsserv HTTPS-. , httpserv (HTTP) . ( , httpsserv )

Linux (Lazarus), winsock synsock windows -clause.

. ( SynHttp.zip) , HTTPS-.

+3

, Synapse, Indy. / indy, . linux (32bit, 64bit, arm...) . TIdHTTPServer. , IOHandlerSSLOpenSSL. OnCommandGet (AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo) . (, A ARequestInfo.Document) HTTP- (AResponseInfo).

+2

Source: https://habr.com/ru/post/1610191/


All Articles