【发布时间】: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/…