Recent

Author Topic: [Solved] ESocketError on Mac  (Read 3550 times)

Frogfather

  • New Member
  • *
  • Posts: 21
[Solved] ESocketError on Mac
« on: March 07, 2021, 07:23:08 pm »
Hello,

I'm having some difficulty making a GET request using fphttpclient with MacOS 10.15.7 Catalina. I've just upgraded to Lazarus 2.0.12 / fpc 3.2.0.

The code, which is intended to query the public api of an energy supplier, is pretty uncontroversial - and actually works fine on Linux Fedora 28.

Code: Pascal  [Select][+][-]
  1. function ToctopusForm.queryApi(api: string): string;
  2. Var S    : string;
  3.   HTTP : TFPHttpClient;
  4. begin
  5.   result:='';
  6.   HTTP := TFPHttpClient.Create(nil);
  7.   HTTP.AllowRedirect:=True;
  8.   HTTP.AddHeader('User-Agent', 'Mozilla/5.0 (compatible; fpweb)');
  9.   HTTP.AddHeader('Content-Type', 'application/json');
  10.   try
  11.     S := HTTP.Get(api);
  12.     writeln(s);
  13.     result:=s;
  14.   finally
  15.     http.free;
  16.   end;
  17. end;                  
  18.  

openssl is installed via homebrew and openssl version -a returns:

OpenSSL 1.1.1h  22 Sep 2020
built on: Sat Oct 10 03:46:20 2020 UTC
platform: darwin64-x86_64-cc
options:  bn(64,64) rc4(16x,int) des(int) idea(int) blowfish(ptr)
compiler: clang -fPIC -arch x86_64 -O3 -Wall -DL_ENDIAN -DOPENSSL_PIC -DOPENSSL_CPUID_OBJ -DOPENSSL_IA32_SSE2 -DOPENSSL_BN_ASM_MONT -DOPENSSL_BN_ASM_MONT5 -DOPENSSL_BN_ASM_GF2m -DSHA1_ASM -DSHA256_ASM -DSHA512_ASM -DKECCAK1600_ASM -DRC4_ASM -DMD5_ASM -DAESNI_ASM -DVPAES_ASM -DGHASH_ASM -DECP_NISTZ256_ASM -DX25519_ASM -DPOLY1305_ASM -D_REENTRANT -DNDEBUG
OPENSSLDIR: "/usr/local/etc/openssl@1.1"
ENGINESDIR: "/usr/local/Cellar/openssl@1.1/1.1.1h/lib/engines-1.1"
Seeding source: os-specific

I've also done
echo 'export PATH="/usr/local/opt/openssl@1.1/bin:$PATH"' >> ~/.zshrc
export LDFLAGS="-L/usr/local/opt/openssl@1.1/lib" 
export CPPFLAGS="-I/usr/local/opt/openssl@1.1/include"

as advised by homebrew and various blogs

I'm testing with https://jsonplaceholder.typicode.com/todos/1 both this and the actual api I want to use work fine on Linux Fedora 28
The error is thrown in procedure TInetSocket.Connect and is ESocketError.

I'm pretty sure the issue is down to something in the openssl installation  but I've tried everything I can think of.  :'(
 
« Last Edit: March 08, 2021, 08:05:06 pm by Frogfather »

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 610
  • Professional amateur ;-P
Re: ESocketError on Mac
« Reply #1 on: March 07, 2021, 08:32:40 pm »
Hi FrogFather,

There's been a change in the way fphttpclient handles SSL.
Pre FPC 3.2.0 you would use ssockets, sslsockets, openssl and fpopenssl, now from FPC 3.2.0 onwards you only need to use opensslsockets.
For Pre FPC 3.2.0 you would also need to add this to the OnGetSocketHandler callback for newer openssl libs:
Code: Pascal  [Select][+][-]
  1.  
  2. {Somewhere else}
  3. begin
  4.   InitSSLInterface;// <-- very important pre 3.2.0, not needed post 3.2.0
  5.   instanceOffpHTTPClient:= TFPHTTPClient.Create;
  6.   instanceOffpHTTPClient.OnGetSocketHandler:=@GetSocketHandler;
  7. end;
  8.  
  9. procedure TSomethingOrOther.GetSocketHandler(Sender: TObject;
  10.   const UseSSL: Boolean; out AHandler: TSocketHandler);
  11. begin
  12.   AHandler := TSSLSocketHandler.Create;
  13.   TSSLSocketHandler(AHandler).SSLType := stTLSv1_2;
  14. end;
  15.  


But this is for Windows/Linux.

I've stumbled on these two threads that mention stuff about macOS:

Hope that helps in any way.

Cheers,
Gus
Lazarus 2.3.0(trunk) FPC 3.3.1(trunk) Ubuntu 21.04 64b Dark Theme
Lazarus 2.0.12(stable) FPC 3.2.2(stable) Ubuntu 21.04 64b Dark Theme
http://github.com/gcarreno

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 610
  • Professional amateur ;-P
Re: ESocketError on Mac
« Reply #2 on: March 07, 2021, 08:36:47 pm »
Hey FrogFather,

Sorry forgot to link to the example where I had to overcome some SSL issues:

https://github.com/wp-xyz/corona/blob/master/source/cdownloadmanager.pas

Pay special attention to the {$IFDEF} wizardry that I had to pull off.

Hope that gives you more insight.

Cheers,
Gus
Lazarus 2.3.0(trunk) FPC 3.3.1(trunk) Ubuntu 21.04 64b Dark Theme
Lazarus 2.0.12(stable) FPC 3.2.2(stable) Ubuntu 21.04 64b Dark Theme
http://github.com/gcarreno

Frogfather

  • New Member
  • *
  • Posts: 21
[Solved] Re: ESocketError on Mac
« Reply #3 on: March 08, 2021, 08:04:46 pm »
Hi Gus,

That fixed it! Many thanks

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 610
  • Professional amateur ;-P
Re: [Solved] Re: ESocketError on Mac
« Reply #4 on: March 08, 2021, 09:17:42 pm »
Hi FrogFather,

That fixed it! Many thanks

You're quite welcome!!

Can I ask what which of my answer solved your problem?
- The cdownloadmanager.pas
- The links to this forum

Cheers,
Gus
Lazarus 2.3.0(trunk) FPC 3.3.1(trunk) Ubuntu 21.04 64b Dark Theme
Lazarus 2.0.12(stable) FPC 3.2.2(stable) Ubuntu 21.04 64b Dark Theme
http://github.com/gcarreno

Frogfather

  • New Member
  • *
  • Posts: 21
Re: [Solved] ESocketError on Mac
« Reply #5 on: May 03, 2021, 09:41:32 pm »
Hi Gus,

Apologies for taking aaaages to reply!

The details in https://forum.lazarus.freepascal.org/index.php/topic,34058.msg396680.html#msg396680
were helpful - I added the missing versions to openssl.pas and recompiled and that fixed it.

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 610
  • Professional amateur ;-P
Re: [Solved] ESocketError on Mac
« Reply #6 on: May 04, 2021, 04:51:21 am »
Hey Frogfather,
Apologies for taking aaaages to reply!

That's quite OK. I'm glad you took the time, thanks!!

The details in https://forum.lazarus.freepascal.org/index.php/topic,34058.msg396680.html#msg396680
were helpful - I added the missing versions to openssl.pas and recompiled and that fixed it.

Okydokes, thanks for that :)

Cheers,
Gus
Lazarus 2.3.0(trunk) FPC 3.3.1(trunk) Ubuntu 21.04 64b Dark Theme
Lazarus 2.0.12(stable) FPC 3.2.2(stable) Ubuntu 21.04 64b Dark Theme
http://github.com/gcarreno

 

TinyPortal © 2005-2018