util_xml.c revision cd5c0afc86ca2eb23d6d12e14590e03cf2f80450
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2000 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
*/
/*
** DAV extension module for Apache 2.0.*
** - XML parser for the body of a request
*/
/* James Clark's Expat parser */
#include "xmlparse.h"
#include "httpd.h"
#include "http_protocol.h"
#include "http_log.h"
#include "http_core.h"
#include "util_xml.h"
#define DEBUG_CR "\r\n"
/* errors related to namespace processing */
/* test for a namespace prefix that begins with [Xx][Mm][Ll] */
#define AP_XML_NS_IS_RESERVED(name) \
/* content for parsing */
typedef struct ap_xml_ctx {
ap_pool_t *p; /* the pool we allocate from */
int error; /* an error has occurred */
/* errors may be AP_XML_NS_ERROR_* or other private errors which will
be defined here (none yet) */
} ap_xml_ctx;
/* struct for scoping namespace declarations */
typedef struct ap_xml_ns_scope {
const char *prefix; /* prefix used for this ns */
int ns; /* index into namespace table */
int emptyURI; /* the namespace URI is the empty string */
/* return namespace table index for a given prefix */
{
/*
** Walk up the tree, looking for a namespace scope that defines this
** prefix.
*/
/*
** It is possible to set the default namespace to an
** empty URI string; this resets the default namespace
** to mean "no namespace." We just found the prefix
** refers to an empty URI, so return "no namespace."
*/
return AP_XML_NS_NONE;
}
}
}
}
/*
* If the prefix is empty (""), this means that a prefix was not
* just above did not locate a default namespace URI (which is stored
* has "no namespace". We have a reserved value for this.
*/
if (*prefix == '\0') {
return AP_XML_NS_NONE;
}
/* not found */
return AP_XML_NS_ERROR_UNKNOWN_PREFIX;
}
{
char *colon;
const char *quoted;
char *elem_name;
/* punt once we find an error */
return;
/* prep the element */
/* fill in the attributes (note: ends up in reverse order) */
while (*attrs) {
}
/* hook the element into the tree */
/* no current element; this also becomes the root */
}
else {
/* this element appeared within the current elem */
/* no first child either */
}
else {
/* hook onto the end of the parent's children */
}
/* this element is now the current element */
}
/* scan the attributes for namespace declarations */
attr;
/* test for xmlns:foo= form and xmlns= form */
if (*prefix == ':')
++prefix;
else if (*prefix != '\0') {
/* advance "prev" since "attr" is still present */
continue;
}
/* quote the URI before we ever start working with it */
/* build and insert the new scope */
/* remove this attribute from the element */
else
/* Note: prev will not be advanced since we just removed "attr" */
}
/* save away the language (in quoted form) */
/* remove this attribute from the element */
else
/* Note: prev will not be advanced since we just removed "attr" */
}
else {
/* advance "prev" since "attr" is still present */
}
}
/*
** If an xml:lang attribute didn't exist (lang==NULL), then copy the
** language from the parent element (if present).
**
** NOTE: elem_size() *depends* upon this pointer equality.
*/
/* adjust the element's namespace */
/*
* The element is using the default namespace, which will always
* be found. Either it will be "no namespace", or a default
* namespace URI has been specified at some point.
*/
}
}
else {
*colon = '\0';
return;
}
}
/* adjust all remaining attributes' namespaces */
/*
* ap_xml_attr defines this as "const" but we dup'd it, so we
* know that we can change it. a bit hacky, but the existing
* structure def is best.
*/
/*
* Attributes do NOT use the default namespace. Therefore,
* we place them into the "no namespace" category.
*/
}
}
else {
*colon = '\0';
return;
}
}
}
}
{
/* punt once we find an error */
return;
/* pop up one level */
}
{
const char *s;
/* punt once we find an error */
return;
/* no children yet. this cdata follows the start tag */
}
else {
/* child elements exist. this cdata follows the last child. */
}
}
{
int result;
{0};
return result;
if (r->remaining == 0) {
return OK;
}
/* ### we should get the encoding from Content-Encoding */
/* ### anything better to do? */
exit(1);
}
if (ap_should_client_block(r)) {
long len;
char *buffer;
char end;
int rv;
size_t total_read = 0;
/* allocate our working buffer */
/* read the body, stuffing it into the parser */
total_read += len;
"XML request body is larger than the configured "
"limit of %lu", (unsigned long)limit_xml_body);
goto read_error;
}
if (rv == 0)
goto parser_error;
}
if (len == -1) {
/* ap_get_client_block() has logged an error */
goto read_error;
}
/* tell the parser that we're done */
if (rv == 0)
goto parser_error;
}
"An undefined namespace prefix was used.");
break;
default:
"There was an error within the XML request body.");
break;
}
/* Apache will supply a default error, plus the error log above. */
return HTTP_BAD_REQUEST;
}
/* ### assert: ctx.cur_elem == NULL */
return OK;
{
/* ### fix this error message (default vs special) */
"XML parser error code: %s (%d).",
/* Apache will supply a default error, plus the error log above. */
return HTTP_BAD_REQUEST;
}
/* Apache will supply a default error, plus whatever was logged. */
return HTTP_BAD_REQUEST;
}
const char *text)
{
/* no text elements yet */
}
else {
/* append to the last text element */
}
}
/* ---------------------------------------------------------------
**
** XML UTILITY FUNCTIONS
*/
/*
** ap_xml_quote_string: quote an XML string
**
** Replace '<', '>', and '&' with '<', '>', and '&'.
** If quotes is true, then replace '"' with '"'.
**
** quotes is typically set to true for XML strings that will occur within
** double quotes -- attribute values.
*/
int quotes)
{
const char *scan;
int len = 0;
int extra = 0;
char *qstr;
char *qscan;
char c;
if (c == '<' || c == '>')
else if (c == '&')
else if (quotes && c == '"')
}
/* nothing to do? */
if (extra == 0)
return s;
if (c == '<') {
*qscan++ = '&';
*qscan++ = 'l';
*qscan++ = 't';
*qscan++ = ';';
}
else if (c == '>') {
*qscan++ = '&';
*qscan++ = 'g';
*qscan++ = 't';
*qscan++ = ';';
}
else if (c == '&') {
*qscan++ = '&';
*qscan++ = 'a';
*qscan++ = 'm';
*qscan++ = 'p';
*qscan++ = ';';
}
else if (quotes && c == '"') {
*qscan++ = '&';
*qscan++ = 'q';
*qscan++ = 'u';
*qscan++ = 'o';
*qscan++ = 't';
*qscan++ = ';';
}
else {
*qscan++ = c;
}
}
*qscan = '\0';
return qstr;
}
/* how many characters for the given integer? */
{
int size = 0;
for (; t; t = t->next)
return size;
}
{
const ap_xml_attr *attr;
size = 0;
if (style == AP_XML_X2T_FULL_NS_LANG) {
int i;
/*
** The outer element will contain xmlns:ns%d="%s" attributes
** and an xml:lang attribute, if applicable.
*/
for (i = namespaces->nelts; i--;) {
/* compute size of: ' xmlns:ns%d="%s"' */
}
/* compute size of: ' xml:lang="%s"' */
}
}
/* compute size of: <%s> */
}
else {
/* compute size of: <ns%d:%s> */
}
if (AP_XML_ELEM_IS_EMPTY(elem)) {
/* insert a closing "/" */
size += 1;
}
else {
/*
* two of above plus "/":
* <ns%d:%s> ... </ns%d:%s>
* OR <%s> ... </%s>
*/
}
/* compute size of: ' %s="%s"' */
}
else {
/* compute size of: ' ns%d:%s="%s"' */
}
}
/*
** If the element has an xml:lang value that is *different* from
** its parent, then add the thing in: ' xml:lang="%s"'.
**
** NOTE: we take advantage of the pointer equality established by
** the parsing for "inheriting" the xml:lang values from parents.
*/
}
}
else if (style == AP_XML_X2T_LANG_INNER) {
/*
* This style prepends the xml:lang value plus a null terminator.
* If a lang value is not present, then we insert a null term.
*/
}
else
size = 0;
/* the size of the child element plus the CDATA that follows it */
}
return size;
}
static char *write_text(char *s, const ap_text *t)
{
for (; t; t = t->next) {
s += len;
}
return s;
}
{
const ap_xml_elem *child;
int ns;
const ap_xml_attr *attr;
}
else {
}
s += len;
else
s += len;
}
/* add the xml:lang value if necessary */
(style == AP_XML_X2T_FULL_NS_LANG ||
s += len;
}
/* add namespace definitions, if required */
if (style == AP_XML_X2T_FULL_NS_LANG) {
int i;
for (i = namespaces->nelts; i--;) {
s += len;
}
}
/* no more to do. close it up and go. */
if (empty) {
*s++ = '/';
*s++ = '>';
return s;
}
/* just close it */
*s++ = '>';
}
else if (style == AP_XML_X2T_LANG_INNER) {
/* prepend the xml:lang value */
s += len;
}
*s++ = '\0';
}
}
}
else {
}
s += len;
}
return s;
}
{
/* convert the element's text */
}
}
/* convert the attribute values */
}
/* convert the child elements */
}
}
/* convert an element to a text string */
{
/* get the exact size, plus a null terminator */
*pbuf = s;
if (psize)
}
const ap_xml_elem *elem)
{
/*
* The prefix (xml...) is already within the prop name, or
* the element simply has no prefix.
*/
}
}
/* return the URI's (existing) index, or insert it and return a new index */
const char *uri)
{
int i;
const char **pelt;
return i;
}
}