目录
Delphi开发Linux与原生内核有关的内容
本文属于不断更新和完善的内容.......
001、关于TWebBrowser在linux下Get浏览超长URL闪退
001.1、原理
001.2、案例
001.3、启示
001.3.1、最好用平台的服务客制化TCustomWebBrowser控件用接口ICustomBrowser指定User-Agent用户代理
001.3.2、使用默认User-Agent代理的话应当控制URL字节长度
001.3.3、三层服务器的BaseURL路径根的长度要尽量短,为静态资源或动态资源留够足够的拼接长度
001.3.4、在Linux下尽量用Post压缩流的方法而非Get,还可以规避类似&转意为\&等
002、关于Linux文件夹的命名
002.1、Linux同一路径下不允许创建与文件名同名的文件夹
003、如何解决Ubuntu假死机、Kill僵尸进程
如果并非是由Linux平台的服务,动态产生的TCustomWebBrowser控件,则不能指定浏览器的客制化的用户代理User-Agent,那么此时使用的是默认代理,即Embarcadero的代理。
使用默认代理时,TWebBrowser的URL长度不能超过255,其在内核API中定义如下:
D:\Delphi10.4Update2\source\rtl\linux\Linuxapi.KernelDefs.pas
ATADDR_BCAST = __u8(255);
// Translated from linux/include/linux/atalk.h
const
ATPORT_FIRST = 1;
{$EXTERNALSYM ATPORT_FIRST}
ATPORT_RESERVED = 128;
{$EXTERNALSYM ATPORT_RESERVED}
ATPORT_LAST = 254; { 254 is only legal on localtalk }
{$EXTERNALSYM ATPORT_LAST}
ATADDR_ANYNET = __u16(0);
{$EXTERNALSYM ATADDR_ANYNET}
ATADDR_ANYNODE = __u8(0);
{$EXTERNALSYM ATADDR_ANYNODE}
ATADDR_ANYPORT = __u8(0);
{$EXTERNALSYM ATADDR_ANYPORT}
ATADDR_BCAST = __u8(255);
{$EXTERNALSYM ATADDR_BCAST}
DDP_MAXSZ = 587;
{$EXTERNALSYM DDP_MAXSZ}
DDP_MAXHOPS = 15; { 4 bits of hop counter }
{$EXTERNALSYM DDP_MAXHOPS}
SIOCATALKDIFADDR = (SIOCPROTOPRIVATE + 0);
{$EXTERNALSYM SIOCATALKDIFADDR}
type
atalk_addr = {packed} record
s_net: __be16;
s_node: __u8;
end;
{$EXTERNALSYM atalk_addr}
__kernel_sa_family_t = sa_family_t;
{$EXTERNALSYM sa_family_t}
sockaddr_at = {packed} record
sat_family: __kernel_sa_family_t;
sat_port: __u8;
sat_addr: atalk_addr;
sat_zero: packed array[0..8-1] of Byte;
end;
{$EXTERNALSYM sockaddr_at}
atalk_netrange = {packed} record
nr_phase: __u8;
nr_firstnet: __be16;
nr_lastnet: __be16;
end;
{$EXTERNALSYM atalk_netrange}
Embarcadero是从下面的SDK中的Linux的.h头文件进行翻译的:
D:\Delphi10.4Update2\PlatformSDKs\AndroidNDK-21-21.0.38860.1461\android-ndk-r21\sysroot\usr\include\linux\atalk.h
D:\Delphi10.4Update2\PlatformSDKs\AndroidNDK-21-21.0.38860.1461\android-ndk-r21\toolchains\llvm\prebuilt\windows-x86_64\sysroot\usr\include\linux
#define ATADDR_BCAST (__u8) 255
/****************************************************************************
****************************************************************************
***
*** This header was automatically generated from a Linux kernel header
*** of the same name, to make information necessary for userspace to
*** call into the kernel available to libc. It contains only constants,
*** structures, and macros generated from the original header, and thus,
*** contains no copyrightable information.
***
*** To edit the content of this header, modify the corresponding
*** source file (e.g. under external/kernel-headers/original/) then
*** run bionic/libc/kernel/tools/update_all.py
***
*** Any manual change here will be lost the next time this script will
*** be run. You've been warned!
***
****************************************************************************
****************************************************************************/
#ifndef _UAPI__LINUX_ATALK_H__
#define _UAPI__LINUX_ATALK_H__
#include
#include
#include
#define ATPORT_FIRST 1
#define ATPORT_RESERVED 128
#define ATPORT_LAST 254
#define ATADDR_ANYNET (__u16) 0
#define ATADDR_ANYNODE (__u8) 0
#define ATADDR_ANYPORT (__u8) 0
#define ATADDR_BCAST (__u8) 255
#define DDP_MAXSZ 587
#define DDP_MAXHOPS 15
#define SIOCATALKDIFADDR (SIOCPROTOPRIVATE + 0)
struct atalk_addr {
__be16 s_net;
__u8 s_node;
};
struct sockaddr_at {
__kernel_sa_family_t sat_family;
__u8 sat_port;
struct atalk_addr sat_addr;
char sat_zero[8];
};
struct atalk_netrange {
__u8 nr_phase;
__be16 nr_firstnet;
__be16 nr_lastnet;
};
#endif
其中:#define ATADDR_BCAST (__u8) 255
procedure TFormHuanYing.PrivacyClick(Sender: TObject);
//const LURL:string='https://www.cpuofbs.com/pulledup_privacy20201020.html';
//超长的测试:
const LURL:string='https://www.cpuofbs.com/app/PulledupIntraweb/Intraweb_rest_Server_CarveoutAppID/wwwroot/nosessionhtmls/pulledup_privacy20201020.html';
const FilesPref = 'file://';
//ATADDR_BCAST = __u8(255);//:详见:
//D:\Delphi10.4Update2\source\rtl\linux\Linuxapi.KernelDefs.pas
//D:\Delphi10.4Update2\PlatformSDKs\AndroidNDK-21-21.0.38860.1461\android-ndk-r21\sysroot\usr\include\linux\atalk.h
begin
if Sender is TRectangle then
begin
if TRectangle(Sender)=Rectangle_Privacy then
begin
Rectangle_Privacy_WebBrowser.Visible:=true;
WebBrowser_Privacy_WebBrowser.Visible:=true;
if Length(TNetEncoding.URL.Decode(LURL))<128 then
WebBrowser_Privacy_WebBrowser.Navigate(LURL)
else WebBrowser_Privacy_WebBrowser.LoadFromStrings('URL路径超长:'+IntToStr(Length(TNetEncoding.URL.Decode(LURL)))+'个字符,注意Linux下长度必须<255个字节,否则闪退',FilesPref);
end;
if TRectangle(Sender)=Rectangle_Privacy_Btn_Yes then
begin
Circle_Privacy.Fill.Color:=TAlphaColorRec.Red;
Text_Privacy.TextSettings.FontColor:=TAlphaColorRec.White;
Text_Privacy.Text:='我已同意《牵引软件隐私政策》';
Rectangle_Privacy.HitTest:=false;
Circle_Privacy.HitTest:=false;
Text_Privacy.HitTest:=false;
Rectangle_Privacy_WebBrowser.Visible:=false;
WebBrowser_Privacy_WebBrowser.Visible:=false;
Rectangle_Open_App.SetFocus;
end;
if TRectangle(Sender)=Rectangle_Privacy_Btn_No then
begin
Circle_Privacy.Fill.Color:=TAlphaColor($FFE0E0E0);
Text_Privacy.TextSettings.FontColor:=TAlphaColor($FF999999);
Rectangle_Privacy_WebBrowser.Visible:=false;
WebBrowser_Privacy_WebBrowser.Visible:=false;
Rectangle_Open_App.SetFocus;
end;
end;
if Sender is TCircle then
begin
Rectangle_Privacy_WebBrowser.Visible:=true;
WebBrowser_Privacy_WebBrowser.Visible:=true;
if Length(TNetEncoding.URL.Decode(LURL))<128 then
WebBrowser_Privacy_WebBrowser.Navigate(LURL)
else WebBrowser_Privacy_WebBrowser.LoadFromStrings('URL路径超长:'+IntToStr(Length(TNetEncoding.URL.Decode(LURL)))+'个字符,注意Linux下长度必须<255个字节,否则闪退',FilesPref);
end;
if Sender is TText then
begin
Rectangle_Privacy_WebBrowser.Visible:=true;
WebBrowser_Privacy_WebBrowser.Visible:=true;
if Length(TNetEncoding.URL.Decode(LURL))<128 then
WebBrowser_Privacy_WebBrowser.Navigate(LURL)
else WebBrowser_Privacy_WebBrowser.LoadFromStrings('URL路径超长:'+IntToStr(Length(TNetEncoding.URL.Decode(LURL)))+'个字符,注意Linux下长度必须<255个字节,否则闪退',FilesPref);
end;
end;
当使用正常长度的URL时:
const LURL:string='https://www.cpuofbs.com/pulledup_privacy20201020.html';
当使用超长的URL时:
const LURL:string='https://www.cpuofbs.com/app/PulledupIntraweb/Intraweb_rest_Server_CarveoutAppID/wwwroot/nosessionhtmls/privacy.html';
这里,如果代码不做控制处理的话,直接闪退并PAServer控制台输出访问地址冲突:
这些在MSWindows下和Android下,是不会发生的:相应的平台已经做了默认的处理,可以有更大的URL长度来适配。
{$IFDEF Linux}
FmyWBFactoryService:TLinuxWBService;
{$ELSE}
FmyWBFactoryService:TWBFactoryService;
{$ENDIF LINUX}
FmyIFMXWBService:IFMXWBService;
FmyICustomBrowser:ICustomBrowser;
FmyCustomWebBrowser:TCustomWebBrowser;
https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.15
RFC2616 : https://www.w3.org/Protocols/rfc2616/rfc2616.html
Linux同一路径下不允许创建与文件名同名的文件夹,否则提示古怪“cannot find specification path”找不到却不提示有冲突,害得你瞎折腾。看来还是必须认真学操作系统指令。
见我博客的这篇博文:
https://blog.csdn.net/pulledup/article/details/115641315