libsagui.pas 44 KB

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