File: HttpBodyFactory.h

package info (click to toggle)
trafficserver 9.2.5%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 53,008 kB
  • sloc: cpp: 345,484; ansic: 31,134; python: 24,200; sh: 7,271; makefile: 3,045; perl: 2,261; java: 277; pascal: 119; sql: 94; xml: 2
file content (264 lines) | stat: -rw-r--r-- 8,958 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/** @file

  A brief file description

  @section license License

  Licensed to the Apache Software Foundation (ASF) under one
  or more contributor license agreements.  See the NOTICE file
  distributed with this work for additional information
  regarding copyright ownership.  The ASF licenses this file
  to you under the Apache License, Version 2.0 (the
  "License"); you may not use this file except in compliance
  with the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
 */

/****************************************************************************

  HttpBodyFactory.h

  This implements a user-customizable response message generation system.

  The concept is simple.  Error/response messages are classified into
  several types, each given a name, such as "request/header_error".

  The HttpBodyFactory can build a message body for each response type.
  The user can create custom message body text for each type (stored
  in a text file directory), containing templates with space-holders for
  variables which are inline-substituted with current values.  The resulting
  body is dynamically allocated and returned.

  The major data types implemented in this file are:

    HttpBodyFactory       The main data structure which is the machine
                          that maintains configuration information, reads
                          user error message template files, and performs
                          the substitution to generate the message bodies.

    HttpBodySet           The data structure representing a set of
                          templates, including the templates and metadata.

    HttpBodyTemplate      The template loaded from the directory to be
                          instantiated with variables, producing a body.


 ****************************************************************************/

#pragma once

#include <strings.h>
#include <sys/types.h>
#include "tscore/ink_platform.h"
#include "HTTP.h"
#include "HttpConfig.h"
#include "HttpCompat.h"
#include "HttpTransact.h"
#include "tscore/ink_sprintf.h"

#include <memory>
#include <unordered_map>

#define HTTP_BODY_TEMPLATE_MAGIC 0xB0DFAC00
#define HTTP_BODY_SET_MAGIC 0xB0DFAC55
#define HTTP_BODY_FACTORY_MAGIC 0xB0DFACFF

////////////////////////////////////////////////////////////////////////
//
//      class HttpBodyTemplate
//
//      An HttpBodyTemplate object represents a template with HTML
//      text, and unexpanded log fields.  The object also has methods
//      to dump out the contents of the template, and to instantiate
//      the template into a buffer given a context.
//
////////////////////////////////////////////////////////////////////////

class HttpBodyTemplate
{
public:
  HttpBodyTemplate();
  ~HttpBodyTemplate();

  void reset();
  int load_from_file(char *dir, char *file);
  bool
  is_sane()
  {
    return (magic == HTTP_BODY_TEMPLATE_MAGIC);
  }
  char *build_instantiated_buffer(HttpTransact::State *context, int64_t *length_return);

  unsigned int magic;
  int64_t byte_count;
  char *template_buffer;
  char *template_pathname;
};

////////////////////////////////////////////////////////////////////////
//
//      class HttpBodySetRawData
//
//      Raw data members of HttpBodySet
//
////////////////////////////////////////////////////////////////////////

struct HttpBodySetRawData {
  using TemplateTable = std::unordered_map<std::string, HttpBodyTemplate *>;
  unsigned int magic  = 0;
  char *set_name;
  char *content_language;
  char *content_charset;
  std::unique_ptr<TemplateTable> table_of_pages;
};

////////////////////////////////////////////////////////////////////////
//
//      class HttpBodySet
//
//      An HttpBodySet object represents a set of body factory
//      templates.  It includes operators to get the hash table of
//      templates, and the associated metadata for the set.
//
//      The raw data members come from HttpBodySetRawData above
//
////////////////////////////////////////////////////////////////////////

class HttpBodySet : public HttpBodySetRawData
{
public:
  HttpBodySet();
  ~HttpBodySet();

  int init(char *set_name, char *dir);
  bool
  is_sane()
  {
    return (magic == HTTP_BODY_SET_MAGIC);
  }

  HttpBodyTemplate *get_template_by_name(const char *name);
  void set_template_by_name(const char *name, HttpBodyTemplate *t);
};

////////////////////////////////////////////////////////////////////////
//
//      class HttpBodyFactory
//
//      An HttpBodyFactory object is the main object which keeps track
//      of all the response body templates, and which provides the
//      methods to create response bodies.
//
//      Once an HttpBodyFactory object is initialized, and the template
//      data has been loaded, the HttpBodyFactory object allows the
//      caller to make error message bodies w/fabricate_with_old_api
//
////////////////////////////////////////////////////////////////////////

class HttpBodyFactory
{
public:
  using BodySetTable = std::unordered_map<std::string, HttpBodySetRawData *>;
  HttpBodyFactory();
  ~HttpBodyFactory();

  ///////////////////////
  // primary user APIs //
  ///////////////////////
  char *fabricate_with_old_api(const char *type, HttpTransact::State *context, int64_t max_buffer_length,
                               int64_t *resulting_buffer_length, char *content_language_out_buf, size_t content_language_buf_size,
                               char *content_type_out_buf, size_t content_type_buf_size, int format_size, const char *format);

  char *
  getFormat(int64_t max_buffer_length, int64_t *resulting_buffer_length, const char *format, ...)
  {
    char *msg = nullptr;
    if (format) {
      va_list ap;

      va_start(ap, format);

      // The length from ink_bvsprintf includes the trailing NUL, so adjust the final
      // length accordingly. Note that ink_bvsprintf() copies the va_list, so we only
      // have to set it up once.
      int l = ink_bvsprintf(nullptr, format, ap);

      if (l <= max_buffer_length) {
        msg                      = (char *)ats_malloc(l);
        *resulting_buffer_length = ink_bvsprintf(msg, format, ap) - 1;
      }
      va_end(ap);
    }
    return msg;
  }

  void dump_template_tables(FILE *fp = stderr);
  void reconfigure();
  static const char *determine_set_by_language(std::unique_ptr<BodySetTable> &table_of_sets, StrList *acpt_language_list,
                                               StrList *acpt_charset_list, float *Q_best_ptr, int *La_best_ptr, int *Lc_best_ptr,
                                               int *I_best_ptr);

  bool is_response_suppressed(HttpTransact::State *context);

private:
  char *fabricate(StrList *acpt_language_list, StrList *acpt_charset_list, const char *type, HttpTransact::State *context,
                  int64_t *resulting_buffer_length, const char **content_language_return, const char **content_charset_return,
                  const char **set_return = nullptr);

  const char *determine_set_by_language(StrList *acpt_language_list, StrList *acpt_charset_list);
  const char *determine_set_by_host(HttpTransact::State *context);
  HttpBodyTemplate *find_template(const char *set, const char *type, HttpBodySet **body_set_return);
  bool
  is_sane()
  {
    return (magic == HTTP_BODY_FACTORY_MAGIC);
  }
  void
  sanity_check()
  {
    ink_assert(is_sane());
  }

private:
  ////////////////////////////
  // initialization methods //
  ////////////////////////////
  void nuke_template_tables();
  std::unique_ptr<BodySetTable> load_sets_from_directory(char *set_dir);
  HttpBodySet *load_body_set_from_directory(char *set_name, char *tmpl_dir);

  /////////////////////////////////////////////////
  // internal data structure concurrency control //
  /////////////////////////////////////////////////
  void
  lock()
  {
    ink_mutex_acquire(&mutex);
  }
  void
  unlock()
  {
    ink_mutex_release(&mutex);
  }

  /////////////////////////////////////
  // manager configuration variables //
  /////////////////////////////////////
  int enable_customizations = 0;    // 0:no custom,1:custom,2:language-targeted
  bool enable_logging       = true; // the user wants body factory logging

  ////////////////////
  // internal state //
  ////////////////////
  unsigned int magic = HTTP_BODY_FACTORY_MAGIC; // magic for sanity checks/debugging
  ink_mutex mutex;                              // prevents reconfig/read races
  bool callbacks_established = false;           // all config variables present
  std::unique_ptr<BodySetTable> table_of_sets;  // sets of template hash tables
};