1 /** @file
2 Licensed to the Apache Software Foundation (ASF) under one
3 or more contributor license agreements. See the NOTICE file
4 distributed with this work for additional information
5 regarding copyright ownership. The ASF licenses this file
6 to you under the Apache License, Version 2.0 (the
7 "License"); you may not use this file except in compliance
8 with the License. You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17 */
18
19 #include "response.h"
20
21 #include <cinttypes>
22 #include <cstring>
23 #include <mutex>
24
25 #include "ts/ts.h"
26
27 // canned body string for a 416, stolen from nginx
28 std::string const &
bodyString416()29 bodyString416()
30 {
31 static std::string bodystr;
32 static std::mutex mutex;
33 std::lock_guard<std::mutex> const guard(mutex);
34
35 if (bodystr.empty()) {
36 bodystr.append("<html>\n");
37 bodystr.append("<head><title>416 Requested Range Not Satisfiable</title></head>\n");
38 bodystr.append("<body bgcolor=\"white\">\n");
39 bodystr.append("<center><h1>416 Requested Range Not Satisfiable</h1></center>");
40 bodystr.append("<hr><center>ATS/");
41 bodystr.append(TS_VERSION_STRING);
42 bodystr.append("</center>\n");
43 bodystr.append("</body>\n");
44 bodystr.append("</html>\n");
45 }
46
47 return bodystr;
48 }
49
50 // Form a 502 response, preliminary
51 std::string
string502(int const httpver)52 string502(int const httpver)
53 {
54 static std::string msg;
55 static std::mutex mutex;
56 std::lock_guard<std::mutex> const guard(mutex);
57
58 if (msg.empty()) {
59 std::string bodystr;
60 bodystr.append("<html>\n");
61 bodystr.append("<head><title>502 Bad Gateway</title></head>\n");
62 bodystr.append("<body bgcolor=\"white\">\n");
63 bodystr.append("<center><h1>502 Bad Gateway: Missing/Malformed "
64 "Content-Range</h1></center>");
65 bodystr.append("<hr><center>ATS/");
66 bodystr.append(TS_VERSION_STRING);
67 bodystr.append("</center>\n");
68 bodystr.append("</body>\n");
69 bodystr.append("</html>\n");
70
71 char hverstr[64];
72 int const hlen =
73 snprintf(hverstr, sizeof(hverstr), "HTTP/%d.%d 502 Bad Gateway\r\n", TS_HTTP_MAJOR(httpver), TS_HTTP_MINOR(httpver));
74 msg.append(hverstr, hlen);
75
76 char clenstr[1024];
77 int const clen = snprintf(clenstr, sizeof(clenstr), "%lu", bodystr.size());
78 msg.append("Content-Length: ");
79 msg.append(clenstr, clen);
80 msg.append("\r\n");
81
82 msg.append("\r\n");
83 msg.append(bodystr);
84 }
85
86 return msg;
87 }
88
89 void
form416HeaderAndBody(HttpHeader & header,int64_t const contentlen,std::string const & bodystr)90 form416HeaderAndBody(HttpHeader &header, int64_t const contentlen, std::string const &bodystr)
91 {
92 header.removeKey(TS_MIME_FIELD_LAST_MODIFIED, TS_MIME_LEN_LAST_MODIFIED);
93 header.removeKey(TS_MIME_FIELD_EXPIRES, TS_MIME_LEN_EXPIRES);
94 header.removeKey(TS_MIME_FIELD_ETAG, TS_MIME_LEN_ETAG);
95 header.removeKey(TS_MIME_FIELD_ACCEPT_RANGES, TS_MIME_LEN_ACCEPT_RANGES);
96
97 header.setStatus(TS_HTTP_STATUS_REQUESTED_RANGE_NOT_SATISFIABLE);
98 char const *const reason = TSHttpHdrReasonLookup(TS_HTTP_STATUS_REQUESTED_RANGE_NOT_SATISFIABLE);
99 header.setReason(reason, strlen(reason));
100
101 char bufstr[256];
102 int buflen = snprintf(bufstr, sizeof(bufstr), "%lu", bodystr.size());
103 header.setKeyVal(TS_MIME_FIELD_CONTENT_LENGTH, TS_MIME_LEN_CONTENT_LENGTH, bufstr, buflen);
104
105 static char const *const ctypestr = "text/html";
106 static int const ctypelen = strlen(ctypestr);
107 header.setKeyVal(TS_MIME_FIELD_CONTENT_TYPE, TS_MIME_LEN_CONTENT_TYPE, ctypestr, ctypelen);
108
109 buflen = snprintf(bufstr, 255, "*/%" PRId64, contentlen);
110 header.setKeyVal(TS_MIME_FIELD_CONTENT_RANGE, TS_MIME_LEN_CONTENT_RANGE, bufstr, buflen);
111 }
112