util_xml.c revision d19cb7f3aad3cca26355b1125dd10ea6fe0f330a
842ae4bd224140319ae7feec1872b93dfd491143fielding/* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
842ae4bd224140319ae7feec1872b93dfd491143fielding * applicable.
842ae4bd224140319ae7feec1872b93dfd491143fielding *
842ae4bd224140319ae7feec1872b93dfd491143fielding * Licensed under the Apache License, Version 2.0 (the "License");
842ae4bd224140319ae7feec1872b93dfd491143fielding * you may not use this file except in compliance with the License.
842ae4bd224140319ae7feec1872b93dfd491143fielding * You may obtain a copy of the License at
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein *
71da3cca78eea6010f89b139ecadb79e6d213c4fnd * http://www.apache.org/licenses/LICENSE-2.0
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein *
71da3cca78eea6010f89b139ecadb79e6d213c4fnd * Unless required by applicable law or agreed to in writing, software
71da3cca78eea6010f89b139ecadb79e6d213c4fnd * distributed under the License is distributed on an "AS IS" BASIS,
71da3cca78eea6010f89b139ecadb79e6d213c4fnd * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
71da3cca78eea6010f89b139ecadb79e6d213c4fnd * See the License for the specific language governing permissions and
71da3cca78eea6010f89b139ecadb79e6d213c4fnd * limitations under the License.
b0fb330a8581c8bfab5e523084f9f39264a52b12gstein */
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein#include "apr_xml.h"
49f1aab8b6762ee5b4afdb3a9cc48e7c00435dd3striker
49f1aab8b6762ee5b4afdb3a9cc48e7c00435dd3striker#include "httpd.h"
49f1aab8b6762ee5b4afdb3a9cc48e7c00435dd3striker#include "http_protocol.h"
49f1aab8b6762ee5b4afdb3a9cc48e7c00435dd3striker#include "http_log.h"
49f1aab8b6762ee5b4afdb3a9cc48e7c00435dd3striker#include "http_core.h"
49f1aab8b6762ee5b4afdb3a9cc48e7c00435dd3striker
49f1aab8b6762ee5b4afdb3a9cc48e7c00435dd3striker#include "util_charset.h"
49f1aab8b6762ee5b4afdb3a9cc48e7c00435dd3striker#include "util_xml.h"
49f1aab8b6762ee5b4afdb3a9cc48e7c00435dd3striker
49f1aab8b6762ee5b4afdb3a9cc48e7c00435dd3striker
49f1aab8b6762ee5b4afdb3a9cc48e7c00435dd3striker/* used for reading input blocks */
49f1aab8b6762ee5b4afdb3a9cc48e7c00435dd3striker#define READ_BLOCKSIZE 2048
49f1aab8b6762ee5b4afdb3a9cc48e7c00435dd3striker
49f1aab8b6762ee5b4afdb3a9cc48e7c00435dd3striker
49f1aab8b6762ee5b4afdb3a9cc48e7c00435dd3strikerAP_DECLARE(int) ap_xml_parse_input(request_rec * r, apr_xml_doc **pdoc)
49f1aab8b6762ee5b4afdb3a9cc48e7c00435dd3striker{
49f1aab8b6762ee5b4afdb3a9cc48e7c00435dd3striker apr_xml_parser *parser;
49f1aab8b6762ee5b4afdb3a9cc48e7c00435dd3striker apr_bucket_brigade *brigade;
49f1aab8b6762ee5b4afdb3a9cc48e7c00435dd3striker int seen_eos;
49f1aab8b6762ee5b4afdb3a9cc48e7c00435dd3striker apr_status_t status;
49f1aab8b6762ee5b4afdb3a9cc48e7c00435dd3striker char errbuf[200];
49f1aab8b6762ee5b4afdb3a9cc48e7c00435dd3striker apr_size_t total_read = 0;
49f1aab8b6762ee5b4afdb3a9cc48e7c00435dd3striker apr_size_t limit_xml_body = ap_get_limit_xml_body(r);
49f1aab8b6762ee5b4afdb3a9cc48e7c00435dd3striker int result = HTTP_BAD_REQUEST;
49f1aab8b6762ee5b4afdb3a9cc48e7c00435dd3striker
49f1aab8b6762ee5b4afdb3a9cc48e7c00435dd3striker parser = apr_xml_parser_create(r->pool);
f4c310fd2555c6faca1f980f00b161eadb089023gstein brigade = apr_brigade_create(r->pool, r->connection->bucket_alloc);
1b21d7b3d97def358b2e923655edeb16613a1c31gstein
1327b173ab33ae5bec795b798d5c54c16a7b5c05gstein seen_eos = 0;
1b21d7b3d97def358b2e923655edeb16613a1c31gstein total_read = 0;
1b21d7b3d97def358b2e923655edeb16613a1c31gstein
1b21d7b3d97def358b2e923655edeb16613a1c31gstein do {
1b21d7b3d97def358b2e923655edeb16613a1c31gstein apr_bucket *bucket;
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* read the body, stuffing it into the parser */
f4c310fd2555c6faca1f980f00b161eadb089023gstein status = ap_get_brigade(r->input_filters, brigade,
f4c310fd2555c6faca1f980f00b161eadb089023gstein AP_MODE_READBYTES, APR_BLOCK_READ,
f4c310fd2555c6faca1f980f00b161eadb089023gstein READ_BLOCKSIZE);
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (status != APR_SUCCESS) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein goto read_error;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein for (bucket = APR_BRIGADE_FIRST(brigade);
847b3922f7dcde6830f4aad49d29c84b4569c260minfrin bucket != APR_BRIGADE_SENTINEL(brigade);
847b3922f7dcde6830f4aad49d29c84b4569c260minfrin bucket = APR_BUCKET_NEXT(bucket))
f4c310fd2555c6faca1f980f00b161eadb089023gstein {
000397350b42c6266351bd618fa07df929fa7c79gstein const char *data;
000397350b42c6266351bd618fa07df929fa7c79gstein apr_size_t len;
000397350b42c6266351bd618fa07df929fa7c79gstein
5ca401d5a2dd63d75464895dc2a6ea292b62fd99gstein if (APR_BUCKET_IS_EOS(bucket)) {
5ca401d5a2dd63d75464895dc2a6ea292b62fd99gstein seen_eos = 1;
5ca401d5a2dd63d75464895dc2a6ea292b62fd99gstein break;
36ef8f77bffe75d1aa327882be1b5bdbe2ff567asf }
36ef8f77bffe75d1aa327882be1b5bdbe2ff567asf
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (APR_BUCKET_IS_METADATA(bucket)) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein continue;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein status = apr_bucket_read(bucket, &data, &len, APR_BLOCK_READ);
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (status != APR_SUCCESS) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein goto read_error;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
9f18c80269be35c0b5653e84b0db0a24044722c4gstein
9f18c80269be35c0b5653e84b0db0a24044722c4gstein total_read += len;
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (limit_xml_body && total_read > limit_xml_body) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
f4c310fd2555c6faca1f980f00b161eadb089023gstein "XML request body is larger than the configured "
f4c310fd2555c6faca1f980f00b161eadb089023gstein "limit of %lu", (unsigned long)limit_xml_body);
f4c310fd2555c6faca1f980f00b161eadb089023gstein result = HTTP_REQUEST_ENTITY_TOO_LARGE;
f4c310fd2555c6faca1f980f00b161eadb089023gstein goto read_error;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein
b47464a901075041e800be2de098a603923fa4f9gstein status = apr_xml_parser_feed(parser, data, len);
f4c310fd2555c6faca1f980f00b161eadb089023gstein if (status) {
f4c310fd2555c6faca1f980f00b161eadb089023gstein goto parser_error;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
49f1aab8b6762ee5b4afdb3a9cc48e7c00435dd3striker
f4c310fd2555c6faca1f980f00b161eadb089023gstein apr_brigade_cleanup(brigade);
f4c310fd2555c6faca1f980f00b161eadb089023gstein } while (!seen_eos);
f4c310fd2555c6faca1f980f00b161eadb089023gstein
98e9c4a310bb623ff788680f88b6bd200ff36a24wrowe apr_brigade_destroy(brigade);
f4c310fd2555c6faca1f980f00b161eadb089023gstein
ef3c32d4a7accb38368c7b9face2ade88d987a8bgstein /* tell the parser that we're done */
ef3c32d4a7accb38368c7b9face2ade88d987a8bgstein status = apr_xml_parser_done(parser, pdoc);
5a9667916c79d8c699b069068e5570aa1c331c80gstein if (status) {
67ec15681c83d4f5e119f0742618569017beb3fbstriker /* Some parsers are stupid and return an error on blank documents. */
ef3c32d4a7accb38368c7b9face2ade88d987a8bgstein if (!total_read) {
ef3c32d4a7accb38368c7b9face2ade88d987a8bgstein *pdoc = NULL;
ef3c32d4a7accb38368c7b9face2ade88d987a8bgstein return OK;
ef3c32d4a7accb38368c7b9face2ade88d987a8bgstein }
5a9667916c79d8c699b069068e5570aa1c331c80gstein ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
9d0665da83d1e22c0ea0e5f6f940f70f75bf5237ianh "XML parser error (at end). status=%d", status);
f5ec9b038bb9db933072ba2c0a8e7bb2a3cedbdagstein return HTTP_BAD_REQUEST;
f4c310fd2555c6faca1f980f00b161eadb089023gstein }
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein#if APR_CHARSET_EBCDIC
ef3c32d4a7accb38368c7b9face2ade88d987a8bgstein apr_xml_parser_convert_doc(r->pool, *pdoc, ap_hdrs_from_ascii);
ef3c32d4a7accb38368c7b9face2ade88d987a8bgstein#endif
67ec15681c83d4f5e119f0742618569017beb3fbstriker return OK;
ef3c32d4a7accb38368c7b9face2ade88d987a8bgstein
9f18c80269be35c0b5653e84b0db0a24044722c4gstein parser_error:
49f1aab8b6762ee5b4afdb3a9cc48e7c00435dd3striker (void) apr_xml_parser_geterror(parser, errbuf, sizeof(errbuf));
9d0665da83d1e22c0ea0e5f6f940f70f75bf5237ianh ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
f4c310fd2555c6faca1f980f00b161eadb089023gstein "XML Parser Error: %s", errbuf);
f4c310fd2555c6faca1f980f00b161eadb089023gstein
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm /* FALLTHRU */
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein read_error:
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* make sure the parser is terminated */
49f1aab8b6762ee5b4afdb3a9cc48e7c00435dd3striker (void) apr_xml_parser_done(parser, NULL);
f4c310fd2555c6faca1f980f00b161eadb089023gstein
b47464a901075041e800be2de098a603923fa4f9gstein apr_brigade_destroy(brigade);
f4c310fd2555c6faca1f980f00b161eadb089023gstein
f4c310fd2555c6faca1f980f00b161eadb089023gstein /* Apache will supply a default error, plus the error log above. */
f4c310fd2555c6faca1f980f00b161eadb089023gstein return result;
f4c310fd2555c6faca1f980f00b161eadb089023gstein}
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm