FPWebModule add HTTPS support lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- TheMouseAUS · 2021-04-27 01:49 Hi, I have written many small apps in Delphi 2007 with Indy10 over the past years, I am self taught and always read examples on how to do things. I needed to be more up-to date with 64bit and thought Lazarus would be a good fit. I have written an application using 'HTTP Server Application' which created FPWebModule. Took me a while to figure things out which is very hard when most of the documentation dates back to 2006 and even the examples in the source folder seem out of date. Anyway I have it all working and I am very happy with the end result. Except to host on the web I now have to switch to HTTPS and I cannot find any simple example on how to do this or if its even possible, otherwise I will have migrate the whole app using Indy 10 instead which isn't ideal. Older posts say no its not possible, newer ones suggest support is added. I am a bit lost. Sorry I am a newbie. ---------------------------------------------------------------------- Handoko · 2021-04-27 04:23 Hello TheMouseAUS, Welcome to the forum. Web and network programming is not my expertise. But I remember there were some issues when accessing secure sites one or two years ago. I heard that issue has been solved in most recent version of Lazarus/FPC. ---------------------------------------------------------------------- trev · 2021-04-27 05:46 There's a Free Pascal HTTPS web server example for FPC 3.0.4, 3.2.0 and 3.3.1 (trunk) included at: /usr/local/share/fpcsrc/fpc- 3.x.x/packages/libmicrohttpd/examples/https_fileserver_example.pp in the FPC distribution. It uses libmicrohttpd rather than fpWeb. There is also an example of an HTTPS web server in the Wiki Light Web Server which uses Synapse and Free Pascal. ---------------------------------------------------------------------- TheMouseAUS · 2021-04-27 06:14 I have manged to create a secure socket on port 443. I added application.useSSL := True and added openSSLsockets, openSSL to uses. It is trying to work but I cannot figure out how to get openSSL to load both the cert and key I generated. I have tried all sorts and keep coming up short. I am trying not to have to change the whole project just to add this otherwise I will just switch to Indy10 which will work. ---------------------------------------------------------------------- xinyiman · 2021-04-27 07:11 I go by heart so I could be wrong. I seem to remember that certificate management is handled by openssl. So based on your architecture (windows, linux or mac) you have to figure out the directory where openssl goes to fetch the certificates. And then you generate them with the openssl tools. ---------------------------------------------------------------------- TheMouseAUS · 2021-04-27 07:12 OK I have tried SSLCTXusecertificatefile(SSLCTXnew(SslMethodV23), 'c:\temp\cert.pem', SSL_FILETYPE_PEM) SSLCTXusePrivateKeyfile(SSLCTXnew(SslMethodV23), 'c:\temp\key.pem', SSL_FILETYPE_PEM) It is returning as OK but still have no certs when I try and load webpage ---------------------------------------------------------------------- MvC · 2021-04-27 11:27 Hi, If you're using a released version of FPC/Lazarus, then setting a certificate requires creating a descendent of THTTPServerHandler and overriding CreateServer. Function TMyHTTPServerHandler.CreateServer : THTTPServer: begin Result:=Inherited CreateServer; // Configure certificate Result.CertificateData.Certificate.FileName:='yourcertificate.pem'; Result.CertificateData.PrivateKey.FileName:='yourprivatekey.pem'; end; Then, create a descendent of THTTPApplication. Override InitializeWebHandler to return your class: function TMyHTTPApplication.InitializeWebHandler: TWebHandler; override; begin Result:=TMyHTTPServerHandler.Create(Self); end; That's it. If you use Trunk FPC, then as of rev. 49276, you can simply configure it in your project file: Application.HTTPhandler.CertificateData.Certificate.FileName:='yourcer tificate.pem'; Application.HTTPhandler.CertificateData.PrivateKey.FileName:='yourpriv atekey.pem'; ---------------------------------------------------------------------- TheMouseAUS · 2021-04-27 12:32 Awesome thanks MVC, please just a little more help so I understand, were in the following project would it go? There no point posting all my project so I created a quick one with all the relevant bits. QuoteFunction TMyHTTPServerHandler.CreateServer : THTTPServer: begin Result:=Inherited CreateServer; // Configure certificate Result.CertificateData.Certificate.FileName:='yourcertificate.pem'; Result.CertificateData.PrivateKey.FileName:='yourprivatekey.pem'; end; Then, create a descendent of THTTPApplication. Override InitializeWebHandler to return your class: function TMyHTTPApplication.InitializeWebHandler: TWebHandler; override; begin Result:=TMyHTTPServerHandler.Create(Self); end; Code: Pascal [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses SysUtils, Classes, httpdefs, fpHTTP, fpWeb , opensslsockets, openssl; type { TFPWebModule1 } TFPWebModule1 = class(TFPWebModule) procedure DataModuleRequest(Sender: TObject; ARequest: TRequest; AResponse: TResponse; var Handled: Boolean); private public end; var FPWebModule1: TFPWebModule1; implementation {$R *.frm} { TFPWebModule1 } procedure TFPWebModule1.DataModuleRequest(Sender: TObject; ARequest: TRequest; AResponse: TResponse; var Handled: Boolean);begin With AResponse.Contents do Begin Add(''); Add(''); Add('
'); Add(' '); Add(' '); Add(' '); Add('