/** * Finalise XML parsing. */ int xml_complete(modsec_rec *msr, char **error_msg) { if (error_msg == NULL) return -1; *error_msg = NULL; /* Only if we have a context, meaning we've done some work. */ if (msr->xml->parsing_ctx != NULL) { /* This is how we signalise the end of parsing to libxml. */ xmlParseChunk(msr->xml->parsing_ctx, NULL, 0, 1); /* Preserve the results for our reference. */ msr->xml->well_formed = msr->xml->parsing_ctx->wellFormed; msr->xml->doc = msr->xml->parsing_ctx->myDoc; /* Clean up everything else. */ xmlFreeParserCtxt(msr->xml->parsing_ctx); msr->xml->parsing_ctx = NULL; msr_log(msr, 4, "XML: Parsing complete (well_formed %u).", msr->xml->well_formed); if (msr->xml->well_formed != 1) { *error_msg = ngx_pstrndup(msr->mp, "XML: Failed parsing document."); return -1; } } return 1; }
/** * Feed one chunk of data to the XML parser. */ int xml_process_chunk(modsec_rec *msr, const char *buf, unsigned int size, char **error_msg) { if (error_msg == NULL) return -1; *error_msg = NULL; /* We want to initialise our parsing context here, to * enable us to pass it the first chunk of data so that * it can attempt to auto-detect the encoding. */ if (msr->xml->parsing_ctx == NULL) { /* First invocation. */ msr_log(msr, 4, "XML: Initialising parser."); /* NOTE When Sax interface is used libxml will not * create the document object, but we need it. msr->xml->sax_handler = (xmlSAXHandler *)ngx_pcalloc(msr->mp, sizeof(xmlSAXHandler)); if (msr->xml->sax_handler == NULL) return -1; msr->xml->sax_handler->error = xml_receive_sax_error; msr->xml->sax_handler->warning = xml_receive_sax_error; msr->xml->parsing_ctx = xmlCreatePushParserCtxt(msr->xml->sax_handler, msr, buf, size, "body.xml"); */ msr->xml->parsing_ctx = xmlCreatePushParserCtxt(NULL, NULL, buf, size, "body.xml"); if (msr->xml->parsing_ctx == NULL) { *error_msg = ngx_pstrndup(msr->mp, "XML: Failed to create parsing context."); return -1; } } else { /* Not a first invocation. */ xmlParseChunk(msr->xml->parsing_ctx, buf, size, 0); if (msr->xml->parsing_ctx->wellFormed != 1) { *error_msg = ngx_pstrndup(msr->mp, "XML: Failed parsing document."); return -1; } } return 1; }
char* sf_var_exp(sf_vartable_t* handle, char *varexp, ngx_pool_t *pool) { //char *dststr = (char*)sf_calloc(SF_MAX_VAR_SIZE); char *dststr = (char*)ngx_pcalloc(pool, SF_MAX_VAR_SIZE); char *c = varexp; char *d = dststr; while(*c != '\0') { if (*c=='(' && *(c+1)=='$') { char *p = c+1; while(*p && *p!=')') { p++; } if (*p == ')') { char *name; sf_vartable_t *v; name = ngx_pstrndup(pool, c+1, p-c-1); v = sf_var_search(handle, name); ngx_pfree(pool, name); if (v) { size_t len = strlen(v->value); if (d+len <dststr +SF_MAX_VAR_SIZE) { strncpy(d, v->value, len); d = d + len; } c= p + 1; } else { if ((d + (p -c + 1)) < (dststr +SF_MAX_VAR_SIZE)) { strncpy(d, c, p-c+1); d = d + (p-c+1); } c = p + 1; } } else { strcpy(d, c); d = d+(p-c); c = p; } } else *d++ = *c++; } return dststr; #if 0 /* ÁÙʱ */ sf_vartable_t *v = sf_var_search(handle, varexp); if (v) { return sf_strdup(v->value); } else { return sf_strdup(""); } #endif }