bottleneck testcase based on rubbos
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / server / util_xml.c
1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2  * contributor license agreements.  See the NOTICE file distributed with
3  * this work for additional information regarding copyright ownership.
4  * The ASF licenses this file to You under the Apache License, Version 2.0
5  * (the "License"); you may not use this file except in compliance with
6  * the License.  You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "apr_xml.h"
18
19 #include "httpd.h"
20 #include "http_protocol.h"
21 #include "http_log.h"
22 #include "http_core.h"
23
24 #include "util_xml.h"
25
26
27 /* used for reading input blocks */
28 #define READ_BLOCKSIZE 2048
29
30
31 AP_DECLARE(int) ap_xml_parse_input(request_rec * r, apr_xml_doc **pdoc)
32 {
33     apr_xml_parser *parser;
34     apr_bucket_brigade *brigade;
35     int seen_eos;
36     apr_status_t status;
37     char errbuf[200];
38     apr_size_t total_read = 0;
39     apr_size_t limit_xml_body = ap_get_limit_xml_body(r);
40     int result = HTTP_BAD_REQUEST;
41
42     parser = apr_xml_parser_create(r->pool);
43     brigade = apr_brigade_create(r->pool, r->connection->bucket_alloc);
44
45     seen_eos = 0;
46     total_read = 0;
47
48     do {
49         apr_bucket *bucket;
50
51         /* read the body, stuffing it into the parser */
52         status = ap_get_brigade(r->input_filters, brigade,
53                                 AP_MODE_READBYTES, APR_BLOCK_READ,
54                                 READ_BLOCKSIZE);
55
56         if (status != APR_SUCCESS) {
57             goto read_error;
58         }
59
60         APR_BRIGADE_FOREACH(bucket, brigade) {
61             const char *data;
62             apr_size_t len;
63
64             if (APR_BUCKET_IS_EOS(bucket)) {
65                 seen_eos = 1;
66                 break;
67             }
68
69             if (APR_BUCKET_IS_METADATA(bucket)) {
70                 continue;
71             }
72
73             status = apr_bucket_read(bucket, &data, &len, APR_BLOCK_READ);
74             if (status != APR_SUCCESS) {
75                 goto read_error;
76             }
77
78             total_read += len;
79             if (limit_xml_body && total_read > limit_xml_body) {
80                 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
81                               "XML request body is larger than the configured "
82                               "limit of %lu", (unsigned long)limit_xml_body);
83                 result = HTTP_REQUEST_ENTITY_TOO_LARGE;
84                 goto read_error;
85             }
86
87             status = apr_xml_parser_feed(parser, data, len);
88             if (status) {
89                 goto parser_error;
90             }
91         }
92
93         apr_brigade_cleanup(brigade);
94     } while (!seen_eos);
95
96     apr_brigade_destroy(brigade);
97
98     /* tell the parser that we're done */
99     status = apr_xml_parser_done(parser, pdoc);
100     if (status) {
101         /* Some parsers are stupid and return an error on blank documents. */
102         if (!total_read) {
103             *pdoc = NULL;
104             return OK;
105         }
106         ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
107                       "XML parser error (at end). status=%d", status);
108         return HTTP_BAD_REQUEST;
109     }
110
111     return OK;
112
113   parser_error:
114     (void) apr_xml_parser_geterror(parser, errbuf, sizeof(errbuf));
115     ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
116                   "%s", errbuf);
117
118     /* FALLTHRU */
119
120   read_error:
121     /* make sure the parser is terminated */
122     (void) apr_xml_parser_done(parser, NULL);
123
124     apr_brigade_destroy(brigade);
125
126     /* Apache will supply a default error, plus the error log above. */
127     return result;
128 }