httpd deduplicate code in http_send_data_ssi

Keep lead-in/lead-out tag in an array
This commit is contained in:
Simon Goldschmidt 2018-06-22 22:23:26 +02:00
parent 0acf37fd75
commit d80e84864e

View File

@ -217,8 +217,6 @@ struct http_ssi_state {
#if !LWIP_HTTPD_SSI_INCLUDE_TAG #if !LWIP_HTTPD_SSI_INCLUDE_TAG
const char *tag_started;/* Pointer to the first opening '<' of the tag. */ const char *tag_started;/* Pointer to the first opening '<' of the tag. */
#endif /* !LWIP_HTTPD_SSI_INCLUDE_TAG */ #endif /* !LWIP_HTTPD_SSI_INCLUDE_TAG */
const char *lead_in; /* Lead in for the potential current tag type. */
const char *lead_out; /* Lead out for the current tag type. */
const char *tag_end; /* Pointer to char after the closing '>' of the tag. */ const char *tag_end; /* Pointer to char after the closing '>' of the tag. */
u32_t parse_left; /* Number of unparsed bytes in buf. */ u32_t parse_left; /* Number of unparsed bytes in buf. */
u16_t tag_index; /* Counter used by tag parsing state machine */ u16_t tag_index; /* Counter used by tag parsing state machine */
@ -226,11 +224,18 @@ struct http_ssi_state {
#if LWIP_HTTPD_SSI_MULTIPART #if LWIP_HTTPD_SSI_MULTIPART
u16_t tag_part; /* Counter passed to and changed by tag insertion function to insert multiple times */ u16_t tag_part; /* Counter passed to and changed by tag insertion function to insert multiple times */
#endif /* LWIP_HTTPD_SSI_MULTIPART */ #endif /* LWIP_HTTPD_SSI_MULTIPART */
u8_t tag_type; /* index into http_ssi_tag_desc array */
u8_t tag_name_len; /* Length of the tag name in string tag_name */ u8_t tag_name_len; /* Length of the tag name in string tag_name */
char tag_name[LWIP_HTTPD_MAX_TAG_NAME_LEN + 1]; /* Last tag name extracted */ char tag_name[LWIP_HTTPD_MAX_TAG_NAME_LEN + 1]; /* Last tag name extracted */
char tag_insert[LWIP_HTTPD_MAX_TAG_INSERT_LEN + 1]; /* Insert string for tag_name */ char tag_insert[LWIP_HTTPD_MAX_TAG_INSERT_LEN + 1]; /* Insert string for tag_name */
enum tag_check_state tag_state; /* State of the tag processor */ enum tag_check_state tag_state; /* State of the tag processor */
}; };
struct http_ssi_tag_description {
const char *lead_in;
const char *lead_out;
};
#endif /* LWIP_HTTPD_SSI */ #endif /* LWIP_HTTPD_SSI */
struct http_state { struct http_state {
@ -318,10 +323,13 @@ static int httpd_num_tags;
static const char **httpd_tags; static const char **httpd_tags;
#endif /* !LWIP_HTTPD_SSI_RAW */ #endif /* !LWIP_HTTPD_SSI_RAW */
static const char httpd_tag_lead_in[] = "<!--#"; /* Define the available tag lead-ins and corresponding lead-outs.
static const char httpd_tag_lead_out[] = "-->"; * ATTENTION: for the algorithm below using this array, it is essential
static const char httpd_tag_lead_in_js[] = "/*#"; * that the lead in differs in the first character! */
static const char httpd_tag_lead_out_js[] = "*/"; const struct http_ssi_tag_description http_ssi_tag_desc[] = {
{"<!--#", "-->"},
{"/*#", "*/"}
};
#endif /* LWIP_HTTPD_SSI */ #endif /* LWIP_HTTPD_SSI */
@ -1213,6 +1221,7 @@ http_send_data_ssi(struct altcp_pcb *pcb, struct http_state *hs)
err_t err = ERR_OK; err_t err = ERR_OK;
u16_t len; u16_t len;
u8_t data_to_send = 0; u8_t data_to_send = 0;
u8_t tag_type;
struct http_ssi_state *ssi = hs->ssi; struct http_ssi_state *ssi = hs->ssi;
LWIP_ASSERT("ssi != NULL", ssi != NULL); LWIP_ASSERT("ssi != NULL", ssi != NULL);
@ -1253,27 +1262,18 @@ http_send_data_ssi(struct altcp_pcb *pcb, struct http_state *hs)
case TAG_NONE: case TAG_NONE:
/* We are not currently processing an SSI tag so scan for the /* We are not currently processing an SSI tag so scan for the
* start of the lead-in marker. */ * start of the lead-in marker. */
if (*ssi->parsed == httpd_tag_lead_in[0]) { for (tag_type = 0; tag_type < LWIP_ARRAYSIZE(http_ssi_tag_desc); tag_type++) {
/* We found what could be the lead-in for a new tag so change if (*ssi->parsed == http_ssi_tag_desc[tag_type].lead_in[0]) {
* state appropriately. */ /* We found what could be the lead-in for a new tag so change
ssi->lead_in = httpd_tag_lead_in; * state appropriately. */
ssi->lead_out = httpd_tag_lead_out; ssi->tag_type = tag_type;
ssi->tag_state = TAG_LEADIN; ssi->tag_state = TAG_LEADIN;
ssi->tag_index = 1; ssi->tag_index = 1;
#if !LWIP_HTTPD_SSI_INCLUDE_TAG #if !LWIP_HTTPD_SSI_INCLUDE_TAG
ssi->tag_started = ssi->parsed; ssi->tag_started = ssi->parsed;
#endif /* !LWIP_HTTPD_SSI_INCLUDE_TAG */ #endif /* !LWIP_HTTPD_SSI_INCLUDE_TAG */
} break;
if (*ssi->parsed == httpd_tag_lead_in_js[0]) { }
/* We found what could be the lead-in for a new tag so change
* state appropriately. */
ssi->lead_in = httpd_tag_lead_in_js;
ssi->lead_out = httpd_tag_lead_out_js;
ssi->tag_state = TAG_LEADIN;
ssi->tag_index = 1;
#if !LWIP_HTTPD_SSI_INCLUDE_TAG
ssi->tag_started = ssi->parsed;
#endif /* !LWIP_HTTPD_SSI_INCLUDE_TAG */
} }
/* Move on to the next character in the buffer */ /* Move on to the next character in the buffer */
@ -1286,12 +1286,12 @@ http_send_data_ssi(struct altcp_pcb *pcb, struct http_state *hs)
* the tag name. */ * the tag name. */
/* Have we reached the end of the leadin? */ /* Have we reached the end of the leadin? */
if (ssi->lead_in[ssi->tag_index] == 0) { if (http_ssi_tag_desc[ssi->tag_type].lead_in[ssi->tag_index] == 0) {
ssi->tag_index = 0; ssi->tag_index = 0;
ssi->tag_state = TAG_FOUND; ssi->tag_state = TAG_FOUND;
} else { } else {
/* Have we found the next character we expect for the tag leadin? */ /* Have we found the next character we expect for the tag leadin? */
if (*ssi->parsed == ssi->lead_in[ssi->tag_index]) { if (*ssi->parsed == http_ssi_tag_desc[ssi->tag_type].lead_in[ssi->tag_index]) {
/* Yes - move to the next one unless we have found the complete /* Yes - move to the next one unless we have found the complete
* leadin, in which case we start looking for the tag itself */ * leadin, in which case we start looking for the tag itself */
ssi->tag_index++; ssi->tag_index++;
@ -1324,7 +1324,7 @@ http_send_data_ssi(struct altcp_pcb *pcb, struct http_state *hs)
/* Have we found the end of the tag name? This is signalled by /* Have we found the end of the tag name? This is signalled by
* us finding the first leadout character or whitespace */ * us finding the first leadout character or whitespace */
if ((*ssi->parsed == ssi->lead_out[0]) || if ((*ssi->parsed == http_ssi_tag_desc[ssi->tag_type].lead_out[0]) ||
(*ssi->parsed == ' ') || (*ssi->parsed == '\t') || (*ssi->parsed == ' ') || (*ssi->parsed == '\t') ||
(*ssi->parsed == '\n') || (*ssi->parsed == '\r')) { (*ssi->parsed == '\n') || (*ssi->parsed == '\r')) {
@ -1338,7 +1338,7 @@ http_send_data_ssi(struct altcp_pcb *pcb, struct http_state *hs)
LWIP_ASSERT("ssi->tag_index <= 0xff", ssi->tag_index <= 0xff); LWIP_ASSERT("ssi->tag_index <= 0xff", ssi->tag_index <= 0xff);
ssi->tag_name_len = (u8_t)ssi->tag_index; ssi->tag_name_len = (u8_t)ssi->tag_index;
ssi->tag_name[ssi->tag_index] = '\0'; ssi->tag_name[ssi->tag_index] = '\0';
if (*ssi->parsed == ssi->lead_out[0]) { if (*ssi->parsed == http_ssi_tag_desc[ssi->tag_type].lead_out[0]) {
ssi->tag_index = 1; ssi->tag_index = 1;
} else { } else {
ssi->tag_index = 0; ssi->tag_index = 0;
@ -1374,7 +1374,7 @@ http_send_data_ssi(struct altcp_pcb *pcb, struct http_state *hs)
} }
/* Have we found the next character we expect for the tag leadout? */ /* Have we found the next character we expect for the tag leadout? */
if (*ssi->parsed == ssi->lead_out[ssi->tag_index]) { if (*ssi->parsed == http_ssi_tag_desc[ssi->tag_type].lead_out[ssi->tag_index]) {
/* Yes - move to the next one unless we have found the complete /* Yes - move to the next one unless we have found the complete
* leadout, in which case we need to call the client to process * leadout, in which case we need to call the client to process
* the tag. */ * the tag. */
@ -1384,7 +1384,7 @@ http_send_data_ssi(struct altcp_pcb *pcb, struct http_state *hs)
ssi->parsed++; ssi->parsed++;
ssi->tag_index++; ssi->tag_index++;
if (ssi->lead_out[ssi->tag_index] == 0) { if (http_ssi_tag_desc[ssi->tag_type].lead_out[ssi->tag_index] == 0) {
/* Call the client to ask for the insert string for the /* Call the client to ask for the insert string for the
* tag we just found. */ * tag we just found. */
#if LWIP_HTTPD_SSI_MULTIPART #if LWIP_HTTPD_SSI_MULTIPART