Annotation of parser3/src/targets/apache13/mod_parser3.c, revision 1.2
1.2 ! paf 1: /** @file
! 2: Parser: apache 1.3 module, part, compiled by Apache.
! 3:
! 4: Copyright (c) 2001-2003 ArtLebedev Group (http://www.artlebedev.com)
! 5: Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
! 6: */
! 7:
! 8: static const char* IDENT_MOD_PARSER3_C="$Date: 2003/08/19 13:52:18 $";
! 9:
! 10: #if _MSC_VER
! 11: #include <winsock2.h>
! 12: #endif
! 13:
! 14: #include "httpd.h"
! 15: #include "http_config.h"
! 16: #include "http_core.h"
! 17: #include "http_log.h"
! 18: #include "http_main.h"
! 19: #include "http_protocol.h"
! 20: #include "util_script.h"
! 21: #include "ap_md5.h"
! 22: #include "ap_alloc.h"
! 23:
! 24: #include "pa_httpd.h"
! 25:
! 26: /*
! 27: * Declare ourselves so the configuration routines can find and know us.
! 28: * We'll fill it in at the end of the module.
! 29: */
! 30: extern module MODULE_VAR_EXPORT parser3_module;
! 31:
! 32: /*
! 33: * Locate our directory configuration record for the current request.
! 34: */
! 35: static Parser_module_config *our_dconfig(request_rec *r) {
! 36: return (Parser_module_config *)
! 37: ap_get_module_config(r->per_dir_config, &parser3_module);
! 38: }
! 39:
! 40: static const char* cmd_parser_config(cmd_parms *cmd, void *mconfig, char *file_spec) {
! 41: Parser_module_config *cfg = (Parser_module_config *) mconfig;
! 42:
! 43: // remember assigned filespec into cfg
! 44: cfg->parser_config_filespec=file_spec;
! 45:
! 46: return NULL;
! 47: }
! 48: static const char* cmd_parser_status_allowed(cmd_parms *cmd, void *mconfig, char *file_spec) {
! 49: //_asm int 3;
! 50: Parser_module_config *cfg = (Parser_module_config *) mconfig;
! 51:
! 52: cfg->parser_status_allowed=1;
! 53:
! 54: return NULL;
! 55: }
! 56:
! 57:
! 58: /*--------------------------------------------------------------------------*/
! 59: /* */
! 60: /* Now we declare our content handlers, which are invoked when the server */
! 61: /* encounters a document which our module is supposed to have a chance to */
! 62: /* see. (See mod_mime's SetHandler and AddHandler directives, and the */
! 63: /* mod_info and mod_status examples, for more details.) */
! 64: /* */
! 65: /* Since content handlers are dumping data directly into the connexion */
! 66: /* (using the r*() routines, such as rputs() and rprintf()) without */
! 67: /* intervention by other parts of the server, they need to make */
! 68: /* sure any accumulated HTTP headers are sent first. This is done by */
! 69: /* calling send_http_header(). Otherwise, no header will be sent at all, */
! 70: /* and the output sent to the client will actually be HTTP-uncompliant. */
! 71: /*--------------------------------------------------------------------------*/
! 72: /*
! 73: * Sample content handler. All this does is display the call list that has
! 74: * been built up so far.
! 75: *
! 76: * The return value instructs the caller concerning what happened and what to
! 77: * do next:
! 78: * OK ("we did our thing")
! 79: * DECLINED ("this isn't something with which we want to get involved")
! 80: * HTTP_mumble ("an error status should be reported")
! 81: */
! 82:
! 83:
! 84: /*--------------------------------------------------------------------------*/
! 85: /* */
! 86: /* Now let's declare routines for each of the callback phase in order. */
! 87: /* (That's the order in which they're listed in the callback list, *not */
! 88: /* the order in which the server calls them! See the command_rec */
! 89: /* declaration near the bottom of this file.) Note that these may be */
! 90: /* called for situations that don't relate primarily to our function - in */
! 91: /* other words, the fixup handler shouldn't assume that the request has */
! 92: /* to do with "example" stuff. */
! 93: /* */
! 94: /* With the exception of the content handler, all of our routines will be */
! 95: /* called for each request, unless an earlier handler from another module */
! 96: /* aborted the sequence. */
! 97: /* */
! 98: /* Handlers that are declared as "int" can return the following: */
! 99: /* */
! 100: /* OK Handler accepted the request and did its thing with it. */
! 101: /* DECLINED Handler took no action. */
! 102: /* HTTP_mumble Handler looked at request and found it wanting. */
! 103: /* */
! 104: /* What the server does after calling a module handler depends upon the */
! 105: /* handler's return value. In all cases, if the handler returns */
! 106: /* DECLINED, the server will continue to the next module with an handler */
! 107: /* for the current phase. However, if the handler return a non-OK, */
! 108: /* non-DECLINED status, the server aborts the request right there. If */
! 109: /* the handler returns OK, the server's next action is phase-specific; */
! 110: /* see the individual handler comments below for details. */
! 111: /* */
! 112: /*--------------------------------------------------------------------------*/
! 113:
! 114: static int parser_handler(request_rec *ar) {
! 115: // record clone
! 116: pa_request_rec lr={
! 117: ar,
! 118: ar->pool,
! 119: ar->header_only,
! 120: &ar->status,
! 121: ar->method,
! 122: ar->headers_out,
! 123: ar->subprocess_env,
! 124: ar->content_type,
! 125: ar->uri,
! 126: ar->filename,
! 127: ar->path_info,
! 128: ar->args,
! 129: &ar->finfo
! 130: };
! 131:
! 132: // config
! 133: Parser_module_config *dcfg=our_dconfig(ar);
! 134:
! 135: return pa_parser_handler(&lr, dcfg);
! 136: }
! 137:
! 138: /*
! 139: * This function is called during server initialisation. Any information
! 140: * that needs to be recorded must be in static cells, since there's no
! 141: * configuration record.
! 142: *
! 143: * There is no return value.
! 144: */
! 145:
! 146: static void parser_server_init(server_rec *s, pool *p) {
! 147: #if MODULE_MAGIC_NUMBER >= 19980527
! 148: ap_add_version_component(pa_version());
! 149: #endif
! 150:
! 151: /*
! 152: * Set up any module cells that ought to be initialised.
! 153: */
! 154: pa_setup_module_cells();
! 155: }
! 156:
! 157: /*
! 158: * This function gets called to create a per-directory configuration
! 159: * record. This will be called for the "default" server environment, and for
! 160: * each directory for which the parser finds any of our directives applicable.
! 161: * If a directory doesn't have any of our directives involved (i.e., they
! 162: * aren't in the .htaccess file, or a <Location>, <Directory>, or related
! 163: * block), this routine will *not* be called - the configuration for the
! 164: * closest ancestor is used.
! 165: *
! 166: * The return value is a pointer to the created module-specific
! 167: * structure.
! 168: */
! 169: static void *parser_create_dir_config(pool *p, char *dirspec) {
! 170: //_asm int 3;
! 171: /*
! 172: * Allocate the space for our record from the pool supplied.
! 173: */
! 174: Parser_module_config *cfg=
! 175: (Parser_module_config *) ap_pcalloc(p, sizeof(Parser_module_config));
! 176: /*
! 177: * Now fill in the defaults. If there are any `parent' configuration
! 178: * records, they'll get merged as part of a separate callback.
! 179: */
! 180:
! 181: return (void *) cfg;
! 182: }
! 183:
! 184: /*
! 185: * This function gets called to merge two per-directory configuration
! 186: * records. This is typically done to cope with things like .htaccess files
! 187: * or <Location> directives for directories that are beneath one for which a
! 188: * configuration record was already created. The routine has the
! 189: * responsibility of creating a new record and merging the contents of the
! 190: * other two into it appropriately. If the module doesn't declare a merge
! 191: * routine, the record for the closest ancestor location (that has one) is
! 192: * used exclusively.
! 193: *
! 194: * The routine MUST NOT modify any of its arguments!
! 195: *
! 196: * The return value is a pointer to the created module-specific structure
! 197: * containing the merged values.
! 198:
! 199: 20011126 paf: noticed, that this is called even on virtual root merge with something "parent",
! 200: while thought that that is part of merge_server...
! 201:
! 202: */
! 203: static void *parser_merge_dir_config(pool *p, void *parent_conf,
! 204: void *newloc_conf) {
! 205: //_asm int 3;
! 206: Parser_module_config *merged_config =
! 207: (Parser_module_config *) ap_pcalloc(p, sizeof(Parser_module_config));
! 208: Parser_module_config *pconf = (Parser_module_config *) parent_conf;
! 209: Parser_module_config *nconf = (Parser_module_config *) newloc_conf;
! 210:
! 211: merged_config->parser_config_filespec = ap_pstrdup(p, nconf->parser_config_filespec?
! 212: nconf->parser_config_filespec:pconf->parser_config_filespec);
! 213: merged_config->parser_status_allowed=
! 214: pconf->parser_status_allowed ||
! 215: nconf->parser_status_allowed;
! 216:
! 217: /*
! 218: * Some things get copied directly from the more-specific record, rather
! 219: * than getting merged.
! 220: */
! 221:
! 222: return (void *) merged_config;
! 223: }
! 224:
! 225: /*
! 226: * This function gets called to create a per-server configuration
! 227: * record. It will always be called for the "default" server.
! 228: *
! 229: * The return value is a pointer to the created module-specific
! 230: * structure.
! 231: */
! 232: static void *parser_create_server_config(pool *p, server_rec *s) {
! 233: //_asm int 3;
! 234: /*
! 235: * As with the parser_create_dir_config() routine, we allocate and fill
! 236: * in an empty record.
! 237: */
! 238: Parser_module_config *cfg=
! 239: (Parser_module_config *) ap_pcalloc(p, sizeof(Parser_module_config));
! 240:
! 241: return (void *) cfg;
! 242: }
! 243:
! 244: /*
! 245: * This function gets called to merge two per-server configuration
! 246: * records. This is typically done to cope with things like virtual hosts and
! 247: * the default server configuration The routine has the responsibility of
! 248: * creating a new record and merging the contents of the other two into it
! 249: * appropriately. If the module doesn't declare a merge routine, the more
! 250: * specific existing record is used exclusively.
! 251: *
! 252: * The routine MUST NOT modify any of its arguments!
! 253: *
! 254: * The return value is a pointer to the created module-specific structure
! 255: * containing the merged values.
! 256: */
! 257: static void *parser_merge_server_config(pool *p, void *server1_conf,
! 258: void *server2_conf)
! 259: {
! 260: //_asm int 3;
! 261:
! 262: Parser_module_config *merged_config =
! 263: (Parser_module_config *) ap_pcalloc(p, sizeof(Parser_module_config));
! 264: Parser_module_config *s1conf = (Parser_module_config *) server1_conf;
! 265: Parser_module_config *s2conf = (Parser_module_config *) server2_conf;
! 266:
! 267: /*
! 268: * Our inheritance rules are our own, and part of our module's semantics.
! 269: * Basically, just note whence we came.
! 270: */
! 271: merged_config->parser_config_filespec = ap_pstrdup(p, s2conf->parser_config_filespec?
! 272: s2conf->parser_config_filespec:s1conf->parser_config_filespec);
! 273: merged_config->parser_status_allowed=
! 274: s1conf->parser_status_allowed ||
! 275: s2conf->parser_status_allowed;
! 276:
! 277: return (void *) merged_config;
! 278: }
! 279:
! 280: /*
! 281: * This routine gives our module an opportunity to translate the URI into an
! 282: * actual filename. If we don't do anything special, the server's default
! 283: * rules (Alias directives and the like) will continue to be followed.
! 284: *
! 285: * The return value is OK, DECLINED, or HTTP_mumble. If we return OK, no
! 286: * further modules are called for this phase.
! 287: */
! 288: static int parser_translate_handler(request_rec *r) {
! 289: Parser_module_config *cfg=our_dconfig(r);
! 290: return DECLINED;
! 291: }
! 292:
! 293: /*
! 294: * This routine is called to check the authentication information sent with
! 295: * the request (such as looking up the user in a database and verifying that
! 296: * the [encrypted] password sent matches the one in the database).
! 297: *
! 298: * The return value is OK, DECLINED, or some HTTP_mumble error (typically
! 299: * HTTP_UNAUTHORIZED). If we return OK, no other modules are given a chance
! 300: * at the request during this phase.
! 301: */
! 302: static int parser_check_user_id(request_rec *r) {
! 303: Parser_module_config *cfg=our_dconfig(r);
! 304: return DECLINED;
! 305: }
! 306:
! 307: /*
! 308: * This routine is called to check to see if the resource being requested
! 309: * requires authorisation.
! 310: *
! 311: * The return value is OK, DECLINED, or HTTP_mumble. If we return OK, no
! 312: * other modules are called during this phase.
! 313: *
! 314: * If *all* modules return DECLINED, the request is aborted with a server
! 315: * error.
! 316: */
! 317: static int parser_auth_checker(request_rec *r) {
! 318: Parser_module_config *cfg=our_dconfig(r);
! 319: return DECLINED;
! 320: }
! 321:
! 322: /*
! 323: * This routine is called to check for any module-specific restrictions placed
! 324: * upon the requested resource. (See the mod_access module for an example.)
! 325: *
! 326: * The return value is OK, DECLINED, or HTTP_mumble. All modules with an
! 327: * handler for this phase are called regardless of whether their predecessors
! 328: * return OK or DECLINED. The first one to return any other status, however,
! 329: * will abort the sequence (and the request) as usual.
! 330: */
! 331: static int parser_access_checker(request_rec *r) {
! 332:
! 333: Parser_module_config *cfg=our_dconfig(r);
! 334: return DECLINED;
! 335: }
! 336:
! 337: /*--------------------------------------------------------------------------*/
! 338: /* */
! 339: /* All of the routines have been declared now. Here's the list of */
! 340: /* directives specific to our module, and information about where they */
! 341: /* may appear and how the command parser should pass them to us for */
! 342: /* processing. Note that care must be taken to ensure that there are NO */
! 343: /* collisions of directive names between modules. */
! 344: /* */
! 345: /*--------------------------------------------------------------------------*/
! 346: /*
! 347: * List of directives specific to our module.
! 348: */
! 349: static const command_rec parser_cmds[] =
! 350: {
! 351: {
! 352: "ParserConfig", /* directive name */
! 353: (const char* (*)(void))((void *)cmd_parser_config), // config action routine
! 354: (void*)0, /* argument to include in call */
! 355: (int)(OR_OPTIONS), /* where available */
! 356: TAKE1, /* arguments */
! 357: "Parser config filespec" // directive description
! 358: },
! 359: {
! 360: "ParserStatusAllowed", /* directive name */
! 361: (const char* (*)(void))((void *)cmd_parser_status_allowed), // config action routine
! 362: (void*)0, /* argument to include in call */
! 363: (int)(ACCESS_CONF), /* where available */
! 364: NO_ARGS, /* arguments */
! 365: "Parser status class can be used" // directive description
! 366: },
! 367: {NULL}
! 368: };
! 369:
! 370: /*--------------------------------------------------------------------------*/
! 371: /* */
! 372: /* Now the list of content handlers available from this module. */
! 373: /* */
! 374: /*--------------------------------------------------------------------------*/
! 375: /*
! 376: * List of content handlers our module supplies. Each handler is defined by
! 377: * two parts: a name by which it can be referenced (such as by
! 378: * {Add,Set}Handler), and the actual routine name. The list is terminated by
! 379: * a NULL block, since it can be of variable length.
! 380: *
! 381: * Note that content-handlers are invoked on a most-specific to least-specific
! 382: * basis; that is, a handler that is declared for "text/plain" will be
! 383: * invoked before one that was declared for "text / *". Note also that
! 384: * if a content-handler returns anything except DECLINED, no other
! 385: * content-handlers will be called.
! 386: */
! 387: static const handler_rec parser_handlers[] =
! 388: {
! 389: {"parser3-handler", parser_handler},
! 390: {NULL}
! 391: };
! 392:
! 393: /*--------------------------------------------------------------------------*/
! 394: /* */
! 395: /* Finally, the list of callback routines and data structures that */
! 396: /* provide the hooks into our module from the other parts of the server. */
! 397: /* */
! 398: /*--------------------------------------------------------------------------*/
! 399: /*
! 400: * Module definition for configuration. If a particular callback is not
! 401: * needed, replace its routine name below with the word NULL.
! 402: *
! 403: * The number in brackets indicates the order in which the routine is called
! 404: * during request processing. Note that not all routines are necessarily
! 405: * called (such as if a resource doesn't have access restrictions).
! 406: */
! 407: module MODULE_VAR_EXPORT parser3_module =
! 408: {
! 409: STANDARD_MODULE_STUFF,
! 410: parser_server_init, /* module initializer */
! 411: parser_create_dir_config, /* per-directory config creator */
! 412: parser_merge_dir_config, /* dir config merger */
! 413: parser_create_server_config, /* server config creator */
! 414: parser_merge_server_config, /* server config merger */
! 415: parser_cmds, /* command table */
! 416: parser_handlers, /* [9] list of handlers */
! 417: parser_translate_handler, /* [2] filename-to-URI translation */
! 418: parser_check_user_id, /* [5] check/validate user_id */
! 419: parser_auth_checker, /* [6] check user_id is valid *here* */
! 420: parser_access_checker, /* [4] check access by host address */
! 421: 0, /* [7] MIME type checker/setter */
! 422: 0, /* [8] fixups */
! 423: 0 /* [10] logger */
! 424: };
! 425:
! 426: #if defined(_MSC_VER)
! 427: # define APACHE_WIN32_SRC "/parser3project/win32/apache13/src"
! 428: # ifdef _DEBUG
! 429: # pragma comment(lib, APACHE_WIN32_SRC "/CoreD/ApacheCore.lib")
! 430: # else
! 431: # pragma comment(lib, APACHE_WIN32_SRC "/CoreR/ApacheCore.lib")
! 432: # endif
! 433: #endif
! 434:
! 435:
! 436: // interface to C++
! 437:
! 438: #define PA_APLOG_EMERG 0 /* system is unusable */
! 439: #define PA_APLOG_ALERT 1 /* action must be taken immediately */
! 440: #define PA_APLOG_CRIT 2 /* critical conditions */
! 441: #define PA_APLOG_ERR 3 /* error conditions */
! 442: #define PA_APLOG_WARNING 4 /* warning conditions */
! 443: #define PA_APLOG_NOTICE 5 /* normal but significant condition */
! 444: #define PA_APLOG_INFO 6 /* informational */
! 445: #define PA_APLOG_DEBUG 7 /* debug-level messages */
! 446:
! 447: #define PA_APLOG_LEVELMASK 7 /* mask off the level value */
! 448:
! 449: #define PA_APLOG_NOERRNO (PA_APLOG_LEVELMASK + 1)
! 450:
! 451: #define PA_APLOG_MARK __FILE__,__LINE__
! 452:
! 453: void pa_ap_log_rerror(const char *file, int line, int level,
! 454: const pa_request_rec *s, const char *fmt, ...) {
! 455: const char* str;
! 456: va_list l;
! 457: va_start(l, fmt);
! 458: str=va_arg(l, const char*);
! 459: va_end(l);
! 460:
! 461: ap_log_rerror(file, line, level,
! 462: (request_rec*)s->real_request_rec, "%s", str);
! 463: }
! 464:
! 465:
! 466: void pa_ap_log_error(const char *file, int line, int level,
! 467: const pa_server_rec *s, const char *fmt, ...) {
! 468: const char* str;
! 469: va_list l;
! 470: va_start(l, fmt);
! 471: str=va_arg(l, const char*);
! 472: va_end(l);
! 473:
! 474: ap_log_error(file, line, level,
! 475: (server_rec*)s, "%s", str);
! 476: }
! 477:
! 478: // ap_alloc.h
! 479:
! 480: const char* pa_ap_table_get(const pa_table *t, const char *name) {
! 481: return ap_table_get((const table*)t, name);
! 482: }
! 483: void pa_ap_table_addn(pa_table *t, const char *name, const char *val) {
! 484: ap_table_addn((table*)t, name, val);
! 485: }
! 486:
! 487: int pa_ap_table_size(const pa_table *t) {
! 488: return ap_table_elts((const table*)t)->nelts;
! 489: }
! 490:
! 491: void pa_ap_table_do(int (*comp) (void *, const char *, const char *),
! 492: void *rec, const pa_table *t, ...) {
! 493: ap_table_do(comp, rec, (table*)t, 0);
! 494: }
! 495:
! 496: char * pa_ap_pstrdup(pa_pool *p, const char *s) {
! 497: return ap_pstrdup((pool*)p, s);
! 498: }
! 499:
! 500: // http_protocol.h
! 501:
! 502: int pa_ap_setup_client_block(pa_request_rec *r, int read_policy) {
! 503: return ap_setup_client_block((request_rec*)r->real_request_rec,
! 504: read_policy);
! 505: }
! 506: int pa_ap_should_client_block(pa_request_rec *r) {
! 507: return ap_should_client_block((request_rec*)r->real_request_rec);
! 508: }
! 509: long pa_ap_get_client_block(pa_request_rec *r, char *buffer, int bufsiz) {
! 510: return ap_get_client_block((request_rec*)r->real_request_rec,
! 511: buffer, bufsiz);
! 512: }
! 513: void pa_ap_send_http_header(pa_request_rec *r) {
! 514: ap_send_http_header((request_rec*)r->real_request_rec);
! 515: }
! 516: int pa_ap_rwrite(const void *buf, int nbyte, pa_request_rec *r) {
! 517: return ap_rwrite(buf, nbyte, (request_rec*)r->real_request_rec);
! 518: }
! 519:
! 520:
! 521: // http_main.h
! 522:
! 523: void pa_ap_hard_timeout(char *s, pa_request_rec *r) {
! 524: ap_hard_timeout(s, (request_rec*)r->real_request_rec);
! 525: }
! 526: void pa_ap_reset_timeout(pa_request_rec *r) {
! 527: ap_reset_timeout((request_rec*)r->real_request_rec);
! 528: }
! 529: void pa_ap_kill_timeout(pa_request_rec *r) {
! 530: ap_kill_timeout((request_rec*)r->real_request_rec);
! 531: }
! 532:
! 533:
! 534: // util_script.h
! 535:
! 536: void pa_ap_add_cgi_vars(pa_request_rec *r) {
! 537: ap_add_cgi_vars((request_rec*)r->real_request_rec);
! 538: }
! 539: void pa_ap_add_common_vars(pa_request_rec *r) {
! 540: ap_add_common_vars((request_rec*)r->real_request_rec);
! 541: }
! 542:
! 543:
! 544: // ap_md5.h
! 545:
! 546: void pa_MD5Init(PA_MD5_CTX *context) { ap_MD5Init((AP_MD5_CTX*)context); }
! 547: void pa_MD5Update(PA_MD5_CTX *context, const unsigned char *input,
! 548: unsigned int inputLen) { ap_MD5Update((AP_MD5_CTX *)context, input, inputLen); }
! 549: void pa_MD5Final(unsigned char digest[MD5_DIGESTSIZE],
! 550: PA_MD5_CTX *context) { ap_MD5Final(digest, (AP_MD5_CTX *)context); }
! 551: void pa_MD5Encode(const unsigned char *password,
! 552: const unsigned char *salt,
! 553: char *result, size_t nbytes) { ap_MD5Encode(password, salt, result, nbytes); }
! 554: void pa_to64(char *s, unsigned long v, int n) { ap_to64(s, v, n); }
! 555:
! 556:
! 557: // signal.h
! 558:
! 559: void (*pa_signal (int sig, void (*disp)(int)))(int) {
! 560: if(sig==PA_SIGPIPE && disp==PA_SIG_IGN)
! 561: return signal(SIGPIPE, SIG_IGN);
! 562:
! 563: return 0;
! 564: }
E-mail: