--- parser3/src/targets/apache13/modules/extra/Attic/mod_parser3.C 2001/03/21 10:58:05 1.1.2.1 +++ parser3/src/targets/apache13/modules/extra/Attic/mod_parser3.C 2001/03/21 14:24:54 1.3 @@ -1,64 +1,12 @@ -/* ==================================================================== - * Copyright (c) 1995-1999 The Apache Group. 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. All advertising materials mentioning features or use of this - * software must display the following acknowledgment: - * "This product includes software developed by the Apache Group - * for use in the Apache HTTP server project (http://www.apache.org/)." - * - * 4. The names "Apache Server" and "Apache Group" must not be used to - * endorse or promote products derived from this software without - * prior written permission. For written permission, please contact - * apache@apache.org. - * - * 5. Products derived from this software may not be called "Apache" - * nor may "Apache" appear in their names without prior written - * permission of the Apache Group. - * - * 6. Redistributions of any form whatsoever must retain the following - * acknowledgment: - * "This product includes software developed by the Apache Group - * for use in the Apache HTTP server project (http://www.apache.org/)." - * - * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY - * EXPRESSED 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 APACHE GROUP OR - * ITS 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. - * ==================================================================== - * - * This software consists of voluntary contributions made by many - * individuals on behalf of the Apache Group and was originally based - * on public domain software written at the National Center for - * Supercomputing Applications, University of Illinois, Urbana-Champaign. - * For more information on the Apache Group and the Apache HTTP server - * project, please see . - * - */ +/** @file + Parser: apache module. -/* - * Apache example module. Provide demonstrations of how modules do things. - * - */ + Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) + + Author: Alexander Petrosyan (http://design.ru/paf) + + $Id: mod_parser3.C,v 1.3 2001/03/21 14:24:54 paf Exp $ +*/ #include "httpd.h" #include "http_config.h" @@ -70,6 +18,11 @@ #include + +#include "pa_common.h" +#include "pa_globals.h" +#include "pa_request.h" + /*--------------------------------------------------------------------------*/ /* */ /* Data declarations. */ @@ -132,7 +85,7 @@ static pool *example_subpool = NULL; * Declare ourselves so the configuration routines can find and know us. * We'll fill it in at the end of the module. */ -module MODULE_VAR_EXPORT example_module; +extern "C" module MODULE_VAR_EXPORT parser3_module; /*--------------------------------------------------------------------------*/ /* */ @@ -262,7 +215,7 @@ module MODULE_VAR_EXPORT example_module; static excfg *our_dconfig(request_rec *r) { - return (excfg *) ap_get_module_config(r->per_dir_config, &example_module); + return (excfg *) ap_get_module_config(r->per_dir_config, &parser3_module); } #if 0 @@ -272,7 +225,7 @@ static excfg *our_dconfig(request_rec *r static excfg *our_sconfig(server_rec *s) { - return (excfg *) ap_get_module_config(s->module_config, &example_module); + return (excfg *) ap_get_module_config(s->module_config, &parser3_module); } /* @@ -281,7 +234,7 @@ static excfg *our_sconfig(server_rec *s) static excfg *our_rconfig(request_rec *r) { - return (excfg *) ap_get_module_config(r->request_config, &example_module); + return (excfg *) ap_get_module_config(r->request_config, &parser3_module); } #endif @@ -311,7 +264,7 @@ static void setup_module_cells() * a pointer to our private configuration record (if available) for the * environment to which the callback is supposed to apply, and some text. We * turn this into a textual representation and add it to the tail of the list. - * The list can be displayed by the example_handler() routine. + * The list can be displayed by the parser3_handler() routine. * * If the call occurs within a request context (i.e., we're passed a request * record), we put the trace into the request pool and attach it to the @@ -491,99 +444,151 @@ static const char *cmd_example(cmd_parms * DECLINED ("this isn't something with which we want to get involved") * HTTP_mumble ("an error status should be reported") */ -static int example_handler(request_rec *r) + + +// service funcs + +int read_post(Pool& pool, char *buf, int max_bytes) { + return 0;/* @todo + int read_size=0; + do { + int chunk_size=read + (fileno(stdin), buf+read_size, min(0x400*0x400, max_bytes-read_size)); + if(chunk_size<0) + break; + read_size+=chunk_size; + } while(read_size(pool.info()); + + if(strcasecmp(key, "content-type")==0) { + /* r->content_type, *not* r->headers_out("Content-type"). If you don't + * set it, it will be filled in with the server's default type (typically + * "text/plain"). You *must* also ensure that r->content_type is lower + * case. + */ + r->content_type = value; + } else + ap_table_merge(r->headers_out, key, value); +} + +void send_header(Pool& pool) { + request_rec *r=static_cast(pool.info()); + ap_send_http_header(r); +} + +void send_body(Pool& pool, const char *buf, size_t size) { + request_rec *r=static_cast(pool.info()); + ap_rwrite(buf, size, r); +} + +///@todo initSocks(); +static int parser3_handler(request_rec *r) { + Pool pool; + pool.set_storage(r->pool); + pool.set_info(r); excfg *dcfg; dcfg = our_dconfig(r); - trace_add(r->server, r, dcfg, "example_handler()"); - /* - * We're about to start sending content, so we need to force the HTTP - * headers to be sent at this point. Otherwise, no headers will be sent - * at all. We can set any we like first, of course. **NOTE** Here's - * where you set the "Content-type" header, and you do so by putting it in - * r->content_type, *not* r->headers_out("Content-type"). If you don't - * set it, it will be filled in with the server's default type (typically - * "text/plain"). You *must* also ensure that r->content_type is lower - * case. - * - * We also need to start a timer so the server can know if the connexion - * is broken. - */ - r->content_type = "text/html"; + + + /* A flag which modules can set, to indicate that the data being + * returned is volatile, and clients should be told not to cache it. + */ + r->no_cache=1; + ap_soft_timeout("send example call trace", r); - ap_send_http_header(r); - /* - * If we're only supposed to send header information (HEAD request), we're - * already there. - */ - if (r->header_only) { - ap_kill_timeout(r); - return OK; - } + PTRY { // global try + const char *filespec_to_process=r->filename; + + // Request info + Request::Info request_info; + const char *document_root= + (const char *)ap_table_get(r->subprocess_env, "DOCUMENT_ROOT"); + if(!document_root) { + static char fake_document_root[MAX_STRING]; + strncpy(fake_document_root, filespec_to_process, MAX_STRING); + rsplit(fake_document_root, '/'); rsplit(fake_document_root, '\\');// strip filename + document_root=fake_document_root; + } + request_info.document_root=document_root; + request_info.path_translated=filespec_to_process; + request_info.method=r->method; + request_info.query_string=r->args; + request_info.uri=r->uri; + request_info.content_type=r->content_type; + const char *content_length = + (const char *)ap_table_get(r->subprocess_env, "CONTENT_LENGTH"); + request_info.content_length=(content_length?atoi(content_length):0); + request_info.cookie=(const char *)ap_table_get(r->subprocess_env, "HTTP_COOKIE"); + + // prepare to process request + Request request(pool, + request_info, + String::UL_HTML_TYPO + ); + + /* move this to httpd.conf | .htaccess + + // some root-controlled location + char *sys_auto_path1; +#ifdef WIN32 + // c:\windows + sys_auto_path1=(char *)pool.malloc(MAX_STRING); + GetWindowsDirectory(sys_auto_path1, MAX_STRING); + strcat(sys_auto_path1, PATH_DELIMITER_STRING); +#else + // ~nobody + sys_auto_path1=getenv("HOME"); +#endif + + // beside by binary + char *sys_auto_path2=(char *)pool.malloc(MAX_STRING); + strncpy(sys_auto_path2, argv[0], MAX_STRING); // filespec of my binary + rsplit(sys_auto_path2, '/'); rsplit(sys_auto_path2, '\\');// strip filename + strcat(sys_auto_path2, PATH_DELIMITER_STRING); + + */ + + // process the request + request.core( + 0/*sys_auto_path1*/, + 0/*sys_auto_path2*/, + r->header_only!=0); + // no actions with request' data past this point + // request.exception not not handled here, but all + // request' data are associated with it's pool=exception + + // successful finish + } PCATCH(e) { // global problem + const char *body=e.comment(); + int content_length=strlen(body); + + // prepare header + (*service_funcs.add_header_attribute)(pool, "content-type", "text/plain"); + char content_length_cstr[MAX_NUMBER]; + snprintf(content_length_cstr, MAX_NUMBER, "%d", content_length); + (*service_funcs.add_header_attribute)(pool, "content-length", + content_length_cstr); + + // send header + (*service_funcs.send_header)(pool); + + // send body + if(!r->header_only) + (*service_funcs.send_body)(pool, body, content_length); + + // unsuccessful finish + } + PEND_CATCH + ap_kill_timeout(r); - /* - * Now send our actual output. Since we tagged this as being - * "text/html", we need to embed any HTML. - */ - ap_rputs(DOCTYPE_HTML_3_2, r); - ap_rputs("\n", r); - ap_rputs(" \n", r); - ap_rputs(" mod_example Module Content-Handler Output\n", r); - ap_rputs(" \n", r); - ap_rputs(" \n", r); - ap_rputs(" \n", r); - ap_rputs("

mod_example Module Content-Handler Output\n", r); - ap_rputs("

\n", r); - ap_rputs("

\n", r); - ap_rprintf(r, " Apache HTTP Server version: \"%s\"\n", - ap_get_server_version()); - ap_rputs("
\n", r); - ap_rprintf(r, " Server built: \"%s\"\n", ap_get_server_built()); - ap_rputs("

\n", r);; - ap_rputs("

\n", r); - ap_rputs(" The format for the callback trace is:\n", r); - ap_rputs("

\n", r); - ap_rputs("
\n", r); - ap_rputs("
n.<routine-name>", r); - ap_rputs("(<routine-data>)\n", r); - ap_rputs("
\n", r); - ap_rputs("
[<applies-to>]\n", r); - ap_rputs("
\n", r); - ap_rputs("
\n", r); - ap_rputs("

\n", r); - ap_rputs(" The <routine-data> is supplied by\n", r); - ap_rputs(" the routine when it requests the trace,\n", r); - ap_rputs(" and the <applies-to> is extracted\n", r); - ap_rputs(" from the configuration record at the time of the trace.\n", r); - ap_rputs(" SVR() indicates a server environment\n", r); - ap_rputs(" (blank means the main or default server, otherwise it's\n", r); - ap_rputs(" the name of the VirtualHost); DIR()\n", r); - ap_rputs(" indicates a location in the URL or filesystem\n", r); - ap_rputs(" namespace.\n", r); - ap_rputs("

\n", r); - ap_rprintf(r, "

Static callbacks so far:

\n
    \n%s
\n", - trace); - ap_rputs("

Request-specific callbacks so far:

\n", r); - ap_rprintf(r, "
    \n%s
\n", ap_table_get(r->notes, TRACE_NOTE)); - ap_rputs("

Environment for this call:

\n", r); - ap_rputs("
    \n", r); - ap_rprintf(r, "
  • Applies-to: %s\n
  • \n", dcfg->loc); - ap_rprintf(r, "
  • \"Example\" directive declared here: %s\n
  • \n", - (dcfg->local ? "YES" : "NO")); - ap_rprintf(r, "
  • \"Example\" inherited: %s\n
  • \n", - (dcfg->congenital ? "YES" : "NO")); - ap_rputs("
\n", r); - ap_rputs(" \n", r); - ap_rputs("\n", r); - /* - * We're all done, so cancel the timeout we set. Since this is probably - * the end of the request we *could* assume this would be done during - * post-processing - but it's possible that another handler might be - * called and inherit our outstanding timer. Not good; to each its own. - */ - ap_kill_timeout(r); /* * We did what we wanted to do, so tell the rest of the server we * succeeded. @@ -631,23 +636,34 @@ static int example_handler(request_rec * /* * All our module-initialiser does is add its trace to the log. */ -static void example_init(server_rec *s, pool *p) +static void parser3_init(server_rec *s, pool *p) { - char *note; char *sname = s->server_hostname; - /* - * Set up any module cells that ought to be initialised. - */ - setup_module_cells(); - /* - * The arbitrary text we add to our trace entry indicates for which server - * we're being called. - */ - sname = (sname != NULL) ? sname : ""; - note = ap_pstrcat(p, "example_init(", sname, ")", NULL); - trace_add(s, NULL, NULL, note); + static bool globals_inited=false; + if(globals_inited) + return; + globals_inited=true; + + static Pool pool; ///< global pool + pool.set_storage(p); + PTRY { + // init global variables + globals_init(pool); + + // Service funcs + service_funcs.read_post=read_post; + service_funcs.add_header_attribute=add_header_attribute; + service_funcs.send_header=send_header; + service_funcs.send_body=send_body; + + //... + } PCATCH(e) { // global problem + const char *body=e.comment(); + // somehow report that error + } + PEND_CATCH } /* @@ -1072,9 +1088,9 @@ static const command_rec example_cmds[] { { "Example", /* directive name */ - cmd_example, /* config action routine */ + (const char *(*)(void))((void *)cmd_example), /* config action routine */ NULL, /* argument to include in call */ - OR_OPTIONS, /* where available */ + (int)OR_OPTIONS, /* where available */ NO_ARGS, /* arguments */ "Example directive - no arguments" /* directive description */ @@ -1099,9 +1115,9 @@ static const command_rec example_cmds[] * if a content-handler returns anything except DECLINED, no other * content-handlers will be called. */ -static const handler_rec example_handlers[] = +static const handler_rec parser3_handlers[] = { - {"example-handler", example_handler}, + {"parser3-handler", parser3_handler}, {NULL} }; @@ -1119,16 +1135,16 @@ static const handler_rec example_handler * during request processing. Note that not all routines are necessarily * called (such as if a resource doesn't have access restrictions). */ -module MODULE_VAR_EXPORT example_module = +module MODULE_VAR_EXPORT parser3_module = { STANDARD_MODULE_STUFF, - example_init, /* module initializer */ + parser3_init, /* module initializer */ example_create_dir_config, /* per-directory config creator */ example_merge_dir_config, /* dir config merger */ example_create_server_config, /* server config creator */ example_merge_server_config, /* server config merger */ example_cmds, /* command table */ - example_handlers, /* [9] list of handlers */ + parser3_handlers, /* [9] list of handlers */ example_translate_handler, /* [2] filename-to-URI translation */ example_check_user_id, /* [5] check/validate user_id */ example_auth_checker, /* [6] check user_id is valid *here* */