MCPcopy Create free account
hub / github.com/apache/httpd / lua_ap_regex

Function lua_ap_regex

modules/lua/lua_request.c:1169–1227  ·  view source on GitHub ↗

* lua_ap_regex; r:regex(string, pattern [, flags]) * - Evaluates a regex and returns captures if matched */

Source from the content-addressed store, hash-verified

1167 * - Evaluates a regex and returns captures if matched
1168 */
1169static int lua_ap_regex(lua_State *L)
1170{
1171 request_rec *r;
1172 int i,
1173 rv,
1174 flags;
1175 const char *pattern,
1176 *source;
1177 char *err;
1178 ap_regex_t regex;
1179 ap_regmatch_t matches[MODLUA_MAX_REG_MATCH+1];
1180
1181 luaL_checktype(L, 1, LUA_TUSERDATA);
1182 luaL_checktype(L, 2, LUA_TSTRING);
1183 luaL_checktype(L, 3, LUA_TSTRING);
1184 r = ap_lua_check_request_rec(L, 1);
1185 source = lua_tostring(L, 2);
1186 pattern = lua_tostring(L, 3);
1187 flags = luaL_optinteger(L, 4, 0);
1188
1189 rv = ap_regcomp(&regex, pattern, flags);
1190 if (rv) {
1191 lua_pushboolean(L, 0);
1192 err = apr_palloc(r->pool, 256);
1193 ap_regerror(rv, &regex, err, 256);
1194 lua_pushstring(L, err);
1195 return 2;
1196 }
1197
1198 if (regex.re_nsub > MODLUA_MAX_REG_MATCH) {
1199 lua_pushboolean(L, 0);
1200 err = apr_palloc(r->pool, 64);
1201 apr_snprintf(err, 64,
1202 "regcomp found %d matches; only %d allowed.",
1203 regex.re_nsub, MODLUA_MAX_REG_MATCH);
1204 lua_pushstring(L, err);
1205 return 2;
1206 }
1207
1208 rv = ap_regexec(&regex, source, MODLUA_MAX_REG_MATCH, matches, 0);
1209 if (rv == AP_REG_NOMATCH) {
1210 lua_pushboolean(L, 0);
1211 return 1;
1212 }
1213
1214 lua_newtable(L);
1215 for (i = 0; i <= regex.re_nsub; i++) {
1216 lua_pushinteger(L, i);
1217 if (matches[i].rm_so >= 0 && matches[i].rm_eo >= 0)
1218 lua_pushstring(L,
1219 apr_pstrndup(r->pool, source + matches[i].rm_so,
1220 matches[i].rm_eo - matches[i].rm_so));
1221 else
1222 lua_pushnil(L);
1223 lua_settable(L, -3);
1224
1225 }
1226 return 1;

Callers

nothing calls this directly

Calls 4

ap_regcompFunction · 0.85
ap_regerrorFunction · 0.85
ap_regexecFunction · 0.85
ap_lua_check_request_recFunction · 0.70

Tested by

no test coverage detected