kibi-swipl-linux-x64-gnu 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (550) hide show
  1. package/README.md +29 -0
  2. package/bin/swipl +0 -0
  3. package/bin/swipl-win +0 -0
  4. package/build-manifest.json +70 -0
  5. package/lib/cmake/swipl/SWIPLConfig.cmake +21 -0
  6. package/lib/cmake/swipl/SWIPLConfigVersion.cmake +65 -0
  7. package/lib/cmake/swipl/SWIPLTargets-release.cmake +28 -0
  8. package/lib/cmake/swipl/SWIPLTargets.cmake +109 -0
  9. package/lib/swipl/ABI +1 -0
  10. package/lib/swipl/LICENSE +41 -0
  11. package/lib/swipl/README.md +116 -0
  12. package/lib/swipl/app/README.md +25 -0
  13. package/lib/swipl/app/app.pl +87 -0
  14. package/lib/swipl/app/pack.pl +350 -0
  15. package/lib/swipl/app/qlf.pl +521 -0
  16. package/lib/swipl/app/splfr.pl +177 -0
  17. package/lib/swipl/app/sys.pl +310 -0
  18. package/lib/swipl/bin/swipl.home +1 -0
  19. package/lib/swipl/boot/apply.pl +73 -0
  20. package/lib/swipl/boot/attvar.pl +344 -0
  21. package/lib/swipl/boot/autoload.pl +1226 -0
  22. package/lib/swipl/boot/bags.pl +283 -0
  23. package/lib/swipl/boot/dcg.pl +388 -0
  24. package/lib/swipl/boot/dicts.pl +193 -0
  25. package/lib/swipl/boot/dwim.pl +485 -0
  26. package/lib/swipl/boot/engines.pl +152 -0
  27. package/lib/swipl/boot/expand.pl +1628 -0
  28. package/lib/swipl/boot/gc.pl +65 -0
  29. package/lib/swipl/boot/history.pl +304 -0
  30. package/lib/swipl/boot/init.pl +4682 -0
  31. package/lib/swipl/boot/iri.pl +64 -0
  32. package/lib/swipl/boot/license.pl +390 -0
  33. package/lib/swipl/boot/load.pl +81 -0
  34. package/lib/swipl/boot/messages.pl +2521 -0
  35. package/lib/swipl/boot/packs.pl +306 -0
  36. package/lib/swipl/boot/predopts.pl +154 -0
  37. package/lib/swipl/boot/qlf.pl +141 -0
  38. package/lib/swipl/boot/rc.pl +263 -0
  39. package/lib/swipl/boot/syspred.pl +1634 -0
  40. package/lib/swipl/boot/tabling.pl +2321 -0
  41. package/lib/swipl/boot/threads.pl +42 -0
  42. package/lib/swipl/boot/toplevel.pl +2248 -0
  43. package/lib/swipl/boot/topvars.pl +261 -0
  44. package/lib/swipl/boot.prc +0 -0
  45. package/lib/swipl/cmake/swipl.cmake +131 -0
  46. package/lib/swipl/customize/README.md +23 -0
  47. package/lib/swipl/customize/edit +101 -0
  48. package/lib/swipl/customize/init.pl +164 -0
  49. package/lib/swipl/demo/README.md +33 -0
  50. package/lib/swipl/demo/likes.pl +44 -0
  51. package/lib/swipl/doc/packages/examples/chr/bool.chr +281 -0
  52. package/lib/swipl/doc/packages/examples/chr/chrdif.chr +87 -0
  53. package/lib/swipl/doc/packages/examples/chr/chrfreeze.chr +6 -0
  54. package/lib/swipl/doc/packages/examples/chr/family.chr +116 -0
  55. package/lib/swipl/doc/packages/examples/chr/fib.chr +24 -0
  56. package/lib/swipl/doc/packages/examples/chr/fibonacci.chr +31 -0
  57. package/lib/swipl/doc/packages/examples/chr/gcd.chr +28 -0
  58. package/lib/swipl/doc/packages/examples/chr/leq.chr +34 -0
  59. package/lib/swipl/doc/packages/examples/chr/listdom.chr +138 -0
  60. package/lib/swipl/doc/packages/examples/chr/primes.chr +29 -0
  61. package/lib/swipl/doc/packages/examples/http/README.md +98 -0
  62. package/lib/swipl/doc/packages/examples/http/calc.pl +92 -0
  63. package/lib/swipl/doc/packages/examples/http/demo_body.pl +232 -0
  64. package/lib/swipl/doc/packages/examples/http/demo_client.pl +165 -0
  65. package/lib/swipl/doc/packages/examples/http/demo_daemon.pl +38 -0
  66. package/lib/swipl/doc/packages/examples/http/demo_files.pl +50 -0
  67. package/lib/swipl/doc/packages/examples/http/demo_hello.pl +15 -0
  68. package/lib/swipl/doc/packages/examples/http/demo_login.pl +280 -0
  69. package/lib/swipl/doc/packages/examples/http/demo_openid.pl +177 -0
  70. package/lib/swipl/doc/packages/examples/http/demo_pwp.pl +20 -0
  71. package/lib/swipl/doc/packages/examples/http/demo_rest.pl +56 -0
  72. package/lib/swipl/doc/packages/examples/http/demo_threads.pl +52 -0
  73. package/lib/swipl/doc/packages/examples/http/linux-init-script +126 -0
  74. package/lib/swipl/doc/packages/examples/http/pwp/context.pwp +23 -0
  75. package/lib/swipl/doc/packages/examples/http/pwp/index.pwp +25 -0
  76. package/lib/swipl/doc/packages/examples/http/pwp/pwp1.pwp +12 -0
  77. package/lib/swipl/doc/packages/examples/http/pwp/pwp2.pwp +10 -0
  78. package/lib/swipl/doc/packages/examples/http/pwp/pwp3.pwp +25 -0
  79. package/lib/swipl/doc/packages/examples/http/pwp/pwp4.pwp +23 -0
  80. package/lib/swipl/doc/packages/examples/http/pwp/pwp5.pwp +11 -0
  81. package/lib/swipl/doc/packages/examples/http/pwp/pwp6.pwp +3 -0
  82. package/lib/swipl/doc/packages/examples/http/pwp/pwp7.pwp +3 -0
  83. package/lib/swipl/doc/packages/examples/http/pwp/pwp8.pwp +3 -0
  84. package/lib/swipl/doc/packages/examples/http/pwp/pwpdb.pl +12 -0
  85. package/lib/swipl/doc/packages/examples/http/systemd-script.service +18 -0
  86. package/lib/swipl/doc/packages/examples/http/upstart-script.conf +20 -0
  87. package/lib/swipl/doc/packages/examples/plunit/read.pl +17 -0
  88. package/lib/swipl/doc/packages/examples/plunit/simple.pl +33 -0
  89. package/lib/swipl/doc/packages/examples/ssl/client.pl +83 -0
  90. package/lib/swipl/doc/packages/examples/ssl/etc/README +52 -0
  91. package/lib/swipl/doc/packages/examples/ssl/etc/client/client-cert.p12 +0 -0
  92. package/lib/swipl/doc/packages/examples/ssl/etc/client/client-cert.pem +85 -0
  93. package/lib/swipl/doc/packages/examples/ssl/etc/client/client-key.pem +30 -0
  94. package/lib/swipl/doc/packages/examples/ssl/etc/demoCA/cacert.pem +82 -0
  95. package/lib/swipl/doc/packages/examples/ssl/etc/demoCA/careq.pem +18 -0
  96. package/lib/swipl/doc/packages/examples/ssl/etc/demoCA/crlnumber +1 -0
  97. package/lib/swipl/doc/packages/examples/ssl/etc/demoCA/index.txt +3 -0
  98. package/lib/swipl/doc/packages/examples/ssl/etc/demoCA/index.txt.attr +1 -0
  99. package/lib/swipl/doc/packages/examples/ssl/etc/demoCA/newcerts/9DC08797A0F63F7C.pem +85 -0
  100. package/lib/swipl/doc/packages/examples/ssl/etc/demoCA/newcerts/9DC08797A0F63F7D.pem +85 -0
  101. package/lib/swipl/doc/packages/examples/ssl/etc/demoCA/private/cakey.pem +30 -0
  102. package/lib/swipl/doc/packages/examples/ssl/etc/demoCA/serial +1 -0
  103. package/lib/swipl/doc/packages/examples/ssl/etc/server/server-cert.pem +85 -0
  104. package/lib/swipl/doc/packages/examples/ssl/etc/server/server-key.pem +30 -0
  105. package/lib/swipl/doc/packages/examples/ssl/https.pl +267 -0
  106. package/lib/swipl/doc/packages/examples/ssl/server.pl +106 -0
  107. package/lib/swipl/include/SWI-Prolog.h +1544 -0
  108. package/lib/swipl/include/SWI-Stream.h +537 -0
  109. package/lib/swipl/include/Yap/YapInterface.h +283 -0
  110. package/lib/swipl/include/sicstus/sicstus.h +452 -0
  111. package/lib/swipl/lib/x86_64-linux/cgi.so +0 -0
  112. package/lib/swipl/lib/x86_64-linux/crypt.so +0 -0
  113. package/lib/swipl/lib/x86_64-linux/crypto4pl.so +0 -0
  114. package/lib/swipl/lib/x86_64-linux/double_metaphone.so +0 -0
  115. package/lib/swipl/lib/x86_64-linux/files.so +0 -0
  116. package/lib/swipl/lib/x86_64-linux/hashstream.so +0 -0
  117. package/lib/swipl/lib/x86_64-linux/http_stream.so +0 -0
  118. package/lib/swipl/lib/x86_64-linux/isub.so +0 -0
  119. package/lib/swipl/lib/x86_64-linux/json.so +0 -0
  120. package/lib/swipl/lib/x86_64-linux/libswipl.so.10 +0 -0
  121. package/lib/swipl/lib/x86_64-linux/mallocinfo.so +0 -0
  122. package/lib/swipl/lib/x86_64-linux/md54pl.so +0 -0
  123. package/lib/swipl/lib/x86_64-linux/memfile.so +0 -0
  124. package/lib/swipl/lib/x86_64-linux/ntriples.so +0 -0
  125. package/lib/swipl/lib/x86_64-linux/pcre4pl.so +0 -0
  126. package/lib/swipl/lib/x86_64-linux/porter_stem.so +0 -0
  127. package/lib/swipl/lib/x86_64-linux/process.so +0 -0
  128. package/lib/swipl/lib/x86_64-linux/prolog_stream.so +0 -0
  129. package/lib/swipl/lib/x86_64-linux/rdf_db.so +0 -0
  130. package/lib/swipl/lib/x86_64-linux/readutil.so +0 -0
  131. package/lib/swipl/lib/x86_64-linux/rlimit.so +0 -0
  132. package/lib/swipl/lib/x86_64-linux/sched.so +0 -0
  133. package/lib/swipl/lib/x86_64-linux/sgml2pl.so +0 -0
  134. package/lib/swipl/lib/x86_64-linux/sha4pl.so +0 -0
  135. package/lib/swipl/lib/x86_64-linux/snowball.so +0 -0
  136. package/lib/swipl/lib/x86_64-linux/socket.so +0 -0
  137. package/lib/swipl/lib/x86_64-linux/ssl4pl.so +0 -0
  138. package/lib/swipl/lib/x86_64-linux/streaminfo.so +0 -0
  139. package/lib/swipl/lib/x86_64-linux/syslog.so +0 -0
  140. package/lib/swipl/lib/x86_64-linux/time.so +0 -0
  141. package/lib/swipl/lib/x86_64-linux/turtle.so +0 -0
  142. package/lib/swipl/lib/x86_64-linux/uid.so +0 -0
  143. package/lib/swipl/lib/x86_64-linux/unix.so +0 -0
  144. package/lib/swipl/lib/x86_64-linux/uri.so +0 -0
  145. package/lib/swipl/lib/x86_64-linux/websocket.so +0 -0
  146. package/lib/swipl/lib/x86_64-linux/zlib4pl.so +0 -0
  147. package/lib/swipl/library/INDEX.pl +1020 -0
  148. package/lib/swipl/library/aggregate.pl +755 -0
  149. package/lib/swipl/library/ansi_term.pl +653 -0
  150. package/lib/swipl/library/apply.pl +426 -0
  151. package/lib/swipl/library/apply_macros.pl +497 -0
  152. package/lib/swipl/library/arithmetic.pl +320 -0
  153. package/lib/swipl/library/assoc.pl +522 -0
  154. package/lib/swipl/library/atom.pl +169 -0
  155. package/lib/swipl/library/backcomp.pl +688 -0
  156. package/lib/swipl/library/base32.pl +291 -0
  157. package/lib/swipl/library/base64.pl +441 -0
  158. package/lib/swipl/library/broadcast.pl +161 -0
  159. package/lib/swipl/library/build/cmake.pl +137 -0
  160. package/lib/swipl/library/build/conan.pl +232 -0
  161. package/lib/swipl/library/build/make.pl +168 -0
  162. package/lib/swipl/library/build/tools.pl +825 -0
  163. package/lib/swipl/library/charsio.pl +195 -0
  164. package/lib/swipl/library/check.pl +1097 -0
  165. package/lib/swipl/library/check_installation.pl +912 -0
  166. package/lib/swipl/library/clp/INDEX.pl +157 -0
  167. package/lib/swipl/library/clp/bounds.pl +2036 -0
  168. package/lib/swipl/library/clp/clp_distinct.pl +263 -0
  169. package/lib/swipl/library/clp/clp_events.pl +92 -0
  170. package/lib/swipl/library/clp/clpb.pl +1896 -0
  171. package/lib/swipl/library/clp/clpfd.pl +7906 -0
  172. package/lib/swipl/library/clp/simplex.pl +1580 -0
  173. package/lib/swipl/library/codesio.pl +182 -0
  174. package/lib/swipl/library/coinduction.pl +183 -0
  175. package/lib/swipl/library/console_input.pl +289 -0
  176. package/lib/swipl/library/csv.pl +611 -0
  177. package/lib/swipl/library/ctypes.pl +161 -0
  178. package/lib/swipl/library/date.pl +370 -0
  179. package/lib/swipl/library/dcg/INDEX.pl +35 -0
  180. package/lib/swipl/library/dcg/basics.pl +514 -0
  181. package/lib/swipl/library/dcg/high_order.pl +227 -0
  182. package/lib/swipl/library/debug.pl +499 -0
  183. package/lib/swipl/library/dialect/bim.pl +186 -0
  184. package/lib/swipl/library/dialect/commons.pl +77 -0
  185. package/lib/swipl/library/dialect/eclipse/test_util_iso.pl +355 -0
  186. package/lib/swipl/library/dialect/hprolog/format.pl +44 -0
  187. package/lib/swipl/library/dialect/hprolog.pl +314 -0
  188. package/lib/swipl/library/dialect/ifprolog.pl +1317 -0
  189. package/lib/swipl/library/dialect/iso/iso_predicates.pl +277 -0
  190. package/lib/swipl/library/dialect/sicstus/README.TXT +1 -0
  191. package/lib/swipl/library/dialect/sicstus/arrays.pl +121 -0
  192. package/lib/swipl/library/dialect/sicstus/block.pl +278 -0
  193. package/lib/swipl/library/dialect/sicstus/lists.pl +167 -0
  194. package/lib/swipl/library/dialect/sicstus/ordsets.pl +52 -0
  195. package/lib/swipl/library/dialect/sicstus/sockets.pl +174 -0
  196. package/lib/swipl/library/dialect/sicstus/system.pl +262 -0
  197. package/lib/swipl/library/dialect/sicstus/terms.pl +57 -0
  198. package/lib/swipl/library/dialect/sicstus/timeout.pl +92 -0
  199. package/lib/swipl/library/dialect/sicstus.pl +582 -0
  200. package/lib/swipl/library/dialect/sicstus4/aggregate.pl +60 -0
  201. package/lib/swipl/library/dialect/sicstus4/between.pl +62 -0
  202. package/lib/swipl/library/dialect/sicstus4/clpfd.pl +75 -0
  203. package/lib/swipl/library/dialect/sicstus4/file_systems.pl +413 -0
  204. package/lib/swipl/library/dialect/sicstus4/lists.pl +274 -0
  205. package/lib/swipl/library/dialect/sicstus4/ordsets.pl +54 -0
  206. package/lib/swipl/library/dialect/sicstus4/samsort.pl +86 -0
  207. package/lib/swipl/library/dialect/sicstus4/sets.pl +124 -0
  208. package/lib/swipl/library/dialect/sicstus4/sockets.pl +164 -0
  209. package/lib/swipl/library/dialect/sicstus4/system.pl +80 -0
  210. package/lib/swipl/library/dialect/sicstus4/terms.pl +89 -0
  211. package/lib/swipl/library/dialect/sicstus4/timeout.pl +43 -0
  212. package/lib/swipl/library/dialect/sicstus4/types.pl +103 -0
  213. package/lib/swipl/library/dialect/sicstus4.pl +200 -0
  214. package/lib/swipl/library/dialect/swi/syspred_options.pl +250 -0
  215. package/lib/swipl/library/dialect/xsb/README.md +20 -0
  216. package/lib/swipl/library/dialect/xsb/basics.pl +318 -0
  217. package/lib/swipl/library/dialect/xsb/consult.pl +87 -0
  218. package/lib/swipl/library/dialect/xsb/curr_sym.pl +57 -0
  219. package/lib/swipl/library/dialect/xsb/error_handler.pl +111 -0
  220. package/lib/swipl/library/dialect/xsb/gensym.pl +53 -0
  221. package/lib/swipl/library/dialect/xsb/gpp.pl +138 -0
  222. package/lib/swipl/library/dialect/xsb/intern.pl +48 -0
  223. package/lib/swipl/library/dialect/xsb/lists.pl +47 -0
  224. package/lib/swipl/library/dialect/xsb/machine.pl +295 -0
  225. package/lib/swipl/library/dialect/xsb/ordsets.pl +53 -0
  226. package/lib/swipl/library/dialect/xsb/setof.pl +54 -0
  227. package/lib/swipl/library/dialect/xsb/source.pl +308 -0
  228. package/lib/swipl/library/dialect/xsb/standard.pl +169 -0
  229. package/lib/swipl/library/dialect/xsb/storage.pl +70 -0
  230. package/lib/swipl/library/dialect/xsb/string.pl +76 -0
  231. package/lib/swipl/library/dialect/xsb/thread.pl +62 -0
  232. package/lib/swipl/library/dialect/xsb/timed_call.pl +158 -0
  233. package/lib/swipl/library/dialect/xsb.pl +688 -0
  234. package/lib/swipl/library/dialect/yap/README.TXT +8 -0
  235. package/lib/swipl/library/dialect/yap.pl +283 -0
  236. package/lib/swipl/library/dialect.pl +123 -0
  237. package/lib/swipl/library/dicts.pl +399 -0
  238. package/lib/swipl/library/dif.pl +426 -0
  239. package/lib/swipl/library/edinburgh.pl +138 -0
  240. package/lib/swipl/library/edit.pl +783 -0
  241. package/lib/swipl/library/error.pl +495 -0
  242. package/lib/swipl/library/exceptions.pl +231 -0
  243. package/lib/swipl/library/explain.pl +543 -0
  244. package/lib/swipl/library/ext/RDF/INDEX.pl +25 -0
  245. package/lib/swipl/library/ext/RDF/rdf.pl +450 -0
  246. package/lib/swipl/library/ext/RDF/rdf_parser.pl +649 -0
  247. package/lib/swipl/library/ext/RDF/rdf_triple.pl +448 -0
  248. package/lib/swipl/library/ext/RDF/rdf_write.pl +701 -0
  249. package/lib/swipl/library/ext/RDF/rewrite_term.pl +177 -0
  250. package/lib/swipl/library/ext/chr/chr/a_star.pl +80 -0
  251. package/lib/swipl/library/ext/chr/chr/binomialheap.pl +142 -0
  252. package/lib/swipl/library/ext/chr/chr/builtins.pl +629 -0
  253. package/lib/swipl/library/ext/chr/chr/chr_compiler_errors.pl +179 -0
  254. package/lib/swipl/library/ext/chr/chr/chr_compiler_options.pl +382 -0
  255. package/lib/swipl/library/ext/chr/chr/chr_compiler_utility.pl +338 -0
  256. package/lib/swipl/library/ext/chr/chr/chr_debug.pl +59 -0
  257. package/lib/swipl/library/ext/chr/chr/chr_hashtable_store.pl +425 -0
  258. package/lib/swipl/library/ext/chr/chr/chr_integertable_store.pl +139 -0
  259. package/lib/swipl/library/ext/chr/chr/chr_messages.pl +176 -0
  260. package/lib/swipl/library/ext/chr/chr/chr_op.pl +53 -0
  261. package/lib/swipl/library/ext/chr/chr/chr_runtime.pl +1016 -0
  262. package/lib/swipl/library/ext/chr/chr/chr_translate.pl +25424 -0
  263. package/lib/swipl/library/ext/chr/chr/clean_code.pl +252 -0
  264. package/lib/swipl/library/ext/chr/chr/find.pl +78 -0
  265. package/lib/swipl/library/ext/chr/chr/guard_entailment.pl +19432 -0
  266. package/lib/swipl/library/ext/chr/chr/listmap.pl +108 -0
  267. package/lib/swipl/library/ext/chr/chr/pairlist.pl +78 -0
  268. package/lib/swipl/library/ext/chr/chr.pl +686 -0
  269. package/lib/swipl/library/ext/clib/INDEX.pl +158 -0
  270. package/lib/swipl/library/ext/clib/cgi.pl +86 -0
  271. package/lib/swipl/library/ext/clib/crypt.pl +44 -0
  272. package/lib/swipl/library/ext/clib/filesex.pl +644 -0
  273. package/lib/swipl/library/ext/clib/hash_stream.pl +119 -0
  274. package/lib/swipl/library/ext/clib/mallocinfo.pl +138 -0
  275. package/lib/swipl/library/ext/clib/md5.pl +68 -0
  276. package/lib/swipl/library/ext/clib/memfile.pl +61 -0
  277. package/lib/swipl/library/ext/clib/process.pl +573 -0
  278. package/lib/swipl/library/ext/clib/prolog_server.pl +157 -0
  279. package/lib/swipl/library/ext/clib/prolog_stream.pl +80 -0
  280. package/lib/swipl/library/ext/clib/rlimit.pl +54 -0
  281. package/lib/swipl/library/ext/clib/sched.pl +85 -0
  282. package/lib/swipl/library/ext/clib/sha.pl +162 -0
  283. package/lib/swipl/library/ext/clib/socket.pl +1102 -0
  284. package/lib/swipl/library/ext/clib/streaminfo.pl +103 -0
  285. package/lib/swipl/library/ext/clib/streampool.pl +136 -0
  286. package/lib/swipl/library/ext/clib/syslog.pl +180 -0
  287. package/lib/swipl/library/ext/clib/time.pl +194 -0
  288. package/lib/swipl/library/ext/clib/udp_broadcast.pl +975 -0
  289. package/lib/swipl/library/ext/clib/uid.pl +220 -0
  290. package/lib/swipl/library/ext/clib/unix.pl +317 -0
  291. package/lib/swipl/library/ext/clib/uri.pl +530 -0
  292. package/lib/swipl/library/ext/clib/uuid.pl +207 -0
  293. package/lib/swipl/library/ext/http/http/INDEX.pl +266 -0
  294. package/lib/swipl/library/ext/http/http/README.md +34 -0
  295. package/lib/swipl/library/ext/http/http/ax.pl +189 -0
  296. package/lib/swipl/library/ext/http/http/dcg_basics.pl +49 -0
  297. package/lib/swipl/library/ext/http/http/graphql.pl +1367 -0
  298. package/lib/swipl/library/ext/http/http/html_decl.pl +416 -0
  299. package/lib/swipl/library/ext/http/http/html_head.pl +654 -0
  300. package/lib/swipl/library/ext/http/http/html_quasiquotations.pl +120 -0
  301. package/lib/swipl/library/ext/http/http/html_write.pl +1425 -0
  302. package/lib/swipl/library/ext/http/http/htmx.pl +139 -0
  303. package/lib/swipl/library/ext/http/http/http_authenticate.pl +364 -0
  304. package/lib/swipl/library/ext/http/http/http_client.pl +412 -0
  305. package/lib/swipl/library/ext/http/http/http_cookie.pl +245 -0
  306. package/lib/swipl/library/ext/http/http/http_cors.pl +176 -0
  307. package/lib/swipl/library/ext/http/http/http_digest.pl +820 -0
  308. package/lib/swipl/library/ext/http/http/http_dirindex.pl +296 -0
  309. package/lib/swipl/library/ext/http/http/http_dispatch.pl +1552 -0
  310. package/lib/swipl/library/ext/http/http/http_dyn_workers.pl +214 -0
  311. package/lib/swipl/library/ext/http/http/http_error.pl +126 -0
  312. package/lib/swipl/library/ext/http/http/http_exception.pl +225 -0
  313. package/lib/swipl/library/ext/http/http/http_files.pl +163 -0
  314. package/lib/swipl/library/ext/http/http/http_header.pl +2820 -0
  315. package/lib/swipl/library/ext/http/http/http_hook.pl +106 -0
  316. package/lib/swipl/library/ext/http/http/http_host.pl +204 -0
  317. package/lib/swipl/library/ext/http/http/http_load.pl +113 -0
  318. package/lib/swipl/library/ext/http/http/http_log.pl +738 -0
  319. package/lib/swipl/library/ext/http/http/http_multipart_plugin.pl +232 -0
  320. package/lib/swipl/library/ext/http/http/http_open.pl +1821 -0
  321. package/lib/swipl/library/ext/http/http/http_openid.pl +1561 -0
  322. package/lib/swipl/library/ext/http/http/http_parameters.pl +504 -0
  323. package/lib/swipl/library/ext/http/http/http_path.pl +380 -0
  324. package/lib/swipl/library/ext/http/http/http_proxy.pl +80 -0
  325. package/lib/swipl/library/ext/http/http/http_pwp.pl +351 -0
  326. package/lib/swipl/library/ext/http/http/http_redis_plugin.pl +332 -0
  327. package/lib/swipl/library/ext/http/http/http_server.pl +105 -0
  328. package/lib/swipl/library/ext/http/http/http_server_files.pl +126 -0
  329. package/lib/swipl/library/ext/http/http/http_server_health.pl +186 -0
  330. package/lib/swipl/library/ext/http/http/http_session.pl +1040 -0
  331. package/lib/swipl/library/ext/http/http/http_sgml_plugin.pl +137 -0
  332. package/lib/swipl/library/ext/http/http/http_stream.pl +376 -0
  333. package/lib/swipl/library/ext/http/http/http_unix_daemon.pl +989 -0
  334. package/lib/swipl/library/ext/http/http/http_wrapper.pl +559 -0
  335. package/lib/swipl/library/ext/http/http/hub.pl +600 -0
  336. package/lib/swipl/library/ext/http/http/js_write.pl +461 -0
  337. package/lib/swipl/library/ext/http/http/mimepack.pl +224 -0
  338. package/lib/swipl/library/ext/http/http/mimetype.pl +276 -0
  339. package/lib/swipl/library/ext/http/http/term_html.pl +797 -0
  340. package/lib/swipl/library/ext/http/http/thread_httpd.pl +1181 -0
  341. package/lib/swipl/library/ext/http/http/web/css/dirindex.css +24 -0
  342. package/lib/swipl/library/ext/http/http/web/css/openid.css +57 -0
  343. package/lib/swipl/library/ext/http/http/web/icons/back.png +0 -0
  344. package/lib/swipl/library/ext/http/http/web/icons/c.png +0 -0
  345. package/lib/swipl/library/ext/http/http/web/icons/compressed.png +0 -0
  346. package/lib/swipl/library/ext/http/http/web/icons/folder.png +0 -0
  347. package/lib/swipl/library/ext/http/http/web/icons/generic.png +0 -0
  348. package/lib/swipl/library/ext/http/http/web/icons/layout.png +0 -0
  349. package/lib/swipl/library/ext/http/http/web/icons/openid-logo-square.png +0 -0
  350. package/lib/swipl/library/ext/http/http/web/icons/openid-logo-tiny.png +0 -0
  351. package/lib/swipl/library/ext/http/http/websocket.pl +729 -0
  352. package/lib/swipl/library/ext/http/http/yadis.pl +205 -0
  353. package/lib/swipl/library/ext/json/INDEX.pl +36 -0
  354. package/lib/swipl/library/ext/json/http/INDEX.pl +16 -0
  355. package/lib/swipl/library/ext/json/http/http_json.pl +490 -0
  356. package/lib/swipl/library/ext/json/http/js_grammar.pl +53 -0
  357. package/lib/swipl/library/ext/json/http/json.pl +48 -0
  358. package/lib/swipl/library/ext/json/http/json_convert.pl +48 -0
  359. package/lib/swipl/library/ext/json/json.pl +1215 -0
  360. package/lib/swipl/library/ext/json/json_convert.pl +681 -0
  361. package/lib/swipl/library/ext/json/json_grammar.pl +349 -0
  362. package/lib/swipl/library/ext/json/json_rpc_client.pl +423 -0
  363. package/lib/swipl/library/ext/json/json_rpc_common.pl +104 -0
  364. package/lib/swipl/library/ext/json/json_rpc_server.pl +489 -0
  365. package/lib/swipl/library/ext/json/json_schema.pl +2111 -0
  366. package/lib/swipl/library/ext/nlp/INDEX.pl +15 -0
  367. package/lib/swipl/library/ext/nlp/double_metaphone.pl +86 -0
  368. package/lib/swipl/library/ext/nlp/isub.pl +162 -0
  369. package/lib/swipl/library/ext/nlp/porter_stem.pl +50 -0
  370. package/lib/swipl/library/ext/nlp/snowball.pl +97 -0
  371. package/lib/swipl/library/ext/pcre/INDEX.pl +17 -0
  372. package/lib/swipl/library/ext/pcre/pcre.pl +974 -0
  373. package/lib/swipl/library/ext/plunit/INDEX.pl +20 -0
  374. package/lib/swipl/library/ext/plunit/plunit.pl +2631 -0
  375. package/lib/swipl/library/ext/plunit/test_wizard.pl +249 -0
  376. package/lib/swipl/library/ext/semweb/semweb/INDEX.pl +288 -0
  377. package/lib/swipl/library/ext/semweb/semweb/rdf11.pl +2106 -0
  378. package/lib/swipl/library/ext/semweb/semweb/rdf11_containers.pl +299 -0
  379. package/lib/swipl/library/ext/semweb/semweb/rdf_cache.pl +278 -0
  380. package/lib/swipl/library/ext/semweb/semweb/rdf_compare.pl +132 -0
  381. package/lib/swipl/library/ext/semweb/semweb/rdf_db.pl +3468 -0
  382. package/lib/swipl/library/ext/semweb/semweb/rdf_edit.pl +1145 -0
  383. package/lib/swipl/library/ext/semweb/semweb/rdf_http_plugin.pl +274 -0
  384. package/lib/swipl/library/ext/semweb/semweb/rdf_library.pl +1021 -0
  385. package/lib/swipl/library/ext/semweb/semweb/rdf_litindex.pl +1035 -0
  386. package/lib/swipl/library/ext/semweb/semweb/rdf_ntriples.pl +422 -0
  387. package/lib/swipl/library/ext/semweb/semweb/rdf_persistency.pl +1576 -0
  388. package/lib/swipl/library/ext/semweb/semweb/rdf_portray.pl +182 -0
  389. package/lib/swipl/library/ext/semweb/semweb/rdf_prefixes.pl +681 -0
  390. package/lib/swipl/library/ext/semweb/semweb/rdf_sandbox.pl +153 -0
  391. package/lib/swipl/library/ext/semweb/semweb/rdf_turtle.pl +54 -0
  392. package/lib/swipl/library/ext/semweb/semweb/rdf_turtle_write.pl +1587 -0
  393. package/lib/swipl/library/ext/semweb/semweb/rdf_zlib_plugin.pl +52 -0
  394. package/lib/swipl/library/ext/semweb/semweb/rdfa.pl +1510 -0
  395. package/lib/swipl/library/ext/semweb/semweb/rdfs.pl +475 -0
  396. package/lib/swipl/library/ext/semweb/semweb/sparql_client.pl +643 -0
  397. package/lib/swipl/library/ext/semweb/semweb/turtle.pl +455 -0
  398. package/lib/swipl/library/ext/sgml/DTD/HTML4.dcl +88 -0
  399. package/lib/swipl/library/ext/sgml/DTD/HTML4.dtd +1092 -0
  400. package/lib/swipl/library/ext/sgml/DTD/HTML4.soc +6 -0
  401. package/lib/swipl/library/ext/sgml/DTD/HTML5.dtd +906 -0
  402. package/lib/swipl/library/ext/sgml/DTD/HTMLlat1.ent +195 -0
  403. package/lib/swipl/library/ext/sgml/DTD/HTMLspec.ent +77 -0
  404. package/lib/swipl/library/ext/sgml/DTD/HTMLsym.ent +241 -0
  405. package/lib/swipl/library/ext/sgml/INDEX.pl +64 -0
  406. package/lib/swipl/library/ext/sgml/c14n2.pl +203 -0
  407. package/lib/swipl/library/ext/sgml/iso_639.pl +631 -0
  408. package/lib/swipl/library/ext/sgml/pwp.pl +705 -0
  409. package/lib/swipl/library/ext/sgml/sgml.pl +815 -0
  410. package/lib/swipl/library/ext/sgml/sgml_write.pl +1003 -0
  411. package/lib/swipl/library/ext/sgml/xpath.pl +620 -0
  412. package/lib/swipl/library/ext/sgml/xsdp_types.pl +232 -0
  413. package/lib/swipl/library/ext/ssl/INDEX.pl +60 -0
  414. package/lib/swipl/library/ext/ssl/crypto.pl +1015 -0
  415. package/lib/swipl/library/ext/ssl/http/INDEX.pl +5 -0
  416. package/lib/swipl/library/ext/ssl/http/http_ssl_plugin.pl +296 -0
  417. package/lib/swipl/library/ext/ssl/saml.pl +609 -0
  418. package/lib/swipl/library/ext/ssl/ssl.pl +647 -0
  419. package/lib/swipl/library/ext/ssl/xmldsig.pl +391 -0
  420. package/lib/swipl/library/ext/ssl/xmlenc.pl +354 -0
  421. package/lib/swipl/library/ext/zlib/INDEX.pl +8 -0
  422. package/lib/swipl/library/ext/zlib/zlib.pl +112 -0
  423. package/lib/swipl/library/fastrw.pl +105 -0
  424. package/lib/swipl/library/files.pl +80 -0
  425. package/lib/swipl/library/gensym.pl +111 -0
  426. package/lib/swipl/library/git.pl +1027 -0
  427. package/lib/swipl/library/hashtable.pl +365 -0
  428. package/lib/swipl/library/heaps.pl +252 -0
  429. package/lib/swipl/library/hotfix.pl +239 -0
  430. package/lib/swipl/library/http/jquery.pl +103 -0
  431. package/lib/swipl/library/http/web/js/jquery-3.6.0.min.js +2 -0
  432. package/lib/swipl/library/increval.pl +217 -0
  433. package/lib/swipl/library/intercept.pl +263 -0
  434. package/lib/swipl/library/iostream.pl +237 -0
  435. package/lib/swipl/library/iri_scheme/file.pl +70 -0
  436. package/lib/swipl/library/lazy_lists.pl +481 -0
  437. package/lib/swipl/library/listing.pl +1362 -0
  438. package/lib/swipl/library/lists.pl +860 -0
  439. package/lib/swipl/library/lynx/INDEX.pl +14 -0
  440. package/lib/swipl/library/lynx/format.pl +380 -0
  441. package/lib/swipl/library/lynx/html_style.pl +129 -0
  442. package/lib/swipl/library/lynx/html_text.pl +865 -0
  443. package/lib/swipl/library/lynx/pldoc_style.pl +87 -0
  444. package/lib/swipl/library/macros.pl +451 -0
  445. package/lib/swipl/library/main.pl +1200 -0
  446. package/lib/swipl/library/make.pl +197 -0
  447. package/lib/swipl/library/modules.pl +129 -0
  448. package/lib/swipl/library/nb_rbtrees.pl +267 -0
  449. package/lib/swipl/library/nb_set.pl +203 -0
  450. package/lib/swipl/library/obfuscate.pl +115 -0
  451. package/lib/swipl/library/occurs.pl +241 -0
  452. package/lib/swipl/library/operators.pl +182 -0
  453. package/lib/swipl/library/option.pl +388 -0
  454. package/lib/swipl/library/optparse.pl +966 -0
  455. package/lib/swipl/library/ordsets.pl +518 -0
  456. package/lib/swipl/library/oset.pl +160 -0
  457. package/lib/swipl/library/pairs.pl +187 -0
  458. package/lib/swipl/library/persistency.pl +713 -0
  459. package/lib/swipl/library/pio.pl +47 -0
  460. package/lib/swipl/library/portray_text.pl +277 -0
  461. package/lib/swipl/library/pprint.pl +940 -0
  462. package/lib/swipl/library/predicate_options.pl +975 -0
  463. package/lib/swipl/library/prolog_autoload.pl +233 -0
  464. package/lib/swipl/library/prolog_breakpoints.pl +396 -0
  465. package/lib/swipl/library/prolog_clause.pl +1104 -0
  466. package/lib/swipl/library/prolog_code.pl +416 -0
  467. package/lib/swipl/library/prolog_codewalk.pl +1248 -0
  468. package/lib/swipl/library/prolog_colour.pl +3301 -0
  469. package/lib/swipl/library/prolog_colour.qlf +0 -0
  470. package/lib/swipl/library/prolog_config.pl +154 -0
  471. package/lib/swipl/library/prolog_coverage.pl +1333 -0
  472. package/lib/swipl/library/prolog_debug.pl +404 -0
  473. package/lib/swipl/library/prolog_deps.pl +675 -0
  474. package/lib/swipl/library/prolog_evaluable.pl +234 -0
  475. package/lib/swipl/library/prolog_format.pl +219 -0
  476. package/lib/swipl/library/prolog_history.pl +161 -0
  477. package/lib/swipl/library/prolog_jiti.pl +333 -0
  478. package/lib/swipl/library/prolog_locale.pl +61 -0
  479. package/lib/swipl/library/prolog_metainference.pl +307 -0
  480. package/lib/swipl/library/prolog_pack.pl +3906 -0
  481. package/lib/swipl/library/prolog_profile.pl +346 -0
  482. package/lib/swipl/library/prolog_qlfmake.pl +533 -0
  483. package/lib/swipl/library/prolog_source.pl +1188 -0
  484. package/lib/swipl/library/prolog_stack.pl +794 -0
  485. package/lib/swipl/library/prolog_trace.pl +365 -0
  486. package/lib/swipl/library/prolog_versions.pl +347 -0
  487. package/lib/swipl/library/prolog_wrap.pl +164 -0
  488. package/lib/swipl/library/prolog_xref.pl +3090 -0
  489. package/lib/swipl/library/prolog_xref.qlf +0 -0
  490. package/lib/swipl/library/pure_input.pl +266 -0
  491. package/lib/swipl/library/qpforeign.pl +695 -0
  492. package/lib/swipl/library/qsave.pl +1516 -0
  493. package/lib/swipl/library/quasi_quotations.pl +300 -0
  494. package/lib/swipl/library/quintus.pl +437 -0
  495. package/lib/swipl/library/random.pl +459 -0
  496. package/lib/swipl/library/rbtrees.pl +1153 -0
  497. package/lib/swipl/library/readln.pl +256 -0
  498. package/lib/swipl/library/readutil.pl +304 -0
  499. package/lib/swipl/library/record.pl +544 -0
  500. package/lib/swipl/library/rwlocks.pl +172 -0
  501. package/lib/swipl/library/sandbox.pl +1384 -0
  502. package/lib/swipl/library/semweb/dc.rdfs +198 -0
  503. package/lib/swipl/library/semweb/eor.rdfs +95 -0
  504. package/lib/swipl/library/semweb/owl.owl +287 -0
  505. package/lib/swipl/library/semweb/rdf_library.ttl +90 -0
  506. package/lib/swipl/library/semweb/rdfs.rdfs +251 -0
  507. package/lib/swipl/library/settings.pl +755 -0
  508. package/lib/swipl/library/shell.pl +462 -0
  509. package/lib/swipl/library/shlib.pl +670 -0
  510. package/lib/swipl/library/solution_sequences.pl +439 -0
  511. package/lib/swipl/library/sort.pl +119 -0
  512. package/lib/swipl/library/statistics.pl +541 -0
  513. package/lib/swipl/library/streams.pl +118 -0
  514. package/lib/swipl/library/strings.pl +469 -0
  515. package/lib/swipl/library/system.pl +95 -0
  516. package/lib/swipl/library/tables.pl +381 -0
  517. package/lib/swipl/library/tableutil.pl +771 -0
  518. package/lib/swipl/library/tabling.pl +42 -0
  519. package/lib/swipl/library/terms.pl +457 -0
  520. package/lib/swipl/library/theme/auto.pl +61 -0
  521. package/lib/swipl/library/theme/dark.pl +455 -0
  522. package/lib/swipl/library/theme/light.pl +45 -0
  523. package/lib/swipl/library/thread.pl +810 -0
  524. package/lib/swipl/library/thread_pool.pl +508 -0
  525. package/lib/swipl/library/threadutil.pl +574 -0
  526. package/lib/swipl/library/tty.pl +316 -0
  527. package/lib/swipl/library/ugraphs.pl +644 -0
  528. package/lib/swipl/library/unicode/INDEX.pl +6 -0
  529. package/lib/swipl/library/unicode/blocks.pl +414 -0
  530. package/lib/swipl/library/url.pl +1129 -0
  531. package/lib/swipl/library/utf8.pl +145 -0
  532. package/lib/swipl/library/varnumbers.pl +221 -0
  533. package/lib/swipl/library/vm.pl +251 -0
  534. package/lib/swipl/library/wfs.pl +205 -0
  535. package/lib/swipl/library/when.pl +263 -0
  536. package/lib/swipl/library/writef.pl +278 -0
  537. package/lib/swipl/library/www_browser.pl +251 -0
  538. package/lib/swipl/library/yall.pl +574 -0
  539. package/lib/swipl/library/zip.pl +211 -0
  540. package/lib/swipl/swipl.home +1 -0
  541. package/lib/vendor/libcrypto.so.3 +0 -0
  542. package/lib/vendor/libpcre2-8.so.0 +0 -0
  543. package/lib/vendor/libssl.so.3 +0 -0
  544. package/lib/vendor/libz.so.1 +0 -0
  545. package/licenses/OpenSSL-LICENSE.txt +177 -0
  546. package/licenses/PCRE2-COPYING +5 -0
  547. package/licenses/README.md +12 -0
  548. package/licenses/SWI-Prolog-LICENSE +41 -0
  549. package/licenses/zlib-LICENSE +22 -0
  550. package/package.json +27 -0
@@ -0,0 +1,1552 @@
1
+ /* Part of SWI-Prolog
2
+
3
+ Author: Jan Wielemaker
4
+ E-mail: J.Wielemaker@vu.nl
5
+ WWW: http://www.swi-prolog.org
6
+ Copyright (c) 2007-2020, University of Amsterdam
7
+ VU University Amsterdam
8
+ CWI, Amsterdam
9
+ All rights reserved.
10
+
11
+ Redistribution and use in source and binary forms, with or without
12
+ modification, are permitted provided that the following conditions
13
+ are met:
14
+
15
+ 1. Redistributions of source code must retain the above copyright
16
+ notice, this list of conditions and the following disclaimer.
17
+
18
+ 2. Redistributions in binary form must reproduce the above copyright
19
+ notice, this list of conditions and the following disclaimer in
20
+ the documentation and/or other materials provided with the
21
+ distribution.
22
+
23
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26
+ FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27
+ COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28
+ INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29
+ BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32
+ LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33
+ ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34
+ POSSIBILITY OF SUCH DAMAGE.
35
+ */
36
+
37
+ :- module(http_dispatch,
38
+ [ http_dispatch/1, % +Request
39
+ http_handler/3, % +Path, +Predicate, +Options
40
+ http_delete_handler/1, % +Path
41
+ http_request_expansion/2, % :Goal, +Rank
42
+ http_reply_file/3, % +File, +Options, +Request
43
+ http_redirect/3, % +How, +Path, +Request
44
+ http_404/2, % +Options, +Request
45
+ http_switch_protocol/2, % :Goal, +Options
46
+ http_current_handler/2, % ?Path, ?Pred
47
+ http_current_handler/3, % ?Path, ?Pred, -Options
48
+ http_location_by_id/2, % +ID, -Location
49
+ http_link_to_id/3, % +ID, +Parameters, -HREF
50
+ http_reload_with_parameters/3, % +Request, +Parameters, -HREF
51
+ http_safe_file/2 % +Spec, +Options
52
+ ]).
53
+ :- use_module(library(lists),
54
+ [ select/3, append/3, append/2, same_length/2, member/2,
55
+ last/2, delete/3
56
+ ]).
57
+ :- autoload(library(apply),
58
+ [partition/4,maplist/3,maplist/2,include/3,exclude/3]).
59
+ :- autoload(library(broadcast),[listen/2]).
60
+ :- autoload(library(error),
61
+ [ must_be/2,
62
+ domain_error/2,
63
+ type_error/2,
64
+ instantiation_error/1,
65
+ existence_error/2,
66
+ permission_error/3
67
+ ]).
68
+ :- autoload(library(filesex),[directory_file_path/3]).
69
+ :- autoload(library(option),[option/3,option/2,merge_options/3]).
70
+ :- autoload(library(pairs),[pairs_values/2]).
71
+ :- if(exists_source(library(time))).
72
+ :- autoload(library(time),[call_with_time_limit/2]).
73
+ :- endif.
74
+ :- autoload(library(uri),
75
+ [ uri_encoded/3,
76
+ uri_data/3,
77
+ uri_components/2,
78
+ uri_query_components/2
79
+ ]).
80
+ :- autoload(library(http/http_header),[http_timestamp/2]).
81
+ :- autoload(library(http/http_path),[http_absolute_location/3]).
82
+ :- autoload(library(http/mimetype),
83
+ [file_content_type/2,file_content_type/3]).
84
+ :- if(exists_source(library(http/thread_httpd))).
85
+ :- autoload(library(http/thread_httpd),[http_spawn/2]).
86
+ :- endif.
87
+ :- use_module(library(settings),[setting/4,setting/2]).
88
+
89
+ :- predicate_options(http_404/2, 1, [index(any)]).
90
+ :- predicate_options(http_reply_file/3, 2,
91
+ [ cache(boolean),
92
+ mime_type(any),
93
+ static_gzip(boolean),
94
+ cached_gzip(boolean),
95
+ pass_to(http_safe_file/2, 2),
96
+ headers(list)
97
+ ]).
98
+ :- predicate_options(http_safe_file/2, 2, [unsafe(boolean)]).
99
+ :- predicate_options(http_switch_protocol/2, 2,
100
+ [ headers(list)
101
+ ]).
102
+
103
+ /** <module> Dispatch requests in the HTTP server
104
+
105
+ Most code doesn't need to use this directly; instead use
106
+ library(http/http_server), which combines this library with the
107
+ typical HTTP libraries that most servers need.
108
+
109
+ This module can be placed between http_wrapper.pl and the application
110
+ code to associate HTTP _locations_ to predicates that serve the pages.
111
+ In addition, it associates parameters with locations that deal with
112
+ timeout handling and user authentication. The typical setup is:
113
+
114
+ ==
115
+ server(Port, Options) :-
116
+ http_server(http_dispatch,
117
+ [ port(Port)
118
+ | Options
119
+ ]).
120
+
121
+ :- http_handler('/index.html', write_index, []).
122
+
123
+ write_index(Request) :-
124
+ ...
125
+ ==
126
+ */
127
+
128
+ :- setting(http:time_limit, nonneg, 300,
129
+ 'Time limit handling a single query (0=infinite)').
130
+
131
+ %! http_handler(+Path, :Closure, +Options) is det.
132
+ %
133
+ % Register Closure as a handler for HTTP requests. Path is either an
134
+ % absolute path such as =|'/home.html'|= or a term Alias(Relative).
135
+ % Where Alias is associated with a concrete path using http:location/3
136
+ % and resolved using http_absolute_location/3. `Relative` can be a
137
+ % single atom or a term `Segment1/Segment2/...`, where each element is
138
+ % either an atom or a variable. If a segment is a variable it matches
139
+ % any segment and the binding may be passed to the closure. If the
140
+ % last segment is a variable it may match multiple segments. This
141
+ % allows registering REST paths, for example:
142
+ %
143
+ % ```
144
+ % :- http_handler(root(user/User), user(Method, User),
145
+ % [ method(Method),
146
+ % methods([get,post,put])
147
+ % ]).
148
+ %
149
+ % user(get, User, Request) :-
150
+ % ...
151
+ % user(post, User, Request) :-
152
+ % ...
153
+ % ```
154
+ %
155
+ % If an HTTP request arrives at the server that matches Path, Closure
156
+ % is called as below, where `Request` is the parsed HTTP request.
157
+ %
158
+ % call(Closure, Request)
159
+ %
160
+ % Options is a list containing the following options:
161
+ %
162
+ % - authentication(+Type)
163
+ % Demand authentication. Authentication methods are plugable. The
164
+ % library http_authenticate.pl provides a plugin for user/password
165
+ % based =Basic= HTTP authentication.
166
+ %
167
+ % - chunked
168
+ % Use =|Transfer-encoding: chunked|= if the client allows for it.
169
+ %
170
+ % - condition(:Goal)
171
+ % If present, the handler is ignored if Goal does not succeed.
172
+ %
173
+ % - content_type(+Term)
174
+ % Specifies the content-type of the reply. This value is currently
175
+ % not used by this library. It enhances the reflexive capabilities
176
+ % of this library through http_current_handler/3.
177
+ %
178
+ % - id(+Atom)
179
+ % Identifier of the handler. The default identifier is the
180
+ % predicate name. Used by http_location_by_id/2 and
181
+ % http_link_to_id/3.
182
+ %
183
+ % - hide_children(+Bool)
184
+ % If =true= on a prefix-handler (see prefix), possible children
185
+ % are masked. This can be used to (temporary) overrule part of the
186
+ % tree.
187
+ %
188
+ % - method(+Method)
189
+ % Declare that the handler processes Method. This is equivalent to
190
+ % methods([Method]). Using method(*) allows for all methods.
191
+ %
192
+ % - methods(+ListOfMethods)
193
+ % Declare that the handler processes all of the given methods. If
194
+ % this option appears multiple times, the methods are combined.
195
+ %
196
+ % - prefix
197
+ % Call Pred on any location that is a specialisation of Path. If
198
+ % multiple handlers match, the one with the longest path is used.
199
+ % Options defined with a prefix handler are the default options
200
+ % for paths that start with this prefix. Note that the handler
201
+ % acts as a fallback handler for the tree below it:
202
+ %
203
+ % ==
204
+ % :- http_handler(/, http_404([index('index.html')]),
205
+ % [spawn(my_pool),prefix]).
206
+ % ==
207
+ %
208
+ % - priority(+Integer)
209
+ % If two handlers handle the same path, the one with the highest
210
+ % priority is used. If equal, the last registered is used. Please
211
+ % be aware that the order of clauses in multifile predicates can
212
+ % change due to reloading files. The default priority is 0 (zero).
213
+ %
214
+ % - spawn(+SpawnOptions)
215
+ % Run the handler in a separate thread. If SpawnOptions is an
216
+ % atom, it is interpreted as a thread pool name (see
217
+ % create_thread_pool/3). Otherwise the options are passed to
218
+ % http_spawn/2 and from there to thread_create/3. These options
219
+ % are typically used to set the stack limits.
220
+ %
221
+ % - time_limit(+Spec)
222
+ % One of =infinite=, =default= or a positive number (seconds). If
223
+ % =default=, the value from the setting =http:time_limit= is
224
+ % taken. The default of this setting is 300 (5 minutes). See
225
+ % setting/2.
226
+ %
227
+ % Note that http_handler/3 is normally invoked as a directive and
228
+ % processed using term-expansion. Using term-expansion ensures proper
229
+ % update through make/0 when the specification is modified.
230
+ %
231
+ % @error existence_error(http_location, Location)
232
+ % @error permission_error(http_method, Method, Location)
233
+ % @see http_reply_file/3 and http_redirect/3 are generic
234
+ % handlers to serve files and achieve redirects.
235
+
236
+ :- dynamic handler/4. % Path, Action, IsPrefix, Options
237
+ :- multifile handler/4.
238
+ :- dynamic generation/1.
239
+
240
+ :- meta_predicate
241
+ http_handler(+, :, +),
242
+ http_current_handler(?, :),
243
+ http_current_handler(?, :, ?),
244
+ http_request_expansion(3, +),
245
+ http_switch_protocol(2, +).
246
+
247
+ http_handler(Path, Pred, Options) :-
248
+ compile_handler(Path, Pred, Options, Clause),
249
+ next_generation,
250
+ assert(Clause).
251
+
252
+ :- multifile
253
+ system:term_expansion/2.
254
+
255
+ system:term_expansion((:- http_handler(Path, Pred, Options)), Clause) :-
256
+ \+ current_prolog_flag(xref, true),
257
+ prolog_load_context(module, M),
258
+ compile_handler(Path, M:Pred, Options, Clause),
259
+ next_generation.
260
+
261
+
262
+ %! http_delete_handler(+Spec) is det.
263
+ %
264
+ % Delete handler for Spec. Typically, this should only be used for
265
+ % handlers that are registered dynamically. Spec is one of:
266
+ %
267
+ % * id(Id)
268
+ % Delete a handler with the given id. The default id is the
269
+ % handler-predicate-name.
270
+ %
271
+ % * path(Path)
272
+ % Delete handler that serves the given path.
273
+
274
+ http_delete_handler(id(Id)) :-
275
+ !,
276
+ clause(handler(_Path, _:Pred, _, Options), true, Ref),
277
+ functor(Pred, DefID, _),
278
+ option(id(Id0), Options, DefID),
279
+ Id == Id0,
280
+ erase(Ref),
281
+ next_generation.
282
+ http_delete_handler(path(Path)) :-
283
+ !,
284
+ retractall(handler(Path, _Pred, _, _Options)),
285
+ next_generation.
286
+ http_delete_handler(Path) :-
287
+ http_delete_handler(path(Path)).
288
+
289
+
290
+ %! next_generation is det.
291
+ %! current_generation(-G) is det.
292
+ %
293
+ % Increment the generation count.
294
+
295
+ next_generation :-
296
+ retractall(id_location_cache(_,_,_,_)),
297
+ with_mutex(http_dispatch, next_generation_unlocked).
298
+
299
+ next_generation_unlocked :-
300
+ retract(generation(G0)),
301
+ !,
302
+ G is G0 + 1,
303
+ assert(generation(G)).
304
+ next_generation_unlocked :-
305
+ assert(generation(1)).
306
+
307
+ current_generation(G) :-
308
+ with_mutex(http_dispatch, generation(G)),
309
+ !.
310
+ current_generation(0).
311
+
312
+
313
+ %! compile_handler(+Path, :Pred, +Options, -Clause) is det.
314
+ %
315
+ % Compile a handler specification.
316
+
317
+ compile_handler(Path, Pred, Options0,
318
+ http_dispatch:handler(Path1, Pred, IsPrefix, Options)) :-
319
+ check_path(Path, Path1, PathOptions),
320
+ check_id(Options0),
321
+ ( memberchk(segment_pattern(_), PathOptions)
322
+ -> IsPrefix = true,
323
+ Options1 = Options0
324
+ ; select(prefix, Options0, Options1)
325
+ -> IsPrefix = true
326
+ ; IsPrefix = false,
327
+ Options1 = Options0
328
+ ),
329
+ partition(ground, Options1, Options2, QueryOptions),
330
+ Pred = M:_,
331
+ maplist(qualify_option(M), Options2, Options3),
332
+ combine_methods(Options3, Options4),
333
+ ( QueryOptions == []
334
+ -> append(PathOptions, Options4, Options)
335
+ ; append(PathOptions, ['$extract'(QueryOptions)|Options4], Options)
336
+ ).
337
+
338
+ qualify_option(M, condition(Pred), condition(M:Pred)) :-
339
+ Pred \= _:_, !.
340
+ qualify_option(_, Option, Option).
341
+
342
+ %! combine_methods(+OptionsIn, -Options) is det.
343
+ %
344
+ % Combine method(M) and methods(MList) options into a single
345
+ % methods(MList) option.
346
+
347
+ combine_methods(Options0, Options) :-
348
+ collect_methods(Options0, Options1, Methods),
349
+ ( Methods == []
350
+ -> Options = Options0
351
+ ; append(Methods, Flat),
352
+ sort(Flat, Unique),
353
+ ( memberchk('*', Unique)
354
+ -> Final = '*'
355
+ ; Final = Unique
356
+ ),
357
+ Options = [methods(Final)|Options1]
358
+ ).
359
+
360
+ collect_methods([], [], []).
361
+ collect_methods([method(M)|T0], T, [[M]|TM]) :-
362
+ !,
363
+ ( M == '*'
364
+ -> true
365
+ ; must_be_method(M)
366
+ ),
367
+ collect_methods(T0, T, TM).
368
+ collect_methods([methods(M)|T0], T, [M|TM]) :-
369
+ !,
370
+ must_be(list, M),
371
+ maplist(must_be_method, M),
372
+ collect_methods(T0, T, TM).
373
+ collect_methods([H|T0], [H|T], TM) :-
374
+ !,
375
+ collect_methods(T0, T, TM).
376
+
377
+ must_be_method(M) :-
378
+ must_be(atom, M),
379
+ ( method(M)
380
+ -> true
381
+ ; domain_error(http_method, M)
382
+ ).
383
+
384
+ method(get).
385
+ method(put).
386
+ method(head).
387
+ method(post).
388
+ method(delete).
389
+ method(patch).
390
+ method(options).
391
+ method(trace).
392
+
393
+
394
+ %! check_path(+PathSpecIn, -PathSpecOut, -Options) is det.
395
+ %
396
+ % Validate the given path specification. We want one of
397
+ %
398
+ % - AbsoluteLocation
399
+ % - Alias(Relative)
400
+ %
401
+ % Similar to absolute_file_name/3, Relative can be a term
402
+ % ``Component/Component/...``. Relative may be a `/` separated list of
403
+ % path segments, some of which may be variables. A variable patches
404
+ % any segment and its binding can be passed to the handler. If such a
405
+ % pattern is found Options is unified with
406
+ % `[segment_pattern(SegmentList)]`.
407
+ %
408
+ % @error domain_error, type_error
409
+ % @see http_absolute_location/3
410
+
411
+ check_path(Path, Path, []) :-
412
+ atom(Path),
413
+ !,
414
+ ( sub_atom(Path, 0, _, _, /)
415
+ -> true
416
+ ; domain_error(absolute_http_location, Path)
417
+ ).
418
+ check_path(Alias, AliasOut, Options) :-
419
+ compound(Alias),
420
+ Alias =.. [Name, Relative],
421
+ !,
422
+ local_path(Relative, Local, Options),
423
+ ( sub_atom(Local, 0, _, _, /)
424
+ -> domain_error(relative_location, Relative)
425
+ ; AliasOut =.. [Name, Local]
426
+ ).
427
+ check_path(PathSpec, _, _) :-
428
+ type_error(path_or_alias, PathSpec).
429
+
430
+ local_path(Atom, Atom, []) :-
431
+ atom(Atom),
432
+ !.
433
+ local_path(Path, Atom, Options) :-
434
+ phrase(path_to_list(Path), Components),
435
+ !,
436
+ ( maplist(atom, Components)
437
+ -> atomic_list_concat(Components, '/', Atom),
438
+ Options = []
439
+ ; append(Pre, [Var|Rest], Components),
440
+ var(Var)
441
+ -> append(Pre, [''], PreSep),
442
+ atomic_list_concat(PreSep, '/', Atom),
443
+ Options = [segment_pattern([Var|Rest])]
444
+ ).
445
+ local_path(Path, _, _) :-
446
+ ground(Path),
447
+ !,
448
+ type_error(relative_location, Path).
449
+ local_path(Path, _, _) :-
450
+ instantiation_error(Path).
451
+
452
+ path_to_list(Var) -->
453
+ { var(Var) },
454
+ !,
455
+ [Var].
456
+ path_to_list(A/B) -->
457
+ !,
458
+ path_to_list(A),
459
+ path_to_list(B).
460
+ path_to_list(Atom) -->
461
+ { atom(Atom) },
462
+ !,
463
+ [Atom].
464
+ path_to_list(Value) -->
465
+ { must_be(atom, Value) }.
466
+
467
+ check_id(Options) :-
468
+ memberchk(id(Id), Options),
469
+ !,
470
+ must_be(atom, Id).
471
+ check_id(_).
472
+
473
+
474
+ %! http_dispatch(Request) is det.
475
+ %
476
+ % Dispatch a Request using http_handler/3 registrations. It performs
477
+ % the following steps:
478
+ %
479
+ % 1. Find a matching handler based on the `path` member of Request.
480
+ % If multiple handlers match due to the `prefix` option or
481
+ % variables in path segments (see http_handler/3), the longest
482
+ % specification is used. If multiple specifications of equal
483
+ % length match the one with the highest priority is used.
484
+ % 2. Check that the handler matches the `method` member of the
485
+ % Request or throw permission_error(http_method, Method, Location)
486
+ % 3. Expand the request using expansion hooks registered by
487
+ % http_request_expansion/3. This may add fields to the request,
488
+ % such the authenticated user, parsed parameters, etc. The
489
+ % hooks may also throw exceptions, notably using http_redirect/3
490
+ % or by throwing `http_reply(Term, ExtraHeader, Context)`
491
+ % exceptions.
492
+ % 4. Extract possible fields from the Request using e.g.
493
+ % method(Method) as one of the options.
494
+ % 5. Call the registered _closure_, optionally spawning the
495
+ % request to a new thread or enforcing a time limit.
496
+
497
+ http_dispatch(Request) :-
498
+ memberchk(path(Path), Request),
499
+ find_handler(Path, Closure, Options),
500
+ supports_method(Request, Options),
501
+ expand_request(Request, Request1, Options),
502
+ extract_from_request(Request1, Options),
503
+ action(Closure, Request1, Options).
504
+
505
+ extract_from_request(Request, Options) :-
506
+ memberchk('$extract'(Fields), Options),
507
+ !,
508
+ extract_fields(Fields, Request).
509
+ extract_from_request(_, _).
510
+
511
+ extract_fields([], _).
512
+ extract_fields([H|T], Request) :-
513
+ memberchk(H, Request),
514
+ extract_fields(T, Request).
515
+
516
+
517
+ %! http_request_expansion(:Goal, +Rank:number)
518
+ %
519
+ % Register Goal for expanding the HTTP request handler. Goal is called
520
+ % as below. If Goal fail the request is passed to the next expansion
521
+ % unmodified.
522
+ %
523
+ % call(Goal, Request0, Request, Options)
524
+ %
525
+ % If multiple goals are registered they expand the request in a
526
+ % pipeline starting with the expansion hook with the lowest rank.
527
+ %
528
+ % Besides rewriting the request, for example by validating the user
529
+ % identity based on HTTP authentication or cookies and adding this to
530
+ % the request, the hook may raise HTTP exceptions to indicate a bad
531
+ % request, permission error, etc. See http_status_reply/4.
532
+ %
533
+ % Initially, auth_expansion/3 is registered with rank `100` to deal
534
+ % with the older http:authenticate/3 hook.
535
+
536
+ http_request_expansion(Goal, Rank) :-
537
+ throw(error(context_error(nodirective, http_request_expansion(Goal, Rank)), _)).
538
+
539
+ :- multifile
540
+ request_expansion/2.
541
+
542
+ system:term_expansion((:- http_request_expansion(Goal, Rank)),
543
+ http_dispatch:request_expansion(M:Callable, Rank)) :-
544
+ must_be(number, Rank),
545
+ prolog_load_context(module, M0),
546
+ strip_module(M0:Goal, M, Callable),
547
+ must_be(callable, Callable).
548
+
549
+ request_expanders(Closures) :-
550
+ findall(Rank-Closure, request_expansion(Closure, Rank), Pairs),
551
+ keysort(Pairs, Sorted),
552
+ pairs_values(Sorted, Closures).
553
+
554
+ %! expand_request(+Request0, -Request, +Options)
555
+ %
556
+ % Expand an HTTP request. Options is a list of combined options
557
+ % provided with the handler registration (see http_handler/3).
558
+
559
+ expand_request(Request0, Request, Options) :-
560
+ request_expanders(Closures),
561
+ expand_request(Closures, Request0, Request, Options).
562
+
563
+ expand_request([], Request, Request, _).
564
+ expand_request([H|T], Request0, Request, Options) :-
565
+ expand_request1(H, Request0, Request1, Options),
566
+ expand_request(T, Request1, Request, Options).
567
+
568
+ expand_request1(Closure, Request0, Request, Options) :-
569
+ call(Closure, Request0, Request, Options),
570
+ !.
571
+ expand_request1(_, Request, Request, _).
572
+
573
+
574
+ %! http_current_handler(+Location, :Closure) is semidet.
575
+ %! http_current_handler(-Location, :Closure) is nondet.
576
+ %
577
+ % True if Location is handled by Closure.
578
+
579
+ http_current_handler(Path, Closure) :-
580
+ atom(Path),
581
+ !,
582
+ path_tree(Tree),
583
+ find_handler(Tree, Path, Closure, _).
584
+ http_current_handler(Path, M:C) :-
585
+ handler(Spec, M:C, _, _),
586
+ http_absolute_location(Spec, Path, []).
587
+
588
+ %! http_current_handler(+Location, :Closure, -Options) is semidet.
589
+ %! http_current_handler(?Location, :Closure, ?Options) is nondet.
590
+ %
591
+ % Resolve the current handler and options to execute it.
592
+
593
+ http_current_handler(Path, Closure, Options) :-
594
+ atom(Path),
595
+ !,
596
+ path_tree(Tree),
597
+ find_handler(Tree, Path, Closure, Options).
598
+ http_current_handler(Path, M:C, Options) :-
599
+ handler(Spec, M:C, _, _),
600
+ http_absolute_location(Spec, Path, []),
601
+ path_tree(Tree),
602
+ find_handler(Tree, Path, _, Options).
603
+
604
+
605
+ %! http_location_by_id(+ID, -Location) is det.
606
+ %
607
+ % True when Location represents the HTTP path to which the handler
608
+ % with identifier ID is bound. Handler identifiers are deduced from
609
+ % the http_handler/3 declaration as follows:
610
+ %
611
+ % $ Explicit id :
612
+ % If a term id(ID) appears in the option list of the handler, ID
613
+ % it is used and takes preference over using the predicate.
614
+ % $ Using the handler predicate :
615
+ % ID matches a handler if the predicate name matches ID. The
616
+ % ID may have a module qualification, e.g., `Module:Pred`
617
+ %
618
+ % If the handler is declared with a pattern, e.g., root(user/User),
619
+ % the location to access a particular _user_ may be accessed using
620
+ % e.g., user('Bob'). The number of arguments to the compound term must
621
+ % match the number of variables in the path pattern.
622
+ %
623
+ % A plain atom ID can be used to find a handler with a pattern. The
624
+ % returned location is the path up to the first variable, e.g.,
625
+ % =|/user/|= in the example above.
626
+ %
627
+ % User code is advised to use http_link_to_id/3 which can also add
628
+ % query parameters to the URL. This predicate is a helper for
629
+ % http_link_to_id/3.
630
+ %
631
+ % @error existence_error(http_handler_id, Id).
632
+ % @see http_link_to_id/3 and the library(http/html_write) construct
633
+ % location_by_id(ID) or its abbreviation `#(ID)`
634
+
635
+ :- dynamic
636
+ id_location_cache/4. % Id, Argv, Location, Segments
637
+
638
+ http_location_by_id(ID, _) :-
639
+ \+ ground(ID),
640
+ !,
641
+ instantiation_error(ID).
642
+ http_location_by_id(M:ID, Location) :-
643
+ compound(ID),
644
+ !,
645
+ compound_name_arguments(ID, Name, Argv),
646
+ http_location_by_id(M:Name, Argv, Location).
647
+ http_location_by_id(M:ID, Location) :-
648
+ atom(ID),
649
+ must_be(atom, M),
650
+ !,
651
+ http_location_by_id(M:ID, -, Location).
652
+ http_location_by_id(ID, Location) :-
653
+ compound(ID),
654
+ !,
655
+ compound_name_arguments(ID, Name, Argv),
656
+ http_location_by_id(Name, Argv, Location).
657
+ http_location_by_id(ID, Location) :-
658
+ atom(ID),
659
+ !,
660
+ http_location_by_id(ID, -, Location).
661
+ http_location_by_id(ID, _) :-
662
+ type_error(location_id, ID).
663
+
664
+ http_location_by_id(ID, Argv, Location) :-
665
+ id_location_cache(ID, Argv, Segments, Path),
666
+ !,
667
+ add_segments(Path, Segments, Location).
668
+ http_location_by_id(ID, Argv, Location) :-
669
+ findall(t(Priority, ArgvP, Segments, Prefix),
670
+ location_by_id(ID, Argv, ArgvP, Segments, Prefix, Priority),
671
+ List),
672
+ sort(1, >=, List, Sorted),
673
+ ( Sorted = [t(_,ArgvP,Segments,Path)]
674
+ -> assert(id_location_cache(ID,ArgvP,Segments,Path)),
675
+ Argv = ArgvP
676
+ ; List == []
677
+ -> existence_error(http_handler_id, ID)
678
+ ; List = [t(P0,ArgvP,Segments,Path),t(P1,_,_,_)|_]
679
+ -> ( P0 =:= P1
680
+ -> print_message(warning,
681
+ http_dispatch(ambiguous_id(ID, Sorted, Path)))
682
+ ; true
683
+ ),
684
+ assert(id_location_cache(ID,Argv,Segments,Path)),
685
+ Argv = ArgvP
686
+ ),
687
+ add_segments(Path, Segments, Location).
688
+
689
+ add_segments(Path0, [], Path) :-
690
+ !,
691
+ Path = Path0.
692
+ add_segments(Path0, Segments, Path) :-
693
+ maplist(uri_encoded(path), Segments, Encoded),
694
+ atomic_list_concat(Encoded, '/', Rest),
695
+ atom_concat(Path0, Rest, Path).
696
+
697
+ location_by_id(ID, -, _, [], Location, Priority) :-
698
+ !,
699
+ location_by_id_raw(ID, L0, _Segments, Priority),
700
+ to_path(L0, Location).
701
+ location_by_id(ID, Argv, ArgvP, Segments, Location, Priority) :-
702
+ location_by_id_raw(ID, L0, Segments, Priority),
703
+ include(var, Segments, ArgvP),
704
+ same_length(Argv, ArgvP),
705
+ to_path(L0, Location).
706
+
707
+ to_path(prefix(Path0), Path) :- % old style prefix notation
708
+ !,
709
+ add_prefix(Path0, Path).
710
+ to_path(Path0, Path) :-
711
+ atomic(Path0), % old style notation
712
+ !,
713
+ add_prefix(Path0, Path).
714
+ to_path(Spec, Path) :- % new style notation
715
+ http_absolute_location(Spec, Path, []).
716
+
717
+ add_prefix(P0, P) :-
718
+ ( catch(setting(http:prefix, Prefix), _, fail),
719
+ Prefix \== ''
720
+ -> atom_concat(Prefix, P0, P)
721
+ ; P = P0
722
+ ).
723
+
724
+ location_by_id_raw(ID, Location, Pattern, Priority) :-
725
+ handler(Location, _, _, Options),
726
+ option(id(ID), Options),
727
+ option(priority(P0), Options, 0),
728
+ option(segment_pattern(Pattern), Options, []),
729
+ Priority is P0+1000. % id(ID) takes preference over predicate
730
+ location_by_id_raw(ID, Location, Pattern, Priority) :-
731
+ handler(Location, M:C, _, Options),
732
+ option(priority(Priority), Options, 0),
733
+ functor(C, PN, _),
734
+ ( ID = M:PN
735
+ -> true
736
+ ; ID = PN
737
+ ),
738
+ option(segment_pattern(Pattern), Options, []).
739
+
740
+ %! http_link_to_id(+HandleID, +Parameters, -HREF)
741
+ %
742
+ % HREF is a link on the local server to a handler with given ID,
743
+ % passing the given Parameters. This predicate is typically used
744
+ % to formulate a HREF that resolves to a handler implementing a
745
+ % particular predicate. The code below provides a typical example.
746
+ % The predicate user_details/1 returns a page with details about a
747
+ % user from a given id. This predicate is registered as a handler.
748
+ % The DCG user_link//1 renders a link to a user, displaying the
749
+ % name and calling user_details/1 when clicked. Note that the
750
+ % location (root(user_details)) is irrelevant in this equation and
751
+ % HTTP locations can thus be moved freely without breaking this
752
+ % code fragment.
753
+ %
754
+ % ```
755
+ % :- http_handler(root(user_details), user_details, []).
756
+ %
757
+ % user_details(Request) :-
758
+ % http_parameters(Request,
759
+ % [ user_id(ID)
760
+ % ]),
761
+ % ...
762
+ %
763
+ % user_link(ID) -->
764
+ % { user_name(ID, Name),
765
+ % http_link_to_id(user_details, [id(ID)], HREF)
766
+ % },
767
+ % html(a([class(user), href(HREF)], Name)).
768
+ % ```
769
+ %
770
+ % @arg HandleID is either an atom, possibly module qualified
771
+ % predicate or a compound term if the handler is defined using
772
+ % a pattern. See http_handler/3 and http_location_by_id/2.
773
+ %
774
+ % @arg Parameters is one of
775
+ %
776
+ % - path_postfix(File) to pass a single value as the last
777
+ % segment of the HTTP location (path). This way of
778
+ % passing a parameter is commonly used in REST APIs.
779
+ %
780
+ % New code should use a path pattern in the handler declaration
781
+ % and a term `HandleID(Arg, ...)`
782
+ %
783
+ % - A list of search parameters for a =GET= request.
784
+ %
785
+ % @see http_location_by_id/2 and http_handler/3 for defining and
786
+ % specifying handler IDs.
787
+
788
+ http_link_to_id(HandleID, path_postfix(File), HREF) :-
789
+ !,
790
+ http_location_by_id(HandleID, HandlerLocation),
791
+ uri_encoded(path, File, EncFile),
792
+ directory_file_path(HandlerLocation, EncFile, Location),
793
+ uri_data(path, Components, Location),
794
+ uri_components(HREF, Components).
795
+ http_link_to_id(HandleID, Parameters, HREF) :-
796
+ must_be(list, Parameters),
797
+ http_location_by_id(HandleID, Location),
798
+ ( Parameters == []
799
+ -> HREF = Location
800
+ ; uri_data(path, Components, Location),
801
+ uri_query_components(String, Parameters),
802
+ uri_data(search, Components, String),
803
+ uri_components(HREF, Components)
804
+ ).
805
+
806
+ %! http_reload_with_parameters(+Request, +Parameters, -HREF) is det.
807
+ %
808
+ % Create a request on the current handler with replaced search
809
+ % parameters.
810
+
811
+ http_reload_with_parameters(Request, NewParams, HREF) :-
812
+ memberchk(path(Path), Request),
813
+ ( memberchk(search(Params), Request)
814
+ -> true
815
+ ; Params = []
816
+ ),
817
+ merge_options(NewParams, Params, AllParams),
818
+ uri_query_components(Search, AllParams),
819
+ uri_data(path, Data, Path),
820
+ uri_data(search, Data, Search),
821
+ uri_components(HREF, Data).
822
+
823
+
824
+ % hook into html_write:attribute_value//1.
825
+
826
+ :- multifile
827
+ html_write:expand_attribute_value//1.
828
+
829
+ html_write:expand_attribute_value(location_by_id(ID)) -->
830
+ { http_location_by_id(ID, Location) },
831
+ html_write:html_quoted_attribute(Location).
832
+ html_write:expand_attribute_value(#(ID)) -->
833
+ { http_location_by_id(ID, Location) },
834
+ html_write:html_quoted_attribute(Location).
835
+
836
+
837
+ %! authentication(+Options, +Request, -Fields) is det.
838
+ %
839
+ % Verify authentication information. If authentication is
840
+ % requested through Options, demand it. The actual verification is
841
+ % done by the multifile predicate http:authenticate/3. The library
842
+ % http_authenticate.pl provides an implementation thereof.
843
+ %
844
+ % @error permission_error(access, http_location, Location)
845
+ % @deprecated This hook predates the extensible request
846
+ % expansion provided by http_request_expansion/2. New hooks should use
847
+ % http_request_expansion/2 instead of http:authenticate/3.
848
+
849
+ :- multifile
850
+ http:authenticate/3.
851
+
852
+ authentication([], _, []).
853
+ authentication([authentication(Type)|Options], Request, Fields) :-
854
+ !,
855
+ ( http:authenticate(Type, Request, XFields)
856
+ -> append(XFields, More, Fields),
857
+ authentication(Options, Request, More)
858
+ ; memberchk(path(Path), Request),
859
+ permission_error(access, http_location, Path)
860
+ ).
861
+ authentication([_|Options], Request, Fields) :-
862
+ authentication(Options, Request, Fields).
863
+
864
+ :- http_request_expansion(auth_expansion, 100).
865
+
866
+ %! auth_expansion(+Request0, -Request, +Options) is semidet.
867
+ %
868
+ % Connect the HTTP authentication infrastructure by means of
869
+ % http_request_expansion/2.
870
+ %
871
+ % @see http:authenticate/3, http_digest.pl and http_authenticate.pl
872
+
873
+ auth_expansion(Request0, Request, Options) :-
874
+ authentication(Options, Request0, Extra),
875
+ append(Extra, Request0, Request).
876
+
877
+ %! find_handler(+Path, -Action, -Options) is det.
878
+ %
879
+ % Find the handler to call from Path. Rules:
880
+ %
881
+ % * If there is a matching handler, use this.
882
+ % * If there are multiple prefix(Path) handlers, use the
883
+ % longest.
884
+ %
885
+ % If there is a handler for =|/dir/|= and the requested path is
886
+ % =|/dir|=, find_handler/3 throws a http_reply exception, causing
887
+ % the wrapper to generate a 301 (Moved Permanently) reply.
888
+ %
889
+ % @error existence_error(http_location, Location)
890
+ % @throw http_reply(moved(Dir))
891
+ % @tbd Introduce automatic redirection to indexes here?
892
+
893
+ find_handler(Path, Action, Options) :-
894
+ path_tree(Tree),
895
+ ( find_handler(Tree, Path, Action, Options),
896
+ eval_condition(Options)
897
+ -> true
898
+ ; \+ sub_atom(Path, _, _, 0, /),
899
+ atom_concat(Path, /, Dir),
900
+ find_handler(Tree, Dir, Action, Options),
901
+ \+ memberchk(segment_pattern(_), Options) % Variables in pattern
902
+ -> throw(http_reply(moved(Dir)))
903
+ ; throw(error(existence_error(http_location, Path), _))
904
+ ).
905
+
906
+
907
+ find_handler([node(prefix(Prefix), PAction, POptions, Children)|_],
908
+ Path, Action, Options) :-
909
+ sub_atom(Path, 0, _, After, Prefix),
910
+ !,
911
+ ( option(hide_children(false), POptions, false),
912
+ find_handler(Children, Path, Action, Options)
913
+ -> true
914
+ ; member(segment_pattern(Pattern, PatAction, PatOptions), POptions),
915
+ copy_term(t(Pattern,PatAction,PatOptions), t(Pattern2,Action,Options)),
916
+ match_segments(After, Path, Pattern2)
917
+ -> true
918
+ ; PAction \== nop
919
+ -> Action = PAction,
920
+ path_info(After, Path, POptions, Options)
921
+ ).
922
+ find_handler([node(Path, Action, Options, _)|_], Path, Action, Options) :- !.
923
+ find_handler([_|Tree], Path, Action, Options) :-
924
+ find_handler(Tree, Path, Action, Options).
925
+
926
+ path_info(0, _, Options,
927
+ [prefix(true)|Options]) :- !.
928
+ path_info(After, Path, Options,
929
+ [path_info(PathInfo),prefix(true)|Options]) :-
930
+ sub_atom(Path, _, After, 0, PathInfo).
931
+
932
+ match_segments(After, Path, [Var]) :-
933
+ !,
934
+ sub_atom(Path, _, After, 0, Var).
935
+ match_segments(After, Path, Pattern) :-
936
+ sub_atom(Path, _, After, 0, PathInfo),
937
+ split_string(PathInfo, "/", "", Segments),
938
+ match_segment_pattern(Pattern, Segments).
939
+
940
+ match_segment_pattern([], []).
941
+ match_segment_pattern([Var], Segments) :-
942
+ !,
943
+ atomic_list_concat(Segments, '/', Var).
944
+ match_segment_pattern([H0|T0], [H|T]) :-
945
+ atom_string(H0, H),
946
+ match_segment_pattern(T0, T).
947
+
948
+
949
+ eval_condition(Options) :-
950
+ ( memberchk(condition(Cond), Options)
951
+ -> catch(Cond, E, (print_message(warning, E), fail))
952
+ ; true
953
+ ).
954
+
955
+
956
+ %! supports_method(+Request, +Options) is det.
957
+ %
958
+ % Verify that the asked http method is supported by the handler.
959
+ % If not, raise an error that will be mapped to a 405 page by the
960
+ % http wrapper.
961
+ %
962
+ % @error permission_error(http_method, Method, Location).
963
+
964
+ supports_method(Request, Options) :-
965
+ ( option(methods(Methods), Options)
966
+ -> ( Methods == '*'
967
+ -> true
968
+ ; memberchk(method(Method), Request),
969
+ memberchk(Method, Methods)
970
+ )
971
+ ; true
972
+ ),
973
+ !.
974
+ supports_method(Request, _Options) :-
975
+ memberchk(path(Location), Request),
976
+ memberchk(method(Method), Request),
977
+ permission_error(http_method, Method, Location).
978
+
979
+
980
+ %! action(+Action, +Request, +Options) is det.
981
+ %
982
+ % Execute the action found. Here we take care of the options
983
+ % =time_limit=, =chunked= and =spawn=.
984
+ %
985
+ % @error goal_failed(Goal)
986
+
987
+ action(Action, Request, Options) :-
988
+ memberchk(chunked, Options),
989
+ !,
990
+ format('Transfer-encoding: chunked~n'),
991
+ spawn_action(Action, Request, Options).
992
+ action(Action, Request, Options) :-
993
+ spawn_action(Action, Request, Options).
994
+
995
+ :- if(current_predicate(http_spawn/2)).
996
+ spawn_action(Action, Request, Options) :-
997
+ option(spawn(Spawn), Options),
998
+ !,
999
+ spawn_options(Spawn, SpawnOption),
1000
+ http_spawn(time_limit_action(Action, Request, Options), SpawnOption).
1001
+ :- endif.
1002
+ spawn_action(Action, Request, Options) :-
1003
+ time_limit_action(Action, Request, Options).
1004
+
1005
+ spawn_options([], []) :- !.
1006
+ spawn_options(Pool, Options) :-
1007
+ atom(Pool),
1008
+ !,
1009
+ Options = [pool(Pool)].
1010
+ spawn_options(List, List).
1011
+
1012
+ :- if(current_predicate(call_with_time_limit/2)).
1013
+ time_limit_action(Action, Request, Options) :-
1014
+ ( option(time_limit(TimeLimit), Options),
1015
+ TimeLimit \== default
1016
+ -> true
1017
+ ; setting(http:time_limit, TimeLimit)
1018
+ ),
1019
+ number(TimeLimit),
1020
+ TimeLimit > 0,
1021
+ !,
1022
+ call_with_time_limit(TimeLimit, call_action(Action, Request, Options)).
1023
+ :- endif.
1024
+ time_limit_action(Action, Request, Options) :-
1025
+ call_action(Action, Request, Options).
1026
+
1027
+
1028
+ %! call_action(+Action, +Request, +Options)
1029
+ %
1030
+ % @tbd reply_file is normal call?
1031
+
1032
+ call_action(reply_file(File, FileOptions), Request, _Options) :-
1033
+ !,
1034
+ http_reply_file(File, FileOptions, Request).
1035
+ call_action(Pred, Request, Options) :-
1036
+ memberchk(path_info(PathInfo), Options),
1037
+ !,
1038
+ call_action(Pred, [path_info(PathInfo)|Request]).
1039
+ call_action(Pred, Request, _Options) :-
1040
+ call_action(Pred, Request).
1041
+
1042
+ call_action(Pred, Request) :-
1043
+ ( call(Pred, Request)
1044
+ -> true
1045
+ ; extend(Pred, [Request], Goal),
1046
+ throw(error(goal_failed(Goal), _))
1047
+ ).
1048
+
1049
+ extend(Var, _, Var) :-
1050
+ var(Var),
1051
+ !.
1052
+ extend(M:G0, Extra, M:G) :-
1053
+ extend(G0, Extra, G).
1054
+ extend(G0, Extra, G) :-
1055
+ G0 =.. List,
1056
+ append(List, Extra, List2),
1057
+ G =.. List2.
1058
+
1059
+ %! http_reply_file(+FileSpec, +Options, +Request) is det.
1060
+ %
1061
+ % Options is a list of
1062
+ %
1063
+ % * cache(+Boolean)
1064
+ % If =true= (default), handle If-modified-since and send
1065
+ % modification time.
1066
+ %
1067
+ % * mime_type(+Type)
1068
+ % Overrule mime-type guessing from the filename as
1069
+ % provided by file_mime_type/2.
1070
+ %
1071
+ % * static_gzip(+Boolean)
1072
+ % If `true` (default `false`) and, in addition to the plain
1073
+ % file, there is a ``.gz`` file that is not older than the
1074
+ % plain file and the client accepts =gzip= encoding, send
1075
+ % the compressed file with ``Transfer-encoding: gzip``.
1076
+ %
1077
+ % * cached_gzip(+Boolean)
1078
+ % If `true` (default `false`) the system maintains cached
1079
+ % gzipped files in a directory accessible using the file
1080
+ % search path `http_gzip_cache` and serves these similar
1081
+ % to the `static_gzip(true)` option. If the gzip file
1082
+ % does not exist or is older than the input the file is
1083
+ % recreated.
1084
+ %
1085
+ % * unsafe(+Boolean)
1086
+ % If =false= (default), validate that FileSpec does not
1087
+ % contain references to parent directories. E.g.,
1088
+ % specifications such as =|www('../../etc/passwd')|= are
1089
+ % not allowed.
1090
+ %
1091
+ % * headers(+List)
1092
+ % Provides additional reply-header fields, encoded as a
1093
+ % list of _|Field(Value)|_.
1094
+ %
1095
+ % If caching is not disabled, it processes the request headers
1096
+ % =|If-modified-since|= and =Range=.
1097
+ %
1098
+ % @throws http_reply(not_modified)
1099
+ % @throws http_reply(file(MimeType, Path))
1100
+
1101
+ http_reply_file(File, Options, Request) :-
1102
+ http_safe_file(File, Options),
1103
+ absolute_file_name(File, Path,
1104
+ [ access(read)
1105
+ ]),
1106
+ ( option(cache(true), Options, true)
1107
+ -> ( memberchk(if_modified_since(Since), Request),
1108
+ time_file(Path, Time),
1109
+ catch(http_timestamp(Time2, Since), _, fail),
1110
+ abs(Time-Time2) < 1 % allow for loss of second fraction
1111
+ -> throw(http_reply(not_modified))
1112
+ ; true
1113
+ ),
1114
+ ( memberchk(range(Range), Request)
1115
+ -> Reply = file(Type, Path, Range)
1116
+ ; option(static_gzip(true), Options),
1117
+ accepts_encoding(Request, gzip),
1118
+ file_name_extension(Path, gz, PathGZ),
1119
+ access_file(PathGZ, read),
1120
+ time_file(PathGZ, TimeGZ),
1121
+ time_file(Path, Time),
1122
+ TimeGZ >= Time
1123
+ -> Reply = gzip_file(Type, PathGZ)
1124
+ ; option(cached_gzip(true), Options),
1125
+ accepts_encoding(Request, gzip),
1126
+ gzip_cached(Path, PathGZ)
1127
+ -> Reply = gzip_file(Type, PathGZ)
1128
+ ; Reply = file(Type, Path)
1129
+ )
1130
+ ; Reply = tmp_file(Type, Path)
1131
+ ),
1132
+ ( option(mime_type(MediaType), Options)
1133
+ -> file_content_type(Path, MediaType, Type)
1134
+ ; file_content_type(Path, Type)
1135
+ -> true
1136
+ ; Type = text/plain % fallback type
1137
+ ),
1138
+ option(headers(Headers), Options, []),
1139
+ throw(http_reply(Reply, Headers)).
1140
+
1141
+ accepts_encoding(Request, Enc) :-
1142
+ memberchk(accept_encoding(Accept), Request),
1143
+ split_string(Accept, ",", " ", Parts),
1144
+ member(Part, Parts),
1145
+ split_string(Part, ";", " ", [EncS|_]),
1146
+ atom_string(Enc, EncS).
1147
+
1148
+ gzip_cached(Path, PathGZ) :-
1149
+ with_mutex(http_reply_file, gzip_cached_sync(Path, PathGZ)).
1150
+
1151
+ gzip_cached_sync(Path, PathGZ) :-
1152
+ time_file(Path, Time),
1153
+ variant_sha1(Path, SHA1),
1154
+ ( absolute_file_name(http_gzip_cache(SHA1),
1155
+ PathGZ,
1156
+ [ access(read),
1157
+ file_errors(fail)
1158
+ ]),
1159
+ time_file(PathGZ, TimeGZ),
1160
+ TimeGZ >= Time
1161
+ -> true
1162
+ ; absolute_file_name(http_gzip_cache(SHA1),
1163
+ PathGZ,
1164
+ [ access(write),
1165
+ file_errors(fail)
1166
+ ])
1167
+ -> setup_call_cleanup(
1168
+ gzopen(PathGZ, write, Out, [type(binary)]),
1169
+ setup_call_cleanup(
1170
+ open(Path, read, In, [type(binary)]),
1171
+ copy_stream_data(In, Out),
1172
+ close(In)),
1173
+ close(Out))
1174
+ ).
1175
+
1176
+ %! http_safe_file(+FileSpec, +Options) is det.
1177
+ %
1178
+ % True if FileSpec is considered _safe_. If it is an atom, it
1179
+ % cannot be absolute and cannot have references to parent
1180
+ % directories. If it is of the form alias(Sub), than Sub cannot
1181
+ % have references to parent directories.
1182
+ %
1183
+ % @error instantiation_error
1184
+ % @error permission_error(read, file, FileSpec)
1185
+
1186
+ http_safe_file(File, _) :-
1187
+ var(File),
1188
+ !,
1189
+ instantiation_error(File).
1190
+ http_safe_file(_, Options) :-
1191
+ option(unsafe(true), Options, false),
1192
+ !.
1193
+ http_safe_file(File, _) :-
1194
+ http_safe_file(File).
1195
+
1196
+ http_safe_file(File) :-
1197
+ compound(File),
1198
+ functor(File, _, 1),
1199
+ !,
1200
+ arg(1, File, Name),
1201
+ safe_name(Name, File).
1202
+ http_safe_file(Name) :-
1203
+ ( is_absolute_file_name(Name)
1204
+ -> permission_error(read, file, Name)
1205
+ ; true
1206
+ ),
1207
+ safe_name(Name, Name).
1208
+
1209
+ safe_name(Name, _) :-
1210
+ must_be(atom, Name),
1211
+ prolog_to_os_filename(FileName, Name),
1212
+ \+ unsafe_name(FileName),
1213
+ !.
1214
+ safe_name(_, Spec) :-
1215
+ permission_error(read, file, Spec).
1216
+
1217
+ unsafe_name(Name) :- Name == '..'.
1218
+ unsafe_name(Name) :- sub_atom(Name, 0, _, _, '../').
1219
+ unsafe_name(Name) :- sub_atom(Name, _, _, _, '/../').
1220
+ unsafe_name(Name) :- sub_atom(Name, _, _, 0, '/..').
1221
+
1222
+
1223
+ %! http_redirect(+How, +To, +Request) is det.
1224
+ %
1225
+ % Redirect to a new location. The argument order, using the
1226
+ % Request as last argument, allows for calling this directly from
1227
+ % the handler declaration:
1228
+ %
1229
+ % ```
1230
+ % :- http_handler(root(.),
1231
+ % http_redirect(moved, myapp('index.html')),
1232
+ % []).
1233
+ % ```
1234
+ %
1235
+ % @param How is one of `moved`, `moved_temporary` or `see_other`
1236
+ % @param To is an atom, a aliased path as defined by
1237
+ % http_absolute_location/3. or a term location_by_id(Id) or its
1238
+ % abbreviations `#(Id)` or `#(Id)+Parameters`. If To is not absolute,
1239
+ % it is resolved relative to the current location.
1240
+
1241
+ http_redirect(How, To, Request) :-
1242
+ must_be(oneof([moved, moved_temporary, see_other]), How),
1243
+ must_be(ground, To),
1244
+ ( id_location(To, URL)
1245
+ -> true
1246
+ ; memberchk(path(Base), Request),
1247
+ http_absolute_location(To, URL, [relative_to(Base)])
1248
+ ),
1249
+ Term =.. [How,URL],
1250
+ throw(http_reply(Term)).
1251
+
1252
+ id_location(location_by_id(Id), URL) :-
1253
+ http_location_by_id(Id, URL).
1254
+ id_location(#(Id), URL) :-
1255
+ http_location_by_id(Id, URL).
1256
+ id_location(#(Id)+Parameters, URL) :-
1257
+ http_link_to_id(Id, Parameters, URL).
1258
+
1259
+
1260
+ %! http_404(+Options, +Request) is det.
1261
+ %
1262
+ % Reply using an "HTTP 404 not found" page. This handler is
1263
+ % intended as fallback handler for _prefix_ handlers. Options
1264
+ % processed are:
1265
+ %
1266
+ % * index(Location)
1267
+ % If there is no path-info, redirect the request to
1268
+ % Location using http_redirect/3.
1269
+ %
1270
+ % @error http_reply(not_found(Path))
1271
+
1272
+ http_404(Options, Request) :-
1273
+ option(index(Index), Options),
1274
+ \+ ( option(path_info(PathInfo), Request),
1275
+ PathInfo \== ''
1276
+ ),
1277
+ !,
1278
+ http_redirect(moved, Index, Request).
1279
+ http_404(_Options, Request) :-
1280
+ option(path(Path), Request),
1281
+ !,
1282
+ throw(http_reply(not_found(Path))).
1283
+ http_404(_Options, Request) :-
1284
+ domain_error(http_request, Request).
1285
+
1286
+
1287
+ %! http_switch_protocol(:Goal, +Options)
1288
+ %
1289
+ % Send an =|"HTTP 101 Switching Protocols"|= reply. After sending
1290
+ % the reply, the HTTP library calls call(Goal, InStream,
1291
+ % OutStream), where InStream and OutStream are the raw streams to
1292
+ % the HTTP client. This allows the communication to continue using
1293
+ % an an alternative protocol.
1294
+ %
1295
+ % If Goal fails or throws an exception, the streams are closed by
1296
+ % the server. Otherwise Goal is responsible for closing the
1297
+ % streams. Note that Goal runs in the HTTP handler thread.
1298
+ % Typically, the handler should be registered using the =spawn=
1299
+ % option if http_handler/3 or Goal must call thread_create/3 to
1300
+ % allow the HTTP worker to return to the worker pool.
1301
+ %
1302
+ % The streams use binary (octet) encoding and have their I/O
1303
+ % timeout set to the server timeout (default 60 seconds). The
1304
+ % predicate set_stream/2 can be used to change the encoding,
1305
+ % change or cancel the timeout.
1306
+ %
1307
+ % This predicate interacts with the server library by throwing an
1308
+ % exception.
1309
+ %
1310
+ % The following options are supported:
1311
+ %
1312
+ % - header(+Headers)
1313
+ % Backward compatible. Use headers(+Headers).
1314
+ % - headers(+Headers)
1315
+ % Additional headers send with the reply. Each header takes the
1316
+ % form Name(Value).
1317
+
1318
+ % @throws http_reply(switch_protocol(Goal, Options))
1319
+
1320
+ http_switch_protocol(Goal, Options) :-
1321
+ throw(http_reply(switching_protocols(Goal, Options))).
1322
+
1323
+
1324
+ /*******************************
1325
+ * PATH COMPILATION *
1326
+ *******************************/
1327
+
1328
+ %! path_tree(-Tree) is det.
1329
+ %
1330
+ % Compile paths into a tree. The tree is multi-rooted and
1331
+ % represented as a list of nodes, where each node has the form:
1332
+ %
1333
+ % node(PathOrPrefix, Action, Options, Children)
1334
+ %
1335
+ % The tree is a potentially complicated structure. It is cached in
1336
+ % a global variable. Note that this cache is per-thread, so each
1337
+ % worker thread holds a copy of the tree. If handler facts are
1338
+ % changed the _generation_ is incremented using next_generation/0
1339
+ % and each worker thread will re-compute the tree on the next
1340
+ % occasion.
1341
+
1342
+ path_tree(Tree) :-
1343
+ current_generation(G),
1344
+ nb_current(http_dispatch_tree, G-Tree),
1345
+ !. % Avoid existence error
1346
+ path_tree(Tree) :-
1347
+ path_tree_nocache(Tree),
1348
+ current_generation(G),
1349
+ nb_setval(http_dispatch_tree, G-Tree).
1350
+
1351
+ path_tree_nocache(Tree) :-
1352
+ findall(Prefix, prefix_handler(Prefix, _, _, _), Prefixes0),
1353
+ sort(Prefixes0, Prefixes),
1354
+ prefix_tree(Prefixes, [], PTree),
1355
+ prefix_options(PTree, [], OPTree),
1356
+ add_paths_tree(OPTree, Tree).
1357
+
1358
+ prefix_handler(Prefix, Action, Options, Priority-PLen) :-
1359
+ handler(Spec, Action, true, Options),
1360
+ ( memberchk(priority(Priority), Options)
1361
+ -> true
1362
+ ; Priority = 0
1363
+ ),
1364
+ ( memberchk(segment_pattern(Pattern), Options)
1365
+ -> length(Pattern, PLen)
1366
+ ; PLen = 0
1367
+ ),
1368
+ Error = error(existence_error(http_alias,_),_),
1369
+ catch(http_absolute_location(Spec, Prefix, []), Error,
1370
+ ( print_message(warning, Error),
1371
+ fail
1372
+ )).
1373
+
1374
+ %! prefix_tree(PrefixList, +Tree0, -Tree)
1375
+ %
1376
+ % @param Tree list(Prefix-list(Children))
1377
+
1378
+ prefix_tree([], Tree, Tree).
1379
+ prefix_tree([H|T], Tree0, Tree) :-
1380
+ insert_prefix(H, Tree0, Tree1),
1381
+ prefix_tree(T, Tree1, Tree).
1382
+
1383
+ insert_prefix(Prefix, Tree0, Tree) :-
1384
+ select(P-T, Tree0, Tree1),
1385
+ sub_atom(Prefix, 0, _, _, P),
1386
+ !,
1387
+ insert_prefix(Prefix, T, T1),
1388
+ Tree = [P-T1|Tree1].
1389
+ insert_prefix(Prefix, Tree, [Prefix-[]|Tree]).
1390
+
1391
+
1392
+ %! prefix_options(+PrefixTree, +DefOptions, -OptionTree)
1393
+ %
1394
+ % Generate the option-tree for all prefix declarations.
1395
+ %
1396
+ % @tbd What to do if there are more?
1397
+
1398
+ prefix_options([], _, []).
1399
+ prefix_options([Prefix-C|T0], DefOptions,
1400
+ [node(prefix(Prefix), Action, PrefixOptions, Children)|T]) :-
1401
+ findall(h(A,O,P), prefix_handler(Prefix,A,O,P), Handlers),
1402
+ sort(3, >=, Handlers, Handlers1),
1403
+ Handlers1 = [h(_,_,P0)|_],
1404
+ same_priority_handlers(Handlers1, P0, Same),
1405
+ option_patterns(Same, SegmentPatterns, Action),
1406
+ last(Same, h(_, Options0, _-_)),
1407
+ merge_options(Options0, DefOptions, Options),
1408
+ append(SegmentPatterns, Options, PrefixOptions),
1409
+ exclude(no_inherit, Options, InheritOpts),
1410
+ prefix_options(C, InheritOpts, Children),
1411
+ prefix_options(T0, DefOptions, T).
1412
+
1413
+ no_inherit(id(_)).
1414
+ no_inherit('$extract'(_)).
1415
+
1416
+ same_priority_handlers([H|T0], P, [H|T]) :-
1417
+ H = h(_,_,P0-_),
1418
+ P = P0-_,
1419
+ !,
1420
+ same_priority_handlers(T0, P, T).
1421
+ same_priority_handlers(_, _, []).
1422
+
1423
+ option_patterns([], [], nop).
1424
+ option_patterns([h(A,_,_-0)|_], [], A) :-
1425
+ !.
1426
+ option_patterns([h(A,O,_)|T0], [segment_pattern(P,A,O)|T], AF) :-
1427
+ memberchk(segment_pattern(P), O),
1428
+ option_patterns(T0, T, AF).
1429
+
1430
+
1431
+ %! add_paths_tree(+OPTree, -Tree) is det.
1432
+ %
1433
+ % Add the plain paths.
1434
+
1435
+ add_paths_tree(OPTree, Tree) :-
1436
+ findall(path(Path, Action, Options),
1437
+ plain_path(Path, Action, Options),
1438
+ Triples),
1439
+ add_paths_tree(Triples, OPTree, Tree).
1440
+
1441
+ add_paths_tree([], Tree, Tree).
1442
+ add_paths_tree([path(Path, Action, Options)|T], Tree0, Tree) :-
1443
+ add_path_tree(Path, Action, Options, [], Tree0, Tree1),
1444
+ add_paths_tree(T, Tree1, Tree).
1445
+
1446
+
1447
+ %! plain_path(-Path, -Action, -Options) is nondet.
1448
+ %
1449
+ % True if {Path,Action,Options} is registered and Path is a plain
1450
+ % (i.e. not _prefix_) location.
1451
+
1452
+ plain_path(Path, Action, Options) :-
1453
+ handler(Spec, Action, false, Options),
1454
+ catch(http_absolute_location(Spec, Path, []), E,
1455
+ (print_message(error, E), fail)).
1456
+
1457
+
1458
+ %! add_path_tree(+Path, +Action, +Options, +Tree0, -Tree) is det.
1459
+ %
1460
+ % Add a path to a tree. If a handler for the same path is already
1461
+ % defined, the one with the highest priority or the latest takes
1462
+ % precedence.
1463
+
1464
+ add_path_tree(Path, Action, Options0, DefOptions, [],
1465
+ [node(Path, Action, Options, [])]) :-
1466
+ !,
1467
+ merge_options(Options0, DefOptions, Options).
1468
+ add_path_tree(Path, Action, Options, _,
1469
+ [node(prefix(Prefix), PA, DefOptions, Children0)|RestTree],
1470
+ [node(prefix(Prefix), PA, DefOptions, Children)|RestTree]) :-
1471
+ sub_atom(Path, 0, _, _, Prefix),
1472
+ !,
1473
+ delete(DefOptions, id(_), InheritOpts),
1474
+ add_path_tree(Path, Action, Options, InheritOpts, Children0, Children).
1475
+ add_path_tree(Path, Action, Options1, DefOptions, [H0|T], [H|T]) :-
1476
+ H0 = node(Path, _, Options2, _),
1477
+ option(priority(P1), Options1, 0),
1478
+ option(priority(P2), Options2, 0),
1479
+ P1 >= P2,
1480
+ !,
1481
+ merge_options(Options1, DefOptions, Options),
1482
+ H = node(Path, Action, Options, []).
1483
+ add_path_tree(Path, Action, Options, DefOptions, [H|T0], [H|T]) :-
1484
+ add_path_tree(Path, Action, Options, DefOptions, T0, T).
1485
+
1486
+
1487
+ /*******************************
1488
+ * MESSAGES *
1489
+ *******************************/
1490
+
1491
+ :- multifile
1492
+ prolog:message/3.
1493
+
1494
+ prolog:message(http_dispatch(ambiguous_id(ID, _List, Selected))) -->
1495
+ [ 'HTTP dispatch: ambiguous handler ID ~q (selected ~q)'-[ID, Selected]
1496
+ ].
1497
+
1498
+
1499
+ /*******************************
1500
+ * XREF *
1501
+ *******************************/
1502
+
1503
+ :- multifile
1504
+ prolog:meta_goal/2.
1505
+ :- dynamic
1506
+ prolog:meta_goal/2.
1507
+
1508
+ prolog:meta_goal(http_handler(_, G, _), [G+1]).
1509
+ prolog:meta_goal(http_current_handler(_, G), [G+1]).
1510
+
1511
+
1512
+ /*******************************
1513
+ * EDIT *
1514
+ *******************************/
1515
+
1516
+ % Allow edit(Location) to edit the implementation for an HTTP location.
1517
+
1518
+ :- multifile
1519
+ prolog_edit:locate/3.
1520
+
1521
+ prolog_edit:locate(Path, Spec, Location) :-
1522
+ atom(Path),
1523
+ sub_atom(Path, 0, _, _, /),
1524
+ Pred = _M:_H,
1525
+ catch(http_current_handler(Path, Pred), _, fail),
1526
+ closure_name_arity(Pred, 1, PI),
1527
+ prolog_edit:locate(PI, Spec, Location).
1528
+
1529
+ closure_name_arity(M:Term, Extra, M:Name/Arity) :-
1530
+ !,
1531
+ callable(Term),
1532
+ functor(Term, Name, Arity0),
1533
+ Arity is Arity0 + Extra.
1534
+ closure_name_arity(Term, Extra, Name/Arity) :-
1535
+ callable(Term),
1536
+ functor(Term, Name, Arity0),
1537
+ Arity is Arity0 + Extra.
1538
+
1539
+
1540
+ /*******************************
1541
+ * CACHE CLEANUP *
1542
+ *******************************/
1543
+
1544
+ :- listen(settings(changed(http:prefix, _, _)),
1545
+ next_generation).
1546
+
1547
+ :- multifile
1548
+ prolog:message_action/2.
1549
+
1550
+ prolog:message_action(make(done(Reload)), _Level) :-
1551
+ Reload \== [],
1552
+ next_generation.