libsagui.pas 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423
  1. (* _
  2. * ___ __ _ __ _ _ _(_)
  3. * / __|/ _` |/ _` | | | | |
  4. * \__ \ (_| | (_| | |_| | |
  5. * |___/\__,_|\__, |\__,_|_|
  6. * |___/
  7. *
  8. * Cross-platform library which helps to develop web servers or frameworks.
  9. *
  10. * Copyright (c) 2012-2025 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. Generics.Collections,
  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 = 5;
  56. SG_VERSION_PATCH = 2;
  57. SG_VERSION_HEX = (SG_VERSION_MAJOR shl 16) or (SG_VERSION_MINOR shl 8) or //FI:O803
  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;
  262. {$IFNDEF DEBUG}inline;{$ENDIF}
  263. var
  264. sg_httpres_sendbinary: function(res: Psg_httpres; buf: Pcvoid; size: csize_t;
  265. const content_type: Pcchar; status: cuint): cint; cdecl;
  266. function sg_httpres_download(res: Psg_httpres;
  267. const filename: Pcchar; status: cuint): cint; {$IFNDEF DEBUG}inline;{$ENDIF}
  268. function sg_httpres_render(res: Psg_httpres;
  269. const filename: Pcchar; status: cuint): cint; {$IFNDEF DEBUG}inline;{$ENDIF}
  270. var
  271. sg_httpres_sendfile2: function(res: Psg_httpres; size: cuint64_t;
  272. max_size: cuint64_t; offset: cuint64_t; const filename: Pcchar;
  273. const disposition: Pcchar; status: cuint): cint; cdecl;
  274. sg_httpres_sendfile: function(res: Psg_httpres; size: cuint64_t;
  275. max_size: cuint64_t; offset: cuint64_t; const filename: Pcchar;
  276. downloaded: cbool; status: cuint): cint; cdecl;
  277. sg_httpres_sendstream: function(res: Psg_httpres; size: cuint64_t;
  278. read_cb: sg_read_cb; handle: Pcvoid; free_cb: sg_free_cb;
  279. status: cuint): cint; cdecl;
  280. function sg_httpres_zsend(res: Psg_httpres; const val: Pcchar;
  281. const content_type: Pcchar; status: cuint): cint;
  282. {$IFNDEF DEBUG}inline;{$ENDIF}
  283. var
  284. sg_httpres_zsendbinary2: function(res: Psg_httpres; level: cint; buf: Pcvoid;
  285. size: csize_t; const content_type: Pcchar; status: cuint): cint; cdecl;
  286. sg_httpres_zsendbinary: function(res: Psg_httpres; buf: Pcvoid; size: csize_t;
  287. const content_type: Pcchar; status: cuint): cint; cdecl;
  288. sg_httpres_zsendstream2: function(res: Psg_httpres; level: cint;
  289. size: cuint64_t; read_cb: sg_read_cb; handle: Pcvoid; free_cb: sg_free_cb;
  290. status: cuint): cint; cdecl;
  291. sg_httpres_zsendstream: function(res: Psg_httpres; read_cb: sg_read_cb;
  292. handle: Pcvoid; free_cb: sg_free_cb; status: cuint): cint; cdecl;
  293. function sg_httpres_zdownload(res: Psg_httpres;
  294. const filename: Pcchar; status: cuint): cint; {$IFNDEF DEBUG}inline;{$ENDIF}
  295. function sg_httpres_zrender(res: Psg_httpres;
  296. const filename: Pcchar; status: cuint): cint; {$IFNDEF DEBUG}inline;{$ENDIF}
  297. var
  298. sg_httpres_zsendfile2: function(res: Psg_httpres; level: cint;
  299. size: cuint64_t; max_size: cuint64_t; offset: cuint64_t;
  300. const filename: Pcchar; const disposition: Pcchar;
  301. status: cuint): cint; cdecl;
  302. sg_httpres_zsendfile: function(res: Psg_httpres; size: cuint64_t;
  303. max_size: cuint64_t; offset: cuint64_t; const filename: Pcchar;
  304. downloaded: cbool; status: cuint): cint; cdecl;
  305. sg_httpres_reset: function(res: Psg_httpres): cint; cdecl;
  306. sg_httpres_clear: function(res: Psg_httpres): cint; cdecl;
  307. sg_httpres_is_empty: function(res: Psg_httpres): cbool; cdecl;
  308. var
  309. sg_httpsrv_new2: function(auth_cb: sg_httpauth_cb; req_cb: sg_httpreq_cb;
  310. err_cb: sg_err_cb; cls: Pcvoid): Psg_httpsrv; cdecl;
  311. sg_httpsrv_new: function(cb: sg_httpreq_cb; cls: Pcvoid): Psg_httpsrv; cdecl;
  312. sg_httpsrv_free: procedure(srv: Psg_httpsrv); cdecl;
  313. sg_httpsrv_tls_listen4: function(srv: Psg_httpsrv; const key: Pcchar;
  314. const pwd: Pcchar; const cert: Pcchar; const trust: Pcchar;
  315. const dhparams: Pcchar; const priorities: Pcchar; const hostname: Pcchar;
  316. port: cuint16_t; backlog: cuint; threaded: cbool): cbool; cdecl;
  317. sg_httpsrv_tls_listen3: function(srv: Psg_httpsrv; const key: Pcchar;
  318. const pwd: Pcchar; const cert: Pcchar; const trust: Pcchar;
  319. const dhparams: Pcchar; const priorities: Pcchar; port: cuint16_t;
  320. threaded: cbool): cbool; cdecl;
  321. sg_httpsrv_tls_listen2: function(srv: Psg_httpsrv; const key: Pcchar;
  322. const pwd: Pcchar; const cert: Pcchar; const trust: Pcchar;
  323. const dhparams: Pcchar; port: cuint16_t; threaded: cbool): cbool; cdecl;
  324. sg_httpsrv_tls_listen: function(srv: Psg_httpsrv; const key: Pcchar;
  325. const cert: Pcchar; port: cuint16_t; threaded: cbool): cbool; cdecl;
  326. sg_httpsrv_listen2: function(srv: Psg_httpsrv; hostname: Pcchar;
  327. port: cuint16_t; backlog: cuint; threaded: cbool): cbool; cdecl;
  328. sg_httpsrv_listen: function(srv: Psg_httpsrv; port: cuint16_t;
  329. threaded: cbool): cbool; cdecl;
  330. sg_httpsrv_shutdown: function(srv: Psg_httpsrv): cint; cdecl;
  331. sg_httpsrv_port: function(srv: Psg_httpsrv): cuint16_t; cdecl;
  332. sg_httpsrv_is_threaded: function(srv: Psg_httpsrv): cbool; cdecl;
  333. sg_httpsrv_set_cli_cb: function(srv: Psg_httpsrv; cb: sg_httpsrv_cli_cb;
  334. cls: Pcvoid): cint; cdecl;
  335. sg_httpsrv_set_upld_cbs: function(srv: Psg_httpsrv; cb: sg_httpupld_cb;
  336. cls: Pcvoid; write_cb: sg_write_cb; free_cb: sg_free_cb;
  337. save_cb: sg_save_cb; save_as_cb: sg_save_as_cb): cint; cdecl;
  338. sg_httpsrv_set_upld_dir: function(srv: Psg_httpsrv;
  339. const dir: Pcchar): cint; cdecl;
  340. sg_httpsrv_upld_dir: function(srv: Psg_httpsrv): Pcchar; cdecl;
  341. sg_httpsrv_set_post_buf_size: function(srv: Psg_httpsrv;
  342. size: csize_t): cint; cdecl;
  343. sg_httpsrv_post_buf_size: function(srv: Psg_httpsrv): csize_t; cdecl;
  344. sg_httpsrv_set_payld_limit: function(srv: Psg_httpsrv;
  345. limit: csize_t): cint; cdecl;
  346. sg_httpsrv_payld_limit: function(srv: Psg_httpsrv): csize_t; cdecl;
  347. sg_httpsrv_set_uplds_limit: function(srv: Psg_httpsrv;
  348. limit: cuint64_t): cint; cdecl;
  349. sg_httpsrv_uplds_limit: function(srv: Psg_httpsrv): cuint64_t; cdecl;
  350. sg_httpsrv_set_thr_pool_size: function(srv: Psg_httpsrv;
  351. size: cuint): cint; cdecl;
  352. sg_httpsrv_thr_pool_size: function(srv: Psg_httpsrv): cuint; cdecl;
  353. sg_httpsrv_set_con_timeout: function(srv: Psg_httpsrv;
  354. timeout: cuint): cint; cdecl;
  355. sg_httpsrv_con_timeout: function(srv: Psg_httpsrv): cuint; cdecl;
  356. sg_httpsrv_set_con_limit: function(srv: Psg_httpsrv;
  357. limit: cuint): cint; cdecl;
  358. sg_httpsrv_con_limit: function(srv: Psg_httpsrv): cuint; cdecl;
  359. sg_httpsrv_handle: function(srv: Psg_httpsrv): Pcvoid; cdecl;
  360. type
  361. PPsg_entrypoint = ^Psg_entrypoint;
  362. Psg_entrypoint = ^sg_entrypoint;
  363. sg_entrypoint = record
  364. end;
  365. var
  366. sg_entrypoint_name: function(entrypoint: Psg_entrypoint): Pcchar; cdecl;
  367. sg_entrypoint_set_user_data: function(entrypoint: Psg_entrypoint;
  368. user_data: Pcvoid): cint; cdecl;
  369. sg_entrypoint_user_data: function(entrypoint: Psg_entrypoint): Pcvoid; cdecl;
  370. type
  371. Psg_entrypoints = ^sg_entrypoints;
  372. sg_entrypoints = record
  373. end;
  374. sg_entrypoints_iter_cb = function(cls: Pcvoid;
  375. entrypoint: Psg_entrypoint): cint; cdecl;
  376. var
  377. sg_entrypoints_new: function: Psg_entrypoints; cdecl;
  378. sg_entrypoints_free: procedure(entrypoints: Psg_entrypoints); cdecl;
  379. sg_entrypoints_add: function(entrypoints: Psg_entrypoints; const path: Pcchar;
  380. user_data: Pcvoid): cint; cdecl;
  381. sg_entrypoints_rm: function(entrypoints: Psg_entrypoints;
  382. const path: Pcchar): cint; cdecl;
  383. sg_entrypoints_iter: function(entrypoints: Psg_entrypoints;
  384. cb: sg_entrypoints_iter_cb; cls: Pcvoid): cint; cdecl;
  385. sg_entrypoints_clear: function(entrypoints: Psg_entrypoints): cint; cdecl;
  386. sg_entrypoints_find: function(entrypoints: Psg_entrypoints;
  387. entrypoint: PPsg_entrypoint; const path: Pcchar): cint; cdecl;
  388. type
  389. PPsg_route = ^Psg_route;
  390. Psg_route = ^sg_route;
  391. sg_route = record
  392. end;
  393. sg_segments_iter_cb = function(cls: Pcvoid; index: cuint;
  394. const segment: Pcchar): cint; cdecl;
  395. sg_vars_iter_cb = function(cls: Pcvoid; const name: Pcchar;
  396. const val: Pcchar): cint; cdecl;
  397. sg_route_cb = procedure(cls: Pcvoid; route: Psg_route); cdecl;
  398. sg_routes_iter_cb = function(cls: Pcvoid; route: Psg_route): cint; cdecl;
  399. var
  400. sg_route_handle: function(route: Psg_route): Pcvoid; cdecl;
  401. sg_route_match: function(route: Psg_route): Pcvoid; cdecl;
  402. sg_route_rawpattern: function(route: Psg_route): Pcchar; cdecl;
  403. sg_route_pattern: function(route: Psg_route): Pcchar; cdecl;
  404. sg_route_path: function(route: Psg_route): Pcchar; cdecl;
  405. sg_route_segments_iter: function(route: Psg_route; cb: sg_segments_iter_cb;
  406. cls: Pcvoid): cint; cdecl;
  407. sg_route_vars_iter: function(route: Psg_route; cb: sg_vars_iter_cb;
  408. cls: Pcvoid): cint; cdecl;
  409. sg_route_user_data: function(route: Psg_route): Pcvoid; cdecl;
  410. var
  411. sg_routes_add2: function(routes: PPsg_route; route: PPsg_route;
  412. const pattern: Pcchar; errmsg: Pcchar; errlen: csize_t;
  413. cb: sg_route_cb; cls: Pcvoid): cint; cdecl;
  414. sg_routes_add: function(routes: Psg_route; const pattern: Pcchar;
  415. cb: sg_route_cb; cls: Pcvoid): cint; cdecl;
  416. sg_routes_rm: function(routes: PPsg_route;
  417. const pattern: Pcchar): cint; cdecl;
  418. sg_routes_iter: function(routes: Psg_route; cb: sg_routes_iter_cb;
  419. cls: Pcvoid): cint; cdecl;
  420. sg_routes_next: function(route: PPsg_route): cint; cdecl;
  421. sg_routes_count: function(routes: Psg_route): cuint; cdecl;
  422. sg_routes_cleanup: function(routes: PPsg_route): cint; cdecl;
  423. type
  424. Psg_router = ^sg_router;
  425. sg_router = record
  426. end;
  427. sg_router_dispatch_cb = function(cls: Pcvoid; const path: Pcchar;
  428. route: Psg_route): cint; cdecl;
  429. sg_router_match_cb = function(cls: Pcvoid; route: Psg_route): cint; cdecl;
  430. var
  431. sg_router_new: function(routes: Psg_route): Psg_router; cdecl;
  432. sg_router_free: procedure(router: Psg_router); cdecl;
  433. sg_router_dispatch2: function(router: Psg_router; const path: Pcchar;
  434. user_data: Pcvoid; dispatch_cb: sg_router_dispatch_cb; cls: Pcvoid;
  435. match_cb: sg_router_match_cb): cint; cdecl;
  436. sg_router_dispatch: function(router: Psg_router; const path: Pcchar;
  437. user_data: Pcvoid): cint; cdecl;
  438. type
  439. Psg_expr = ^sg_expr;
  440. sg_expr = record
  441. end;
  442. type
  443. sg_expr_err_type = cenum;
  444. const
  445. SG_EXPR_ERR_UNKNOWN = 0; //FI:O803
  446. SG_EXPR_ERR_UNEXPECTED_NUMBER = 1; //FI:O803
  447. SG_EXPR_ERR_UNEXPECTED_WORD = 2; //FI:O803
  448. SG_EXPR_ERR_UNEXPECTED_PARENS = 3; //FI:O803
  449. SG_EXPR_ERR_MISSING_OPERAND = 4; //FI:O803
  450. SG_EXPR_ERR_UNKNOWN_OPERATOR = 5; //FI:O803
  451. SG_EXPR_ERR_INVALID_FUNC_NAME = 6; //FI:O803
  452. SG_EXPR_ERR_BAD_PARENS = 7; //FI:O803
  453. SG_EXPR_ERR_TOO_FEW_FUNC_ARGS = 8; //FI:O803
  454. SG_EXPR_ERR_FIRST_ARG_IS_NOT_VAR = 9; //FI:O803
  455. SG_EXPR_ERR_BAD_VARIABLE_NAME = 10; //FI:O803
  456. SG_EXPR_ERR_BAD_ASSIGNMENT = 11; //FI:O803
  457. type
  458. Psg_expr_argument = ^sg_expr_argument;
  459. sg_expr_argument = record
  460. end;
  461. type
  462. sg_expr_func = function(cls: Pcvoid; args: Psg_expr_argument;
  463. const identifier: Pcchar): cdouble; cdecl;
  464. type
  465. Psg_expr_extension = ^sg_expr_extension;
  466. sg_expr_extension = record
  467. func: sg_expr_func;
  468. identifier: Pcchar;
  469. cls: Pcvoid;
  470. end;
  471. var
  472. sg_expr_new: function: Psg_expr; cdecl;
  473. sg_expr_free: procedure(expr: Psg_expr); cdecl;
  474. sg_expr_compile: function(expr: Psg_expr; const str: Pcchar; len: csize_t;
  475. extensions: Psg_expr_extension): cint; cdecl;
  476. sg_expr_clear: function(expr: Psg_expr): cint; cdecl;
  477. sg_expr_eval: function(expr: Psg_expr): cdouble; cdecl;
  478. sg_expr_var: function(expr: Psg_expr; const name: Pcchar;
  479. len: csize_t): cdouble; cdecl;
  480. sg_expr_set_var: function(expr: Psg_expr; const name: Pcchar; len: csize_t;
  481. const val: cdouble): cint; cdecl;
  482. sg_expr_arg: function(args: Psg_expr_argument; index: cint): cdouble; cdecl;
  483. sg_expr_near: function(expr: Psg_expr): cint; cdecl;
  484. sg_expr_err: function(expr: Psg_expr): sg_expr_err_type; cdecl;
  485. sg_expr_strerror: function(expr: Psg_expr): Pcchar; cdecl;
  486. sg_expr_calc: function(const str: Pcchar; len: csize_t): cdouble; cdecl;
  487. type
  488. { ESgLibNotLoaded }
  489. ESgLibNotLoaded = class(EFileNotFoundException);
  490. { ESgUnloadEvent }
  491. ESgUnloadEvent = class(Exception);
  492. { TSgLibUnloadHolder }
  493. TSgLibUnloadHolder = class sealed
  494. private
  495. FEvent: TNotifyEvent;
  496. FSender: TObject;
  497. public
  498. constructor Create(AEvent: TNotifyEvent; ASender: TObject);
  499. property Event: TNotifyEvent read FEvent;
  500. property Sender: TObject read FSender;
  501. end;
  502. { TSgUnloadEvents }
  503. TSgUnloadEvents = class sealed
  504. private
  505. FCS: TCriticalSection;
  506. FList: TObjectList<TSgLibUnloadHolder>;
  507. protected
  508. property CS: TCriticalSection read FCS;
  509. property List: TObjectList<TSgLibUnloadHolder> read FList;
  510. public
  511. constructor Create(ACS: TCriticalSection); virtual;
  512. destructor Destroy; override;
  513. procedure Add(AEvent: TNotifyEvent; ASender: TObject); virtual;
  514. procedure Remove(AEvent: TNotifyEvent); virtual;
  515. procedure Clear; virtual;
  516. procedure Call; virtual;
  517. end;
  518. { SgLib }
  519. SgLib = record
  520. private class var
  521. GCS: TCriticalSection;
  522. GUnloadEvents: TSgUnloadEvents;
  523. GLastName: TFileName;
  524. GHandle: TLibHandle;
  525. private
  526. class function InternalLoad(
  527. const AName: TFileName): TLibHandle; static;
  528. public
  529. class procedure Init; static;
  530. class procedure Done; static;
  531. class function GetLastName: string; static;
  532. class procedure CheckVersion(AVersion: Integer); overload; static;
  533. class procedure CheckVersion; overload; static;
  534. {$IFNDEF DEBUG}inline;{$ENDIF}
  535. class procedure CheckLastError(ALastError: Integer); static;
  536. {$IFNDEF DEBUG}inline;{$ENDIF}
  537. class function Load(const AName: TFileName): TLibHandle; static;
  538. class function Unload: TLibHandle; static;
  539. class function IsLoaded: Boolean; static;
  540. class procedure Check; static;
  541. class property UnloadEvents: TSgUnloadEvents read GUnloadEvents;
  542. class property Handle: TLibHandle read GHandle;
  543. end;
  544. function cpow(const X, Y: cdouble): cdouble; cdecl;
  545. {$IFNDEF DEBUG}inline;{$ENDIF}
  546. function cfmod(const X, Y: cdouble): cdouble; cdecl;
  547. {$IFNDEF DEBUG}inline;{$ENDIF}
  548. implementation
  549. function SameNotifyEvent(AN1, AN2: TNotifyEvent): Boolean;
  550. {$IFNDEF DEBUG}inline;{$ENDIF}
  551. begin
  552. Result := (TMethod(AN1).Code = TMethod(AN2).Code) and
  553. (TMethod(AN1).Data = TMethod(AN2).Data);
  554. end;
  555. function cpow(const X, Y: cdouble): cdouble;
  556. begin
  557. Result := Power(X, Y);
  558. end;
  559. function cfmod(const X, Y: cdouble): cdouble;
  560. begin
  561. Result := X - Y * Int(X / Y);
  562. end;
  563. function sg_httpres_send(res: Psg_httpres; const val: Pcchar;
  564. const content_type: Pcchar; status: cuint): cint;
  565. var
  566. len: csize_t;
  567. begin
  568. if Assigned(val) then
  569. len := Length(val)
  570. else
  571. len := 0;
  572. Result := sg_httpres_sendbinary(res, val, len, content_type, status);
  573. end;
  574. function sg_httpres_download(res: Psg_httpres; const filename: Pcchar;
  575. status: cuint): cint;
  576. begin
  577. Result := sg_httpres_sendfile2(res, 0, 0, 0, filename, 'attachment', status);
  578. end;
  579. function sg_httpres_render(res: Psg_httpres; const filename: Pcchar;
  580. status: cuint): cint;
  581. begin
  582. Result := sg_httpres_sendfile2(res, 0, 0, 0, filename, 'inline', status);
  583. end;
  584. function sg_httpres_zsend(res: Psg_httpres; const val: Pcchar;
  585. const content_type: Pcchar; status: cuint): cint;
  586. var
  587. len: csize_t;
  588. begin
  589. if Assigned(val) then
  590. len := Length(val)
  591. else
  592. len := 0;
  593. Result := sg_httpres_zsendbinary(res, val, len, content_type, status);
  594. end;
  595. function sg_httpres_zdownload(res: Psg_httpres; const filename: Pcchar;
  596. status: cuint): cint;
  597. begin
  598. Result := sg_httpres_zsendfile2(res, 1, 0, 0, 0, filename, 'attachment',
  599. status);
  600. end;
  601. function sg_httpres_zrender(res: Psg_httpres; const filename: Pcchar;
  602. status: cuint): cint;
  603. begin
  604. Result := sg_httpres_zsendfile2(res, 1, 0, 0, 0, filename, 'inline', status);
  605. end;
  606. { TSgLibUnloadHolder }
  607. constructor TSgLibUnloadHolder.Create(AEvent: TNotifyEvent;
  608. ASender: TObject);
  609. begin
  610. inherited Create;
  611. FEvent := AEvent;
  612. FSender := ASender;
  613. end;
  614. { TSgUnloadEvents }
  615. constructor TSgUnloadEvents.Create(ACS: TCriticalSection);
  616. begin
  617. inherited Create;
  618. if not Assigned(ACS) then
  619. raise EArgumentNilException.CreateFmt(SParamIsNil, ['ACS']);
  620. FList := TObjectList<TSgLibUnloadHolder>.Create;
  621. FCS := ACS;
  622. end;
  623. destructor TSgUnloadEvents.Destroy;
  624. begin
  625. Clear;
  626. FList.Free;
  627. inherited Destroy;
  628. end;
  629. procedure TSgUnloadEvents.Add(AEvent: TNotifyEvent; ASender: TObject);
  630. var
  631. I: Integer;
  632. begin
  633. if not Assigned(AEvent) then
  634. raise EArgumentNilException.CreateFmt(SParamIsNil, ['AEvent']);
  635. FCS.Acquire;
  636. try
  637. for I := 0 to Pred(FList.Count) do
  638. if SameNotifyEvent(TSgLibUnloadHolder(FList[I]).Event, AEvent) then
  639. raise ESgUnloadEvent.Create(SSgUnloadEventAlreadyRegistered);
  640. FList.Add(TSgLibUnloadHolder.Create(AEvent, ASender));
  641. finally
  642. FCS.Release;
  643. end;
  644. end;
  645. procedure TSgUnloadEvents.Remove(AEvent: TNotifyEvent);
  646. var
  647. I: Integer;
  648. begin
  649. if not Assigned(AEvent) then
  650. raise EArgumentNilException.CreateFmt(SParamIsNil, ['AEvent']);
  651. FCS.Acquire;
  652. try
  653. for I := 0 to Pred(FList.Count) do
  654. if SameNotifyEvent(TSgLibUnloadHolder(FList[I]).Event, AEvent) then
  655. begin
  656. FList.Delete(I);
  657. Break;
  658. end;
  659. finally
  660. FCS.Release;
  661. end;
  662. end;
  663. procedure TSgUnloadEvents.Clear;
  664. begin
  665. FCS.Acquire;
  666. try
  667. FList.Clear;
  668. finally
  669. FCS.Release;
  670. end;
  671. end;
  672. procedure TSgUnloadEvents.Call;
  673. var
  674. H: TSgLibUnloadHolder;
  675. I: Integer;
  676. begin
  677. FCS.Acquire;
  678. try
  679. for I := Pred(FList.Count) downto 0 do
  680. begin
  681. H := FList[I] as TSgLibUnloadHolder;
  682. H.Event(H.Sender);
  683. end;
  684. finally
  685. FCS.Release;
  686. end;
  687. end;
  688. { SgLib }
  689. class procedure SgLib.Init;
  690. begin
  691. GCS := TCriticalSection.Create;
  692. GUnloadEvents := TSgUnloadEvents.Create(GCS);
  693. InternalLoad(SG_LIB_NAME);
  694. end;
  695. class procedure SgLib.Done;
  696. begin
  697. GCS.Acquire;
  698. try
  699. Unload;
  700. GUnloadEvents.Free;
  701. finally
  702. GCS.Release;
  703. GCS.Free;
  704. end;
  705. end;
  706. class function SgLib.GetLastName: string;
  707. begin
  708. Result := GLastName;
  709. end;
  710. class procedure SgLib.CheckVersion(AVersion: Integer);
  711. var
  712. N: cint;
  713. begin
  714. try
  715. if not Assigned(sg_version) then
  716. raise EInvalidOpException.CreateFmt(SSgLibInvalid, [GetLastName]);
  717. N := ((AVersion shr 8) and $FF);
  718. if (((AVersion shr 16) and $FF) <> SG_VERSION_MAJOR) or
  719. (N < SG_VERSION_MINOR) or (((AVersion and $FF) < SG_VERSION_PATCH) and
  720. (N <= SG_VERSION_MINOR)) then
  721. raise EInvalidOpException.CreateFmt(SSgLibVersion, [SG_VERSION_MAJOR,
  722. SG_VERSION_MINOR, SG_VERSION_PATCH]);
  723. except
  724. Unload;
  725. raise;
  726. end;
  727. end;
  728. class procedure SgLib.CheckVersion;
  729. begin
  730. CheckVersion(sg_version);
  731. end;
  732. class procedure SgLib.CheckLastError(ALastError: Integer);
  733. var
  734. E: EOSError;
  735. P: array[0..SG_ERR_SIZE-1] of cchar;
  736. S: string;
  737. begin
  738. if (ALastError = 0) or (not Assigned(sg_strerror)) then
  739. Exit;
  740. sg_strerror(ALastError, @P[0], SG_ERR_SIZE);
  741. {$IFDEF FPC}
  742. SetString(S, @P[0], Length(Pcchar(@P[0])));
  743. SetCodePage(RawByteString(S), CP_UTF8, False);
  744. {$ELSE}
  745. S := TMarshal.ReadStringAsUtf8(TPtrWrapper.Create(@P[0]));
  746. {$ENDIF}
  747. E := EOSError.Create(S.TrimRight);
  748. E.ErrorCode := ALastError;
  749. raise E;
  750. end;
  751. class function SgLib.InternalLoad(const AName: TFileName): TLibHandle;
  752. begin //FI:C101
  753. GCS.Acquire;
  754. try
  755. if GHandle <> NilHandle then
  756. Exit(GHandle);
  757. GHandle := SafeLoadLibrary(AName);
  758. if GHandle = NilHandle then
  759. Exit(NilHandle);
  760. GLastName := AName;
  761. sg_version := GetProcAddress(GHandle, 'sg_version');
  762. sg_version_str := GetProcAddress(GHandle, 'sg_version_str');
  763. CheckVersion;
  764. sg_mm_set := GetProcAddress(GHandle, 'sg_mm_set');
  765. sg_malloc := GetProcAddress(GHandle, 'sg_malloc');
  766. sg_alloc := GetProcAddress(GHandle, 'sg_alloc');
  767. sg_realloc := GetProcAddress(GHandle, 'sg_realloc');
  768. sg_free := GetProcAddress(GHandle, 'sg_free');
  769. sg_math_set := GetProcAddress(GHandle, 'sg_math_set');
  770. sg_strerror := GetProcAddress(GHandle, 'sg_strerror');
  771. sg_is_post := GetProcAddress(GHandle, 'sg_is_post');
  772. sg_extract_entrypoint := GetProcAddress(GHandle, 'sg_extract_entrypoint');
  773. sg_tmpdir := GetProcAddress(GHandle, 'sg_tmpdir');
  774. sg_eor := GetProcAddress(GHandle, 'sg_eor');
  775. sg_ip := GetProcAddress(GHandle, 'sg_ip');
  776. sg_str_new := GetProcAddress(GHandle, 'sg_str_new');
  777. sg_str_free := GetProcAddress(GHandle, 'sg_str_free');
  778. sg_str_write := GetProcAddress(GHandle, 'sg_str_write');
  779. sg_str_printf_va := GetProcAddress(GHandle, 'sg_str_printf_va');
  780. sg_str_printf := GetProcAddress(GHandle, 'sg_str_printf');
  781. sg_str_content := GetProcAddress(GHandle, 'sg_str_content');
  782. sg_str_length := GetProcAddress(GHandle, 'sg_str_length');
  783. sg_str_clear := GetProcAddress(GHandle, 'sg_str_clear');
  784. sg_strmap_name := GetProcAddress(GHandle, 'sg_strmap_name');
  785. sg_strmap_val := GetProcAddress(GHandle, 'sg_strmap_val');
  786. sg_strmap_add := GetProcAddress(GHandle, 'sg_strmap_add');
  787. sg_strmap_set := GetProcAddress(GHandle, 'sg_strmap_set');
  788. sg_strmap_find := GetProcAddress(GHandle, 'sg_strmap_find');
  789. sg_strmap_get := GetProcAddress(GHandle, 'sg_strmap_get');
  790. sg_strmap_rm := GetProcAddress(GHandle, 'sg_strmap_rm');
  791. sg_strmap_iter := GetProcAddress(GHandle, 'sg_strmap_iter');
  792. sg_strmap_sort := GetProcAddress(GHandle, 'sg_strmap_sort');
  793. sg_strmap_count := GetProcAddress(GHandle, 'sg_strmap_count');
  794. sg_strmap_next := GetProcAddress(GHandle, 'sg_strmap_next');
  795. sg_strmap_cleanup := GetProcAddress(GHandle, 'sg_strmap_cleanup');
  796. sg_httpauth_set_realm := GetProcAddress(GHandle, 'sg_httpauth_set_realm');
  797. sg_httpauth_realm := GetProcAddress(GHandle, 'sg_httpauth_realm');
  798. sg_httpauth_deny2 := GetProcAddress(GHandle, 'sg_httpauth_deny2');
  799. sg_httpauth_deny := GetProcAddress(GHandle, 'sg_httpauth_deny');
  800. sg_httpauth_cancel := GetProcAddress(GHandle, 'sg_httpauth_cancel');
  801. sg_httpauth_usr := GetProcAddress(GHandle, 'sg_httpauth_usr');
  802. sg_httpauth_pwd := GetProcAddress(GHandle, 'sg_httpauth_pwd');
  803. sg_httpuplds_iter := GetProcAddress(GHandle, 'sg_httpuplds_iter');
  804. sg_httpuplds_next := GetProcAddress(GHandle, 'sg_httpuplds_next');
  805. sg_httpuplds_count := GetProcAddress(GHandle, 'sg_httpuplds_count');
  806. sg_httpupld_handle := GetProcAddress(GHandle, 'sg_httpupld_handle');
  807. sg_httpupld_dir := GetProcAddress(GHandle, 'sg_httpupld_dir');
  808. sg_httpupld_field := GetProcAddress(GHandle, 'sg_httpupld_field');
  809. sg_httpupld_name := GetProcAddress(GHandle, 'sg_httpupld_name');
  810. sg_httpupld_mime := GetProcAddress(GHandle, 'sg_httpupld_mime');
  811. sg_httpupld_encoding := GetProcAddress(GHandle, 'sg_httpupld_encoding');
  812. sg_httpupld_size := GetProcAddress(GHandle, 'sg_httpupld_size');
  813. sg_httpupld_save := GetProcAddress(GHandle, 'sg_httpupld_save');
  814. sg_httpupld_save_as := GetProcAddress(GHandle, 'sg_httpupld_save_as');
  815. sg_httpreq_srv := GetProcAddress(GHandle, 'sg_httpreq_srv');
  816. sg_httpreq_headers := GetProcAddress(GHandle, 'sg_httpreq_headers');
  817. sg_httpreq_cookies := GetProcAddress(GHandle, 'sg_httpreq_cookies');
  818. sg_httpreq_params := GetProcAddress(GHandle, 'sg_httpreq_params');
  819. sg_httpreq_fields := GetProcAddress(GHandle, 'sg_httpreq_fields');
  820. sg_httpreq_version := GetProcAddress(GHandle, 'sg_httpreq_version');
  821. sg_httpreq_method := GetProcAddress(GHandle, 'sg_httpreq_method');
  822. sg_httpreq_path := GetProcAddress(GHandle, 'sg_httpreq_path');
  823. sg_httpreq_payload := GetProcAddress(GHandle, 'sg_httpreq_payload');
  824. sg_httpreq_is_uploading := GetProcAddress(GHandle, 'sg_httpreq_is_uploading');
  825. sg_httpreq_uploads := GetProcAddress(GHandle, 'sg_httpreq_uploads');
  826. sg_httpreq_client := GetProcAddress(GHandle, 'sg_httpreq_client');
  827. sg_httpreq_tls_session := GetProcAddress(GHandle, 'sg_httpreq_tls_session');
  828. sg_httpreq_isolate := GetProcAddress(GHandle, 'sg_httpreq_isolate');
  829. sg_httpreq_set_user_data := GetProcAddress(GHandle, 'sg_httpreq_set_user_data');
  830. sg_httpreq_user_data := GetProcAddress(GHandle, 'sg_httpreq_user_data');
  831. sg_httpres_headers := GetProcAddress(GHandle, 'sg_httpres_headers');
  832. sg_httpres_set_cookie := GetProcAddress(GHandle, 'sg_httpres_set_cookie');
  833. sg_httpres_sendbinary := GetProcAddress(GHandle, 'sg_httpres_sendbinary');
  834. sg_httpres_sendfile2 := GetProcAddress(GHandle, 'sg_httpres_sendfile2');
  835. sg_httpres_sendfile := GetProcAddress(GHandle, 'sg_httpres_sendfile');
  836. sg_httpres_sendstream := GetProcAddress(GHandle, 'sg_httpres_sendstream');
  837. sg_httpres_zsendbinary2 := GetProcAddress(GHandle, 'sg_httpres_zsendbinary2');
  838. sg_httpres_zsendbinary := GetProcAddress(GHandle, 'sg_httpres_zsendbinary');
  839. sg_httpres_zsendstream := GetProcAddress(GHandle, 'sg_httpres_zsendstream');
  840. sg_httpres_zsendstream2 := GetProcAddress(GHandle, 'sg_httpres_zsendstream2');
  841. sg_httpres_zsendfile2 := GetProcAddress(GHandle, 'sg_httpres_zsendfile2');
  842. sg_httpres_zsendfile := GetProcAddress(GHandle, 'sg_httpres_zsendfile');
  843. sg_httpres_reset := GetProcAddress(GHandle, 'sg_httpres_reset');
  844. sg_httpres_clear := GetProcAddress(GHandle, 'sg_httpres_clear');
  845. sg_httpres_is_empty := GetProcAddress(GHandle, 'sg_httpres_is_empty');
  846. sg_httpsrv_new2 := GetProcAddress(GHandle, 'sg_httpsrv_new2');
  847. sg_httpsrv_new := GetProcAddress(GHandle, 'sg_httpsrv_new');
  848. sg_httpsrv_free := GetProcAddress(GHandle, 'sg_httpsrv_free');
  849. sg_httpsrv_tls_listen4 := GetProcAddress(GHandle, 'sg_httpsrv_tls_listen4');
  850. sg_httpsrv_tls_listen3 := GetProcAddress(GHandle, 'sg_httpsrv_tls_listen3');
  851. sg_httpsrv_tls_listen2 := GetProcAddress(GHandle, 'sg_httpsrv_tls_listen2');
  852. sg_httpsrv_tls_listen := GetProcAddress(GHandle, 'sg_httpsrv_tls_listen');
  853. sg_httpsrv_listen2 := GetProcAddress(GHandle, 'sg_httpsrv_listen2');
  854. sg_httpsrv_listen := GetProcAddress(GHandle, 'sg_httpsrv_listen');
  855. sg_httpsrv_shutdown := GetProcAddress(GHandle, 'sg_httpsrv_shutdown');
  856. sg_httpsrv_port := GetProcAddress(GHandle, 'sg_httpsrv_port');
  857. sg_httpsrv_is_threaded := GetProcAddress(GHandle, 'sg_httpsrv_is_threaded');
  858. sg_httpsrv_set_cli_cb := GetProcAddress(GHandle, 'sg_httpsrv_set_cli_cb');
  859. sg_httpsrv_set_upld_cbs := GetProcAddress(GHandle, 'sg_httpsrv_set_upld_cbs');
  860. sg_httpsrv_set_upld_dir := GetProcAddress(GHandle, 'sg_httpsrv_set_upld_dir');
  861. sg_httpsrv_upld_dir := GetProcAddress(GHandle, 'sg_httpsrv_upld_dir');
  862. sg_httpsrv_set_post_buf_size := GetProcAddress(GHandle, 'sg_httpsrv_set_post_buf_size');
  863. sg_httpsrv_post_buf_size := GetProcAddress(GHandle, 'sg_httpsrv_post_buf_size');
  864. sg_httpsrv_set_payld_limit := GetProcAddress(GHandle, 'sg_httpsrv_set_payld_limit');
  865. sg_httpsrv_payld_limit := GetProcAddress(GHandle, 'sg_httpsrv_payld_limit');
  866. sg_httpsrv_set_uplds_limit := GetProcAddress(GHandle, 'sg_httpsrv_set_uplds_limit');
  867. sg_httpsrv_uplds_limit := GetProcAddress(GHandle, 'sg_httpsrv_uplds_limit');
  868. sg_httpsrv_set_thr_pool_size := GetProcAddress(GHandle, 'sg_httpsrv_set_thr_pool_size');
  869. sg_httpsrv_thr_pool_size := GetProcAddress(GHandle, 'sg_httpsrv_thr_pool_size');
  870. sg_httpsrv_set_con_timeout := GetProcAddress(GHandle, 'sg_httpsrv_set_con_timeout');
  871. sg_httpsrv_con_timeout := GetProcAddress(GHandle, 'sg_httpsrv_con_timeout');
  872. sg_httpsrv_set_con_limit := GetProcAddress(GHandle, 'sg_httpsrv_set_con_limit');
  873. sg_httpsrv_con_limit := GetProcAddress(GHandle, 'sg_httpsrv_con_limit');
  874. sg_httpsrv_handle := GetProcAddress(GHandle, 'sg_httpsrv_handle');
  875. sg_entrypoint_name := GetProcAddress(GHandle, 'sg_entrypoint_name');
  876. sg_entrypoint_set_user_data := GetProcAddress(GHandle, 'sg_entrypoint_set_user_data');
  877. sg_entrypoint_user_data := GetProcAddress(GHandle, 'sg_entrypoint_user_data');
  878. sg_entrypoints_new := GetProcAddress(GHandle, 'sg_entrypoints_new');
  879. sg_entrypoints_free := GetProcAddress(GHandle, 'sg_entrypoints_free');
  880. sg_entrypoints_add := GetProcAddress(GHandle, 'sg_entrypoints_add');
  881. sg_entrypoints_rm := GetProcAddress(GHandle, 'sg_entrypoints_rm');
  882. sg_entrypoints_iter := GetProcAddress(GHandle, 'sg_entrypoints_iter');
  883. sg_entrypoints_clear := GetProcAddress(GHandle, 'sg_entrypoints_clear');
  884. sg_entrypoints_find := GetProcAddress(GHandle, 'sg_entrypoints_find');
  885. sg_route_handle := GetProcAddress(GHandle, 'sg_route_handle');
  886. sg_route_match := GetProcAddress(GHandle, 'sg_route_match');
  887. sg_route_rawpattern := GetProcAddress(GHandle, 'sg_route_rawpattern');
  888. sg_route_pattern := GetProcAddress(GHandle, 'sg_route_pattern');
  889. sg_route_path := GetProcAddress(GHandle, 'sg_route_path');
  890. sg_route_segments_iter := GetProcAddress(GHandle, 'sg_route_segments_iter');
  891. sg_route_vars_iter := GetProcAddress(GHandle, 'sg_route_vars_iter');
  892. sg_route_user_data := GetProcAddress(GHandle, 'sg_route_user_data');
  893. sg_routes_add2 := GetProcAddress(GHandle, 'sg_routes_add2');
  894. sg_routes_add := GetProcAddress(GHandle, 'sg_routes_add');
  895. sg_routes_rm := GetProcAddress(GHandle, 'sg_routes_rm');
  896. sg_routes_iter := GetProcAddress(GHandle, 'sg_routes_iter');
  897. sg_routes_next := GetProcAddress(GHandle, 'sg_routes_next');
  898. sg_routes_count := GetProcAddress(GHandle, 'sg_routes_count');
  899. sg_routes_cleanup := GetProcAddress(GHandle, 'sg_routes_cleanup');
  900. sg_router_new := GetProcAddress(GHandle, 'sg_router_new');
  901. sg_router_free := GetProcAddress(GHandle, 'sg_router_free');
  902. sg_router_dispatch2 := GetProcAddress(GHandle, 'sg_router_dispatch2');
  903. sg_router_dispatch := GetProcAddress(GHandle, 'sg_router_dispatch');
  904. sg_expr_new := GetProcAddress(GHandle, 'sg_expr_new');
  905. sg_expr_free := GetProcAddress(GHandle, 'sg_expr_free');
  906. sg_expr_compile := GetProcAddress(GHandle, 'sg_expr_compile');
  907. sg_expr_clear := GetProcAddress(GHandle, 'sg_expr_clear');
  908. sg_expr_eval := GetProcAddress(GHandle, 'sg_expr_eval');
  909. sg_expr_var := GetProcAddress(GHandle, 'sg_expr_var');
  910. sg_expr_set_var := GetProcAddress(GHandle, 'sg_expr_set_var');
  911. sg_expr_arg := GetProcAddress(GHandle, 'sg_expr_arg');;
  912. sg_expr_near := GetProcAddress(GHandle, 'sg_expr_near');
  913. sg_expr_err := GetProcAddress(GHandle, 'sg_expr_err');
  914. sg_expr_strerror := GetProcAddress(GHandle, 'sg_expr_strerror');
  915. sg_expr_calc := GetProcAddress(GHandle, 'sg_expr_calc');
  916. sg_math_set(cpow, cfmod);
  917. Result := GHandle;
  918. finally
  919. GCS.Release;
  920. end;
  921. end;
  922. class function SgLib.Load(const AName: TFileName): TLibHandle;
  923. begin
  924. if AName = '' then
  925. raise EArgumentException.Create(SSgLibEmptyName);
  926. Result := SgLib.InternalLoad(AName);
  927. if Result = NilHandle then
  928. begin
  929. {$IFDEF MSWINDOWS}
  930. if GetLastError = ERROR_BAD_EXE_FORMAT then
  931. raise ESgLibNotLoaded.CreateFmt(SSgLibInvalid, [AName]);
  932. {$ENDIF}
  933. raise ESgLibNotLoaded.CreateFmt(SSgLibNotLoaded, [AName])
  934. end;
  935. end;
  936. class function SgLib.Unload: TLibHandle;
  937. begin //FI:C101
  938. GCS.Acquire;
  939. try
  940. if GHandle = NilHandle then
  941. Exit(NilHandle);
  942. UnloadEvents.Call;
  943. if not FreeLibrary(GHandle) then
  944. Exit(GHandle);
  945. GHandle := NilHandle;
  946. GLastName := '';
  947. sg_version := nil;
  948. sg_version_str := nil;
  949. sg_mm_set := nil;
  950. sg_malloc := nil;
  951. sg_alloc := nil;
  952. sg_realloc := nil;
  953. sg_free := nil;
  954. sg_math_set := nil;
  955. sg_strerror := nil;
  956. sg_is_post := nil;
  957. sg_extract_entrypoint := nil;
  958. sg_tmpdir := nil;
  959. sg_eor := nil;
  960. sg_ip := nil;
  961. sg_str_new := nil;
  962. sg_str_free := nil;
  963. sg_str_write := nil;
  964. sg_str_printf_va := nil;
  965. sg_str_printf := nil;
  966. sg_str_content := nil;
  967. sg_str_length := nil;
  968. sg_str_clear := nil;
  969. sg_strmap_name := nil;
  970. sg_strmap_val := nil;
  971. sg_strmap_add := nil;
  972. sg_strmap_set := nil;
  973. sg_strmap_find := nil;
  974. sg_strmap_get := nil;
  975. sg_strmap_rm := nil;
  976. sg_strmap_iter := nil;
  977. sg_strmap_sort := nil;
  978. sg_strmap_count := nil;
  979. sg_strmap_next := nil;
  980. sg_strmap_cleanup := nil;
  981. sg_httpauth_set_realm := nil;
  982. sg_httpauth_realm := nil;
  983. sg_httpauth_deny2 := nil;
  984. sg_httpauth_deny := nil;
  985. sg_httpauth_cancel := nil;
  986. sg_httpauth_usr := nil;
  987. sg_httpauth_pwd := nil;
  988. sg_httpuplds_iter := nil;
  989. sg_httpuplds_next := nil;
  990. sg_httpuplds_count := nil;
  991. sg_httpupld_handle := nil;
  992. sg_httpupld_dir := nil;
  993. sg_httpupld_field := nil;
  994. sg_httpupld_name := nil;
  995. sg_httpupld_mime := nil;
  996. sg_httpupld_encoding := nil;
  997. sg_httpupld_size := nil;
  998. sg_httpupld_save := nil;
  999. sg_httpupld_save_as := nil;
  1000. sg_httpreq_srv := nil;
  1001. sg_httpreq_headers := nil;
  1002. sg_httpreq_cookies := nil;
  1003. sg_httpreq_params := nil;
  1004. sg_httpreq_fields := nil;
  1005. sg_httpreq_version := nil;
  1006. sg_httpreq_method := nil;
  1007. sg_httpreq_path := nil;
  1008. sg_httpreq_payload := nil;
  1009. sg_httpreq_is_uploading := nil;
  1010. sg_httpreq_uploads := nil;
  1011. sg_httpreq_client := nil;
  1012. sg_httpreq_tls_session := nil;
  1013. sg_httpreq_isolate := nil;
  1014. sg_httpreq_set_user_data := nil;
  1015. sg_httpreq_user_data := nil;
  1016. sg_httpres_headers := nil;
  1017. sg_httpres_set_cookie := nil;
  1018. sg_httpres_sendbinary := nil;
  1019. sg_httpres_sendfile2 := nil;
  1020. sg_httpres_sendfile := nil;
  1021. sg_httpres_sendstream := nil;
  1022. sg_httpres_zsendbinary2 := nil;
  1023. sg_httpres_zsendbinary := nil;
  1024. sg_httpres_zsendstream2 := nil;
  1025. sg_httpres_zsendstream := nil;
  1026. sg_httpres_zsendfile2 := nil;
  1027. sg_httpres_zsendfile := nil;
  1028. sg_httpres_reset := nil;
  1029. sg_httpres_clear := nil;
  1030. sg_httpres_is_empty := nil;
  1031. sg_httpsrv_new2 := nil;
  1032. sg_httpsrv_new := nil;
  1033. sg_httpsrv_free := nil;
  1034. sg_httpsrv_tls_listen4 := nil;
  1035. sg_httpsrv_tls_listen3 := nil;
  1036. sg_httpsrv_tls_listen2 := nil;
  1037. sg_httpsrv_tls_listen := nil;
  1038. sg_httpsrv_listen2 := nil;
  1039. sg_httpsrv_listen := nil;
  1040. sg_httpsrv_shutdown := nil;
  1041. sg_httpsrv_port := nil;
  1042. sg_httpsrv_is_threaded := nil;
  1043. sg_httpsrv_set_cli_cb := nil;
  1044. sg_httpsrv_set_upld_cbs := nil;
  1045. sg_httpsrv_set_upld_dir := nil;
  1046. sg_httpsrv_upld_dir := nil;
  1047. sg_httpsrv_set_post_buf_size := nil;
  1048. sg_httpsrv_post_buf_size := nil;
  1049. sg_httpsrv_set_payld_limit := nil;
  1050. sg_httpsrv_payld_limit := nil;
  1051. sg_httpsrv_set_uplds_limit := nil;
  1052. sg_httpsrv_uplds_limit := nil;
  1053. sg_httpsrv_set_thr_pool_size := nil;
  1054. sg_httpsrv_thr_pool_size := nil;
  1055. sg_httpsrv_set_con_timeout := nil;
  1056. sg_httpsrv_con_timeout := nil;
  1057. sg_httpsrv_set_con_limit := nil;
  1058. sg_httpsrv_con_limit := nil;
  1059. sg_httpsrv_handle := nil;
  1060. sg_entrypoint_name := nil;
  1061. sg_entrypoint_set_user_data := nil;
  1062. sg_entrypoint_user_data := nil;
  1063. sg_entrypoints_new := nil;
  1064. sg_entrypoints_free := nil;
  1065. sg_entrypoints_add := nil;
  1066. sg_entrypoints_rm := nil;
  1067. sg_entrypoints_iter := nil;
  1068. sg_entrypoints_clear := nil;
  1069. sg_entrypoints_find := nil;
  1070. sg_route_handle := nil;
  1071. sg_route_match := nil;
  1072. sg_route_rawpattern := nil;
  1073. sg_route_pattern := nil;
  1074. sg_route_path := nil;
  1075. sg_route_segments_iter := nil;
  1076. sg_route_vars_iter := nil;
  1077. sg_route_user_data := nil;
  1078. sg_routes_add2 := nil;
  1079. sg_routes_add := nil;
  1080. sg_routes_rm := nil;
  1081. sg_routes_iter := nil;
  1082. sg_routes_next := nil;
  1083. sg_routes_count := nil;
  1084. sg_routes_cleanup := nil;
  1085. sg_router_new := nil;
  1086. sg_router_free := nil;
  1087. sg_router_dispatch2 := nil;
  1088. sg_router_dispatch := nil;
  1089. sg_expr_new := nil;
  1090. sg_expr_free := nil;
  1091. sg_expr_compile := nil;
  1092. sg_expr_clear := nil;
  1093. sg_expr_eval := nil;
  1094. sg_expr_var := nil;
  1095. sg_expr_set_var := nil;
  1096. sg_expr_arg := nil;
  1097. sg_expr_near := nil;
  1098. sg_expr_err := nil;
  1099. sg_expr_strerror := nil;
  1100. sg_expr_calc := nil;
  1101. Result := NilHandle;
  1102. finally
  1103. GCS.Release;
  1104. end;
  1105. end;
  1106. class function SgLib.IsLoaded: Boolean;
  1107. begin
  1108. GCS.Acquire;
  1109. try
  1110. Result := GHandle <> NilHandle;
  1111. finally
  1112. GCS.Release;
  1113. end;
  1114. end;
  1115. class procedure SgLib.Check;
  1116. begin
  1117. if GHandle = NilHandle then
  1118. raise ESgLibNotLoaded.CreateFmt(SSgLibNotLoaded,
  1119. [IfThen(GLastName = '', SG_LIB_NAME, GLastName)]);
  1120. end;
  1121. initialization
  1122. SgLib.Init;
  1123. finalization
  1124. SgLib.Done;
  1125. end.