tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From p...@locus.apache.org
Subject cvs commit: jakarta-tomcat-4.0/connector/src/webapplib wa.c wa.h wa_connection.c wa_connection.h wa_host.c wa_host.h wa_provider.c wa_provider.h wa_provider_info.c wa_request.c wa_request.h
Date Fri, 24 Nov 2000 18:10:42 GMT
pier        00/11/24 10:10:42

  Added:       connector/src/apache-1.3 mod_webapp.c
               connector/src/webapplib wa.c wa.h wa_connection.c
                        wa_connection.h wa_host.c wa_host.h wa_provider.c
                        wa_provider.h wa_provider_info.c wa_request.c
                        wa_request.h
  Log:
  Added the module (done) and the library it relies upon (partial)...
  
  Revision  Changes    Path
  1.1                  jakarta-tomcat-4.0/connector/src/apache-1.3/mod_webapp.c
  
  Index: mod_webapp.c
  ===================================================================
  /* ========================================================================= *
   *                                                                           *
   *                 The Apache Software License,  Version 1.1                 *
   *                                                                           *
   *         Copyright (c) 1999, 2000  The Apache Software Foundation.         *
   *                           All rights reserved.                            *
   *                                                                           *
   * ========================================================================= *
   *                                                                           *
   * Redistribution and use in source and binary forms,  with or without modi- *
   * fication, are permitted provided that the following conditions are met:   *
   *                                                                           *
   * 1. Redistributions of source code  must retain the above copyright notice *
   *    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 end-user documentation  included with the redistribution,  if any, *
   *    must include the following acknowlegement:                             *
   *                                                                           *
   *       "This product includes  software developed  by the Apache  Software *
   *        Foundation <http://www.apache.org/>."                              *
   *                                                                           *
   *    Alternately, this acknowlegement may appear in the software itself, if *
   *    and wherever such third-party acknowlegements normally appear.         *
   *                                                                           *
   * 4. The names  "The  Jakarta  Project",  "Tomcat",  and  "Apache  Software *
   *    Foundation"  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 Software Foundation.                                            *
   *                                                                           *
   * THIS SOFTWARE IS PROVIDED "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  SOFTWARE  FOUNDATION 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 indivi- *
   * duals on behalf of the  Apache Software Foundation.  For more information *
   * on the Apache Software Foundation, please see <http://www.apache.org/>.   *
   *                                                                           *
   * ========================================================================= */
  #include <httpd.h>
  #include <http_config.h>
  #include <http_core.h>
  #include <http_log.h>
  #include <http_main.h>
  #include <http_protocol.h>
  #include <util_script.h>
  #include <wa.h>
  
  
  /* ************************************************************************* */
  /* GENERIC DECLARATIONS                                                      */
  /* ************************************************************************* */
  
  /* Module declaration */
  module webapp_module;
  /* Callback structure declaration */
  static wa_callbacks webapp_callbacks;
  
  
  /* ************************************************************************* */
  /* CONFUGURATION ROUTINES                                                    */
  /* ************************************************************************* */
  
  /**
   * Configure a webapp connection.
   *
   * @param name The connection unique name.
   * @param connector The name of the WebApp connector to use.
   * @param parameters Connector-specific parameters (can be NULL).
   */
  static const char *webapp_config_connection (cmd_parms *cmd, void *mconfig,
                                               char *name, char *provider,
                                               char *parameter) {
      return(wa_connection_create(name, provider, parameter));
  }
  
  /**
   * Configure a webapp application mount point.
   *
   * @param name The web application name.
   * @param cname The connection name.
   * @param path The web application root path.
   */
  static const char *webapp_config_mount (cmd_parms *cmd, void *mconfig,
                                          char *name, char *cname, char *path) {
      server_rec *s=cmd->server;
      wa_host *host=NULL;
      wa_connection *conn=NULL;
      const char *mesg=NULL;
  
      // Retrieve (or create) our host structure
      host=wa_host_get(s->server_hostname,s->port);
      if (host==NULL) {
          mesg=wa_host_create(s->server_hostname,s->port);
          if (mesg!=NULL) return(mesg);
          host=wa_host_get(s->server_hostname,s->port);
          if (host==NULL) return("Cannot retrieve host information");
      }
  
      // Setup the webapp name, connection and path
      conn=wa_connection_get(cname);
      if (conn==NULL) return("Invalid connection name specified");
      return(wa_host_setapp(host, name, path, conn));
  
      return (NULL);
  }
  
  
  /* ************************************************************************* */
  /* WEB-APPLICATION INITIALIZATION AND REQUEST HANDLING                       */
  /* ************************************************************************* */
  
  /**
   * "Translate" (or better, match) an HTTP request into a webapp request.
   *
   * @param r The Apache request structure.
   */
  static int webapp_translate(request_rec *r) {
      wa_host *host=NULL;
      wa_application *appl=NULL;
      wa_request *req=NULL;
      
      // Check if this host was recognized
      host=wa_host_get(r->server->server_hostname, r->server->port);
      if (host==NULL) return(DECLINED);
      
      // Check if the uri is contained in one of our web applications root path
      appl=wa_host_findapp(host, r->uri);
      if (appl==NULL) return(DECLINED);
  
      // The uri path is matched by the application, set the handler and return
      r->handler=ap_pstrdup(r->pool,"webapp-handler");
      
      // Create a new request structure
      req=(wa_request *)ap_palloc(r->pool,sizeof(wa_request));
      req->host=host;
      req->application=appl;
  
      // Set the webapp request structure into Apache's request structure
      ap_set_module_config(r->request_config, &webapp_module, req);
      return(OK);
  }
  
  /**
   * Handle a request thru a configured web application.
   *
   * @param r The Apache request structure.
   */
  static int webapp_handler(request_rec *r) {
      const char *message=NULL;
      wa_request *req=NULL;
      
      // Try to get a hold on the webapp request structure
      req=(wa_request *)ap_get_module_config(r->request_config, &webapp_module);
      if (req==NULL) return(DECLINED);
  
      // Set up basic parameters in the request structure
      req->data=r;
      req->method=(char *)r->method;
      req->uri=r->uri;
      req->arguments=r->args;
      req->protocol=r->protocol;
      
      // Copy headers into webapp request structure
      if (!r->headers_in) {
          array_header *arr=ap_table_elts(r->headers_in);
          table_entry *ele=(table_entry *)arr->elts;
          int count=arr->nelts;
          int x=0;
  
          // Allocate memory for pointers
          req->header_names=(char **)ap_palloc(r->pool,count*sizeof(char *));
          req->header_values=(char **)ap_palloc(r->pool,count*sizeof(char *));
  
          // Copy header pointers one by one
          for (x=0; x<count;x++) {
              if (ele[x].key==NULL) continue;
              req->header_names[x]=ele[x].key;
              req->header_values[x]=ele[x].val;
          }
      }
  
      // Try to handle the request
      message=wa_request_handle(req,&webapp_callbacks);
  
      // We got an error message (critical error, not from providers)
      if (message!=NULL) {
          ap_log_error(APLOG_MARK,APLOG_CRIT,r->server,"%s",message);
          return(HTTP_INTERNAL_SERVER_ERROR);
      }
  
      return OK;
  }
  
  /**
   * Destroy webapp connections.
   */
  static void webapp_destroy(void *k) {
      wa_connection_destroy();
  }
  
  /**
   * Initialize webapp connections.
   */
  static void webapp_init(server_rec *s, pool *p) {
      // Register our cleanup function
  #ifdef WIN32
      // Under Win32 we clean up when the process exits, since web server
      // children are threads (sockets, connections and all the rest resides
      // in the same memory space.
      ap_register_cleanup(p, NULL, webapp_destroy, ap_null_cleanup);
  #else
      // Under UNIX we clean up when a child exists, since web server children
      // are processes, and not threads.
      ap_register_cleanup(p, NULL, ap_null_cleanup, webapp_destroy);
  #endif    
  
      // Initialize connections
      wa_connection_init();
  }
  
  /* ************************************************************************* */
  /* WEBAPP LIBRARY CALLBACK FUNCTIONS                                         */
  /* ************************************************************************* */
  
  /**
   * Log data on the web server log file.
   *
   * @param file The source file of this log entry.
   * @param line The line number within the source file of this log entry.
   * @param data The web-server specific data (wa_request->data).
   * @param fmt The format string (printf style).
   * @param ... All other parameters (if any) depending on the format.
   */
  static void webapp_callback_log(void *data, const char *file, int line,
                                  const char *fmt, ...) {
      request_rec *r=(request_rec *)data;
      va_list ap;
      
      va_start (ap,fmt);
      if (r==NULL) {
          fprintf(stderr,"[%s:%d] ",file,line);
          vfprintf(stderr,fmt,ap);
          fprintf(stderr,"\n");
      } else {
          char *message=ap_pvsprintf(r->pool,fmt,ap);
          ap_log_error(file,line,APLOG_ERR,r->server,"%s",message);
      }
  }
  
  /**
   * Allocate memory while processing a request.
   *
   * @param data The web-server specific data (wa_request->data).
   * @param size The size in bytes of the memory to allocate.
   * @return A pointer to the allocated memory or NULL.
   */
  static void *webapp_callback_alloc(void *data, int size) {
      request_rec *r=(request_rec *)data;
  
      if (r==NULL) {
          webapp_callback_log(r,WA_LOG,"Apache request_rec pointer is NULL");
          return(NULL);
      }
      return(ap_palloc(r->pool,size));
  }
  
  /**
   * Read part of the request content.
   *
   * @param data The web-server specific data (wa_request->data).
   * @param buf The buffer that will hold the data.
   * @param len The buffer length.
   * @return The number of bytes read, 0 on end of file or -1 on error.
   */
  static int webapp_callback_read(void *data, char *buf, int len) {
      request_rec *r=(request_rec *)data;
  
      if (r==NULL) {
          webapp_callback_log(r,WA_LOG,"Apache request_rec pointer is NULL");
          return(-1);
      }
  
      return((int)ap_get_client_block(r,buf,len));
  }
  
  /**
   * Set the HTTP response status code.
   *
   * @param data The web-server specific data (wa_request->data).
   * @param status The HTTP status code for the response.
   * @return TRUE on success, FALSE otherwise
   */
  static boolean webapp_callback_setstatus(void *data, int status) {
      request_rec *r=(request_rec *)data;
  
      if (r==NULL) {
          webapp_callback_log(r,WA_LOG,"Apache request_rec pointer is NULL");
          return(FALSE);
      }
  
      r->status=status;
      return(TRUE);
  }
  
  /**
   * Set the HTTP response mime content type.
   *
   * @param data The web-server specific data (wa_request->data).
   * @param type The mime content type of the HTTP response.
   * @return TRUE on success, FALSE otherwise
   */
  static boolean webapp_callback_settype(void *data, char *type) {
      request_rec *r=(request_rec *)data;
  
      if (r==NULL) {
          webapp_callback_log(r,WA_LOG,"Apache request_rec pointer is NULL");
          return(FALSE);
      }
  
      r->content_type=type;
      return(TRUE);
  }
  
  /**
   * Set an HTTP mime header.
   *
   * @param data The web-server specific data (wa_request->data).
   * @param name The mime header name.
   * @param value The mime header value.
   * @return TRUE on success, FALSE otherwise
   */
  static boolean webapp_callback_setheader(void *data, char *name, char *value) {
      request_rec *r=(request_rec *)data;
  
      if (r==NULL) {
          webapp_callback_log(r,WA_LOG,"Apache request_rec pointer is NULL");
          return(FALSE);
      }
  
      ap_table_add(r->headers_out, name, value);
      return(TRUE);
  }
  
  /**
   * Commit the first part of the response (status and headers).
   *
   * @param data The web-server specific data (wa_request->data).
   * @return TRUE on success, FALSE otherwise
   */
  static boolean webapp_callback_commit(void *data) {
      request_rec *r=(request_rec *)data;
  
      if (r==NULL) {
          webapp_callback_log(r,WA_LOG,"Apache request_rec pointer is NULL");
          return(FALSE);
      }
  
      ap_send_http_header(r);
      return(TRUE);
  }
  
  /**
   * Write part of the response data back to the client.
   *
   * @param buf The buffer holding the data to be written.
   * @param len The number of characters to be written.
   * @return The number of characters written to the client or -1 on error.
   */
  static int webapp_callback_write(void *data, char *buf, int len) {
      request_rec *r=(request_rec *)data;
  
      if (r==NULL) {
          webapp_callback_log(r,WA_LOG,"Apache request_rec pointer is NULL");
          return(FALSE);
      }
  
      return(ap_rwrite(buf, len, r));
  }
  
  /**
   * Flush any unwritten response data to the client.
   *
   * @param data The web-server specific data (wa_request->data).
   * @return TRUE on success, FALSE otherwise
   */
  static boolean webapp_callback_flush(void *data) {
      request_rec *r=(request_rec *)data;
  
      if (r==NULL) {
          webapp_callback_log(r,WA_LOG,"Apache request_rec pointer is NULL");
          return(FALSE);
      }
  
      ap_rflush(r);
      return(TRUE);
  }
  
  
  /* ************************************************************************* */
  /* STRUCTURES, FUNCTIONS AND DATA POINTERS                                   */
  /* ************************************************************************* */
  
  /* Our callback functions structure */
  static wa_callbacks webapp_callbacks = {
      webapp_callback_log,
      webapp_callback_alloc,
      webapp_callback_read,
      webapp_callback_setstatus,
      webapp_callback_settype,
      webapp_callback_setheader,
      webapp_callback_commit,
      webapp_callback_write,
      webapp_callback_flush,
  };
  
  /* List of all available configuration directives */
  static const command_rec webapp_commands[] = {
      {
          "WebAppConnection",         // directive name
          webapp_config_connection,   // config action routine
          NULL,                       // argument to include in call
          RSRC_CONF,                  // where available
          TAKE23,                     // arguments
          "<name> <connector> [optional parameter]"
      }, {
          "WebAppMount",              // directive name
          webapp_config_mount,        // config action routine
          NULL,                       // argument to include in call
          RSRC_CONF,                  // where available
          TAKE3,                      // arguments
          "<name> <connection> <uri-path>"
      }, {NULL}
  };
  
  /* List of all available Apache handlers */
  static const handler_rec webapp_handlers[] = {
      {"webapp-handler", webapp_handler},
      {NULL}
  };
  
  /* Apache module declaration */
  module webapp_module = {
      STANDARD_MODULE_STUFF,
  #ifdef WIN32
      webapp_init,                        /* module initializer */
  #else
      NULL,                               /* module initializer */
  #endif
      NULL,                               /* per-directory config creator */
      NULL,                               /* dir config merger */
      NULL,                               /* server config creator */
      NULL,                               /* server config merger */
      webapp_commands,                    /* command table */
      webapp_handlers,                    /* [9] list of handlers */
      webapp_translate,                   /* [2] filename-to-URI translation */
      NULL,                               /* [5] check/validate user_id */
      NULL,                               /* [6] check user_id is valid *here* */
      NULL,                               /* [4] check access by host address */
      NULL,                               /* [7] MIME type checker/setter */
      NULL,                               /* [8] fixups */
      NULL,                               /* [10] logger */
      NULL,                               /* [3] header parser */
  #ifdef WIN32
      NULL,                               /* child initializer */
  #else
      webapp_init,                        /* child initializer */
  #endif
      NULL,                               /* child exit/cleanup */
      NULL                                /* [1] post read_request handling */
  };
  
  
  
  1.1                  jakarta-tomcat-4.0/connector/src/webapplib/wa.c
  
  Index: wa.c
  ===================================================================
  int main(int argc, char *argv[]) {
      return(0);
  }
  
  
  1.1                  jakarta-tomcat-4.0/connector/src/webapplib/wa.h
  
  Index: wa.h
  ===================================================================
  #ifndef _WA_H_
  #define _WA_H_
  
  /* Generic includes */
  #include <stdio.h>
  #include <stdlib.h>
  #include <string.h>
  
  /* Define TRUE and FALSE */
  #ifndef TRUE
  #define TRUE 1
  #endif
  
  #ifndef FALSE
  #define FALSE 0
  #endif
  
  /* Define the log mark */
  #define WA_LOG __FILE__,__LINE__
  
  /* Types definitions */
  typedef int boolean;
  typedef struct wa_connection wa_connection;
  typedef struct wa_application wa_application;
  typedef struct wa_host wa_host;
  typedef struct wa_provider wa_provider;
  typedef struct wa_header wa_header;
  typedef struct wa_request wa_request;
  typedef struct wa_callbacks wa_callbacks;
  
  /* Other includes from the webapp lib */
  #include <wa_host.h>
  #include <wa_connection.h>
  #include <wa_provider.h>
  #include <wa_request.h>
  
  /**
   * The wa_callbacks structure contains function pointers for callbacks to the
   * web server.
   */
  struct wa_callbacks {
      /**
       * Log data on the web server log file.
       *
       * @param file The source file of this log entry.
       * @param line The line number within the source file of this log entry.
       * @param data The web-server specific data (wa_request->data).
       * @param fmt The format string (printf style).
       * @param ... All other parameters (if any) depending on the format.
       */
      void (*log)(void *data, const char *file, int line, const char *fmt, ...);
  
      /**
       * Allocate memory while processing a request.
       *
       * @param data The web-server specific data (wa_request->data).
       * @param size The size in bytes of the memory to allocate.
       * @return A pointer to the allocated memory or NULL.
       */
      void *(*alloc)(void *data, int size);
  
      /**
       * Read part of the request content.
       *
       * @param data The web-server specific data (wa_request->data).
       * @param buf The buffer that will hold the data.
       * @param len The buffer length.
       * @return The number of bytes read, 0 on end of file or -1 on error.
       */
      int (*read)(void *data, char *buf, int len);
  
      /**
       * Set the HTTP response status code.
       *
       * @param data The web-server specific data (wa_request->data).
       * @param status The HTTP status code for the response.
       * @return TRUE on success, FALSE otherwise
       */
      boolean (*setstatus)(void *data, int status);
  
      /**
       * Set the HTTP response mime content type.
       *
       * @param data The web-server specific data (wa_request->data).
       * @param type The mime content type of the HTTP response.
       * @return TRUE on success, FALSE otherwise
       */
      boolean (*settype)(void *data, char *type);
  
      /**
       * Set an HTTP mime header.
       *
       * @param data The web-server specific data (wa_request->data).
       * @param name The mime header name.
       * @param value The mime header value.
       * @return TRUE on success, FALSE otherwise
       */
      boolean (*setheader)(void *data, char *name, char *value);
  
      /**
       * Commit the first part of the response (status and headers).
       *
       * @param data The web-server specific data (wa_request->data).
       * @return TRUE on success, FALSE otherwise
       */
      boolean (*commit)(void *data);
  
      /**
       * Write part of the response data back to the client.
       *
       * @param buf The buffer holding the data to be written.
       * @param len The number of characters to be written.
       * @return The number of characters written to the client or -1 on error.
       */
      int (*write)(void *data, char *buf, int len);
  
      /**
       * Flush any unwritten response data to the client.
       *
       * @param data The web-server specific data (wa_request->data).
       * @return TRUE on success, FALSE otherwise
       */
      boolean (*flush)(void *);
  };
  
  #endif // ifdef _WA_H_
  
  
  
  1.1                  jakarta-tomcat-4.0/connector/src/webapplib/wa_connection.c
  
  Index: wa_connection.c
  ===================================================================
  #include <wa.h>
  
  /* The list of all available connections */
  wa_connection *wa_connections=NULL;
  
  /**
   * Create a new connection.
   *
   * @param name The connection unique name.
   * @param prov The provider name (must be compiled in).
   * @param parm An extra configuration parameter (might be null).
   * @return NULL or a string describing te error message.
   */
  const char *wa_connection_create(char *name, char *prov, char *parm) {
      const char *mesg=NULL;
      wa_connection *conn=NULL;
      wa_connection *curr=NULL;
      wa_provider *p=NULL;
  
      // Check basic parameters
      if (name==NULL) return("Connection name not specified");
      if (prov==NULL) return("Connection provider not specified");
      
      // Try to retrieve the provider by name
      if ((p=wa_provider_get(prov))==NULL) return("Provider not found");
      
      // Allocate connection structure and set basic values
      conn=(wa_connection *)malloc(sizeof(struct wa_connection));
      conn->name=strdup(name);
      conn->prov=p;
      conn->next=NULL;
  
      // Call the provider specific configuration function
      mesg=((*p->configure)(conn,parm));
      if (mesg!=NULL) return(mesg);
  
      // Store this connection in our configurations
      if (wa_connections==NULL) {
          wa_connections=conn;
          return(NULL);
      }
  
      // Iterate thru the list of connections
      curr=wa_connections;
      while(curr!=NULL) {
          if (strcasecmp(curr->name,name)==0)
              return("Duplicate connection name");
          if (curr->next==NULL) {
              curr->next=conn;
              return(NULL);
          }
          curr=curr->next;
      }
  
      // Why the hack are we here?
      return("Unknown error trying to create connection");
  }
  
  /**
   * Get a specific webapp connection.
   *
   * @param name The connection name.
   * @return The wa_connection associated with the name or NULL.
   */
  wa_connection *wa_connection_get(char *name) {
      wa_connection *curr=wa_connections;
  
      // Iterate thru our hosts chain
      while(curr!=NULL) {
          if (strcasecmp(curr->name,name)==0) return(curr);
          else curr=curr->next;
      }
      
      // No host found, sorry!
      return(NULL);
  }
  
  /**
   * Initialize all configured connections.
   */
  void wa_connection_init(void) {
      wa_connection *curr=wa_connections;
  
      // Iterate thru our hosts chain
      while(curr!=NULL) {
          (*curr->prov->init)(curr);
          curr=curr->next;
      }
  }
  
  /**
   * Initialize all configured connections.
   */
  void wa_connection_destroy(void) {
      wa_connection *curr=wa_connections;
  
      // Iterate thru our hosts chain
      while(curr!=NULL) {
          (*curr->prov->destroy)(curr);
          curr=curr->next;
      }
  }
  
  
  
  
  1.1                  jakarta-tomcat-4.0/connector/src/webapplib/wa_connection.h
  
  Index: wa_connection.h
  ===================================================================
  #ifndef _WA_CONNECTION_H_
  #define _WA_CONNECTION_H_
  
  /**
   * The wa_connection structure represents a connection to a webapp server.
   */
  struct wa_connection {
      char *name;             // The connection unique name
      wa_provider *prov;      // The connection provider
      void *conf;             // Provider specific configurations
      wa_connection *next;    // The next configured provider
  };
  
  /* The list of configured connections */
  extern wa_connection *wa_connections;
  
  /* Function prototype declaration */
  // Create a new connection.
  const char *wa_connection_create(char *, char *, char *);
  // Get a specific webapp connection.
  wa_connection *wa_connection_get(char *);
  // Initialize all configured connections.
  void wa_connection_init(void);
  // Initialize all configured connections.
  void wa_connection_destroy(void);
  
  #endif // ifdef _WA_CONNECTION_H_
  
  
  
  1.1                  jakarta-tomcat-4.0/connector/src/webapplib/wa_host.c
  
  Index: wa_host.c
  ===================================================================
  #include <wa.h>
  
  /* The list of configured hosts */
  static wa_host *wa_hosts=NULL;
  
  /**
   * Create configuration for a new host.
   *
   * @param name The host primary name.
   * @param port The host primary port.
   * @return NULL or an error message.
   */
  const char *wa_host_create(char *name, int port) {
      wa_host *host=NULL;
      wa_host *curr=NULL;
  
      // Check supplied parameters
      if (name==NULL) return("Host name unspecified");
      if ((port<1)||(port>65535)) return("Invalid port number");
  
      // Allocate wa_host structure and fill it
      host=(wa_host *)malloc(sizeof(wa_host));
      host->name=strdup(name);
      host->port=port;
      host->apps=NULL;
      host->next=NULL;
  
      // This is the first host we configure
      if (wa_hosts==NULL) {
          wa_hosts=host;
          return(NULL);
      }
      
      // We need to check for duplicate hosts
      curr=wa_hosts;
      while(curr!=NULL) {
          // Check for duplicate hosts definitions
          if((strcasecmp(curr->name, name)==0) && (curr->port==port)) {
              return("Host already configured");
          }
          // If this is the last configured host we found our way out
          if(curr->next==NULL) {
              curr->next=host;
              return(NULL);
          }
          // Process the next host in the chain
          curr=curr->next;
      }
  
      // Why are we here?
      return("Unknown error creating host configuration");
  }
  
  /**
   * Get the host configuration.
   *
   * @param name The host primary name.
   * @param port The host primary port.
   * @return The wa_host associated with the host or NULL.
   */
  wa_host *wa_host_get(char *name, int port) {
      wa_host *curr=wa_hosts;
  
      // Iterate thru our hosts chain
      while(curr!=NULL) {
          if((strcasecmp(curr->name, name)==0) && (curr->port==port))
              return(curr);
          else curr=curr->next;
      }
      
      // No host found, sorry!
      return(NULL);
  }
  
  /**
   * Configure a web application for a specific host.
   *
   * @param host The wa_host structure of the host.
   * @param name The web application name.
   * @param path The web application root URI path.
   * @return NULL or an error message.
   */
  const char *wa_host_setapp(wa_host *host, char *name, char *path,
                             wa_connection *conn) {
      wa_application *appl=NULL;
      wa_application *curr=NULL;
      int slashes=0;
      int pathlen=0;
  
      // Check the supplied parameters
      if(host==NULL) return("Host not specified");
      if(name==NULL) return("Web application name not specified");
      if(conn==NULL) return("Connection not specified");
      if(strlen(name)==0) return("Invalid web application name");
      if(path==NULL) return("Web application root path not specified");
      if((pathlen=strlen(path))==0) return("Invalid web application root path");
  
      // Create a new structure and put the name
      appl=(wa_application *)malloc(sizeof(wa_application));
      appl->name=strdup(name);
      appl->conn=conn;
  
      // Check for leading/trailing slashes. Set slashes to 1 if the leading
      // slash is missing, to 2 if the trailing one is missing or to 3 in case
      // both leading and trailing slashes are missing
      if(path[0]!='/') slashes+=1;
      if(path[pathlen-1]!='/') slashes+=2;
  
      // Copy the root path
      if (slashes==0) appl->path=strdup(path);
      if (slashes==1) {
          appl->path=(char *)malloc((pathlen+2)*sizeof(char));
          appl->path[0]='/';
          strncpy(appl->path+1,path,pathlen);
          appl->path[pathlen+1]='\0';
      }
      if (slashes==2) {
          appl->path=(char *)malloc((pathlen+2)*sizeof(char));
          strncpy(appl->path,path,pathlen);
          appl->path[pathlen]='/';
          appl->path[pathlen+1]='\0';
      } 
      if (slashes==3) {
          appl->path=(char *)malloc((pathlen+3)*sizeof(char));
          appl->path[0]='/';
          strncpy(appl->path+1,path,pathlen);
          appl->path[pathlen+1]='/';
          appl->path[pathlen+2]='\0';
      }
  
      // Check if this is the first web application we configure
      if (host->apps==NULL) {
          host->apps=appl;
          return(NULL);
      }
  
      // We need to check all other webapps
      curr=host->apps;
      while(curr!=NULL) {
          // We don't check for web application names as the same web application
          // can be mounted under two different root paths. But we need to check
          // for different root paths.
          char *cpath=curr->path;
          char *npath=appl->path;
          if ((strstr(cpath,npath)==cpath)||(strstr(npath,cpath)==npath))
              return("Another web application uses the same root path");
          // If this is the last configured web application we found our way out
          if (curr->next==NULL) {
              curr->next=appl;
              return(NULL);
          }
          // Process the next web application
          curr=curr->next;
      }
      // Why are we here?
      return("Unknown error creating webapp configuration");
  }
  
  
  /**
   * Configure a web application for a specific host.
   *
   * @param h The host primary name.
   * @param p The host primary port.
   * @param name The web application name.
   * @param path The web application root URI path.
   * @return NULL or an error message.
   */
  const char *wa_host_setapp_byname(char *h, int p, char *name, char *path,
                             wa_connection *conn) {
      wa_host *host=wa_host_get(h, p);
      
      if (host==NULL) return("Host not configured");
      return(wa_host_setapp(host, name, path, conn));
  }
  
  /**
   * Retrieve a web application for a specific host.
   *
   * @param host The wa_host structure of the host.
   * @param uri The URI to be me matched against web application root paths.
   * @return A wa_application structure pointer or NULL.
   */
  wa_application *wa_host_findapp(wa_host *host, char *uri) {
      wa_application *appl=NULL;
  
      if (host==NULL) return(NULL);
  
      // Iterate thru the host web applications
      appl=host->apps;
      while(appl!=NULL) {
          if(strstr(uri,appl->path)==uri) return(appl);
          appl=appl->next;
      }
  
      // Nope, not found!
      return(NULL);
  }
  
  /**
   * Retrieve a web application for a specific host.
   *
   * @param h The host primary name.
   * @param p The host primary port.
   * @param uri The URI to be me matched against web application root paths.
   * @return A wa_application structure pointer or NULL.
   */
  wa_application *wa_host_findapp_byname(char *h, int p, char *uri) {
      wa_host *host=wa_host_get(h, p);
      
      if (host==NULL) return(NULL);
      return(wa_host_findapp(host, uri));
  }
  
  
  
  1.1                  jakarta-tomcat-4.0/connector/src/webapplib/wa_host.h
  
  Index: wa_host.h
  ===================================================================
  #ifndef _WA_HOST_H_
  #define _WA_HOST_H_
  
  /**
   * The wa_application structure contains all required configuration data for
   * web applications.
   */
  struct wa_application {
      char *name;             // The name of this web application
      char *path;             // The web application root URI path
      wa_connection *conn;    // Pointer to the appropriate connection
      wa_application *next;   // Pointer to the next web application
  };
  
  /**
   * The wa_host structure represents a configured host.
   */
  struct wa_host {
      char *name;             // The main name of this host
      int port;               // The main port server
      wa_application *apps;   // The list of configured web applications
      wa_host *next;          // Pointer to the next configured host
  };
  
  /* The list of configured hosts */
  extern wa_host *wa_hosts;
  
  /* Function prototype declaration */
  // Create configuration for a new host.
  const char *wa_host_create(char *, int);
  // Get the host configuration.
  wa_host *wa_host_get(char *, int);
  // Configure a web application for a specific host.
  const char *wa_host_setapp(wa_host *, char *, char *, wa_connection *);
  // Configure a web application for a specific host.
  const char *wa_host_setapp_byname(char *, int, char *, char *, wa_connection *);
  // Retrieve a web application for a specific host.
  wa_application *wa_host_findapp(wa_host *, char *);
  // Retrieve a web application for a specific host.
  wa_application *wa_host_findapp_byname(char *, int , char *);
  
  #endif // ifdef _WA_HOST_H_
  
  
  
  1.1                  jakarta-tomcat-4.0/connector/src/webapplib/wa_provider.c
  
  Index: wa_provider.c
  ===================================================================
  #include <wa.h>
  
  /**
   * Retrieve a provider.
   *
   * @param name The provider name.
   * @return The wa_provider structure or NULL.
   */
  wa_provider *wa_provider_get(char *name) {
      int x=0;
      
      if (name==NULL) return(NULL);
      
      while(TRUE) {
          if (wa_providers[x]==NULL) return(NULL);
          if (strcasecmp(wa_providers[x]->name,name)==0)
              return(wa_providers[x]);
          x++;
      }
  }
  
  /* The list of all compiled in providers */
  wa_provider *wa_providers[] = {
      &wa_provider_info,
      NULL,
  };
  
  
  
  
  1.1                  jakarta-tomcat-4.0/connector/src/webapplib/wa_provider.h
  
  Index: wa_provider.h
  ===================================================================
  #ifndef _WA_PROVIDER_H_
  #define _WA_PROVIDER_H_
  
  /**
   * The wa_provider structure describes a connection provider.
   */
  struct wa_provider {
      // The name of the current provider
      const char *name;
      // Configure a connection
      const char *(*configure) (wa_connection *, char *);
      // Initialize a connection
      void (*init) (wa_connection *);
      // Clean up a connection
      void (*destroy) (wa_connection *);
      // Get a descriptive string on a connection (based on wa_connection->conf)
      char *(*describe) (wa_connection *);
      // Handle an HTTP request
      void (*handle) (wa_request *, wa_callbacks *);
  };
  
  /* The list of all compiled in providers */
  extern wa_provider *wa_providers[];
  
  /* Pointers to the different providers */
  extern wa_provider wa_provider_info;
  
  /* Function prototype declaration */
  // Retrieve a provider.
  wa_provider *wa_provider_get(char *);
  
  #endif // ifndef _WA_PROVIDER_H_
  
  
  1.1                  jakarta-tomcat-4.0/connector/src/webapplib/wa_provider_info.c
  
  Index: wa_provider_info.c
  ===================================================================
  #include <wa.h>
  
  static const char *wa_info_configure(wa_connection *conn, char *param) {
      if(conn==NULL) return("Connection not specified");
      if (param==NULL) conn->conf=strdup("[No data supplied]");
      else conn->conf=strdup(param);
      return(NULL);
  }
  
  static char *wa_info_describe(wa_connection *conn) {
      char buf[1024];
      
      if(conn==NULL) return("Null connection specified");
      sprintf(buf, "Extra parameters: %s", (char *)conn->conf);
      return(strdup(buf));
  }
  
  void wa_info_init(wa_connection *conn) {
  }
  
  void wa_info_destroy(wa_connection *conn) {
  }
  
  void wa_info_handle(wa_request *req, wa_callbacks *cb) {
      (*cb->setstatus)(req->data,200);
      (*cb->settype)(req->data,"text/html");
      (*cb->setheader)(req->data,"The-Freaks","come/out");
      (*cb->commit)(req->data);
      (*cb->write)(req->data,"<HTML><BODY>TEST</BODY></HTML>",30);
      (*cb->flush)(req->data);
  }
  
  wa_provider wa_provider_info = {
      "info",
      wa_info_configure,
      wa_info_init,
      wa_info_destroy,
      wa_info_describe,
      wa_info_handle,
  };
  
  
  
  1.1                  jakarta-tomcat-4.0/connector/src/webapplib/wa_request.c
  
  Index: wa_request.c
  ===================================================================
  #include <wa.h>
  
  /* Function prototype declaration */
  // Handle a request.
  const char *wa_request_handle(wa_request *req, wa_callbacks *cb) {
      if (cb==NULL) return("Callback structure pointer is NULL");
      if (req==NULL) return("Request structure pointer is NULL");
      if (req->host==NULL) return("Web application host is NULL");
      if (req->application==NULL) return("Web application structure is NULL");
      if (req->method==NULL) return("Request method is NULL");
      if (req->uri==NULL) return("Request URI is NULL ");
      if (req->protocol==NULL) return("Request protocol is NULL");
  
      (*req->application->conn->prov->handle)(req,cb);
      return(NULL);
  }    
  
  
  
  1.1                  jakarta-tomcat-4.0/connector/src/webapplib/wa_request.h
  
  Index: wa_request.h
  ===================================================================
  #ifndef _WA_REQUEST_H_
  #define _WA_REQUEST_H_
  
  /**
   * The wa_request structure embodies an HTTP request.
   */
  struct wa_request {
      wa_host *host;                  // The host handling the request
      wa_application *application;    // The application that needs to be called
      void *data;                     // The web-server specific callback data
      char *method;                   // The HTTP method (GET, POST)
      char *uri;                      // The HTTP URI requested
      char *arguments;                // The HTTP query arguments
      char *protocol;                 // The HTTP protocol (HTTP/1.0, HTTP/1.1)
      int header_count;               // The number of headers in this request
      char **header_names;            // The array of header names
      char **header_values;           // The array of header values
  };
  
  /* Function prototype declaration */
  // Handle a request.
  const char *wa_request_handle(wa_request *, wa_callbacks *);
  
  #endif // ifdef _WA_HOST_H_
  
  
  

Mime
View raw message