Skip to content

Commit

Permalink
Merge pull request #288 from nodogsplash/simple_templer
Browse files Browse the repository at this point in the history
simplify templater
  • Loading branch information
mwarning committed Sep 3, 2018
2 parents e1f7fb7 + f171b0a commit 9313bdc
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 102 deletions.
49 changes: 23 additions & 26 deletions src/http_microhttpd.c
Original file line number Diff line number Diff line change
Expand Up @@ -884,7 +884,6 @@ static int get_host_value_callback(void *cls, enum MHD_ValueKind kind, const cha
static int show_templated_page(struct MHD_Connection *connection, t_client *client, char *page)
{
struct MHD_Response *response;
struct templater templor;
s_config *config = config_get_config();
int ret = -1;
char filename[PATH_MAX];
Expand Down Expand Up @@ -959,31 +958,29 @@ static int show_templated_page(struct MHD_Connection *connection, t_client *clie
safe_asprintf(&pagesdir, "/%s", config->pagesdir);
safe_asprintf(&imagesdir, "/%s", config->imagesdir);

tmpl_init_templor(&templor);
tmpl_set_variable(&templor, "authaction", authaction);
tmpl_set_variable(&templor, "denyaction", denyaction);
tmpl_set_variable(&templor, "authtarget", authtarget);
tmpl_set_variable(&templor, "clientip", client->ip);
tmpl_set_variable(&templor, "clientmac", client->mac);
tmpl_set_variable(&templor, "clientupload", clientupload);
tmpl_set_variable(&templor, "clientdownload", clientdownload);

tmpl_set_variable(&templor, "gatewaymac", config->gw_mac);
tmpl_set_variable(&templor, "gatewayname", config->gw_name);

tmpl_set_variable(&templor, "imagesdir", imagesdir);
tmpl_set_variable(&templor, "pagesdir", pagesdir);

tmpl_set_variable(&templor, "maxclients", maxclients);
tmpl_set_variable(&templor, "nclients", nclients);

tmpl_set_variable(&templor, "redir", redirect_url);
tmpl_set_variable(&templor, "tok", client->token);
tmpl_set_variable(&templor, "token", client->token);
tmpl_set_variable(&templor, "uptime", uptime);
tmpl_set_variable(&templor, "version", VERSION);

tmpl_parse(&templor, page_result, size + TMPLVAR_SIZE, page_tmpl, size);
struct template vars[] = {
{"authaction", authaction},
{"denyaction", denyaction},
{"authtarget", authtarget},
{"clientip", client->ip},
{"clientmac", client->mac},
{"clientupload", clientupload},
{"clientdownload", clientdownload},
{"gatewaymac", config->gw_mac},
{"gatewayname", config->gw_name},
{"imagesdir", imagesdir},
{"pagesdir", pagesdir},
{"maxclients", maxclients},
{"nclients", nclients},
{"redir", redirect_url},
{"tok", client->token},
{"token", client->token},
{"uptime", uptime},
{"version", VERSION},
{NULL, NULL}
};

tmpl_parse(vars, page_result, size + TMPLVAR_SIZE, page_tmpl, size);

free(page_tmpl);
free(uptime);
Expand Down
63 changes: 9 additions & 54 deletions src/template.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,27 @@
#include "debug.h"
#include "safe.h"

#ifndef ARRAY_SIZE
#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
#endif

static int get_variable_index(const struct templater *templor, const char *name)
static const char *get_variable_value(const struct template *vars, const char *name)
{
int i;

for (i = 0; i < templor->var_count; i++) {
if (strcmp(name, templor->variables[i].name) == 0) {
return i;
i = 0;
while (vars[i].name) {
if (strcmp(vars[i].name, name) == 0) {
return vars[i].value;
}
i += 1;
}

return -1;
}

static const char *get_variable_value(const struct templater *templor, const char *name)
{
int idx;

idx = get_variable_index(templor, name);

return (idx < 0) ? NULL : templor->variables[idx].value;
return NULL;
}

/* This is compatible with the old nodogsplash templater.
* Variable names starts with an '$'.
* Variable ending is detected if when first non-alphanumeric char is shown - except underline ('_').
*/
int tmpl_parse(struct templater *templor, char *dst, size_t dst_len, const char *src, size_t src_len)
int tmpl_parse(struct template *vars, char *dst, size_t dst_len, const char *src, size_t src_len)
{
int src_i = 0; /* track input buffer position */
int dst_i = 0;
Expand Down Expand Up @@ -79,7 +69,7 @@ int tmpl_parse(struct templater *templor, char *dst, size_t dst_len, const char

memset(varname, 0x0, sizeof(varname));
strncpy(varname, varnameptr, varlen);
value = get_variable_value(templor, varname);
value = get_variable_value(vars, varname);

/* check if varname was found in valid variable names */
if (value == NULL) {
Expand All @@ -99,38 +89,3 @@ int tmpl_parse(struct templater *templor, char *dst, size_t dst_len, const char

return 0;
}

int tmpl_set_variable(struct templater *templor, const char *name, const char *value)
{
int idx;

if (!name || !value) {
debug(LOG_ERR, "set variable with NULL name or value. name: %s, value: %s", name, value);
return -1;
}

if (templor->var_count >= ARRAY_SIZE(templor->variables)) {
// no more variable space
debug(LOG_ERR, "No more space for variable %s in templater.", name);
return -1;
}

idx = get_variable_index(templor, name);
if (idx >= 0) {
// variable already set
debug(LOG_ERR, "Variable %s already set in templater.", name);
return -1;
}

idx = templor->var_count;
templor->variables[idx].name = name;
templor->variables[idx].value = value;
templor->var_count += 1;

return 0;
}

void tmpl_init_templor(struct templater *templor)
{
memset(templor, 0, sizeof(struct templater));
}
28 changes: 6 additions & 22 deletions src/template.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@
#include <stdlib.h>

/**
* @brief holds all valid variable name/value pairs
* @brief holds one variable name/value pairs
*
*/
struct templater {
struct {
const char *name;
const char *value;
} variables[32]; /* must have enough space to hold all variables */
int var_count;
struct template {
const char *name;
const char *value;
};

/**
Expand All @@ -23,21 +21,7 @@ struct templater {
* @param src_len
* @return
*/
int tmpl_parse(struct templater *templor, char *dst, size_t dst_len, const char *src, size_t src_len);
int tmpl_parse(struct template *vars, char *dst, size_t dst_len, const char *src, size_t src_len);

/**
* @brief set a variable
* @param templor
* @param name
* @param value
* @return
*/
int tmpl_set_variable(struct templater *templor, const char *name, const char *value);

/**
* @brief initialize templator
* @param templor
*/
void tmpl_init_templor(struct templater *templor);

#endif // TEMPLATE_H

0 comments on commit 9313bdc

Please sign in to comment.