~ps10gel/ubuntu/xenial/trafficserver/6.2.0

« back to all changes in this revision

Viewing changes to proxy/http2/HttpMessageBody.cc

  • 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
  Routines to construct and manipulate message bodies and format error responses
 
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
#include "ink_unused.h"  /* MAGIC_EDITING_TAG */
 
25
#include "HttpMessageBody.h"
 
26
#include "HttpConfig.h"
 
27
#include "HttpAssert.h"
 
28
 
 
29
/** This routine returns a constant string name for the status_code. */
 
30
const char *
 
31
HttpMessageBody::StatusCodeName(HTTPStatus status_code)
 
32
{
 
33
  return http_hdr_reason_lookup(status_code);
 
34
}
 
35
 
 
36
/**
 
37
  This routine creates an HTTP error message body for the status code and
 
38
  printf format string format and args va, allocates a response buffer
 
39
  (using malloc), and places the result body in the buffer. The body
 
40
  will be NUL terminated.
 
41
 
 
42
  The caller must free() the returned object when done.
 
43
 
 
44
  The reason string allows you to override the default reason phrase for
 
45
  the status code. If it is NULL, the default is used. If format is NULL
 
46
  or "", no additional text is added.
 
47
 
 
48
  NULL is returned if the resulting length exceeds max_buffer_length.
 
49
 
 
50
*/
 
51
char *
 
52
HttpMessageBody::MakeErrorBodyVA(int64_t max_buffer_length,
 
53
                                 int64_t *resulting_buffer_length,
 
54
                                 const HttpConfigParams * config,
 
55
                                 HTTPStatus status_code, const char *reason, const char *format, va_list va)
 
56
{
 
57
  NOWARN_UNUSED(config);
 
58
  char *p, *outbuf = NULL;
 
59
  char error_title[128];
 
60
  int pass;
 
61
  int64_t l, output_length;
 
62
 
 
63
  if (reason == NULL)
 
64
    reason = (char *) (StatusCodeName(status_code));
 
65
 
 
66
  output_length = 0;
 
67
  *resulting_buffer_length = 0;
 
68
 
 
69
  for (pass = 1; pass <= 2; pass++) {
 
70
    if (pass == 2) {
 
71
      if (outbuf)
 
72
        xfree(outbuf);
 
73
      if (output_length > max_buffer_length)
 
74
        return (NULL);
 
75
      else
 
76
        outbuf = (char *) xmalloc(output_length);
 
77
    }
 
78
 
 
79
    l = 0;
 
80
    p = outbuf;
 
81
 
 
82
    error_title[sizeof(error_title) - 1] = '\0';
 
83
    strncpy(error_title, reason, sizeof(error_title) - 1);
 
84
 
 
85
    p = (pass == 1 ? (char *) NULL : &(outbuf[l]));
 
86
    l += ink_bsprintf(p, "<HEAD><TITLE>%s</TITLE></HEAD>\n", error_title) - 1;
 
87
 
 
88
    p = (pass == 1 ? (char *) NULL : &(outbuf[l]));
 
89
    l += ink_bsprintf(p, "<BODY BGCOLOR=\"white\" FGCOLOR=\"black\">") - 1;
 
90
 
 
91
    p = (pass == 1 ? (char *) NULL : &(outbuf[l]));
 
92
    //l += ink_bsprintf(p,"<H1>%s</H1><HR>\n",error_title) - 1;
 
93
    l += ink_bsprintf(p, "\n") - 1;
 
94
 
 
95
    p = (pass == 1 ? (char *) NULL : &(outbuf[l]));
 
96
    l += ink_bsprintf(p, "<FONT FACE=\"Helvetica,Arial\"><B>\n") - 1;
 
97
 
 
98
    if (format && *format) {
 
99
      p = (pass == 1 ? (char *) NULL : &(outbuf[l]));
 
100
      //l += ink_bsprintf(p,"Description: ") - 1;
 
101
      l += ink_bsprintf(p, " ") - 1;
 
102
 
 
103
      p = (pass == 1 ? (char *) NULL : &(outbuf[l]));
 
104
      l += ink_bvsprintf(p, format, va) - 1;
 
105
    }
 
106
 
 
107
    p = (pass == 1 ? (char *) NULL : &(outbuf[l]));
 
108
    l += ink_bsprintf(p, "</B></FONT>\n") - 1;
 
109
 
 
110
    p = (pass == 1 ? (char *) NULL : &(outbuf[l]));
 
111
    //l += ink_bsprintf(p,"<HR>\n") - 1;
 
112
    l += ink_bsprintf(p, "\n") - 1;
 
113
 
 
114
    // Moved trailing info into a comment
 
115
    p = (pass == 1 ? (char *) NULL : &(outbuf[l]));
 
116
    l += ink_bsprintf(p, "<!-- default \"%s\" response (%d) -->\n", reason, status_code) - 1;
 
117
 
 
118
    p = (pass == 1 ? (char *) NULL : &(outbuf[l]));
 
119
    l += ink_bsprintf(p, "</BODY>\n") - 1;
 
120
 
 
121
    l++;                        // leave room for trailing NUL
 
122
 
 
123
    if (pass == 2) {
 
124
      HTTP_ASSERT(l == output_length);
 
125
    }
 
126
    output_length = l;
 
127
  }
 
128
 
 
129
  *resulting_buffer_length = output_length;
 
130
  return (outbuf);
 
131
}