~ps10gel/ubuntu/xenial/trafficserver/6.2.0

« back to all changes in this revision

Viewing changes to proxy/http2/HttpBodyFactory.h

  • Committer: Bazaar Package Importer
  • Author(s): Arno Toell
  • Date: 2011-01-13 11:49:18 UTC
  • Revision ID: james.westby@ubuntu.com-20110113114918-vu422h8dknrgkj15
Tags: upstream-2.1.5-unstable
ImportĀ upstreamĀ versionĀ 2.1.5-unstable

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/** @file
 
2
 
 
3
  A brief file description
 
4
 
 
5
  @section license License
 
6
 
 
7
  Licensed to the Apache Software Foundation (ASF) under one
 
8
  or more contributor license agreements.  See the NOTICE file
 
9
  distributed with this work for additional information
 
10
  regarding copyright ownership.  The ASF licenses this file
 
11
  to you under the Apache License, Version 2.0 (the
 
12
  "License"); you may not use this file except in compliance
 
13
  with the License.  You may obtain a copy of the License at
 
14
 
 
15
      http://www.apache.org/licenses/LICENSE-2.0
 
16
 
 
17
  Unless required by applicable law or agreed to in writing, software
 
18
  distributed under the License is distributed on an "AS IS" BASIS,
 
19
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
20
  See the License for the specific language governing permissions and
 
21
  limitations under the License.
 
22
 */
 
23
 
 
24
/****************************************************************************
 
25
 
 
26
  HttpBodyFactory.h
 
27
 
 
28
  This implements a user-customizable response message generation system.
 
29
 
 
30
  The concept is simple.  Error/response messages are classified into
 
31
  several types, each given a name, such as "request/header_error".
 
32
 
 
33
  The HttpBodyFactory can build a message body for each response type.
 
34
  The user can create custom message body text for each type (stored
 
35
  in a text file directory), containing templates with space-holders for
 
36
  variables which are inline-substituted with curent values.  The resulting
 
37
  body is dynamically allocated and returned.
 
38
 
 
39
  The major data types implemented in this file are:
 
40
 
 
41
    HttpBodyFactory       The main data structure which is the machine
 
42
                          that maintains configuration information, reads
 
43
                          user error message template files, and performs
 
44
                          the substitution to generate the message bodies.
 
45
 
 
46
    HttpBodySet           The data structure representing a set of
 
47
                          templates, including the templates and metadata.
 
48
 
 
49
    HttpBodyTemplate      The template loaded from the directory to be
 
50
                          instantiated with variables, producing a body.
 
51
 
 
52
 
 
53
 ****************************************************************************/
 
54
 
 
55
#ifndef _HttpBodyFactory_h_
 
56
#define _HttpBodyFactory_h_
 
57
 
 
58
#include <strings.h>
 
59
#include <sys/types.h>
 
60
#include "libts.h"
 
61
#include "HTTP.h"
 
62
#include "HttpConfig.h"
 
63
#include "HttpCompat.h"
 
64
#include "HttpTransact.h"
 
65
#include "Error.h"
 
66
#include "Main.h"
 
67
#include "RawHashTable.h"
 
68
 
 
69
 
 
70
#define HTTP_BODY_TEMPLATE_MAGIC 0xB0DFAC00
 
71
#define HTTP_BODY_SET_MAGIC      0xB0DFAC55
 
72
#define HTTP_BODY_FACTORY_MAGIC  0xB0DFACFF
 
73
 
 
74
////////////////////////////////////////////////////////////////////////
 
75
//
 
76
//      class HttpBodyTemplate
 
77
//
 
78
//      An HttpBodyTemplate object represents a template with HTML
 
79
//      text, and unexpanded log fields.  The object also has methods
 
80
//      to dump out the contents of the template, and to instantiate
 
81
//      the template into a buffer given a context.
 
82
//
 
83
////////////////////////////////////////////////////////////////////////
 
84
 
 
85
class HttpBodyTemplate
 
86
{
 
87
public:
 
88
  HttpBodyTemplate();
 
89
  ~HttpBodyTemplate();
 
90
 
 
91
  void reset();
 
92
  int load_from_file(char *dir, char *file);
 
93
  bool is_sane()
 
94
  {
 
95
    return (magic == HTTP_BODY_TEMPLATE_MAGIC);
 
96
  }
 
97
  char *build_instantiated_buffer(HttpTransact::State * context, int64_t *length_return);
 
98
 
 
99
  unsigned int magic;
 
100
  int64_t byte_count;
 
101
  char *template_buffer;
 
102
  char *template_pathname;
 
103
};
 
104
 
 
105
 
 
106
////////////////////////////////////////////////////////////////////////
 
107
//
 
108
//      class HttpBodySet
 
109
//
 
110
//      An HttpBodySet object represents a set of body factory
 
111
//      templates.  It includes operators to get the hash table of
 
112
//      templates, and the associated metadata for the set.
 
113
//
 
114
//      The raw data members come from HttpBodySetRawData, which
 
115
//      is defined in proxy/hdrs/HttpCompat.h
 
116
//
 
117
////////////////////////////////////////////////////////////////////////
 
118
 
 
119
class HttpBodySet:public HttpBodySetRawData
 
120
{
 
121
public:
 
122
  HttpBodySet();
 
123
  ~HttpBodySet();
 
124
 
 
125
  int init(char *set_name, char *dir);
 
126
  bool is_sane()
 
127
  {
 
128
    return (magic == HTTP_BODY_SET_MAGIC);
 
129
  }
 
130
 
 
131
  HttpBodyTemplate *get_template_by_name(const char *name);
 
132
  void set_template_by_name(const char *name, HttpBodyTemplate * t);
 
133
};
 
134
 
 
135
 
 
136
////////////////////////////////////////////////////////////////////////
 
137
//
 
138
//      class HttpBodyFactory
 
139
//
 
140
//      An HttpBodyFactory object is the main object which keeps track
 
141
//      of all the response body templates, and which provides the
 
142
//      methods to create response bodies.
 
143
//
 
144
//      Once an HttpBodyFactory object is initialized, and the template
 
145
//      data has been loaded, the HttpBodyFactory object allows the
 
146
//      caller to make error message bodies w/fabricate_with_old_api
 
147
//
 
148
////////////////////////////////////////////////////////////////////////
 
149
 
 
150
class HttpBodyFactory
 
151
{
 
152
public:
 
153
  HttpBodyFactory();
 
154
  ~HttpBodyFactory();
 
155
 
 
156
  ///////////////////////
 
157
  // primary user APIs //
 
158
  ///////////////////////
 
159
  char *fabricate_with_old_api(const char *type, HttpTransact::State * context,
 
160
                               int64_t max_buffer_length, int64_t *resulting_buffer_length,
 
161
                               char* content_language_out_buf,
 
162
                               size_t content_language_buf_size,
 
163
                               char* content_type_out_buf,
 
164
                               size_t content_type_buf_size,
 
165
                               HTTPStatus status_code, const char *reason_or_null, const char *format, va_list ap);
 
166
 
 
167
  char *fabricate_with_old_api_build_va(const char *type, HttpTransact::State * context,
 
168
                                        int64_t max_buffer_length, int64_t *resulting_buffer_length,
 
169
                                        char* content_language_out_buf,
 
170
                                        size_t content_language_buf_size,
 
171
                                        char* content_type_out_buf,
 
172
                                        size_t content_type_buf_size,
 
173
                                        HTTPStatus status_code, const char *reason_or_null, const char *format, ...)
 
174
  {
 
175
    va_list ap;
 
176
      va_start(ap, format);
 
177
      return (fabricate_with_old_api(type, context, max_buffer_length,
 
178
                                     resulting_buffer_length,
 
179
                                     content_language_out_buf, content_language_buf_size,
 
180
                                     content_type_out_buf, content_type_buf_size,
 
181
                                     status_code, reason_or_null, format, ap));
 
182
  }
 
183
 
 
184
  void dump_template_tables(FILE * fp = stderr);
 
185
  void reconfigure();
 
186
 
 
187
private:
 
188
 
 
189
  char *fabricate(StrList * acpt_language_list,
 
190
                  StrList * acpt_charset_list,
 
191
                  const char *type, HttpTransact::State * context,
 
192
                  int64_t *resulting_buffer_length,
 
193
                  const char **content_language_return,
 
194
                  const char **content_charset_return, const char **set_return = NULL);
 
195
 
 
196
  const char *determine_set_by_language(StrList * acpt_language_list, StrList * acpt_charset_list);
 
197
  HttpBodyTemplate *find_template(const char *set, const char *type, HttpBodySet ** body_set_return);
 
198
  bool is_response_suppressed(HttpTransact::State * context);
 
199
  bool is_sane()
 
200
  {
 
201
    return (magic == HTTP_BODY_FACTORY_MAGIC);
 
202
  }
 
203
  void sanity_check()
 
204
  {
 
205
    ink_assert(is_sane());
 
206
  }
 
207
 
 
208
private:
 
209
  ////////////////////////////
 
210
  // initialization methods //
 
211
  ////////////////////////////
 
212
  void nuke_template_tables();
 
213
  RawHashTable *load_sets_from_directory(char *set_dir);
 
214
  HttpBodySet *load_body_set_from_directory(char *set_name, char *tmpl_dir);
 
215
 
 
216
  /////////////////////////////////////////////////
 
217
  // internal data structure concurrency control //
 
218
  /////////////////////////////////////////////////
 
219
  void lock()
 
220
  {
 
221
    ink_mutex_acquire(&mutex);
 
222
  }
 
223
  void unlock()
 
224
  {
 
225
    ink_mutex_release(&mutex);
 
226
  }
 
227
 
 
228
  /////////////////////////////////////
 
229
  // manager configuration variables //
 
230
  /////////////////////////////////////
 
231
  int enable_customizations;    // 0:no custom,1:custom,2:language-targeted
 
232
  bool enable_logging;          // the user wants body factory logging
 
233
  char *directory_of_template_sets;     // root directory for template sets
 
234
  int response_suppression_mode;        // when to suppress responses
 
235
 
 
236
  ////////////////////
 
237
  // internal state //
 
238
  ////////////////////
 
239
  unsigned int magic;           // magic for sanity checks/debugging
 
240
  ink_mutex mutex;              // prevents reconfig/read races
 
241
  bool callbacks_established;   // all config variables present
 
242
  RawHashTable *table_of_sets;  // sets of template hash tables
 
243
};
 
244
 
 
245
#endif