• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

HttpBodyFactory.h

Go to the documentation of this file.
00001 /** @file
00002 
00003   A brief file description
00004 
00005   @section license License
00006 
00007   Licensed to the Apache Software Foundation (ASF) under one
00008   or more contributor license agreements.  See the NOTICE file
00009   distributed with this work for additional information
00010   regarding copyright ownership.  The ASF licenses this file
00011   to you under the Apache License, Version 2.0 (the
00012   "License"); you may not use this file except in compliance
00013   with the License.  You may obtain a copy of the License at
00014 
00015       http://www.apache.org/licenses/LICENSE-2.0
00016 
00017   Unless required by applicable law or agreed to in writing, software
00018   distributed under the License is distributed on an "AS IS" BASIS,
00019   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00020   See the License for the specific language governing permissions and
00021   limitations under the License.
00022  */
00023 
00024 /****************************************************************************
00025 
00026   HttpBodyFactory.h
00027 
00028   This implements a user-customizable response message generation system.
00029 
00030   The concept is simple.  Error/response messages are classified into
00031   several types, each given a name, such as "request/header_error".
00032 
00033   The HttpBodyFactory can build a message body for each response type.
00034   The user can create custom message body text for each type (stored
00035   in a text file directory), containing templates with space-holders for
00036   variables which are inline-substituted with curent values.  The resulting
00037   body is dynamically allocated and returned.
00038 
00039   The major data types implemented in this file are:
00040 
00041     HttpBodyFactory       The main data structure which is the machine
00042                           that maintains configuration information, reads
00043                           user error message template files, and performs
00044                           the substitution to generate the message bodies.
00045 
00046     HttpBodySet           The data structure representing a set of
00047                           templates, including the templates and metadata.
00048 
00049     HttpBodyTemplate      The template loaded from the directory to be
00050                           instantiated with variables, producing a body.
00051 
00052 
00053  ****************************************************************************/
00054 
00055 #ifndef _HttpBodyFactory_h_
00056 #define _HttpBodyFactory_h_
00057 
00058 #include <strings.h>
00059 #include <sys/types.h>
00060 #include "libts.h"
00061 #include "HTTP.h"
00062 #include "HttpConfig.h"
00063 #include "HttpCompat.h"
00064 #include "HttpTransact.h"
00065 #include "Error.h"
00066 #include "Main.h"
00067 #include "RawHashTable.h"
00068 
00069 
00070 #define HTTP_BODY_TEMPLATE_MAGIC 0xB0DFAC00
00071 #define HTTP_BODY_SET_MAGIC      0xB0DFAC55
00072 #define HTTP_BODY_FACTORY_MAGIC  0xB0DFACFF
00073 
00074 ////////////////////////////////////////////////////////////////////////
00075 //
00076 //      class HttpBodyTemplate
00077 //
00078 //      An HttpBodyTemplate object represents a template with HTML
00079 //      text, and unexpanded log fields.  The object also has methods
00080 //      to dump out the contents of the template, and to instantiate
00081 //      the template into a buffer given a context.
00082 //
00083 ////////////////////////////////////////////////////////////////////////
00084 
00085 class HttpBodyTemplate
00086 {
00087 public:
00088   HttpBodyTemplate();
00089   ~HttpBodyTemplate();
00090 
00091   void reset();
00092   int load_from_file(char *dir, char *file);
00093   bool is_sane()
00094   {
00095     return (magic == HTTP_BODY_TEMPLATE_MAGIC);
00096   }
00097   char *build_instantiated_buffer(HttpTransact::State * context, int64_t *length_return);
00098 
00099   unsigned int magic;
00100   int64_t byte_count;
00101   char *template_buffer;
00102   char *template_pathname;
00103 };
00104 
00105 
00106 ////////////////////////////////////////////////////////////////////////
00107 //
00108 //      class HttpBodySet
00109 //
00110 //      An HttpBodySet object represents a set of body factory
00111 //      templates.  It includes operators to get the hash table of
00112 //      templates, and the associated metadata for the set.
00113 //
00114 //      The raw data members come from HttpBodySetRawData, which
00115 //      is defined in proxy/hdrs/HttpCompat.h
00116 //
00117 ////////////////////////////////////////////////////////////////////////
00118 
00119 class HttpBodySet:public HttpBodySetRawData
00120 {
00121 public:
00122   HttpBodySet();
00123   ~HttpBodySet();
00124 
00125   int init(char *set_name, char *dir);
00126   bool is_sane()
00127   {
00128     return (magic == HTTP_BODY_SET_MAGIC);
00129   }
00130 
00131   HttpBodyTemplate *get_template_by_name(const char *name);
00132   void set_template_by_name(const char *name, HttpBodyTemplate * t);
00133 };
00134 
00135 
00136 ////////////////////////////////////////////////////////////////////////
00137 //
00138 //      class HttpBodyFactory
00139 //
00140 //      An HttpBodyFactory object is the main object which keeps track
00141 //      of all the response body templates, and which provides the
00142 //      methods to create response bodies.
00143 //
00144 //      Once an HttpBodyFactory object is initialized, and the template
00145 //      data has been loaded, the HttpBodyFactory object allows the
00146 //      caller to make error message bodies w/fabricate_with_old_api
00147 //
00148 ////////////////////////////////////////////////////////////////////////
00149 
00150 class HttpBodyFactory
00151 {
00152 public:
00153   HttpBodyFactory();
00154   ~HttpBodyFactory();
00155 
00156   ///////////////////////
00157   // primary user APIs //
00158   ///////////////////////
00159   char *fabricate_with_old_api(const char *type, HttpTransact::State * context,
00160                                int64_t max_buffer_length, int64_t *resulting_buffer_length,
00161                                char* content_language_out_buf,
00162                                size_t content_language_buf_size,
00163                                char* content_type_out_buf,
00164                                size_t content_type_buf_size,
00165                                const char *format, va_list ap);
00166 
00167   char *fabricate_with_old_api_build_va(const char *type, HttpTransact::State * context,
00168                                         int64_t max_buffer_length, int64_t *resulting_buffer_length,
00169                                         char* content_language_out_buf, size_t content_language_buf_size,
00170                                         char* content_type_out_buf, size_t content_type_buf_size,
00171                                         const char *format, ...)
00172   {
00173     char * msg;
00174     va_list ap;
00175 
00176     va_start(ap, format);
00177     msg = fabricate_with_old_api(type, context, max_buffer_length, resulting_buffer_length,
00178                                    content_language_out_buf, content_language_buf_size,
00179                                    content_type_out_buf, content_type_buf_size, format, ap);
00180     va_end(ap);
00181     return msg;
00182   }
00183 
00184   void dump_template_tables(FILE * fp = stderr);
00185   void reconfigure();
00186 
00187 private:
00188 
00189   char *fabricate(StrList * acpt_language_list,
00190                   StrList * acpt_charset_list,
00191                   const char *type, HttpTransact::State * context,
00192                   int64_t *resulting_buffer_length,
00193                   const char **content_language_return,
00194                   const char **content_charset_return, const char **set_return = NULL);
00195 
00196   const char *determine_set_by_language(StrList * acpt_language_list, StrList * acpt_charset_list);
00197   HttpBodyTemplate *find_template(const char *set, const char *type, HttpBodySet ** body_set_return);
00198   bool is_response_suppressed(HttpTransact::State * context);
00199   bool is_sane()
00200   {
00201     return (magic == HTTP_BODY_FACTORY_MAGIC);
00202   }
00203   void sanity_check()
00204   {
00205     ink_assert(is_sane());
00206   }
00207 
00208 private:
00209   ////////////////////////////
00210   // initialization methods //
00211   ////////////////////////////
00212   void nuke_template_tables();
00213   RawHashTable *load_sets_from_directory(char *set_dir);
00214   HttpBodySet *load_body_set_from_directory(char *set_name, char *tmpl_dir);
00215 
00216   /////////////////////////////////////////////////
00217   // internal data structure concurrency control //
00218   /////////////////////////////////////////////////
00219   void lock()
00220   {
00221     ink_mutex_acquire(&mutex);
00222   }
00223   void unlock()
00224   {
00225     ink_mutex_release(&mutex);
00226   }
00227 
00228   /////////////////////////////////////
00229   // manager configuration variables //
00230   /////////////////////////////////////
00231   int enable_customizations;    // 0:no custom,1:custom,2:language-targeted
00232   bool enable_logging;          // the user wants body factory logging
00233   int response_suppression_mode;        // when to suppress responses
00234 
00235   ////////////////////
00236   // internal state //
00237   ////////////////////
00238   unsigned int magic;           // magic for sanity checks/debugging
00239   ink_mutex mutex;              // prevents reconfig/read races
00240   bool callbacks_established;   // all config variables present
00241   RawHashTable *table_of_sets;  // sets of template hash tables
00242 };
00243 
00244 #endif

Generated by  doxygen 1.7.1