Lighttpd1.4.20源码分析之插件系统(1)—plugin结构体和插件接口

在lighttpd中,使用插件的形式来增加服务的功能。同时,lighttpd提供了一个插件的公共接口给开发者,方便第三方提供额外的插件。Lighttpd的插件接口主要提供在plugin.h文件中。其中,plugin结构体是最核心的部分。plugin结构体的定义如下:

1typedefstruct

2{

3size_t version;

4

5buffername;/name of the plugin/

6

7void
(init) ();

8handler_t(
set_defaults) (serversrv,voidp_d);

9handler_t(cleanup) (serversrv,voidp_d);

10

11/


12 is called ... 纯虚函数,在子类中要予以赋值。

13/

14handler_t(handle_trigger) (serversrv,voidp_d);/once a second/

15handler_t(
handle_sighup) (serversrv,voidp_d);/at a signup/

16handler_t(handle_uri_raw) (serversrv, connectioncon,voidp_d);/after uri_raw is set/

17handler_t(handle_uri_clean) (serversrv, connectioncon,voidp_d);/after uri is set/

18handler_t(handle_docroot) (serversrv, connectioncon,voidp_d);/getting the document-root/

19handler_t(handle_physical) (serversrv, connectioncon,voidp_d);/mapping url to physical path/

20handler_t(handle_request_done) (serversrv, connectioncon,voidp_d);/at the end of a request/

21handler_t(handle_connection_close) (serversrv, connectioncon,voidp_d);/at the end of a connection/

22handler_t(handle_joblist) (serversrv, connectioncon,voidp_d);/after all events are handled/

23handler_t(handle_subrequest_start) (serversrv, connectioncon,voidp_d);

24

25/

26
when a handler for the request has to be found

27/

28handler_t(
handle_subrequest) (serversrv, connectioncon,voidp_d);//

29handler_t(
connection_reset) (serversrv, connectioncon,voidp_d);//

30void
data;

31

32/

33
dlopen handle

34/

35void
lib;

36} plugin;

可以看出,在结构体plugin的设计中,作者使用了面向对象的思想。plugin结构体就是一个虚基类,其中的数据成员,如name,version等都是子类公有的。而随后的一系列函数指针则是虚函数,这些函数指针在plugin结构体中并没有进行赋值,要求所有的子类必须对其进行赋值。不同的子类对这些函数指针赋不同的值,在进行函数调用的时候就可以实现多态。另外,c语言毕竟不支持面向对象,因此,在通过c实现面向对象的时候大多情况先是要靠人的理解,而不是语言上的约束。如,这里说plugin结构体是一个虚基类,实际上所有的子类都是这个结构体的实例,而子类的实例只有一个,也就是他自己。这就和C++中的子类不同了。在plugin结构体中,version成员比较重要。很明显,这个成员标记这个插件的版本。在plugin结构体中定义的那一系列函数指针是插件的对外接口,也就是插件对lighttpd的接口,lighttpd只知道这些接口,通过调用这些接口来完成工作。随着lighttpd的不断改进,这些接口可能满足不了服务器的要求,因此要对其进行改进,这样就有可能造成以前开发的插件无法使用。通过version成员,在加载插件的时候判断这个插件是否符合当前服务器的版本,也就是接口是否相符。如果不相符,则不加载插件,这样就可以避免由于接口的不相符造成服务器的崩溃等问题。这些函数指针在lighttpd的文档中被称作'hooks'。分为serverwide hooks和connectionwide hooks,serverwide hooks是有服务器调用的,主要处理一些初始化等辅助的工作,包括:init,cleanup, set_defaults, handle_trigger和handle_sighup。connectionwide hooks主要是面向连接的,在处理连接的时候调用这些hooks完成相应的工作。这些hooks大部分在函数http_response_prepare()中被调用。至于这些hooks是在哪被调用,都完成哪些功能,在后面分析具体的插件的时候会详细介绍。有兴趣的读者可以阅读lighttpd源码包中doc文件夹下的plugins文件。在plugin.h中,plugin结构体的定义后面还有一堆的函数声明:1intplugins_load(serversrv);

2voidplugins_free(server
srv);

这两个很明显是加载和释放插件函数。1handler_t plugins_call_handle_uri_raw(serversrv, connectioncon);

2handler_t plugins_call_handle_uri_clean(serversrv, connectioncon);

3handler_t plugins_call_handle_subrequest_start(serversrv, connectioncon);

4handler_t plugins_call_handle_subrequest(serversrv, connectioncon);

5handler_t plugins_call_handle_request_done(serversrv, connectioncon);

6handler_t plugins_call_handle_docroot(serversrv, connectioncon);

7handler_t plugins_call_handle_physical(serversrv, connectioncon);

8handler_t plugins_call_handle_connection_close(serversrv, connectioncon);

9handler_t plugins_call_handle_joblist(serversrv, connectioncon);

10handler_t plugins_call_connection_reset(serversrv, connectioncon);

11

12handler_t plugins_call_handle_trigger(serversrv);

13handler_t plugins_call_handle_sighup(server
srv);

14

15handler_t plugins_call_init(serversrv);

16handler_t plugins_call_set_defaults(server
srv);

17handler_t plugins_call_cleanup(server*srv);


这一系列的plugins_call_XXXXX函数则是插件对外的接口。也就是说,lighttpd服务器通过这些函数,调用插件进行工作。lighttpd在调用插件的时候并不知道到底调用的是哪些插件,而仅仅调用上面的函数。这些函数再调用相应的插件的函数,从而完成工作。具体怎么调用插件的函数,放在后面的文章中介绍。最后面的config_XXXXXX函数是处理一些配置问题,暂不讨论。在plugin.h文件中还定义了一些宏:1#defineSERVER_FUNC(x) \

2statichandler_t x(serversrv,voidp_d)

3#defineCONNECTION_FUNC(x) \

4statichandler_t x(serversrv, connectioncon,voidp_d)

5#defineINIT_FUNC(x) static void
x()

6

7#defineFREE_FUNC SERVER_FUNC

8#defineTRIGGER_FUNC SERVER_FUNC

9#defineSETDEFAULTS_FUNC SERVER_FUNC

10#defineSIGHUP_FUNC SERVER_FUNC

11#defineSUBREQUEST_FUNC CONNECTION_FUNC

12#defineJOBLIST_FUNC CONNECTION_FUNC

13#definePHYSICALPATH_FUNC CONNECTION_FUNC

14#defineREQUESTDONE_FUNC CONNECTION_FUNC

15#defineURIHANDLER_FUNC CONNECTION_FUNC

前面的三个宏(SERVER_FUNC, CONNECTION_FUNC和INIT_FUNC)定义了函数签名的模板。后面的一系列宏和plugin结构体中的函数指针对应,确定这些函数指针所对应的函数签名。在进行插件开发的时候,插件中的函数签名要使用上面的宏来生成。这样可以保证接口的统一。最后,还要提一下plugin.c文件中的结构体:1typedefstruct

2{

3PLUGIN_DATA;

4} plugin_data;

PLUGIN_DATA是一个宏,定义为:#define PLUGIN_DATA size_t id。这个结构体用来存放插件所需要使用的数据,这个结构体作为plugin结构体中函数指针的最后一个参数:void p_d传入对应的函数中。在plugin.c结构体中plugin_data的定义很简单,仅仅包含一个数据成员id。在mod_.c/h文件中,同样也包含有plugin_data结构体的定义。如:mod_cgi.c中,1typedefstruct{

2PLUGIN_DATA;

3buffer_pid_t cgi_pid;

4buffertmp_buf;

5buffer
parse_response;

6plugin_config**config_storage;

7plugin_config conf;

8} plugin_data;

在mod_cml.h中:1typedefstruct{

2PLUGIN_DATA;

3bufferbasedir;

4buffer
baseurl;

5buffertrigger_handler;

6plugin_config
*config_storage;

7plugin_config conf;

8} plugin_data;

等等。这些定义有一个共通的特点,那就是第一个成员都是PLUGIN_DATA。这又是一个技巧。所有这些plugin_data相当于是plugin.c中plugin_data的子类。这些子类开始的部分和父类相同,这就允许子类的指针转换成父类指针,然后再转换回去,并保证数据不会丢失。这样,lighttpd所面对的插件数据接口是plugin.c中定义的plugin_data,当lighttpd在调用插件中的函数,并把数据传进去的时候,插件可以再把数据的类型还原回去。这样,对于lighttpd,所面对的数据接口就只有一个,插件所需要的数据可以不对lighttpd公开,这就很好的隐藏了数据。同时也简化了lighttpd的复杂度,提高了程序的扩展性。下一篇中,将解释lighttpd中插件的加载和初始化。

原文链接: https://www.cnblogs.com/kernel_hcy/archive/2010/03/11/1683809.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/8773

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月6日 下午8:01
下一篇 2023年2月6日 下午8:01

相关推荐