File size: 12,863 Bytes
fd49381 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 |
/*=========================================================================
XML-RPC server method registry
Method services
===========================================================================
These are the functions that implement the method objects that
the XML-RPC method registry uses.
By Bryan Henderson, December 2006.
Contributed to the public domain by its author.
=========================================================================*/
#define _XOPEN_SOURCE 600 /* Make sure strdup() is in <string.h> */
#include "xmlrpc_config.h"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "bool.h"
#include "mallocvar.h"
#include "xmlrpc-c/base_int.h"
#include "xmlrpc-c/string_int.h"
#include "xmlrpc-c/base.h"
#include "registry.h"
#include "method.h"
static void
signatureDestroy(struct xmlrpc_signature * const signatureP) {
if (signatureP->argList)
free((void*)signatureP->argList);
free(signatureP);
}
static void
translateTypeSpecifierToName(xmlrpc_env * const envP,
char const typeSpecifier,
const char ** const typeNameP) {
switch (typeSpecifier) {
case 'i': *typeNameP = "int"; break;
case 'b': *typeNameP = "boolean"; break;
case 'd': *typeNameP = "double"; break;
case 's': *typeNameP = "string"; break;
case '8': *typeNameP = "dateTime.iso8601"; break;
case '6': *typeNameP = "base64"; break;
case 'S': *typeNameP = "struct"; break;
case 'A': *typeNameP = "array"; break;
case 'n': *typeNameP = "nil"; break;
case 'I': *typeNameP = "i8"; break;
default:
xmlrpc_faultf(envP,
"Method registry contains invalid signature "
"data. It contains the type specifier '%c'",
typeSpecifier);
*typeNameP = NULL; /* quiet compiler warning */
}
}
/* MSVC 8 complains that const char ** is incompatible with void * in the
REALLOCARRAY. It's not.
*/
#pragma warning(push)
#pragma warning(disable:4090)
static void
makeRoomInArgList(xmlrpc_env * const envP,
struct xmlrpc_signature * const signatureP,
unsigned int const minArgCount) {
if (signatureP->argListSpace < minArgCount) {
REALLOCARRAY(signatureP->argList, minArgCount);
if (signatureP->argList == NULL) {
xmlrpc_faultf(envP, "Couldn't get memory for a argument list for "
"a method signature with %u arguments", minArgCount);
signatureP->argListSpace = 0;
}
}
}
#pragma warning(pop)
static void
parseArgumentTypeSpecifiers(xmlrpc_env * const envP,
const char * const startP,
struct xmlrpc_signature * const signatureP,
const char ** const nextPP) {
const char * cursorP;
cursorP = startP; /* start at the beginning */
while (!envP->fault_occurred && *cursorP != ',' && *cursorP != '\0') {
const char * typeName;
translateTypeSpecifierToName(envP, *cursorP, &typeName);
if (!envP->fault_occurred) {
++cursorP;
makeRoomInArgList(envP, signatureP, signatureP->argCount + 1);
signatureP->argList[signatureP->argCount++] = typeName;
}
}
if (!envP->fault_occurred) {
if (*cursorP) {
XMLRPC_ASSERT(*cursorP == ',');
++cursorP; /* Move past the signature and comma */
}
}
if (envP->fault_occurred)
free((void*)signatureP->argList);
*nextPP = cursorP;
}
static void
parseOneSignature(xmlrpc_env * const envP,
const char * const startP,
struct xmlrpc_signature ** const signaturePP,
const char ** const nextPP) {
/*----------------------------------------------------------------------------
Parse one signature from the signature string that starts at 'startP'.
Return that signature as a signature object *signaturePP.
Return as *nextP the location in the signature string of the next
signature (i.e. right after the next comma). If there is no next
signature (the string ends before any comma), make it point to the
terminating NUL.
-----------------------------------------------------------------------------*/
struct xmlrpc_signature * signatureP;
MALLOCVAR(signatureP);
if (signatureP == NULL)
xmlrpc_faultf(envP, "Couldn't get memory for signature");
else {
const char * cursorP;
signatureP->argListSpace = 0; /* Start with no argument space */
signatureP->argList = NULL; /* Nothing allocated yet */
signatureP->argCount = 0; /* Start with no arguments */
cursorP = startP; /* start at the beginning */
if (*cursorP == ',' || *cursorP == '\0')
xmlrpc_faultf(envP, "empty signature (a signature "
"must have at least return value type)");
else {
translateTypeSpecifierToName(envP, *cursorP, &signatureP->retType);
++cursorP;
if (*cursorP != ':')
xmlrpc_faultf(envP, "No colon (':') after "
"the result type specifier");
else {
++cursorP;
parseArgumentTypeSpecifiers(envP, cursorP, signatureP, nextPP);
}
}
if (envP->fault_occurred)
free(signatureP);
}
*signaturePP = signatureP;
}
static void
destroySignatures(struct xmlrpc_signature * const firstSignatureP) {
struct xmlrpc_signature * p;
struct xmlrpc_signature * nextP;
for (p = firstSignatureP; p; p = nextP) {
nextP = p->nextP;
signatureDestroy(p);
}
}
static void
listSignatures(xmlrpc_env * const envP,
const char * const sigListString,
struct xmlrpc_signature ** const firstSignaturePP) {
struct xmlrpc_signature ** p;
const char * cursorP;
*firstSignaturePP = NULL; /* Start with empty list */
p = firstSignaturePP;
cursorP = &sigListString[0];
while (!envP->fault_occurred && *cursorP != '\0') {
struct xmlrpc_signature * signatureP;
parseOneSignature(envP, cursorP, &signatureP, &cursorP);
/* cursorP now points at next signature in the list or the
terminating NUL.
*/
if (!envP->fault_occurred) {
signatureP->nextP = NULL;
*p = signatureP;
p = &signatureP->nextP;
}
}
if (envP->fault_occurred)
destroySignatures(*firstSignaturePP);
}
static void
signatureListCreate(xmlrpc_env * const envP,
const char * const sigListString,
xmlrpc_signatureList ** const signatureListPP) {
xmlrpc_signatureList * signatureListP;
XMLRPC_ASSERT_ENV_OK(envP);
MALLOCVAR(signatureListP);
if (signatureListP == NULL)
xmlrpc_faultf(envP, "Could not allocate memory for signature list");
else {
signatureListP->firstSignatureP = NULL;
if (sigListString == NULL || xmlrpc_streq(sigListString, "?")) {
/* No signatures -- leave the list empty */
} else {
listSignatures(envP, sigListString,
&signatureListP->firstSignatureP);
if (!envP->fault_occurred) {
if (!signatureListP->firstSignatureP)
xmlrpc_faultf(envP, "Signature string is empty.");
if (envP->fault_occurred)
destroySignatures(signatureListP->firstSignatureP);
}
}
if (envP->fault_occurred)
free(signatureListP);
*signatureListPP = signatureListP;
}
}
static void
signatureListDestroy(xmlrpc_signatureList * const signatureListP) {
destroySignatures(signatureListP->firstSignatureP);
free(signatureListP);
}
static void
makeSignatureList(xmlrpc_env * const envP,
const char * const signatureString,
xmlrpc_signatureList ** const signatureListPP) {
xmlrpc_env env;
xmlrpc_env_init(&env);
signatureListCreate(&env, signatureString, signatureListPP);
if (env.fault_occurred)
xmlrpc_faultf(envP, "Can't interpret signature string '%s'. %s",
signatureString, env.fault_string);
xmlrpc_env_clean(&env);
}
void
xmlrpc_methodCreate(xmlrpc_env * const envP,
xmlrpc_method1 methodFnType1,
xmlrpc_method2 methodFnType2,
void * const userData,
const char * const signatureString,
const char * const helpText,
size_t const stackSize,
xmlrpc_methodInfo ** const methodPP) {
xmlrpc_methodInfo * methodP;
XMLRPC_ASSERT_ENV_OK(envP);
MALLOCVAR(methodP);
if (methodP == NULL)
xmlrpc_faultf(envP, "Unable to allocate storage for a method "
"descriptor");
else {
methodP->methodFnType1 = methodFnType1;
methodP->methodFnType2 = methodFnType2;
methodP->userData = userData;
methodP->helpText = xmlrpc_strdupsol(helpText);
methodP->stackSize = stackSize;
makeSignatureList(envP, signatureString, &methodP->signatureListP);
if (envP->fault_occurred) {
xmlrpc_strfree(methodP->helpText);
free(methodP);
}
*methodPP = methodP;
}
}
void
xmlrpc_methodDestroy(xmlrpc_methodInfo * const methodP) {
signatureListDestroy(methodP->signatureListP);
xmlrpc_strfree(methodP->helpText);
free(methodP);
}
void
xmlrpc_methodListCreate(xmlrpc_env * const envP,
xmlrpc_methodList ** const methodListPP) {
xmlrpc_methodList * methodListP;
XMLRPC_ASSERT_ENV_OK(envP);
MALLOCVAR(methodListP);
if (methodListP == NULL)
xmlrpc_faultf(envP, "Couldn't allocate method list descriptor");
else {
methodListP->firstMethodP = NULL;
methodListP->lastMethodP = NULL;
*methodListPP = methodListP;
}
}
void
xmlrpc_methodListDestroy(xmlrpc_methodList * methodListP) {
xmlrpc_methodNode * p;
xmlrpc_methodNode * nextP;
for (p = methodListP->firstMethodP; p; p = nextP) {
nextP = p->nextP;
xmlrpc_methodDestroy(p->methodP);
xmlrpc_strfree(p->methodName);
free(p);
}
free(methodListP);
}
void
xmlrpc_methodListLookupByName(xmlrpc_methodList * const methodListP,
const char * const methodName,
xmlrpc_methodInfo ** const methodPP) {
/* We do a simple linear lookup along a linked list.
If speed is important, we can make this a binary tree instead.
*/
xmlrpc_methodNode * p;
xmlrpc_methodInfo * methodP;
for (p = methodListP->firstMethodP, methodP = NULL;
p && !methodP;
p = p->nextP) {
if (xmlrpc_streq(p->methodName, methodName))
methodP = p->methodP;
}
*methodPP = methodP;
}
void
xmlrpc_methodListAdd(xmlrpc_env * const envP,
xmlrpc_methodList * const methodListP,
const char * const methodName,
xmlrpc_methodInfo * const methodP) {
xmlrpc_methodInfo * existingMethodP;
XMLRPC_ASSERT_ENV_OK(envP);
xmlrpc_methodListLookupByName(methodListP, methodName, &existingMethodP);
if (existingMethodP)
xmlrpc_faultf(envP, "Method named '%s' already registered",
methodName);
else {
xmlrpc_methodNode * methodNodeP;
MALLOCVAR(methodNodeP);
if (methodNodeP == NULL)
xmlrpc_faultf(envP, "Couldn't allocate method node");
else {
methodNodeP->methodName = strdup(methodName);
methodNodeP->methodP = methodP;
methodNodeP->nextP = NULL;
if (!methodListP->firstMethodP)
methodListP->firstMethodP = methodNodeP;
if (methodListP->lastMethodP)
methodListP->lastMethodP->nextP = methodNodeP;
methodListP->lastMethodP = methodNodeP;
}
}
}
|