libsagui.pas 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388
  1. (* _
  2. * ___ __ _ __ _ _ _(_)
  3. * / __|/ _` |/ _` | | | | |
  4. * \__ \ (_| | (_| | |_| | |
  5. * |___/\__,_|\__, |\__,_|_|
  6. * |___/
  7. *
  8. * Cross-platform library which helps to develop web servers or frameworks.
  9. *
  10. * Copyright (c) 2012-2020 Silvio Clecio <[email protected]>
  11. *
  12. * Brook framework is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU Lesser General Public
  14. * License as published by the Free Software Foundation; either
  15. * version 2.1 of the License, or (at your option) any later version.
  16. *
  17. * Brook framework is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. * Lesser General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Lesser General Public
  23. * License along with Brook framework; if not, write to the Free Software
  24. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  25. *)
  26. { Cross-platform low-level Pascal binding for the Sagui library. }
  27. unit libsagui;
  28. {$IFDEF FPC}
  29. {$MODE DELPHI}
  30. {$PACKRECORDS C}
  31. {$ENDIF}
  32. interface
  33. uses
  34. RTLConsts,
  35. SysUtils,
  36. StrUtils,
  37. Math,
  38. Classes,
  39. Contnrs,
  40. {$IFDEF MSWINDOWS}
  41. Windows,
  42. {$ENDIF}
  43. {$IFDEF FPC}
  44. DynLibs,
  45. {$ENDIF}
  46. SyncObjs;
  47. {$IFNDEF FPC}
  48. const
  49. NilHandle = HMODULE(0);
  50. type
  51. TLibHandle = HMODULE;
  52. {$ENDIF}
  53. const
  54. SG_VERSION_MAJOR = 3;
  55. SG_VERSION_MINOR = 1;
  56. SG_VERSION_PATCH = 3;
  57. SG_VERSION_HEX = (SG_VERSION_MAJOR shl 16) or (SG_VERSION_MINOR shl 8) or
  58. SG_VERSION_PATCH;
  59. SG_VERSION_MAJOR_STR = '3';
  60. SG_ERR_SIZE = 256;
  61. {$IF NOT DEFINED(FPC)}
  62. SharedSuffix =
  63. {$IF DEFINED(MSWINDOWS)}
  64. 'dll'
  65. {$ELSEIF DEFINED(MACOS)}
  66. 'dylib'
  67. {$ELSE}
  68. 'so'
  69. {$ENDIF};
  70. {$ENDIF}
  71. SG_LIB_NAME = Concat(
  72. {$IFDEF MSWINDOWS}
  73. 'libsagui-', SG_VERSION_MAJOR_STR
  74. {$ELSE}
  75. 'libsagui'
  76. {$ENDIF}, '.', SharedSuffix
  77. {$IFDEF LINUX}
  78. , '.', SG_VERSION_MAJOR_STR
  79. {$ENDIF}
  80. );
  81. resourcestring
  82. SSgLibEmptyName = 'Empty library name.';
  83. SSgLibNotLoaded = Concat('Library ''%s'' not loaded. If it is not available ',
  84. 'on the system, please download it at: https://github.com/risoflora/libsagui/releases');
  85. SSgLibInvalid = 'Invalid library ''%s''.';
  86. SSgLibVersion = 'Application requires Sagui library v%d.%d.%d or higher.';
  87. SSgUnloadEventAlreadyRegistered = 'Unload event already registered.';
  88. type
  89. cchar = Byte;
  90. Pcchar = MarshaledAString;
  91. cbool = Boolean;
  92. Pcbool = ^Boolean;
  93. cuint16_t = UInt16;
  94. cint = Int32;
  95. cuint = UInt32;
  96. cuint64_t = UInt64;
  97. csize_t = NativeUInt;
  98. cssize_t = NativeInt;
  99. cdouble = Double;
  100. ctime_t = NativeInt;
  101. Pcvoid = Pointer;
  102. PPcvoid = PPointer;
  103. cenum = cint;
  104. cva_list = Pointer;
  105. sg_malloc_func = function(size: csize_t): Pcvoid; cdecl;
  106. sg_realloc_func = function(ptr: Pcvoid; size: csize_t): Pcvoid; cdecl;
  107. sg_free_func = procedure(ptr: Pcvoid); cdecl;
  108. sg_pow_func = function(const x: cdouble; const y: cdouble): cdouble; cdecl;
  109. sg_fmod_func = function(const x: cdouble; const y: cdouble): cdouble; cdecl;
  110. sg_err_cb = procedure(cls: Pcvoid; const err: Pcchar); cdecl;
  111. sg_write_cb = function(handle: Pcvoid; offset: cuint64_t; const buf: Pcchar;
  112. size: csize_t): cssize_t; cdecl;
  113. sg_read_cb = function(handle: Pcvoid; offset: cuint64_t; buf: Pcchar;
  114. size: csize_t): cssize_t; cdecl;
  115. sg_free_cb = procedure(handle: Pcvoid); cdecl;
  116. sg_save_cb = function(handle: Pcvoid; overwritten: cbool): cint; cdecl;
  117. sg_save_as_cb = function(handle: Pcvoid; const path: Pcchar;
  118. overwritten: cbool): cint; cdecl;
  119. var
  120. sg_version: function: cuint; cdecl;
  121. sg_version_str: function: Pcchar; cdecl;
  122. sg_mm_set: function(malloc_func: sg_malloc_func;
  123. realloc_func: sg_realloc_func; free_func: sg_free_func): cint; cdecl;
  124. sg_malloc: function(size: csize_t): Pcvoid; cdecl;
  125. sg_alloc: function(size: csize_t): Pcvoid; cdecl;
  126. sg_realloc: function(ptr: Pcvoid; size: csize_t): Pcvoid; cdecl;
  127. sg_free: procedure(ptr: Pcvoid); cdecl;
  128. sg_math_set: function(pow_func: sg_pow_func;
  129. fmod_func: sg_fmod_func): cint; cdecl;
  130. sg_strerror: function(errnum: cint; errmsg: Pcchar;
  131. errlen: csize_t): Pcchar; cdecl;
  132. sg_is_post: function(const method: Pcchar): cbool; cdecl;
  133. sg_extract_entrypoint: function(const path: Pcchar): Pcchar; cdecl;
  134. sg_tmpdir: function: Pcchar; cdecl;
  135. sg_eor: function(err: cbool): cssize_t; cdecl;
  136. sg_ip: function(const socket: Pcvoid; buf: Pcchar;
  137. size: csize_t): cint; cdecl;
  138. type
  139. Psg_str = ^sg_str;
  140. sg_str = record
  141. end;
  142. var
  143. sg_str_new: function: Psg_str; cdecl;
  144. sg_str_free: procedure(str: Psg_str); cdecl;
  145. sg_str_write: function(str: Psg_str; const val: Pcchar;
  146. len: csize_t): cint; cdecl;
  147. sg_str_printf_va: function(str: Psg_str; const fmt: Pcchar;
  148. ap: cva_list): cint; cdecl;
  149. sg_str_printf: function(str: Psg_str; const fmt: Pcchar): cint; cdecl
  150. {$IFNDEF _FIXINSIGHT_}varargs{$ENDIF};
  151. sg_str_content: function(str: Psg_str): Pcchar; cdecl;
  152. sg_str_length: function(str: Psg_str): csize_t; cdecl;
  153. sg_str_clear: function(str: Psg_str): cint; cdecl;
  154. type
  155. PPsg_strmap = ^Psg_strmap;
  156. Psg_strmap = ^sg_strmap;
  157. sg_strmap = record
  158. end;
  159. sg_strmap_iter_cb = function(cls: Pcvoid; pair: Psg_strmap): cint; cdecl;
  160. sg_strmap_sort_cb = function(cls: Pcvoid; pair_a: Psg_strmap;
  161. pair_b: Psg_strmap): cint; cdecl;
  162. var
  163. sg_strmap_name: function(pair: Psg_strmap): Pcchar; cdecl;
  164. sg_strmap_val: function(pair: Psg_strmap): Pcchar; cdecl;
  165. sg_strmap_add: function(map: PPsg_strmap; const name: Pcchar;
  166. const val: Pcchar): cint; cdecl;
  167. sg_strmap_set: function(map: PPsg_strmap; const name: Pcchar;
  168. const val: Pcchar): cint; cdecl;
  169. sg_strmap_find: function(map: Psg_strmap; const name: Pcchar;
  170. pair: PPsg_strmap): cint; cdecl;
  171. sg_strmap_get: function(map: Psg_strmap; const name: Pcchar): Pcchar; cdecl;
  172. sg_strmap_rm: function(map: PPsg_strmap; const name: Pcchar): cint; cdecl;
  173. sg_strmap_iter: function(map: Psg_strmap; cb: sg_strmap_iter_cb;
  174. cls: Pcvoid): cint; cdecl;
  175. sg_strmap_sort: function(map: PPsg_strmap; cb: sg_strmap_sort_cb;
  176. cls: Pcvoid): cint; cdecl;
  177. sg_strmap_count: function(map: Psg_strmap): cuint; cdecl;
  178. sg_strmap_next: function(next: PPsg_strmap): cint; cdecl;
  179. sg_strmap_cleanup: procedure(map: PPsg_strmap); cdecl;
  180. type
  181. Psg_httpauth = ^sg_httpauth;
  182. sg_httpauth = record
  183. end;
  184. PPsg_httpupld = ^Psg_httpupld;
  185. Psg_httpupld = ^sg_httpupld;
  186. sg_httpupld = record
  187. end;
  188. Psg_httpreq = ^sg_httpreq;
  189. sg_httpreq = record
  190. end;
  191. Psg_httpres = ^sg_httpres;
  192. sg_httpres = record
  193. end;
  194. Psg_httpsrv = ^sg_httpsrv;
  195. sg_httpsrv = record
  196. end;
  197. type
  198. sg_httpsrv_cli_cb = procedure(cls: Pcvoid; const client: Pcvoid;
  199. closed: Pcbool); cdecl;
  200. type
  201. sg_httpauth_cb = function(cls: Pcvoid; auth: Psg_httpauth; req: Psg_httpreq;
  202. res: Psg_httpres): cbool; cdecl;
  203. sg_httpupld_cb = function(cls: Pcvoid; handle: PPcvoid; const dir: Pcchar;
  204. const field: Pcchar; const name: Pcchar; const mime: Pcchar;
  205. const encoding: Pcchar): cint; cdecl;
  206. sg_httpuplds_iter_cb = function(cls: Pcvoid; upld: Psg_httpupld): cint; cdecl;
  207. sg_httpreq_cb = procedure(cls: Pcvoid; req: Psg_httpreq;
  208. res: Psg_httpres); cdecl;
  209. var
  210. sg_httpauth_set_realm: function(auth: Psg_httpauth;
  211. const realm: Pcchar): cint; cdecl;
  212. sg_httpauth_realm: function(auth: Psg_httpauth): pcchar; cdecl;
  213. sg_httpauth_deny2: function(auth: Psg_httpauth; const reason: Pcchar;
  214. const content_type: Pcchar; status: cuint): cint; cdecl;
  215. sg_httpauth_deny: function(auth: Psg_httpauth; const reason: Pcchar;
  216. const content_type: Pcchar): cint; cdecl;
  217. sg_httpauth_cancel: function(auth: Psg_httpauth): cint; cdecl;
  218. sg_httpauth_usr: function(auth: Psg_httpauth): Pcchar; cdecl;
  219. sg_httpauth_pwd: function(auth: Psg_httpauth): Pcchar; cdecl;
  220. var
  221. sg_httpuplds_iter: function(uplds: Psg_httpupld; cb: sg_httpuplds_iter_cb;
  222. cls: Pcvoid): cint; cdecl;
  223. sg_httpuplds_next: function(upld: PPsg_httpupld): cint; cdecl;
  224. sg_httpuplds_count: function(uplds: Psg_httpupld): cuint; cdecl;
  225. var
  226. sg_httpupld_handle: function(uplds: Psg_httpupld): Pcvoid; cdecl;
  227. sg_httpupld_dir: function(uplds: Psg_httpupld): Pcchar; cdecl;
  228. sg_httpupld_field: function(uplds: Psg_httpupld): Pcchar; cdecl;
  229. sg_httpupld_name: function(uplds: Psg_httpupld): Pcchar; cdecl;
  230. sg_httpupld_mime: function(uplds: Psg_httpupld): Pcchar; cdecl;
  231. sg_httpupld_encoding: function(uplds: Psg_httpupld): Pcchar; cdecl;
  232. sg_httpupld_size: function(uplds: Psg_httpupld): cuint64_t; cdecl;
  233. sg_httpupld_save: function(upld: Psg_httpupld;
  234. overwritten: cbool): cint; cdecl;
  235. sg_httpupld_save_as: function(upld: Psg_httpupld; const path: Pcchar;
  236. overwritten: cbool): cint; cdecl;
  237. var
  238. sg_httpreq_srv: function(req: Psg_httpreq): Psg_httpsrv; cdecl;
  239. sg_httpreq_headers: function(req: Psg_httpreq): PPsg_strmap; cdecl;
  240. sg_httpreq_cookies: function(req: Psg_httpreq): PPsg_strmap; cdecl;
  241. sg_httpreq_params: function(req: Psg_httpreq): PPsg_strmap; cdecl;
  242. sg_httpreq_fields: function(req: Psg_httpreq): PPsg_strmap; cdecl;
  243. sg_httpreq_version: function(req: Psg_httpreq): Pcchar; cdecl;
  244. sg_httpreq_method: function(req: Psg_httpreq): Pcchar; cdecl;
  245. sg_httpreq_path: function(req: Psg_httpreq): Pcchar; cdecl;
  246. sg_httpreq_payload: function(req: Psg_httpreq): Psg_str; cdecl;
  247. sg_httpreq_is_uploading: function(req: Psg_httpreq): cbool; cdecl;
  248. sg_httpreq_uploads: function(req: Psg_httpreq): Psg_httpupld; cdecl;
  249. sg_httpreq_client: function(req: Psg_httpreq): Pcvoid; cdecl;
  250. sg_httpreq_tls_session: function(req: Psg_httpreq): Pcvoid; cdecl;
  251. sg_httpreq_isolate: function(req: Psg_httpreq; cb: sg_httpreq_cb;
  252. cls: Pcvoid): cint; cdecl;
  253. sg_httpreq_set_user_data: function(req: Psg_httpreq;
  254. data: Pcvoid): cint; cdecl;
  255. sg_httpreq_user_data: function(req: Psg_httpreq): Pcvoid; cdecl;
  256. var
  257. sg_httpres_headers: function(res: Psg_httpres): PPsg_strmap; cdecl;
  258. sg_httpres_set_cookie: function(res: Psg_httpres; const name: Pcchar;
  259. const val: Pcchar): cint; cdecl;
  260. function sg_httpres_send(res: Psg_httpres; const val: Pcchar;
  261. const content_type: Pcchar; status: cuint): cint; inline;
  262. var
  263. sg_httpres_sendbinary: function(res: Psg_httpres; buf: Pcvoid; size: csize_t;
  264. const content_type: Pcchar; status: cuint): cint; cdecl;
  265. function sg_httpres_download(res: Psg_httpres;
  266. const filename: Pcchar; status: cuint): cint; inline;
  267. function sg_httpres_render(res: Psg_httpres;
  268. const filename: Pcchar; status: cuint): cint; inline;
  269. var
  270. sg_httpres_sendfile2: function(res: Psg_httpres; size: cuint64_t;
  271. max_size: cuint64_t; offset: cuint64_t; const filename: Pcchar;
  272. const disposition: Pcchar; status: cuint): cint; cdecl;
  273. sg_httpres_sendfile: function(res: Psg_httpres; size: cuint64_t;
  274. max_size: cuint64_t; offset: cuint64_t; const filename: Pcchar;
  275. downloaded: cbool; status: cuint): cint; cdecl;
  276. sg_httpres_sendstream: function(res: Psg_httpres; size: cuint64_t;
  277. read_cb: sg_read_cb; handle: Pcvoid; free_cb: sg_free_cb;
  278. status: cuint): cint; cdecl;
  279. function sg_httpres_zsend(res: Psg_httpres; const val: Pcchar;
  280. const content_type: Pcchar; status: cuint): cint; inline;
  281. var
  282. sg_httpres_zsendbinary2: function(res: Psg_httpres; level: cint; buf: Pcvoid;
  283. size: csize_t; const content_type: Pcchar; status: cuint): cint; cdecl;
  284. sg_httpres_zsendbinary: function(res: Psg_httpres; buf: Pcvoid; size: csize_t;
  285. const content_type: Pcchar; status: cuint): cint; cdecl;
  286. sg_httpres_zsendstream2: function(res: Psg_httpres; level: cint;
  287. size: cuint64_t; read_cb: sg_read_cb; handle: Pcvoid; free_cb: sg_free_cb;
  288. status: cuint): cint; cdecl;
  289. sg_httpres_zsendstream: function(res: Psg_httpres; read_cb: sg_read_cb;
  290. handle: Pcvoid; free_cb: sg_free_cb; status: cuint): cint; cdecl;
  291. function sg_httpres_zdownload(res: Psg_httpres;
  292. const filename: Pcchar; status: cuint): cint; inline;
  293. function sg_httpres_zrender(res: Psg_httpres;
  294. const filename: Pcchar; status: cuint): cint; inline;
  295. var
  296. sg_httpres_zsendfile2: function(res: Psg_httpres; level: cint;
  297. size: cuint64_t; max_size: cuint64_t; offset: cuint64_t;
  298. const filename: Pcchar; const disposition: Pcchar;
  299. status: cuint): cint; cdecl;
  300. sg_httpres_zsendfile: function(res: Psg_httpres; size: cuint64_t;
  301. max_size: cuint64_t; offset: cuint64_t; const filename: Pcchar;
  302. downloaded: cbool; status: cuint): cint; cdecl;
  303. sg_httpres_clear: function(res: Psg_httpres): cint; cdecl;
  304. var
  305. sg_httpsrv_new2: function(auth_cb: sg_httpauth_cb; req_cb: sg_httpreq_cb;
  306. err_cb: sg_err_cb; cls: Pcvoid): Psg_httpsrv; cdecl;
  307. sg_httpsrv_new: function(cb: sg_httpreq_cb; cls: Pcvoid): Psg_httpsrv; cdecl;
  308. sg_httpsrv_free: procedure(srv: Psg_httpsrv); cdecl;
  309. sg_httpsrv_tls_listen2: function(srv: Psg_httpsrv; const key: Pcchar;
  310. const pwd: Pcchar; const cert: Pcchar; const trust: Pcchar;
  311. const dhparams: Pcchar; port: cuint16_t; threaded: cbool): cbool; cdecl;
  312. sg_httpsrv_tls_listen: function(srv: Psg_httpsrv; const key: Pcchar;
  313. const cert: Pcchar; port: cuint16_t; threaded: cbool): cbool; cdecl;
  314. sg_httpsrv_listen: function(srv: Psg_httpsrv; port: cuint16_t;
  315. threaded: cbool): cbool; cdecl;
  316. sg_httpsrv_shutdown: function(srv: Psg_httpsrv): cint; cdecl;
  317. sg_httpsrv_port: function(srv: Psg_httpsrv): cuint16_t; cdecl;
  318. sg_httpsrv_is_threaded: function(srv: Psg_httpsrv): cbool; cdecl;
  319. sg_httpsrv_set_cli_cb: function(srv: Psg_httpsrv; cb: sg_httpsrv_cli_cb;
  320. cls: Pcvoid): cint; cdecl;
  321. sg_httpsrv_set_upld_cbs: function(srv: Psg_httpsrv; cb: sg_httpupld_cb;
  322. cls: Pcvoid; write_cb: sg_write_cb; free_cb: sg_free_cb;
  323. save_cb: sg_save_cb; save_as_cb: sg_save_as_cb): cint; cdecl;
  324. sg_httpsrv_set_upld_dir: function(srv: Psg_httpsrv;
  325. const dir: Pcchar): cint; cdecl;
  326. sg_httpsrv_upld_dir: function(srv: Psg_httpsrv): Pcchar; cdecl;
  327. sg_httpsrv_set_post_buf_size: function(srv: Psg_httpsrv;
  328. size: csize_t): cint; cdecl;
  329. sg_httpsrv_post_buf_size: function(srv: Psg_httpsrv): csize_t; cdecl;
  330. sg_httpsrv_set_payld_limit: function(srv: Psg_httpsrv;
  331. limit: csize_t): cint; cdecl;
  332. sg_httpsrv_payld_limit: function(srv: Psg_httpsrv): csize_t; cdecl;
  333. sg_httpsrv_set_uplds_limit: function(srv: Psg_httpsrv;
  334. limit: cuint64_t): cint; cdecl;
  335. sg_httpsrv_uplds_limit: function(srv: Psg_httpsrv): cuint64_t; cdecl;
  336. sg_httpsrv_set_thr_pool_size: function(srv: Psg_httpsrv;
  337. size: cuint): cint; cdecl;
  338. sg_httpsrv_thr_pool_size: function(srv: Psg_httpsrv): cuint; cdecl;
  339. sg_httpsrv_set_con_timeout: function(srv: Psg_httpsrv;
  340. timeout: cuint): cint; cdecl;
  341. sg_httpsrv_con_timeout: function(srv: Psg_httpsrv): cuint; cdecl;
  342. sg_httpsrv_set_con_limit: function(srv: Psg_httpsrv;
  343. limit: cuint): cint; cdecl;
  344. sg_httpsrv_con_limit: function(srv: Psg_httpsrv): cuint; cdecl;
  345. sg_httpsrv_handle: function(srv: Psg_httpsrv): Pcvoid; cdecl;
  346. type
  347. PPsg_entrypoint = ^Psg_entrypoint;
  348. Psg_entrypoint = ^sg_entrypoint;
  349. sg_entrypoint = record
  350. end;
  351. var
  352. sg_entrypoint_name: function(entrypoint: Psg_entrypoint): Pcchar; cdecl;
  353. sg_entrypoint_set_user_data: function(entrypoint: Psg_entrypoint;
  354. user_data: Pcvoid): cint; cdecl;
  355. sg_entrypoint_user_data: function(entrypoint: Psg_entrypoint): Pcvoid; cdecl;
  356. type
  357. Psg_entrypoints = ^sg_entrypoints;
  358. sg_entrypoints = record
  359. end;
  360. sg_entrypoints_iter_cb = function(cls: Pcvoid;
  361. entrypoint: Psg_entrypoint): cint; cdecl;
  362. var
  363. sg_entrypoints_new: function: Psg_entrypoints; cdecl;
  364. sg_entrypoints_free: procedure(entrypoints: Psg_entrypoints); cdecl;
  365. sg_entrypoints_add: function(entrypoints: Psg_entrypoints; const path: Pcchar;
  366. user_data: Pcvoid): cint; cdecl;
  367. sg_entrypoints_rm: function(entrypoints: Psg_entrypoints;
  368. const path: Pcchar): cint; cdecl;
  369. sg_entrypoints_iter: function(entrypoints: Psg_entrypoints;
  370. cb: sg_entrypoints_iter_cb; cls: Pcvoid): cint; cdecl;
  371. sg_entrypoints_clear: function(entrypoints: Psg_entrypoints): cint; cdecl;
  372. sg_entrypoints_find: function(entrypoints: Psg_entrypoints;
  373. entrypoint: PPsg_entrypoint; const path: Pcchar): cint; cdecl;
  374. type
  375. PPsg_route = ^Psg_route;
  376. Psg_route = ^sg_route;
  377. sg_route = record
  378. end;
  379. sg_segments_iter_cb = function(cls: Pcvoid; index: cuint;
  380. const segment: Pcchar): cint; cdecl;
  381. sg_vars_iter_cb = function(cls: Pcvoid; const name: Pcchar;
  382. const val: Pcchar): cint; cdecl;
  383. sg_route_cb = procedure(cls: Pcvoid; route: Psg_route); cdecl;
  384. sg_routes_iter_cb = function(cls: Pcvoid; route: Psg_route): cint; cdecl;
  385. var
  386. sg_route_handle: function(route: Psg_route): Pcvoid; cdecl;
  387. sg_route_match: function(route: Psg_route): Pcvoid; cdecl;
  388. sg_route_rawpattern: function(route: Psg_route): Pcchar; cdecl;
  389. sg_route_pattern: function(route: Psg_route): Pcchar; cdecl;
  390. sg_route_path: function(route: Psg_route): Pcchar; cdecl;
  391. sg_route_segments_iter: function(route: Psg_route; cb: sg_segments_iter_cb;
  392. cls: Pcvoid): cint; cdecl;
  393. sg_route_vars_iter: function(route: Psg_route; cb: sg_vars_iter_cb;
  394. cls: Pcvoid): cint; cdecl;
  395. sg_route_user_data: function(route: Psg_route): Pcvoid; cdecl;
  396. var
  397. sg_routes_add2: function(routes: PPsg_route; route: PPsg_route;
  398. const pattern: Pcchar; errmsg: Pcchar; errlen: csize_t;
  399. cb: sg_route_cb; cls: Pcvoid): cint; cdecl;
  400. sg_routes_add: function(routes: Psg_route; const pattern: Pcchar;
  401. cb: sg_route_cb; cls: Pcvoid): cint; cdecl;
  402. sg_routes_rm: function(routes: PPsg_route;
  403. const pattern: Pcchar): cint; cdecl;
  404. sg_routes_iter: function(routes: Psg_route; cb: sg_routes_iter_cb;
  405. cls: Pcvoid): cint; cdecl;
  406. sg_routes_next: function(route: PPsg_route): cint; cdecl;
  407. sg_routes_count: function(routes: Psg_route): cuint; cdecl;
  408. sg_routes_cleanup: function(routes: PPsg_route): cint; cdecl;
  409. type
  410. Psg_router = ^sg_router;
  411. sg_router = record
  412. end;
  413. sg_router_dispatch_cb = function(cls: Pcvoid; const path: Pcchar;
  414. route: Psg_route): cint; cdecl;
  415. sg_router_match_cb = function(cls: Pcvoid; route: Psg_route): cint; cdecl;
  416. var
  417. sg_router_new: function(routes: Psg_route): Psg_router; cdecl;
  418. sg_router_free: procedure(router: Psg_router); cdecl;
  419. sg_router_dispatch2: function(router: Psg_router; const path: Pcchar;
  420. user_data: Pcvoid; dispatch_cb: sg_router_dispatch_cb; cls: Pcvoid;
  421. match_cb: sg_router_match_cb): cint; cdecl;
  422. sg_router_dispatch: function(router: Psg_router; const path: Pcchar;
  423. user_data: Pcvoid): cint; cdecl;
  424. type
  425. Psg_expr = ^sg_expr;
  426. sg_expr = record
  427. end;
  428. type
  429. sg_expr_err_type = cenum;
  430. const
  431. SG_EXPR_ERR_UNKNOWN = 0;
  432. SG_EXPR_ERR_UNEXPECTED_NUMBER = 1;
  433. SG_EXPR_ERR_UNEXPECTED_WORD = 2;
  434. SG_EXPR_ERR_UNEXPECTED_PARENS = 3;
  435. SG_EXPR_ERR_MISSING_OPERAND = 4;
  436. SG_EXPR_ERR_UNKNOWN_OPERATOR = 5;
  437. SG_EXPR_ERR_INVALID_FUNC_NAME = 6;
  438. SG_EXPR_ERR_BAD_PARENS = 7;
  439. SG_EXPR_ERR_TOO_FEW_FUNC_ARGS = 8;
  440. SG_EXPR_ERR_FIRST_ARG_IS_NOT_VAR = 9;
  441. SG_EXPR_ERR_BAD_VARIABLE_NAME = 10;
  442. SG_EXPR_ERR_BAD_ASSIGNMENT = 11;
  443. type
  444. Psg_expr_argument = ^sg_expr_argument;
  445. sg_expr_argument = record
  446. end;
  447. type
  448. sg_expr_func = function(cls: Pcvoid; args: Psg_expr_argument;
  449. const identifier: Pcchar): cdouble; cdecl;
  450. type
  451. Psg_expr_extension = ^sg_expr_extension;
  452. sg_expr_extension = record
  453. func: sg_expr_func;
  454. identifier: Pcchar;
  455. cls: Pcvoid;
  456. end;
  457. var
  458. sg_expr_new: function: Psg_expr; cdecl;
  459. sg_expr_free: procedure(expr: Psg_expr); cdecl;
  460. sg_expr_compile: function(expr: Psg_expr; const str: Pcchar; len: csize_t;
  461. extensions: Psg_expr_extension): cint; cdecl;
  462. sg_expr_clear: function(expr: Psg_expr): cint; cdecl;
  463. sg_expr_eval: function(expr: Psg_expr): cdouble; cdecl;
  464. sg_expr_var: function(expr: Psg_expr; const name: Pcchar;
  465. len: csize_t): cdouble; cdecl;
  466. sg_expr_set_var: function(expr: Psg_expr; const name: Pcchar; len: csize_t;
  467. const val: cdouble): cint; cdecl;
  468. sg_expr_arg: function(args: Psg_expr_argument; index: cint): cdouble; cdecl;
  469. sg_expr_near: function(expr: Psg_expr): cint; cdecl;
  470. sg_expr_err: function(expr: Psg_expr): sg_expr_err_type; cdecl;
  471. sg_expr_strerror: function(expr: Psg_expr): Pcchar; cdecl;
  472. sg_expr_calc: function(const str: Pcchar; len: csize_t): cdouble; cdecl;
  473. type
  474. { ESgLibNotLoaded }
  475. ESgLibNotLoaded = class(EFileNotFoundException);
  476. { ESgUnloadEvent }
  477. ESgUnloadEvent = class(Exception);
  478. { TSgUnloadEvents }
  479. TSgUnloadEvents = class sealed
  480. private
  481. FCS: TCriticalSection;
  482. FList: TObjectList;
  483. protected
  484. property CS: TCriticalSection read FCS;
  485. property List: TObjectList read FList;
  486. public
  487. constructor Create(ACS: TCriticalSection); virtual;
  488. destructor Destroy; override;
  489. procedure Add(AEvent: TNotifyEvent; ASender: TObject); virtual;
  490. procedure Remove(AEvent: TNotifyEvent); virtual;
  491. procedure Clear; virtual;
  492. procedure Call; virtual;
  493. end;
  494. { SgLib }
  495. SgLib = record
  496. private class var
  497. GCS: TCriticalSection;
  498. GUnloadEvents: TSgUnloadEvents;
  499. GLastName: TFileName;
  500. GHandle: TLibHandle;
  501. private
  502. class function InternalLoad(
  503. const AName: TFileName): TLibHandle; static;
  504. public
  505. class procedure Init; static;
  506. class procedure Done; static;
  507. class function GetLastName: string; static;
  508. class procedure CheckVersion(AVersion: Integer); overload; static;
  509. class procedure CheckVersion; overload; static; inline;
  510. class procedure CheckLastError(ALastError: Integer); static; inline;
  511. class function Load(const AName: TFileName): TLibHandle; static;
  512. class function Unload: TLibHandle; static;
  513. class function IsLoaded: Boolean; static;
  514. class procedure Check; static;
  515. class property UnloadEvents: TSgUnloadEvents read GUnloadEvents;
  516. class property Handle: TLibHandle read GHandle;
  517. end;
  518. function cpow(const X, Y: cdouble): cdouble; cdecl; inline;
  519. function cfmod(const X, Y: cdouble): cdouble; cdecl; inline;
  520. implementation
  521. function SameNotifyEvent(AN1, AN2: TNotifyEvent): Boolean; inline;
  522. begin
  523. Result := (TMethod(AN1).Code = TMethod(AN2).Code) and
  524. (TMethod(AN1).Data = TMethod(AN2).Data);
  525. end;
  526. function cpow(const X, Y: cdouble): cdouble;
  527. begin
  528. Result := Power(X, Y);
  529. end;
  530. function cfmod(const X, Y: cdouble): cdouble;
  531. begin
  532. Result := X - Y * Int(X / Y);
  533. end;
  534. function sg_httpres_send(res: Psg_httpres; const val: Pcchar;
  535. const content_type: Pcchar; status: cuint): cint;
  536. var
  537. len: csize_t;
  538. begin
  539. if Assigned(val) then
  540. len := Length(val)
  541. else
  542. len := 0;
  543. Result := sg_httpres_sendbinary(res, val, len, content_type, status);
  544. end;
  545. function sg_httpres_download(res: Psg_httpres; const filename: Pcchar;
  546. status: cuint): cint;
  547. begin
  548. Result := sg_httpres_sendfile2(res, 0, 0, 0, filename, 'attachment', status);
  549. end;
  550. function sg_httpres_render(res: Psg_httpres; const filename: Pcchar;
  551. status: cuint): cint;
  552. begin
  553. Result := sg_httpres_sendfile2(res, 0, 0, 0, filename, 'inline', status);
  554. end;
  555. function sg_httpres_zsend(res: Psg_httpres; const val: Pcchar;
  556. const content_type: Pcchar; status: cuint): cint;
  557. var
  558. len: csize_t;
  559. begin
  560. if Assigned(val) then
  561. len := Length(val)
  562. else
  563. len := 0;
  564. Result := sg_httpres_zsendbinary(res, val, len, content_type, status);
  565. end;
  566. function sg_httpres_zdownload(res: Psg_httpres; const filename: Pcchar;
  567. status: cuint): cint;
  568. begin
  569. Result := sg_httpres_zsendfile2(res, 1, 0, 0, 0, filename, 'attachment',
  570. status);
  571. end;
  572. function sg_httpres_zrender(res: Psg_httpres; const filename: Pcchar;
  573. status: cuint): cint;
  574. begin
  575. Result := sg_httpres_zsendfile2(res, 1, 0, 0, 0, filename, 'inline', status);
  576. end;
  577. { TSgLibUnloadHolder }
  578. type
  579. TSgLibUnloadHolder = class
  580. private
  581. FEvent: TNotifyEvent;
  582. FSender: TObject;
  583. public
  584. constructor Create(AEvent: TNotifyEvent; ASender: TObject);
  585. property Event: TNotifyEvent read FEvent;
  586. property Sender: TObject read FSender;
  587. end;
  588. constructor TSgLibUnloadHolder.Create(AEvent: TNotifyEvent;
  589. ASender: TObject);
  590. begin
  591. inherited Create;
  592. FEvent := AEvent;
  593. FSender := ASender;
  594. end;
  595. { TSgUnloadEvents }
  596. constructor TSgUnloadEvents.Create(ACS: TCriticalSection);
  597. begin
  598. inherited Create;
  599. if not Assigned(ACS) then
  600. raise EArgumentNilException.CreateFmt(SParamIsNil, ['ACS']);
  601. FList := TObjectList.Create;
  602. FCS := ACS;
  603. end;
  604. destructor TSgUnloadEvents.Destroy;
  605. begin
  606. Clear;
  607. FList.Free;
  608. inherited Destroy;
  609. end;
  610. procedure TSgUnloadEvents.Add(AEvent: TNotifyEvent; ASender: TObject);
  611. var
  612. I: Integer;
  613. begin
  614. if not Assigned(AEvent) then
  615. raise EArgumentNilException.CreateFmt(SParamIsNil, ['AEvent']);
  616. FCS.Acquire;
  617. try
  618. for I := 0 to Pred(FList.Count) do
  619. if SameNotifyEvent(TSgLibUnloadHolder(FList[I]).Event, AEvent) then
  620. raise ESgUnloadEvent.Create(SSgUnloadEventAlreadyRegistered);
  621. FList.Add(TSgLibUnloadHolder.Create(AEvent, ASender));
  622. finally
  623. FCS.Release;
  624. end;
  625. end;
  626. procedure TSgUnloadEvents.Remove(AEvent: TNotifyEvent);
  627. var
  628. I: Integer;
  629. begin
  630. if not Assigned(AEvent) then
  631. raise EArgumentNilException.CreateFmt(SParamIsNil, ['AEvent']);
  632. FCS.Acquire;
  633. try
  634. for I := 0 to Pred(FList.Count) do
  635. if SameNotifyEvent(TSgLibUnloadHolder(FList[I]).Event, AEvent) then
  636. begin
  637. FList.Delete(I);
  638. Break;
  639. end;
  640. finally
  641. FCS.Release;
  642. end;
  643. end;
  644. procedure TSgUnloadEvents.Clear;
  645. begin
  646. FCS.Acquire;
  647. try
  648. FList.Clear;
  649. finally
  650. FCS.Release;
  651. end;
  652. end;
  653. procedure TSgUnloadEvents.Call;
  654. var
  655. H: TSgLibUnloadHolder;
  656. I: Integer;
  657. begin
  658. FCS.Acquire;
  659. try
  660. for I := Pred(FList.Count) downto 0 do
  661. begin
  662. H := FList[I] as TSgLibUnloadHolder;
  663. H.Event(H.Sender);
  664. end;
  665. finally
  666. FCS.Release;
  667. end;
  668. end;
  669. { SgLib }
  670. class procedure SgLib.Init;
  671. begin
  672. GCS := TCriticalSection.Create;
  673. GUnloadEvents := TSgUnloadEvents.Create(GCS);
  674. InternalLoad(SG_LIB_NAME);
  675. end;
  676. class procedure SgLib.Done;
  677. begin
  678. GCS.Acquire;
  679. try
  680. Unload;
  681. GUnloadEvents.Free;
  682. finally
  683. GCS.Release;
  684. GCS.Free;
  685. end;
  686. end;
  687. class function SgLib.GetLastName: string;
  688. begin
  689. Result := GLastName;
  690. end;
  691. class procedure SgLib.CheckVersion(AVersion: Integer);
  692. var
  693. N: cint;
  694. begin
  695. try
  696. if not Assigned(sg_version) then
  697. raise EInvalidOpException.CreateFmt(SSgLibInvalid, [GetLastName]);
  698. N := ((AVersion shr 8) and $FF);
  699. if (((AVersion shr 16) and $FF) <> SG_VERSION_MAJOR) or
  700. (N < SG_VERSION_MINOR) or (((AVersion and $FF) < SG_VERSION_PATCH) and
  701. (N <= SG_VERSION_MINOR)) then
  702. raise EInvalidOpException.CreateFmt(SSgLibVersion, [SG_VERSION_MAJOR,
  703. SG_VERSION_MINOR, SG_VERSION_PATCH]);
  704. except
  705. Unload;
  706. raise;
  707. end;
  708. end;
  709. class procedure SgLib.CheckVersion;
  710. begin
  711. CheckVersion(sg_version);
  712. end;
  713. class procedure SgLib.CheckLastError(ALastError: Integer);
  714. var
  715. E: EOSError;
  716. P: array[0..SG_ERR_SIZE-1] of cchar;
  717. S: string;
  718. begin
  719. if (ALastError = 0) or (not Assigned(sg_strerror)) then
  720. Exit;
  721. sg_strerror(ALastError, @P[0], SG_ERR_SIZE);
  722. {$IFDEF FPC}
  723. SetString(S, @P[0], Length(Pcchar(@P[0])));
  724. SetCodePage(RawByteString(S), CP_UTF8, False);
  725. {$ELSE}
  726. S := TMarshal.ReadStringAsUtf8(TPtrWrapper.Create(@P[0]));
  727. {$ENDIF}
  728. E := EOSError.Create(S.TrimRight);
  729. E.ErrorCode := ALastError;
  730. raise E;
  731. end;
  732. class function SgLib.InternalLoad(const AName: TFileName): TLibHandle;
  733. begin //FI:C101
  734. GCS.Acquire;
  735. try
  736. if GHandle <> NilHandle then
  737. Exit(GHandle);
  738. GHandle := SafeLoadLibrary(AName);
  739. if GHandle = NilHandle then
  740. Exit(NilHandle);
  741. GLastName := AName;
  742. sg_version := GetProcAddress(GHandle, 'sg_version');
  743. sg_version_str := GetProcAddress(GHandle, 'sg_version_str');
  744. CheckVersion;
  745. sg_mm_set := GetProcAddress(GHandle, 'sg_mm_set');
  746. sg_malloc := GetProcAddress(GHandle, 'sg_malloc');
  747. sg_alloc := GetProcAddress(GHandle, 'sg_alloc');
  748. sg_realloc := GetProcAddress(GHandle, 'sg_realloc');
  749. sg_free := GetProcAddress(GHandle, 'sg_free');
  750. sg_math_set := GetProcAddress(GHandle, 'sg_math_set');
  751. sg_strerror := GetProcAddress(GHandle, 'sg_strerror');
  752. sg_is_post := GetProcAddress(GHandle, 'sg_is_post');
  753. sg_extract_entrypoint := GetProcAddress(GHandle, 'sg_extract_entrypoint');
  754. sg_tmpdir := GetProcAddress(GHandle, 'sg_tmpdir');
  755. sg_eor := GetProcAddress(GHandle, 'sg_eor');
  756. sg_ip := GetProcAddress(GHandle, 'sg_ip');
  757. sg_str_new := GetProcAddress(GHandle, 'sg_str_new');
  758. sg_str_free := GetProcAddress(GHandle, 'sg_str_free');
  759. sg_str_write := GetProcAddress(GHandle, 'sg_str_write');
  760. sg_str_printf_va := GetProcAddress(GHandle, 'sg_str_printf_va');
  761. sg_str_printf := GetProcAddress(GHandle, 'sg_str_printf');
  762. sg_str_content := GetProcAddress(GHandle, 'sg_str_content');
  763. sg_str_length := GetProcAddress(GHandle, 'sg_str_length');
  764. sg_str_clear := GetProcAddress(GHandle, 'sg_str_clear');
  765. sg_strmap_name := GetProcAddress(GHandle, 'sg_strmap_name');
  766. sg_strmap_val := GetProcAddress(GHandle, 'sg_strmap_val');
  767. sg_strmap_add := GetProcAddress(GHandle, 'sg_strmap_add');
  768. sg_strmap_set := GetProcAddress(GHandle, 'sg_strmap_set');
  769. sg_strmap_find := GetProcAddress(GHandle, 'sg_strmap_find');
  770. sg_strmap_get := GetProcAddress(GHandle, 'sg_strmap_get');
  771. sg_strmap_rm := GetProcAddress(GHandle, 'sg_strmap_rm');
  772. sg_strmap_iter := GetProcAddress(GHandle, 'sg_strmap_iter');
  773. sg_strmap_sort := GetProcAddress(GHandle, 'sg_strmap_sort');
  774. sg_strmap_count := GetProcAddress(GHandle, 'sg_strmap_count');
  775. sg_strmap_next := GetProcAddress(GHandle, 'sg_strmap_next');
  776. sg_strmap_cleanup := GetProcAddress(GHandle, 'sg_strmap_cleanup');
  777. sg_httpauth_set_realm := GetProcAddress(GHandle, 'sg_httpauth_set_realm');
  778. sg_httpauth_realm := GetProcAddress(GHandle, 'sg_httpauth_realm');
  779. sg_httpauth_deny2 := GetProcAddress(GHandle, 'sg_httpauth_deny2');
  780. sg_httpauth_deny := GetProcAddress(GHandle, 'sg_httpauth_deny');
  781. sg_httpauth_cancel := GetProcAddress(GHandle, 'sg_httpauth_cancel');
  782. sg_httpauth_usr := GetProcAddress(GHandle, 'sg_httpauth_usr');
  783. sg_httpauth_pwd := GetProcAddress(GHandle, 'sg_httpauth_pwd');
  784. sg_httpuplds_iter := GetProcAddress(GHandle, 'sg_httpuplds_iter');
  785. sg_httpuplds_next := GetProcAddress(GHandle, 'sg_httpuplds_next');
  786. sg_httpuplds_count := GetProcAddress(GHandle, 'sg_httpuplds_count');
  787. sg_httpupld_handle := GetProcAddress(GHandle, 'sg_httpupld_handle');
  788. sg_httpupld_dir := GetProcAddress(GHandle, 'sg_httpupld_dir');
  789. sg_httpupld_field := GetProcAddress(GHandle, 'sg_httpupld_field');
  790. sg_httpupld_name := GetProcAddress(GHandle, 'sg_httpupld_name');
  791. sg_httpupld_mime := GetProcAddress(GHandle, 'sg_httpupld_mime');
  792. sg_httpupld_encoding := GetProcAddress(GHandle, 'sg_httpupld_encoding');
  793. sg_httpupld_size := GetProcAddress(GHandle, 'sg_httpupld_size');
  794. sg_httpupld_save := GetProcAddress(GHandle, 'sg_httpupld_save');
  795. sg_httpupld_save_as := GetProcAddress(GHandle, 'sg_httpupld_save_as');
  796. sg_httpreq_srv := GetProcAddress(GHandle, 'sg_httpreq_srv');
  797. sg_httpreq_headers := GetProcAddress(GHandle, 'sg_httpreq_headers');
  798. sg_httpreq_cookies := GetProcAddress(GHandle, 'sg_httpreq_cookies');
  799. sg_httpreq_params := GetProcAddress(GHandle, 'sg_httpreq_params');
  800. sg_httpreq_fields := GetProcAddress(GHandle, 'sg_httpreq_fields');
  801. sg_httpreq_version := GetProcAddress(GHandle, 'sg_httpreq_version');
  802. sg_httpreq_method := GetProcAddress(GHandle, 'sg_httpreq_method');
  803. sg_httpreq_path := GetProcAddress(GHandle, 'sg_httpreq_path');
  804. sg_httpreq_payload := GetProcAddress(GHandle, 'sg_httpreq_payload');
  805. sg_httpreq_is_uploading := GetProcAddress(GHandle, 'sg_httpreq_is_uploading');
  806. sg_httpreq_uploads := GetProcAddress(GHandle, 'sg_httpreq_uploads');
  807. sg_httpreq_client := GetProcAddress(GHandle, 'sg_httpreq_client');
  808. sg_httpreq_tls_session := GetProcAddress(GHandle, 'sg_httpreq_tls_session');
  809. sg_httpreq_isolate := GetProcAddress(GHandle, 'sg_httpreq_isolate');
  810. sg_httpreq_set_user_data := GetProcAddress(GHandle, 'sg_httpreq_set_user_data');
  811. sg_httpreq_user_data := GetProcAddress(GHandle, 'sg_httpreq_user_data');
  812. sg_httpres_headers := GetProcAddress(GHandle, 'sg_httpres_headers');
  813. sg_httpres_set_cookie := GetProcAddress(GHandle, 'sg_httpres_set_cookie');
  814. sg_httpres_sendbinary := GetProcAddress(GHandle, 'sg_httpres_sendbinary');
  815. sg_httpres_sendfile2 := GetProcAddress(GHandle, 'sg_httpres_sendfile2');
  816. sg_httpres_sendfile := GetProcAddress(GHandle, 'sg_httpres_sendfile');
  817. sg_httpres_sendstream := GetProcAddress(GHandle, 'sg_httpres_sendstream');
  818. sg_httpres_zsendbinary2 := GetProcAddress(GHandle, 'sg_httpres_zsendbinary2');
  819. sg_httpres_zsendbinary := GetProcAddress(GHandle, 'sg_httpres_zsendbinary');
  820. sg_httpres_zsendstream := GetProcAddress(GHandle, 'sg_httpres_zsendstream');
  821. sg_httpres_zsendstream2 := GetProcAddress(GHandle, 'sg_httpres_zsendstream2');
  822. sg_httpres_zsendfile2 := GetProcAddress(GHandle, 'sg_httpres_zsendfile2');
  823. sg_httpres_zsendfile := GetProcAddress(GHandle, 'sg_httpres_zsendfile');
  824. sg_httpres_clear := GetProcAddress(GHandle, 'sg_httpres_clear');
  825. sg_httpsrv_new2 := GetProcAddress(GHandle, 'sg_httpsrv_new2');
  826. sg_httpsrv_new := GetProcAddress(GHandle, 'sg_httpsrv_new');
  827. sg_httpsrv_free := GetProcAddress(GHandle, 'sg_httpsrv_free');
  828. sg_httpsrv_tls_listen2 := GetProcAddress(GHandle, 'sg_httpsrv_tls_listen2');
  829. sg_httpsrv_tls_listen := GetProcAddress(GHandle, 'sg_httpsrv_tls_listen');
  830. sg_httpsrv_listen := GetProcAddress(GHandle, 'sg_httpsrv_listen');
  831. sg_httpsrv_shutdown := GetProcAddress(GHandle, 'sg_httpsrv_shutdown');
  832. sg_httpsrv_port := GetProcAddress(GHandle, 'sg_httpsrv_port');
  833. sg_httpsrv_is_threaded := GetProcAddress(GHandle, 'sg_httpsrv_is_threaded');
  834. sg_httpsrv_set_cli_cb := GetProcAddress(GHandle, 'sg_httpsrv_set_cli_cb');
  835. sg_httpsrv_set_upld_cbs := GetProcAddress(GHandle, 'sg_httpsrv_set_upld_cbs');
  836. sg_httpsrv_set_upld_dir := GetProcAddress(GHandle, 'sg_httpsrv_set_upld_dir');
  837. sg_httpsrv_upld_dir := GetProcAddress(GHandle, 'sg_httpsrv_upld_dir');
  838. sg_httpsrv_set_post_buf_size := GetProcAddress(GHandle, 'sg_httpsrv_set_post_buf_size');
  839. sg_httpsrv_post_buf_size := GetProcAddress(GHandle, 'sg_httpsrv_post_buf_size');
  840. sg_httpsrv_set_payld_limit := GetProcAddress(GHandle, 'sg_httpsrv_set_payld_limit');
  841. sg_httpsrv_payld_limit := GetProcAddress(GHandle, 'sg_httpsrv_payld_limit');
  842. sg_httpsrv_set_uplds_limit := GetProcAddress(GHandle, 'sg_httpsrv_set_uplds_limit');
  843. sg_httpsrv_uplds_limit := GetProcAddress(GHandle, 'sg_httpsrv_uplds_limit');
  844. sg_httpsrv_set_thr_pool_size := GetProcAddress(GHandle, 'sg_httpsrv_set_thr_pool_size');
  845. sg_httpsrv_thr_pool_size := GetProcAddress(GHandle, 'sg_httpsrv_thr_pool_size');
  846. sg_httpsrv_set_con_timeout := GetProcAddress(GHandle, 'sg_httpsrv_set_con_timeout');
  847. sg_httpsrv_con_timeout := GetProcAddress(GHandle, 'sg_httpsrv_con_timeout');
  848. sg_httpsrv_set_con_limit := GetProcAddress(GHandle, 'sg_httpsrv_set_con_limit');
  849. sg_httpsrv_con_limit := GetProcAddress(GHandle, 'sg_httpsrv_con_limit');
  850. sg_httpsrv_handle := GetProcAddress(GHandle, 'sg_httpsrv_handle');
  851. sg_entrypoint_name := GetProcAddress(GHandle, 'sg_entrypoint_name');
  852. sg_entrypoint_set_user_data := GetProcAddress(GHandle, 'sg_entrypoint_set_user_data');
  853. sg_entrypoint_user_data := GetProcAddress(GHandle, 'sg_entrypoint_user_data');
  854. sg_entrypoints_new := GetProcAddress(GHandle, 'sg_entrypoints_new');
  855. sg_entrypoints_free := GetProcAddress(GHandle, 'sg_entrypoints_free');
  856. sg_entrypoints_add := GetProcAddress(GHandle, 'sg_entrypoints_add');
  857. sg_entrypoints_rm := GetProcAddress(GHandle, 'sg_entrypoints_rm');
  858. sg_entrypoints_iter := GetProcAddress(GHandle, 'sg_entrypoints_iter');
  859. sg_entrypoints_clear := GetProcAddress(GHandle, 'sg_entrypoints_clear');
  860. sg_entrypoints_find := GetProcAddress(GHandle, 'sg_entrypoints_find');
  861. sg_route_handle := GetProcAddress(GHandle, 'sg_route_handle');
  862. sg_route_match := GetProcAddress(GHandle, 'sg_route_match');
  863. sg_route_rawpattern := GetProcAddress(GHandle, 'sg_route_rawpattern');
  864. sg_route_pattern := GetProcAddress(GHandle, 'sg_route_pattern');
  865. sg_route_path := GetProcAddress(GHandle, 'sg_route_path');
  866. sg_route_segments_iter := GetProcAddress(GHandle, 'sg_route_segments_iter');
  867. sg_route_vars_iter := GetProcAddress(GHandle, 'sg_route_vars_iter');
  868. sg_route_user_data := GetProcAddress(GHandle, 'sg_route_user_data');
  869. sg_routes_add2 := GetProcAddress(GHandle, 'sg_routes_add2');
  870. sg_routes_add := GetProcAddress(GHandle, 'sg_routes_add');
  871. sg_routes_rm := GetProcAddress(GHandle, 'sg_routes_rm');
  872. sg_routes_iter := GetProcAddress(GHandle, 'sg_routes_iter');
  873. sg_routes_next := GetProcAddress(GHandle, 'sg_routes_next');
  874. sg_routes_count := GetProcAddress(GHandle, 'sg_routes_count');
  875. sg_routes_cleanup := GetProcAddress(GHandle, 'sg_routes_cleanup');
  876. sg_router_new := GetProcAddress(GHandle, 'sg_router_new');
  877. sg_router_free := GetProcAddress(GHandle, 'sg_router_free');
  878. sg_router_dispatch2 := GetProcAddress(GHandle, 'sg_router_dispatch2');
  879. sg_router_dispatch := GetProcAddress(GHandle, 'sg_router_dispatch');
  880. sg_expr_new := GetProcAddress(GHandle, 'sg_expr_new');
  881. sg_expr_free := GetProcAddress(GHandle, 'sg_expr_free');
  882. sg_expr_compile := GetProcAddress(GHandle, 'sg_expr_compile');
  883. sg_expr_clear := GetProcAddress(GHandle, 'sg_expr_clear');
  884. sg_expr_eval := GetProcAddress(GHandle, 'sg_expr_eval');
  885. sg_expr_var := GetProcAddress(GHandle, 'sg_expr_var');
  886. sg_expr_set_var := GetProcAddress(GHandle, 'sg_expr_set_var');
  887. sg_expr_arg := GetProcAddress(GHandle, 'sg_expr_arg');;
  888. sg_expr_near := GetProcAddress(GHandle, 'sg_expr_near');
  889. sg_expr_err := GetProcAddress(GHandle, 'sg_expr_err');
  890. sg_expr_strerror := GetProcAddress(GHandle, 'sg_expr_strerror');
  891. sg_expr_calc := GetProcAddress(GHandle, 'sg_expr_calc');
  892. sg_math_set(cpow, cfmod);
  893. Result := GHandle;
  894. finally
  895. GCS.Release;
  896. end;
  897. end;
  898. class function SgLib.Load(const AName: TFileName): TLibHandle;
  899. begin
  900. if AName = '' then
  901. raise EArgumentException.Create(SSgLibEmptyName);
  902. Result := SgLib.InternalLoad(AName);
  903. if Result = NilHandle then
  904. begin
  905. {$IFDEF MSWINDOWS}
  906. if GetLastError = ERROR_BAD_EXE_FORMAT then
  907. raise ESgLibNotLoaded.CreateFmt(SSgLibInvalid, [AName]);
  908. {$ENDIF}
  909. raise ESgLibNotLoaded.CreateFmt(SSgLibNotLoaded, [AName])
  910. end;
  911. end;
  912. class function SgLib.Unload: TLibHandle;
  913. begin //FI:C101
  914. GCS.Acquire;
  915. try
  916. if GHandle = NilHandle then
  917. Exit(NilHandle);
  918. UnloadEvents.Call;
  919. if not FreeLibrary(GHandle) then
  920. Exit(GHandle);
  921. GHandle := NilHandle;
  922. GLastName := '';
  923. sg_version := nil;
  924. sg_version_str := nil;
  925. sg_mm_set := nil;
  926. sg_malloc := nil;
  927. sg_alloc := nil;
  928. sg_realloc := nil;
  929. sg_free := nil;
  930. sg_math_set := nil;
  931. sg_strerror := nil;
  932. sg_is_post := nil;
  933. sg_extract_entrypoint := nil;
  934. sg_tmpdir := nil;
  935. sg_eor := nil;
  936. sg_ip := nil;
  937. sg_str_new := nil;
  938. sg_str_free := nil;
  939. sg_str_write := nil;
  940. sg_str_printf_va := nil;
  941. sg_str_printf := nil;
  942. sg_str_content := nil;
  943. sg_str_length := nil;
  944. sg_str_clear := nil;
  945. sg_strmap_name := nil;
  946. sg_strmap_val := nil;
  947. sg_strmap_add := nil;
  948. sg_strmap_set := nil;
  949. sg_strmap_find := nil;
  950. sg_strmap_get := nil;
  951. sg_strmap_rm := nil;
  952. sg_strmap_iter := nil;
  953. sg_strmap_sort := nil;
  954. sg_strmap_count := nil;
  955. sg_strmap_next := nil;
  956. sg_strmap_cleanup := nil;
  957. sg_httpauth_set_realm := nil;
  958. sg_httpauth_realm := nil;
  959. sg_httpauth_deny2 := nil;
  960. sg_httpauth_deny := nil;
  961. sg_httpauth_cancel := nil;
  962. sg_httpauth_usr := nil;
  963. sg_httpauth_pwd := nil;
  964. sg_httpuplds_iter := nil;
  965. sg_httpuplds_next := nil;
  966. sg_httpuplds_count := nil;
  967. sg_httpupld_handle := nil;
  968. sg_httpupld_dir := nil;
  969. sg_httpupld_field := nil;
  970. sg_httpupld_name := nil;
  971. sg_httpupld_mime := nil;
  972. sg_httpupld_encoding := nil;
  973. sg_httpupld_size := nil;
  974. sg_httpupld_save := nil;
  975. sg_httpupld_save_as := nil;
  976. sg_httpreq_srv := nil;
  977. sg_httpreq_headers := nil;
  978. sg_httpreq_cookies := nil;
  979. sg_httpreq_params := nil;
  980. sg_httpreq_fields := nil;
  981. sg_httpreq_version := nil;
  982. sg_httpreq_method := nil;
  983. sg_httpreq_path := nil;
  984. sg_httpreq_payload := nil;
  985. sg_httpreq_is_uploading := nil;
  986. sg_httpreq_uploads := nil;
  987. sg_httpreq_client := nil;
  988. sg_httpreq_tls_session := nil;
  989. sg_httpreq_isolate := nil;
  990. sg_httpreq_set_user_data := nil;
  991. sg_httpreq_user_data := nil;
  992. sg_httpres_headers := nil;
  993. sg_httpres_set_cookie := nil;
  994. sg_httpres_sendbinary := nil;
  995. sg_httpres_sendfile2 := nil;
  996. sg_httpres_sendfile := nil;
  997. sg_httpres_sendstream := nil;
  998. sg_httpres_zsendbinary2 := nil;
  999. sg_httpres_zsendbinary := nil;
  1000. sg_httpres_zsendstream2 := nil;
  1001. sg_httpres_zsendstream := nil;
  1002. sg_httpres_zsendfile2 := nil;
  1003. sg_httpres_zsendfile := nil;
  1004. sg_httpres_clear := nil;
  1005. sg_httpsrv_new2 := nil;
  1006. sg_httpsrv_new := nil;
  1007. sg_httpsrv_free := nil;
  1008. sg_httpsrv_tls_listen2 := nil;
  1009. sg_httpsrv_tls_listen := nil;
  1010. sg_httpsrv_listen := nil;
  1011. sg_httpsrv_shutdown := nil;
  1012. sg_httpsrv_port := nil;
  1013. sg_httpsrv_is_threaded := nil;
  1014. sg_httpsrv_set_cli_cb := nil;
  1015. sg_httpsrv_set_upld_cbs := nil;
  1016. sg_httpsrv_set_upld_dir := nil;
  1017. sg_httpsrv_upld_dir := nil;
  1018. sg_httpsrv_set_post_buf_size := nil;
  1019. sg_httpsrv_post_buf_size := nil;
  1020. sg_httpsrv_set_payld_limit := nil;
  1021. sg_httpsrv_payld_limit := nil;
  1022. sg_httpsrv_set_uplds_limit := nil;
  1023. sg_httpsrv_uplds_limit := nil;
  1024. sg_httpsrv_set_thr_pool_size := nil;
  1025. sg_httpsrv_thr_pool_size := nil;
  1026. sg_httpsrv_set_con_timeout := nil;
  1027. sg_httpsrv_con_timeout := nil;
  1028. sg_httpsrv_set_con_limit := nil;
  1029. sg_httpsrv_con_limit := nil;
  1030. sg_httpsrv_handle := nil;
  1031. sg_entrypoint_name := nil;
  1032. sg_entrypoint_set_user_data := nil;
  1033. sg_entrypoint_user_data := nil;
  1034. sg_entrypoints_new := nil;
  1035. sg_entrypoints_free := nil;
  1036. sg_entrypoints_add := nil;
  1037. sg_entrypoints_rm := nil;
  1038. sg_entrypoints_iter := nil;
  1039. sg_entrypoints_clear := nil;
  1040. sg_entrypoints_find := nil;
  1041. sg_route_handle := nil;
  1042. sg_route_match := nil;
  1043. sg_route_rawpattern := nil;
  1044. sg_route_pattern := nil;
  1045. sg_route_path := nil;
  1046. sg_route_segments_iter := nil;
  1047. sg_route_vars_iter := nil;
  1048. sg_route_user_data := nil;
  1049. sg_routes_add2 := nil;
  1050. sg_routes_add := nil;
  1051. sg_routes_rm := nil;
  1052. sg_routes_iter := nil;
  1053. sg_routes_next := nil;
  1054. sg_routes_count := nil;
  1055. sg_routes_cleanup := nil;
  1056. sg_router_new := nil;
  1057. sg_router_free := nil;
  1058. sg_router_dispatch2 := nil;
  1059. sg_router_dispatch := nil;
  1060. sg_expr_new := nil;
  1061. sg_expr_free := nil;
  1062. sg_expr_compile := nil;
  1063. sg_expr_clear := nil;
  1064. sg_expr_eval := nil;
  1065. sg_expr_var := nil;
  1066. sg_expr_set_var := nil;
  1067. sg_expr_arg := nil;
  1068. sg_expr_near := nil;
  1069. sg_expr_err := nil;
  1070. sg_expr_strerror := nil;
  1071. sg_expr_calc := nil;
  1072. Result := NilHandle;
  1073. finally
  1074. GCS.Release;
  1075. end;
  1076. end;
  1077. class function SgLib.IsLoaded: Boolean;
  1078. begin
  1079. GCS.Acquire;
  1080. try
  1081. Result := GHandle <> NilHandle;
  1082. finally
  1083. GCS.Release;
  1084. end;
  1085. end;
  1086. class procedure SgLib.Check;
  1087. begin
  1088. if GHandle = NilHandle then
  1089. raise ESgLibNotLoaded.CreateFmt(SSgLibNotLoaded,
  1090. [IfThen(GLastName = '', SG_LIB_NAME, GLastName)]);
  1091. end;
  1092. initialization
  1093. SgLib.Init;
  1094. finalization
  1095. SgLib.Done;
  1096. end.