File: response.cc

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 (111 lines) | stat: -rw-r--r-- 3,862 bytes parent folder | download | duplicates (3)
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
/** @file
  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.
 */

#include "response.h"

#include <cinttypes>
#include <cstring>
#include <mutex>

#include "ts/ts.h"

// canned body string for a 416, stolen from nginx
std::string const &
bodyString416()
{
  static std::string bodystr;
  static std::mutex mutex;
  std::lock_guard<std::mutex> const guard(mutex);

  if (bodystr.empty()) {
    bodystr.append("<html>\n");
    bodystr.append("<head><title>416 Requested Range Not Satisfiable</title></head>\n");
    bodystr.append("<body bgcolor=\"white\">\n");
    bodystr.append("<center><h1>416 Requested Range Not Satisfiable</h1></center>");
    bodystr.append("<hr><center>ATS/");
    bodystr.append(TS_VERSION_STRING);
    bodystr.append("</center>\n");
    bodystr.append("</body>\n");
    bodystr.append("</html>\n");
  }

  return bodystr;
}

// Form a 502 response, preliminary
std::string
string502(int const httpver)
{
  static std::string msg;
  static std::mutex mutex;
  std::lock_guard<std::mutex> const guard(mutex);

  if (msg.empty()) {
    std::string bodystr;
    bodystr.append("<html>\n");
    bodystr.append("<head><title>502 Bad Gateway</title></head>\n");
    bodystr.append("<body bgcolor=\"white\">\n");
    bodystr.append("<center><h1>502 Bad Gateway: Missing/Malformed "
                   "Content-Range</h1></center>");
    bodystr.append("<hr><center>ATS/");
    bodystr.append(TS_VERSION_STRING);
    bodystr.append("</center>\n");
    bodystr.append("</body>\n");
    bodystr.append("</html>\n");

    char hverstr[64];
    int const hlen =
      snprintf(hverstr, sizeof(hverstr), "HTTP/%d.%d 502 Bad Gateway\r\n", TS_HTTP_MAJOR(httpver), TS_HTTP_MINOR(httpver));
    msg.append(hverstr, hlen);

    char clenstr[1024];
    int const clen = snprintf(clenstr, sizeof(clenstr), "%lu", bodystr.size());
    msg.append("Content-Length: ");
    msg.append(clenstr, clen);
    msg.append("\r\n");

    msg.append("\r\n");
    msg.append(bodystr);
  }

  return msg;
}

void
form416HeaderAndBody(HttpHeader &header, int64_t const contentlen, std::string const &bodystr)
{
  header.removeKey(TS_MIME_FIELD_LAST_MODIFIED, TS_MIME_LEN_LAST_MODIFIED);
  header.removeKey(TS_MIME_FIELD_EXPIRES, TS_MIME_LEN_EXPIRES);
  header.removeKey(TS_MIME_FIELD_ETAG, TS_MIME_LEN_ETAG);
  header.removeKey(TS_MIME_FIELD_ACCEPT_RANGES, TS_MIME_LEN_ACCEPT_RANGES);

  header.setStatus(TS_HTTP_STATUS_REQUESTED_RANGE_NOT_SATISFIABLE);
  char const *const reason = TSHttpHdrReasonLookup(TS_HTTP_STATUS_REQUESTED_RANGE_NOT_SATISFIABLE);
  header.setReason(reason, strlen(reason));

  char bufstr[256];
  int buflen = snprintf(bufstr, sizeof(bufstr), "%lu", bodystr.size());
  header.setKeyVal(TS_MIME_FIELD_CONTENT_LENGTH, TS_MIME_LEN_CONTENT_LENGTH, bufstr, buflen);

  static char const *const ctypestr = "text/html";
  static int const ctypelen         = strlen(ctypestr);
  header.setKeyVal(TS_MIME_FIELD_CONTENT_TYPE, TS_MIME_LEN_CONTENT_TYPE, ctypestr, ctypelen);

  buflen = snprintf(bufstr, 255, "*/%" PRId64, contentlen);
  header.setKeyVal(TS_MIME_FIELD_CONTENT_RANGE, TS_MIME_LEN_CONTENT_RANGE, bufstr, buflen);
}