gpt4 book ai didi

lazarus - 如何在lazarus中使用synapse创建https服务器

转载 作者:行者123 更新时间:2023-12-02 01:17:45 25 4
gpt4 key购买 nike

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

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

我的示例代码:

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.

最佳答案

据我所知,有两个来源,其中包括 Synapse 中 HTTP(s) 服务器的示例。

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

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

如果您使用的是 Linux (Lazarus),则需要将每次出现的 winsock 更改为 synsock 并删除所有 windows -条款。

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

关于lazarus - 如何在lazarus中使用synapse创建https服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32937076/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com