【问题标题】:How can create https server using synapse in lazarus如何在拉撒路中使用突触创建 https 服务器
【发布时间】:2016-01-01 10:18:15
【问题描述】:

我正在尝试使用突触在 lazarus 中创建 https 服务器,但我失败了。我想让我的服务器从其他 https 客户端接收数据。 我正在使用我的浏览器使用https://localhost:1500 发送请求,而我的服务器正在接收信号。但是当我尝试读取发送的数据时,我什么也没有收到。当我测试简单的 http 服务器时一切正常。但是现在在 https 的情况下它不起作用。我使用 ubuntu 15.04 作为我的操作系统

s := ASocket.RecvString(timeout); //不返回任何内容

我的示例代码:

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.

【问题讨论】:

  • 您是否遵循(或找到)ararat.cz/synapse/doku.php/public:howto:httpsserver 的说明?
  • 不,我在其他示例中找到了我将尝试检查 HTTP Server Demo(我猜是突触附带)的示例,并将尝试相应地更改它。
  • 不幸的是,http 服务器示例女巫带有突触是 Windows 特定的,因为它使用 winsock 单元女巫 Lazarus 至少在 Linux 中不支持
  • 据我所知,您可以用 synsock 替换所有出现的 winsock,它应该可以在 Linux 上的 Lazarus 中工作。 (我可能错了,但试试看)
  • 虽然我建议使用最新的 svn 版本的 synapse,但稳定版本 40 包括 httpsserv(https 服务器)示例。尝试更改 synsock 中的 winsock 并删除任何 windows 子句。如果这不起作用,您可能想尝试调整此示例:mail-archive.com/synalist-public@lists.sourceforge.net/…

标签: lazarus synapse fpc


【解决方案1】:

据我所知,有两个来源,以 Synapse 中的 HTTP(s) 服务器为例。

第一个示例在Synapse stable package (release 40) 中。虽然我建议您使用SVN version(您可以使用该页面上的Download Snapshot 按钮),但您仍然可以使用“40 版包”中的示例。

synapse40\source\demo\httpsserv 中的示例应该可以用作 HTTPS 服务器。如果不是,您可以使用 httpserv (HTTP) 示例并将其更改为 shown here。 (但我认为 httpsserv 与那些修改是一样的)

如果您使用的是 Linux (Lazarus),则需要将每次出现的 winsock 更改为 synsock 并删除任何 windows 子句。

另一个例子是found here。 (Direct download of SynHttp.zip) 据我所知,它还具有 HTTPS 服务器功能。

【讨论】:

    【解决方案2】:

    我知道您想使用 Synapse,但您可能想看看 Indy。多年来,我一直在使用 indy 开发服务器/客户端应用程序,我喜欢它。它适用于 windows 和 linux(32 位、64 位、arm...),并且具有一些不错的功能。您可以使用 TIdHTTPServer 组件。此外得到一个 IOHandlerSSLOpenSSL。事件 OnCommandGet(AContext: TIdContext; ARequestInfo:TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo) 是最重要的一个。它允许访问请求(如 ARequestInfo.Document)和您的 HTTP 响应 (AResponseInfo)。

    【讨论】:

    • 我用过 indy,效果很好。但是它与 Lazarus 有一些不好的问题,比如我的自动构建服务器无法 100% 编译它,因为 Lazarus 中的一些错误或者它们是软件包。编译时会抛出找不到 IdStreamVCL :(。所以我现在尝试使用其他方法。但如果我找不到任何东西,那么是的,Indy 就是这样。
    • 为 Lazarus 安装 Indy 是一团糟,但您不需要 :) 您可以将 indy 文件夹复制到您的项目文件夹中。在 Lazarus 中按 Shift + Ctrl + F11 并选择 Compiler Options -> Paths。将您的 Indy 源文件夹(Indy\source)添加到其他单元文件中,然后您就完成了 :) 只需添加您需要的单元并在运行时创建组件。
    • 您的建议帮助我解决了 indy 的问题。这不是我想打赌它比原来更好的最佳解决方案。但由于这没有回答我原来的问题,我不会接受这个作为答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-26
    • 1970-01-01
    • 2019-09-18
    • 2013-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多