libsagui.pas 44 KB

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