| 1 | /* |
| 2 | * Copyright (c) 2022 Joris Vink <[email protected]> |
| 3 | * |
| 4 | * Permission to use, copy, modify, and distribute this software for any |
| 5 | * purpose with or without fee is hereby granted, provided that the above |
| 6 | * copyright notice and this permission notice appear in all copies. |
| 7 | * |
| 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 15 | */ |
| 16 | |
| 17 | #include <sys/types.h> |
| 18 | #include <sys/stat.h> |
| 19 | |
| 20 | #include <dlfcn.h> |
| 21 | |
| 22 | #include "kore.h"cxx . . $ `<file>/include/kore/kore.h`/ |
| 23 | #include "http.h"cxx . . $ `<file>/include/kore/http.h`/ |
| 24 | |
| 25 | struct kore_routecxx . . $ kore_route# * |
| 26 | kore_route_createcxx . . $ kore_route_create(8c09fc083371aa6a).(struct kore_domaincxx . . $ kore_domain# *dom, const char *path, int type) |
| 27 | { |
| 28 | struct kore_routecxx . . $ kore_route# *rt; |
| 29 | |
| 30 | rt = kore_calloccxx . . $ kore_calloc(63a7bf2941bfcd7a).(1, sizeof(*rt)); |
| 31 | rt->domcxx . . $ kore_route#dom. = dom; |
| 32 | rt->typecxx . . $ kore_route#type. = type; |
| 33 | rt->pathcxx . . $ kore_route#path. = kore_strdupcxx . . $ kore_strdup(a8b8e32347182212).(path); |
| 34 | rt->methodscxx . . $ kore_route#methods. = HTTP_METHOD_ALLcxx . . $ `include/kore/http.h:237:9`!; |
| 35 | |
| 36 | TAILQ_INITcxx . . $ `<external>/2546723208511035544/queue.h:387:9`!(&rt->params); |
| 37 | |
| 38 | if (rt->typecxx . . $ kore_route#type. == HANDLER_TYPE_DYNAMICcxx . . $ `include/kore/kore.h:407:9`!) { |
| 39 | if (regcompcxx . . $ regcomp(aa05013d3db24b3e).(&rt->rctxcxx . . $ kore_route#rctx., rt->pathcxx . . $ kore_route#path., REG_EXTENDEDcxx . . $ `<external>/18190116149591951179/regex.h:310:9`! | REG_NOSUBcxx . . $ `<external>/18190116149591951179/regex.h:323:9`!)) { |
| 40 | kore_route_freecxx . . $ kore_route_free(7f61c70a4656f6ea).(rt); |
| 41 | return (NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | TAILQ_INSERT_TAILcxx . . $ `<external>/2546723208511035544/queue.h:402:9`!(&dom->routes, rt, list); |
| 46 | |
| 47 | return (rt); |
| 48 | } |
| 49 | |
| 50 | void |
| 51 | kore_route_freecxx . . $ kore_route_free(7f61c70a4656f6ea).(struct kore_routecxx . . $ kore_route# *rt) |
| 52 | { |
| 53 | struct kore_route_paramscxx . . $ kore_route_params# *param; |
| 54 | |
| 55 | if (rt == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) |
| 56 | return; |
| 57 | |
| 58 | kore_freecxx . . $ kore_free(5d22bdacc48458e8).(rt->funccxx . . $ kore_route#func.); |
| 59 | kore_freecxx . . $ kore_free(5d22bdacc48458e8).(rt->pathcxx . . $ kore_route#path.); |
| 60 | |
| 61 | if (rt->typecxx . . $ kore_route#type. == HANDLER_TYPE_DYNAMICcxx . . $ `include/kore/kore.h:407:9`!) |
| 62 | regfreecxx . . $ regfree(b1814a8fd6af504b).(&rt->rctxcxx . . $ kore_route#rctx.); |
| 63 | |
| 64 | /* Drop all validators associated with this handler */ |
| 65 | while ((param = TAILQ_FIRSTcxx . . $ `<external>/2546723208511035544/queue.h:458:9`!(&rt->params)) != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) { |
| 66 | TAILQ_REMOVEcxx . . $ `<external>/2546723208511035544/queue.h:426:9`!(&rt->params, param, list); |
| 67 | kore_freecxx . . $ kore_free(5d22bdacc48458e8).(param->namecxx . . $ kore_route_params#name.); |
| 68 | kore_freecxx . . $ kore_free(5d22bdacc48458e8).(param); |
| 69 | } |
| 70 | |
| 71 | kore_freecxx . . $ kore_free(5d22bdacc48458e8).(rt); |
| 72 | } |
| 73 | |
| 74 | void |
| 75 | kore_route_callbackcxx . . $ kore_route_callback(20c3529eac994d0e).(struct kore_routecxx . . $ kore_route# *rt, const char *func) |
| 76 | { |
| 77 | if ((rt->rcallcxx . . $ kore_route#rcall. = kore_runtime_getcallcxx . . $ kore_runtime_getcall(b514b5951ac4d62f).(func)) == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) |
| 78 | fatalcxx . . $ fatal(f6bc48f0426f26ee).("callback '%s' for '%s' not found", func, rt->pathcxx . . $ kore_route#path.); |
| 79 | |
| 80 | kore_freecxx . . $ kore_free(5d22bdacc48458e8).(rt->funccxx . . $ kore_route#func.); |
| 81 | rt->funccxx . . $ kore_route#func. = kore_strdupcxx . . $ kore_strdup(a8b8e32347182212).(func); |
| 82 | } |
| 83 | |
| 84 | int |
| 85 | kore_route_lookupcxx . . $ kore_route_lookup(975f4cf35c41ba06).(struct http_requestcxx . . $ http_request# *req, struct kore_domaincxx . . $ kore_domain# *dom, |
| 86 | int method, struct kore_routecxx . . $ kore_route# **out) |
| 87 | { |
| 88 | struct kore_routecxx . . $ kore_route# *rt; |
| 89 | int exists; |
| 90 | |
| 91 | exists = 0; |
| 92 | *out = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 93 | |
| 94 | TAILQ_FOREACHcxx . . $ `<external>/2546723208511035544/queue.h:435:9`!(rt, &dom->routes, list) { |
| 95 | if (rt->typecxx . . $ kore_route#type. == HANDLER_TYPE_STATICcxx . . $ `include/kore/kore.h:406:9`!) { |
| 96 | if (!strcmpcxx . . $ strcmp(5ae29d726ae57f88).(rt->pathcxx . . $ kore_route#path., req->pathcxx . . $ http_request#path.)) { |
| 97 | if (rt->methodscxx . . $ kore_route#methods. & method) { |
| 98 | *out = rt; |
| 99 | return (1); |
| 100 | } |
| 101 | exists++; |
| 102 | } |
| 103 | } else { |
| 104 | if (!regexeccxx . . $ regexec(7b724a18a85d26ac).(&rt->rctxcxx . . $ kore_route#rctx., req->pathcxx . . $ http_request#path., |
| 105 | HTTP_CAPTURE_GROUPScxx . . $ `include/kore/http.h:257:9`!, req->cgroupscxx . . $ http_request#cgroups., 0)) { |
| 106 | if (rt->methodscxx . . $ kore_route#methods. & method) { |
| 107 | *out = rt; |
| 108 | return (1); |
| 109 | } |
| 110 | exists++; |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | return (exists); |
| 116 | } |
| 117 | |
| 118 | void |
| 119 | kore_route_reloadcxx . . $ kore_route_reload(49f6e7a06ebc5aa8).(void) |
| 120 | { |
| 121 | struct kore_routecxx . . $ kore_route# *rt; |
| 122 | struct kore_servercxx . . $ kore_server# *srv; |
| 123 | struct kore_domaincxx . . $ kore_domain# *dom; |
| 124 | |
| 125 | LIST_FOREACHcxx . . $ `<external>/2546723208511035544/queue.h:134:9`!(srv, &kore_servers, list) { |
| 126 | TAILQ_FOREACHcxx . . $ `<external>/2546723208511035544/queue.h:435:9`!(dom, &srv->domains, list) { |
| 127 | TAILQ_FOREACHcxx . . $ `<external>/2546723208511035544/queue.h:435:9`!(rt, &dom->routes, list) { |
| 128 | kore_freecxx . . $ kore_free(5d22bdacc48458e8).(rt->rcallcxx . . $ kore_route#rcall.); |
| 129 | rt->rcallcxx . . $ kore_route#rcall. = kore_runtime_getcallcxx . . $ kore_runtime_getcall(b514b5951ac4d62f).(rt->funccxx . . $ kore_route#func.); |
| 130 | if (rt->rcallcxx . . $ kore_route#rcall. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) { |
| 131 | fatalcxx . . $ fatal(f6bc48f0426f26ee).("no function '%s' for route '%s'", |
| 132 | rt->funccxx . . $ kore_route#func., rt->pathcxx . . $ kore_route#path.); |
| 133 | } |
| 134 | rt->errorscxx . . $ kore_route#errors. = 0; |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | } |