1 /* Copyright (C) 2001 by Eric Kidd. All rights reserved.
3 ** Redistribution and use in source and binary forms, with or without
4 ** modification, are permitted provided that the following conditions
6 ** 1. Redistributions of source code must retain the above copyright
7 ** notice, this list of conditions and the following disclaimer.
8 ** 2. Redistributions in binary form must reproduce the above copyright
9 ** notice, this list of conditions and the following disclaimer in the
10 ** documentation and/or other materials provided with the distribution.
11 ** 3. The name of the author may not be used to endorse or promote products
12 ** derived from this software without specific prior written permission.
14 ** THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 ** ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 ** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 ** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 ** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 ** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include "xmlrpc_config.h"
33 /* Windows NT stdout binary mode fix. */
40 #include "xmlrpc_server.h"
41 #include "xmlrpc_cgi.h"
44 /*=========================================================================
46 **=========================================================================
47 ** These routines send various kinds of responses to the server.
50 static void send_xml (char *xml_data
, size_t xml_len
)
52 /* Send our CGI headers back to the server.
53 ** XXX - Coercing 'size_t' to 'unsigned long' might be unsafe under
54 ** really weird circumstances. */
55 fprintf(stdout
, "Status: 200 OK\n");
56 /* Handle authentication cookie being sent back. */
57 if (getenv("HTTP_COOKIE_AUTH") != NULL
)
58 fprintf(stdout
, "Set-Cookie: auth=%s\n", getenv("HTTP_COOKIE_AUTH"));
59 fprintf(stdout
, "Content-type: text/xml; charset=\"utf-8\"\n");
60 fprintf(stdout
, "Content-length: %ld\n\n", (unsigned long) xml_len
);
62 /* Blast out our data. */
63 fwrite(xml_data
, sizeof(char), xml_len
, stdout
);
66 static void send_error (int code
, char *message
, xmlrpc_env
*env
)
68 /* Send an error header. */
69 fprintf(stdout
, "Status: %d %s\n", code
, message
);
70 fprintf(stdout
, "Content-type: text/html\n\n");
72 /* Send an error message. */
73 fprintf(stdout
, "<title>%d %s</title>\n", code
, message
);
74 fprintf(stdout
, "<h1>%d %s</h1>\n", code
, message
);
75 fprintf(stdout
, "<p>An error occurred processing your request.</p>\n");
77 /* Print out the XML-RPC fault, if present. */
78 if (env
&& env
->fault_occurred
)
79 fprintf(stdout
, "<p>XML-RPC Fault #%d: %s</p>\n",
80 env
->fault_code
, env
->fault_string
);
84 /*=========================================================================
85 ** die_if_fault_occurred
86 **=========================================================================
87 ** Certain kinds of errors aren't worth the trouble of generating
88 ** an XML-RPC fault. For these, we just send status 500 to our web server
89 ** and log the fault to our server log.
92 static void die_if_fault_occurred (xmlrpc_env
*env
)
94 if (env
->fault_occurred
) {
95 fprintf(stderr
, "Unexpected XML-RPC fault: %s (%d)\n",
96 env
->fault_string
, env
->fault_code
);
97 send_error(500, "Internal Server Error", env
);
103 /*=========================================================================
104 ** Initialization, Cleanup & Method Registry
105 **=========================================================================
106 ** These are all related, so we group them together.
109 static xmlrpc_registry
*registry
;
111 void xmlrpc_cgi_init (int flags ATTR_UNUSED
)
115 xmlrpc_env_init(&env
);
116 registry
= xmlrpc_registry_new(&env
);
117 die_if_fault_occurred(&env
);
118 xmlrpc_env_clean(&env
);
121 /* Fix from Jeff Stewart: NT opens stdin and stdout in text mode
122 ** by default, badly confusing our length calculations. So we need
123 ** to set these file handles to binary. */
124 _setmode(_fileno(stdout
), _O_BINARY
);
125 _setmode(_fileno(stdin
), _O_BINARY
);
129 void xmlrpc_cgi_cleanup (void)
131 xmlrpc_registry_free(registry
);
134 xmlrpc_registry
*xmlrpc_cgi_registry (void)
139 void xmlrpc_cgi_add_method (char *method_name
,
140 xmlrpc_method method
,
144 xmlrpc_env_init(&env
);
145 xmlrpc_registry_add_method(&env
, registry
, NULL
, method_name
,
147 die_if_fault_occurred(&env
);
148 xmlrpc_env_clean(&env
);
152 xmlrpc_cgi_add_method_w_doc (char *method_name
,
153 xmlrpc_method method
,
159 xmlrpc_env_init(&env
);
160 xmlrpc_registry_add_method_w_doc(&env
, registry
, NULL
, method_name
,
161 method
, user_data
, signature
, help
);
162 die_if_fault_occurred(&env
);
163 xmlrpc_env_clean(&env
);
167 /*=========================================================================
169 **=========================================================================
170 ** Slurp the body of the request into an xmlrpc_mem_block.
173 static xmlrpc_mem_block
*get_body (xmlrpc_env
*env
, size_t length
)
175 xmlrpc_mem_block
*result
;
179 XMLRPC_ASSERT_ENV_OK(env
);
181 /* Error-handling preconditions. */
184 /* XXX - Puke if length is too big. */
186 /* Allocate our memory block. */
187 result
= xmlrpc_mem_block_new(env
, length
);
188 XMLRPC_FAIL_IF_FAULT(env
);
189 contents
= XMLRPC_TYPED_MEM_BLOCK_CONTENTS(char, result
);
191 /* Get our data off the network.
192 ** XXX - Coercing 'size_t' to 'unsigned long' might be unsafe under
193 ** really weird circumstances. */
194 count
= fread(contents
, sizeof(char), length
, stdin
);
196 XMLRPC_FAIL2(env
, XMLRPC_INTERNAL_ERROR
,
197 "Expected %ld bytes, received %ld",
198 (unsigned long) length
, (unsigned long) count
);
201 if (env
->fault_occurred
) {
203 xmlrpc_mem_block_free(result
);
210 /*=========================================================================
211 ** xmlrpc_cgi_process_call
212 **=========================================================================
213 ** Parse the incoming XML-RPC call, find the right method, call it, and
214 ** serialize our response.
217 void xmlrpc_cgi_process_call (void)
220 char *method
, *type
, *length_str
;
222 xmlrpc_mem_block
*input
, *output
;
223 char *input_data
, *output_data
;
224 size_t input_size
, output_size
;
228 /* Error-handling preconditions. */
229 xmlrpc_env_init(&env
);
230 input
= output
= NULL
;
232 /* Set up a default error message. */
233 code
= 500; message
= "Internal Server Error";
235 /* Get our HTTP information from the environment. */
236 method
= getenv("REQUEST_METHOD");
237 type
= getenv("CONTENT_TYPE");
238 length_str
= getenv("CONTENT_LENGTH");
240 /* Perform some sanity checks. */
241 if (!method
|| 0 != strcmp(method
, "POST")) {
242 code
= 405; message
= "Method Not Allowed";
243 XMLRPC_FAIL(&env
, XMLRPC_INTERNAL_ERROR
, "Expected HTTP method POST");
245 if (!type
|| 0 != strcmp(type
, "text/xml")) {
246 code
= 400; message
= "Bad Request";
247 XMLRPC_FAIL(&env
, XMLRPC_INTERNAL_ERROR
, "Expected text/xml content");
250 code
= 411; message
= "Length Required";
251 XMLRPC_FAIL(&env
, XMLRPC_INTERNAL_ERROR
, "Content-length required");
254 /* Get our content length. */
255 length
= atoi(length_str
);
257 code
= 400; message
= "Bad Request";
258 XMLRPC_FAIL(&env
, XMLRPC_INTERNAL_ERROR
, "Content-length must be > 0");
261 /* SECURITY: Make sure our content length is legal.
262 ** XXX - We can cast 'input_len' because we know it's >= 0, yes? */
263 if ((size_t) length
> xmlrpc_limit_get(XMLRPC_XML_SIZE_LIMIT_ID
)) {
264 code
= 400; message
= "Bad Request";
265 XMLRPC_FAIL(&env
, XMLRPC_LIMIT_EXCEEDED_ERROR
,
266 "XML-RPC request too large");
270 input
= get_body(&env
, length
);
271 XMLRPC_FAIL_IF_FAULT(&env
);
272 input_data
= XMLRPC_TYPED_MEM_BLOCK_CONTENTS(char, input
);
273 input_size
= XMLRPC_TYPED_MEM_BLOCK_SIZE(char, input
);
275 /* Process our call. */
276 output
= xmlrpc_registry_process_call(&env
, registry
, NULL
,
277 input_data
, input_size
);
278 XMLRPC_FAIL_IF_FAULT(&env
);
279 output_data
= XMLRPC_TYPED_MEM_BLOCK_CONTENTS(char, output
);
280 output_size
= XMLRPC_TYPED_MEM_BLOCK_SIZE(char, output
);
283 send_xml(output_data
, output_size
);
287 xmlrpc_mem_block_free(input
);
289 xmlrpc_mem_block_free(output
);
291 if (env
.fault_occurred
)
292 send_error(code
, message
, &env
);
294 xmlrpc_env_clean(&env
);