3
Routines to construct and manipulate message bodies and format error responses
5
@section license License
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
15
http://www.apache.org/licenses/LICENSE-2.0
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.
24
#include "ink_unused.h" /* MAGIC_EDITING_TAG */
25
#include "HttpMessageBody.h"
26
#include "HttpConfig.h"
27
#include "HttpAssert.h"
29
/** This routine returns a constant string name for the status_code. */
31
HttpMessageBody::StatusCodeName(HTTPStatus status_code)
33
return http_hdr_reason_lookup(status_code);
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.
42
The caller must free() the returned object when done.
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.
48
NULL is returned if the resulting length exceeds max_buffer_length.
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)
57
NOWARN_UNUSED(config);
58
char *p, *outbuf = NULL;
59
char error_title[128];
61
int64_t l, output_length;
64
reason = (char *) (StatusCodeName(status_code));
67
*resulting_buffer_length = 0;
69
for (pass = 1; pass <= 2; pass++) {
73
if (output_length > max_buffer_length)
76
outbuf = (char *) xmalloc(output_length);
82
error_title[sizeof(error_title) - 1] = '\0';
83
strncpy(error_title, reason, sizeof(error_title) - 1);
85
p = (pass == 1 ? (char *) NULL : &(outbuf[l]));
86
l += ink_bsprintf(p, "<HEAD><TITLE>%s</TITLE></HEAD>\n", error_title) - 1;
88
p = (pass == 1 ? (char *) NULL : &(outbuf[l]));
89
l += ink_bsprintf(p, "<BODY BGCOLOR=\"white\" FGCOLOR=\"black\">") - 1;
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;
95
p = (pass == 1 ? (char *) NULL : &(outbuf[l]));
96
l += ink_bsprintf(p, "<FONT FACE=\"Helvetica,Arial\"><B>\n") - 1;
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;
103
p = (pass == 1 ? (char *) NULL : &(outbuf[l]));
104
l += ink_bvsprintf(p, format, va) - 1;
107
p = (pass == 1 ? (char *) NULL : &(outbuf[l]));
108
l += ink_bsprintf(p, "</B></FONT>\n") - 1;
110
p = (pass == 1 ? (char *) NULL : &(outbuf[l]));
111
//l += ink_bsprintf(p,"<HR>\n") - 1;
112
l += ink_bsprintf(p, "\n") - 1;
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;
118
p = (pass == 1 ? (char *) NULL : &(outbuf[l]));
119
l += ink_bsprintf(p, "</BODY>\n") - 1;
121
l++; // leave room for trailing NUL
124
HTTP_ASSERT(l == output_length);
129
*resulting_buffer_length = output_length;