summaryrefslogtreecommitdiffstats
path: root/Utilities/cmxmlrpc/xmlrpc_server_abyss.c
blob: 5964c33c0a1d70cd204baae98aa510be585849f7 (plain)
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
/* Copyright (C) 2001 by First Peer, Inc. 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 the
**    documentation and/or other materials provided with the distribution.
** 3. The name of the author may not be used to endorse or promote products
**    derived from this software without specific prior written permission. 
**  
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
** ANY EXPRESS 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 AUTHOR OR 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.
**
** There is more copyright information in the bottom half of this file. 
** Please see it for more details. */

#include "xmlrpc_config.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "abyss.h"

#include "xmlrpc.h"
#include "xmlrpc_server.h"
#include "xmlrpc_int.h"
#include "xmlrpc_server_abyss.h"
#include "xmlrpc_server_abyss_int.h"


/*=========================================================================
**  die_if_fault_occurred
**=========================================================================
**  If certain kinds of out-of-memory errors occur during server setup,
**  we want to quit and print an error.
*/

static void die_if_fault_occurred(xmlrpc_env *env) {
    if (env->fault_occurred) {
        fprintf(stderr, "Unexpected XML-RPC fault: %s (%d)\n",
                env->fault_string, env->fault_code);
        exit(1);
    }
}



/*=========================================================================
**  send_xml_data
**=========================================================================
**  Blast some XML data back to the client.
*/

static void 
send_xml_data (TSession * const r, 
               char *     const buffer, 
               uint64     const len) {

    const char * const http_cookie = NULL;
        /* This used to set http_cookie to getenv("HTTP_COOKIE"), but
           that doesn't make any sense -- environment variables are not
           appropriate for this.  So for now, cookie code is disabled.
           - Bryan 2004.10.03.
        */

    /* fwrite(buffer, sizeof(char), len, stderr); */

    /* XXX - Is it safe to chunk our response? */
    ResponseChunked(r);

    ResponseStatus(r, 200);
    
    if (http_cookie) {
        /* There's an auth cookie, so pass it back in the response. */

        char *cookie_response;
 
        cookie_response = malloc(10+strlen(http_cookie));
        sprintf(cookie_response, "auth=%s", http_cookie);
        
        /* Return abyss response. */
        ResponseAddField(r, "Set-Cookie", cookie_response);

        free(cookie_response);
    }   
 
  
    ResponseContentType(r, "text/xml; charset=\"utf-8\"");
    ResponseContentLength(r, len);
    
    ResponseWrite(r);
    
    HTTPWrite(r, buffer, len);
    HTTPWriteEnd(r);
}



/*=========================================================================
**  send_error
**=========================================================================
**  Send an error back to the client.
*/

static void
send_error(TSession *   const abyssSessionP, 
           unsigned int const status) {

    ResponseStatus(abyssSessionP, (uint16) status);
    ResponseError(abyssSessionP);
}



/*=========================================================================
**  get_buffer_data
**=========================================================================
**  Extract some data from the TConn's underlying input buffer. Do not
**  extract more than 'max'.
*/

static void
get_buffer_data(TSession * const r, 
                int        const max, 
                char **    const out_start, 
                int *      const out_len) {

    /* Point to the start of our data. */
    *out_start = &r->conn->buffer[r->conn->bufferpos];

    /* Decide how much data to retrieve. */
    *out_len = r->conn->buffersize - r->conn->bufferpos;
    if (*out_len > max)
        *out_len = max;

    /* Update our buffer position. */
    r->conn->bufferpos += *out_len;
}



/*=========================================================================
**  get_body
**=========================================================================
**  Slurp the body of the request into an xmlrpc_mem_block.
*/

static void
getBody(xmlrpc_env *        const envP,
        TSession *          const abyssSessionP,
        unsigned int        const contentSize,
        xmlrpc_mem_block ** const bodyP) {
/*----------------------------------------------------------------------------
   Get the entire body from the Abyss session and return it as the new
   memblock *bodyP.

   The first chunk of the body may already be in Abyss's buffer.  We
   retrieve that before reading more.
-----------------------------------------------------------------------------*/
    xmlrpc_mem_block * body;

    body = xmlrpc_mem_block_new(envP, 0);
    if (!envP->fault_occurred) {
        unsigned int bytesRead;
        char * chunkPtr;
        int chunkLen;

        bytesRead = 0;

        while (!envP->fault_occurred && bytesRead < contentSize) {
            get_buffer_data(abyssSessionP, contentSize - bytesRead, 
                            &chunkPtr, &chunkLen);
            bytesRead += chunkLen;

            XMLRPC_TYPED_MEM_BLOCK_APPEND(char, envP, body, 
                                          chunkPtr, chunkLen);
            
            if (bytesRead < contentSize) {
                /* Get the next chunk of data from the connection into the
                   buffer 
                */
                abyss_bool succeeded;
            
                /* Reset our read buffer & flush data from previous reads. */
                ConnReadInit(abyssSessionP->conn);
            
                /* Read more network data into our buffer. If we encounter
                   a timeout, exit immediately. We're very forgiving about
                   the timeout here. We allow a full timeout per network
                   read, which would allow somebody to keep a connection
                   alive nearly indefinitely. But it's hard to do anything
                   intelligent here without very complicated code. 
                */
                succeeded = ConnRead(abyssSessionP->conn,
                                     abyssSessionP->server->timeout);
                if (!succeeded)
                    xmlrpc_env_set_fault_formatted(
                        envP, XMLRPC_TIMEOUT_ERROR, "Timed out waiting for "
                        "client to send its POST data");
            }
        }
        if (envP->fault_occurred)
            xmlrpc_mem_block_free(body);
        else
            *bodyP = body;
    }
}



static void
storeCookies(TSession *     const httpRequestP,
             unsigned int * const httpErrorP) {
/*----------------------------------------------------------------------------
   Get the cookie settings from the HTTP headers and remember them for
   use in responses.
-----------------------------------------------------------------------------*/
    const char * const cookie = RequestHeaderValue(httpRequestP, "cookie");
    if (cookie) {
        /* 
           Setting the value in an environment variable doesn't make
           any sense.  So for now, cookie code is disabled.
           -Bryan 04.10.03.

        setenv("HTTP_COOKIE", cookie, 1);
        */
    }
    /* TODO: parse HTTP_COOKIE to find auth pair, if there is one */

    *httpErrorP = 0;
}




static void
validateContentType(TSession *     const httpRequestP,
                    unsigned int * const httpErrorP) {
/*----------------------------------------------------------------------------
   If the client didn't specify a content-type of "text/xml", return      
   "400 Bad Request".  We can't allow the client to default this header,
   because some firewall software may rely on all XML-RPC requests
   using the POST method and a content-type of "text/xml". 
-----------------------------------------------------------------------------*/
    const char * const content_type =
        RequestHeaderValue(httpRequestP, "content-type");
    if (content_type == NULL || strcmp(content_type, "text/xml") != 0)
        *httpErrorP = 400;
    else
        *httpErrorP = 0;
}



static void
processContentLength(TSession *     const httpRequestP,
                     unsigned int * const inputLenP,
                     unsigned int * const httpErrorP) {
/*----------------------------------------------------------------------------
  Make sure the content length is present and non-zero.  This is
  technically required by XML-RPC, but we only enforce it because we
  don't want to figure out how to safely handle HTTP < 1.1 requests
  without it.  If the length is missing, return "411 Length Required". 
-----------------------------------------------------------------------------*/
    const char * const content_length = 
        RequestHeaderValue(httpRequestP, "content-length");
    if (content_length == NULL)
        *httpErrorP = 411;
    else {
        int const contentLengthValue = atoi(content_length);
        if (contentLengthValue <= 0)
            *httpErrorP = 400;
        else {
            *httpErrorP = 0;
            *inputLenP = (unsigned int)contentLengthValue;
        }
    }
}


/****************************************************************************
    Abyss handlers (to be registered with and called by Abyss)
****************************************************************************/

/* XXX - This variable is *not* currently threadsafe. Once the server has
** been started, it must be treated as read-only. */
static xmlrpc_registry *global_registryP;

static const char * trace_abyss;

static void
processCall(TSession * const abyssSessionP,
            int        const inputLen) {
/*----------------------------------------------------------------------------
   Handle an RPC request.  This is an HTTP request that has the proper form
   to be one of our RPCs.
-----------------------------------------------------------------------------*/
    xmlrpc_env env;

    if (trace_abyss)
        fprintf(stderr, "xmlrpc_server_abyss RPC2 handler processing RPC.\n");

    xmlrpc_env_init(&env);

    /* SECURITY: Make sure our content length is legal.
       XXX - We can cast 'inputLen' because we know it's >= 0, yes? 
    */
    if ((size_t) inputLen > xmlrpc_limit_get(XMLRPC_XML_SIZE_LIMIT_ID))
        xmlrpc_env_set_fault_formatted(
            &env, XMLRPC_LIMIT_EXCEEDED_ERROR,
            "XML-RPC request too large (%d bytes)", inputLen);
    else {
        xmlrpc_mem_block *body;
        /* Read XML data off the wire. */
        getBody(&env, abyssSessionP, inputLen, &body);
        if (!env.fault_occurred) {
            xmlrpc_mem_block * output;
            /* Process the RPC. */
            output = xmlrpc_registry_process_call(
                &env, global_registryP, NULL, 
                XMLRPC_MEMBLOCK_CONTENTS(char, body),
                XMLRPC_MEMBLOCK_SIZE(char, body));
            if (!env.fault_occurred) {
            /* Send our the result. */
                send_xml_data(abyssSessionP, 
                              XMLRPC_MEMBLOCK_CONTENTS(char, output),
                              XMLRPC_MEMBLOCK_SIZE(char, output));
                
                XMLRPC_MEMBLOCK_FREE(char, output);
            }
            XMLRPC_MEMBLOCK_FREE(char, body);
        }
    }
    if (env.fault_occurred) {
        if (env.fault_code == XMLRPC_TIMEOUT_ERROR)
            send_error(abyssSessionP, 408); /* 408 Request Timeout */
        else
            send_error(abyssSessionP, 500); /* 500 Internal Server Error */
    }

    xmlrpc_env_clean(&env);
}



/*=========================================================================
**  xmlrpc_server_abyss_rpc2_handler
**=========================================================================
**  This handler processes all requests to '/RPC2'. See the header for
**  more documentation.
*/

xmlrpc_bool 
xmlrpc_server_abyss_rpc2_handler (TSession * const r) {

    xmlrpc_bool retval;

    if (trace_abyss)
        fprintf(stderr, "xmlrpc_server_abyss RPC2 handler called.\n");

    /* We handle only requests to /RPC2, the default XML-RPC URL.
       Everything else we pass through to other handlers. 
    */
    if (strcmp(r->uri, "/RPC2") != 0)
        retval = FALSE;
    else {
        retval = TRUE;

        /* We understand only the POST HTTP method.  For anything else, return
           "405 Method Not Allowed". 
        */
        if (r->method != m_post)
            send_error(r, 405);
        else {
            unsigned int httpError;
            storeCookies(r, &httpError);
            if (httpError)
                send_error(r, httpError);
            else {
                unsigned int httpError;
                validateContentType(r, &httpError);
                if (httpError)
                    send_error(r, httpError);
                else {
                    unsigned int httpError;
                    int inputLen;

                    processContentLength(r, &inputLen, &httpError);
                    if (httpError)
                        send_error(r, httpError);

                    processCall(r, inputLen);
                }
            }
        }
    }
    if (trace_abyss)
        fprintf(stderr, "xmlrpc_server_abyss RPC2 handler returning.\n");
    return retval;
}



/*=========================================================================
**  xmlrpc_server_abyss_default_handler
**=========================================================================
**  This handler returns a 404 Not Found for all requests. See the header
**  for more documentation.
*/

xmlrpc_bool 
xmlrpc_server_abyss_default_handler (TSession * const r) {
    send_error(r, 404);

    return TRUE;
}



/**************************************************************************
**
** The code below was adapted from the main.c file of the Abyss webserver
** project. In addition to the other copyrights on this file, the following
** code is also under this copyright:
**
** Copyright (C) 2000 by Moez Mahfoudh <mmoez@bigfoot.com>.
** 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 the
**    documentation and/or other materials provided with the distribution.
** 3. The name of the author may not be used to endorse or promote products
**    derived from this software without specific prior written permission.
** 
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
** ANY EXPRESS 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 AUTHOR OR 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.
**
**************************************************************************/

#include <time.h>
#include <fcntl.h>

#ifdef _WIN32
#include <io.h>
#else
/* Must check this
#include <sys/io.h>
*/
#endif  /* _WIN32 */

#ifdef _UNIX
#include <sys/signal.h>
#include <sys/wait.h>
#include <grp.h>
#endif


#ifdef _UNIX
static void 
sigterm(int const sig) {
    TraceExit("Signal %d received. Exiting...\n",sig);
}
#endif


#ifdef _UNIX
static void 
sigchld(int const sig ATTR_UNUSED) {
/*----------------------------------------------------------------------------
   This is a signal handler for a SIGCHLD signal (which informs us that
   one of our child processes has terminated).

   We respond by reaping the zombie process.

   Implementation note: In some systems, just setting the signal handler
   to SIG_IGN (ignore signal) does this.  In others, it doesn't.
-----------------------------------------------------------------------------*/
    pid_t pid;
    int status;
    
    /* Reap defunct children until there aren't any more. */
    for (;;) {
        pid = waitpid( (pid_t) -1, &status, WNOHANG );
    
        /* none left */
        if (pid==0)
            break;
    
        if (pid<0) {
            /* because of ptrace */
            if (errno==EINTR)   
                continue;
        
            break;
        }
    }
}
#endif /* _UNIX */

static TServer globalSrv;
    /* When you use the old interface (xmlrpc_server_abyss_init(), etc.),
       this is the Abyss server to which they refer.  Obviously, there can be
       only one Abyss server per program using this interface.
    */


void 
xmlrpc_server_abyss_init(int          const flags ATTR_UNUSED, 
                         const char * const config_file) {

    DateInit();
    MIMETypeInit();

    ServerCreate(&globalSrv, "XmlRpcServer", 8080, DEFAULT_DOCS, NULL);
    
    ConfReadServerFile(config_file, &globalSrv);

    xmlrpc_server_abyss_init_registry();
        /* Installs /RPC2 handler and default handler that use the
           built-in registry.
        */

    ServerInit(&globalSrv);
}



static void
setupSignalHandlers(void) {
#ifdef _UNIX
    struct sigaction mysigaction;
    
    sigemptyset(&mysigaction.sa_mask);
    mysigaction.sa_flags = 0;

    /* These signals abort the program, with tracing */
    mysigaction.sa_handler = sigterm;
    sigaction(SIGTERM, &mysigaction, NULL);
    sigaction(SIGINT,  &mysigaction, NULL);
    sigaction(SIGHUP,  &mysigaction, NULL);
    sigaction(SIGUSR1, &mysigaction, NULL);

    /* This signal indicates connection closed in the middle */
    mysigaction.sa_handler = SIG_IGN;
    sigaction(SIGPIPE, &mysigaction, NULL);
    
    /* This signal indicates a child process (request handler) has died */
    mysigaction.sa_handler = sigchld;
    sigaction(SIGCHLD, &mysigaction, NULL);
#endif
}    



static void
runServer(TServer *  const srvP,
          runfirstFn const runfirst,
          void *     const runfirstArg) {

    setupSignalHandlers();

#ifdef _UNIX
    /* Become a daemon */
    switch (fork()) {
    case 0:
        break;
    case -1:
        TraceExit("Unable to become a daemon");
    default:
        exit(0);
    };
    
    setsid();

    /* Change the current user if we are root */
    if (getuid()==0) {
        if (srvP->uid == (uid_t)-1)
            TraceExit("Can't run under root privileges.  "
                      "Please add a User option in your "
                      "Abyss configuration file.");

#ifdef HAVE_SETGROUPS   
        if (setgroups(0,NULL)==(-1))
            TraceExit("Failed to setup the group.");
        if (srvP->gid != (gid_t)-1)
            if (setgid(srvP->gid)==(-1))
                TraceExit("Failed to change the group.");
#endif
        
        if (setuid(srvP->uid) == -1)
            TraceExit("Failed to change the user.");
    };
    
    if (srvP->pidfile!=(-1)) {
        char z[16];
    
        sprintf(z,"%d",getpid());
        FileWrite(&srvP->pidfile,z,strlen(z));
        FileClose(&srvP->pidfile);
    };
#endif
    
    /* We run the user supplied runfirst after forking, but before accepting
       connections (helpful when running with threads)
    */
    if (runfirst)
        runfirst(runfirstArg);

    ServerRun(srvP);

    /* We can't exist here because ServerRun doesn't return */
    XMLRPC_ASSERT(FALSE);
}



void 
xmlrpc_server_abyss_run_first(runfirstFn const runfirst,
                              void *     const runfirstArg) {
    
    runServer(&globalSrv, runfirst, runfirstArg);
}



void 
xmlrpc_server_abyss_run(void) {
    runServer(&globalSrv, NULL, NULL);
}



void
xmlrpc_server_abyss_set_handlers(TServer *         const srvP,
                                 xmlrpc_registry * const registryP) {

    /* Abyss ought to have a way to register with a handler an argument
       that gets passed to the handler every time it is called.  That's
       where we should put the registry handle.  But we don't find such
       a thing in Abyss, so we use the global variable 'global_registryP'.
    */
    global_registryP = registryP;

    trace_abyss = getenv("XMLRPC_TRACE_ABYSS");
                                 
    ServerAddHandler(srvP, xmlrpc_server_abyss_rpc2_handler);
    ServerDefaultHandler(srvP, xmlrpc_server_abyss_default_handler);
}



void
xmlrpc_server_abyss(xmlrpc_env *                      const envP,
                    const xmlrpc_server_abyss_parms * const parmsP,
                    unsigned int                      const parm_size) {
 
    XMLRPC_ASSERT_ENV_OK(envP);

    if (parm_size < XMLRPC_APSIZE(registryP))
        xmlrpc_env_set_fault_formatted(
            envP, XMLRPC_INTERNAL_ERROR,
            "You must specify members at least up through "
            "'registryP' in the server parameters argument.  "
            "That would mean the parameter size would be >= %u "
            "but you specified a size of %u",
            XMLRPC_APSIZE(registryP), parm_size);
    else {
        TServer srv;
        runfirstFn runfirst;
        void * runfirstArg;

        DateInit();
        MIMETypeInit();
        
        ServerCreate(&srv, "XmlRpcServer", 8080, DEFAULT_DOCS, NULL);
        
        ConfReadServerFile(parmsP->config_file_name, &srv);
        
        xmlrpc_server_abyss_set_handlers(&srv, parmsP->registryP);
        
        ServerInit(&srv);

        if (parm_size >= XMLRPC_APSIZE(runfirst_arg)) {
            runfirst    = parmsP->runfirst;
            runfirstArg = parmsP->runfirst_arg;
        } else {
            runfirst    = NULL;
            runfirstArg = NULL;
        }
        runServer(&srv, runfirst, runfirstArg);
    }
}



/*=========================================================================
**  XML-RPC Server Method Registry
**=========================================================================
**  A simple front-end to our method registry.
*/

/* XXX - This variable is *not* currently threadsafe. Once the server has
** been started, it must be treated as read-only. */
static xmlrpc_registry *builtin_registryP;

void 
xmlrpc_server_abyss_init_registry(void) {

    /* This used to just create the registry and Caller would be
       responsible for adding the handlers that use it.

       But that isn't very modular -- the handlers and registry go
       together; there's no sense in using the built-in registry and
       not the built-in handlers because if you're custom building
       something, you can just make your own regular registry.  So now
       we tie them together, and we don't export our handlers.  
    */
    xmlrpc_env env;

    xmlrpc_env_init(&env);
    builtin_registryP = xmlrpc_registry_new(&env);
    die_if_fault_occurred(&env);
    xmlrpc_env_clean(&env);

    xmlrpc_server_abyss_set_handlers(&globalSrv, builtin_registryP);
}



xmlrpc_registry *
xmlrpc_server_abyss_registry(void) {

    /* This is highly deprecated.  If you want to mess with a registry,
       make your own with xmlrpc_registry_new() -- don't mess with the
       internal one.
    */
    return builtin_registryP;
}



/* A quick & easy shorthand for adding a method. */
void 
xmlrpc_server_abyss_add_method (char *        const method_name,
                                xmlrpc_method const method,
                                void *        const user_data) {
    xmlrpc_env env;

    xmlrpc_env_init(&env);
    xmlrpc_registry_add_method(&env, builtin_registryP, NULL, method_name,
                               method, user_data);
    die_if_fault_occurred(&env);
    xmlrpc_env_clean(&env);
}



void
xmlrpc_server_abyss_add_method_w_doc (char *        const method_name,
                                      xmlrpc_method const method,
                                      void *        const user_data,
                                      char *        const signature,
                                      char *        const help) {

    xmlrpc_env env;
    xmlrpc_env_init(&env);
    xmlrpc_registry_add_method_w_doc(
        &env, builtin_registryP, NULL, method_name,
        method, user_data, signature, help);
    die_if_fault_occurred(&env);
    xmlrpc_env_clean(&env);    
}