StatusLine.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2020 The Squid Software Foundation and contributors
3  *
4  * Squid software is distributed under GPLv2+ license and includes
5  * contributions from numerous individuals and organizations.
6  * Please see the COPYING and CONTRIBUTORS files for details.
7  */
8 
9 /* DEBUG: section 57 HTTP Status-line */
10 
11 #include "squid.h"
12 #include "base/Packable.h"
13 #include "Debug.h"
15 #include "http/StatusLine.h"
16 #include "parser/forward.h"
17 #include "parser/Tokenizer.h"
18 
19 #include <algorithm>
20 
21 void
23 {
25 }
26 
27 void
29 {
31 }
32 
33 /* set values */
34 void
35 Http::StatusLine::set(const AnyP::ProtocolVersion &newVersion, const Http::StatusCode newStatus, const char *newReason)
36 {
37  protocol = AnyP::PROTO_HTTP;
38  version = newVersion;
39  status_ = newStatus;
40  /* Note: no xstrdup for 'reason', assumes constant 'reasons' */
41  reason_ = newReason;
42 }
43 
44 const char *
46 {
47  return reason_ ? reason_ : Http::StatusCodeString(status());
48 }
49 
50 void
52 {
53  assert(p);
54 
55  auto packedStatus = status();
56  auto packedReason = reason();
57 
58  if (packedStatus == Http::scNone) {
59  static unsigned int reports = 0;
60  if (++reports <= 100)
61  debugs(57, DBG_IMPORTANT, "BUG: the internalized response lacks status-code");
62  packedStatus = Http::scInternalServerError;
63  packedReason = Http::StatusCodeString(packedStatus); // ignore custom reason_ (if any)
64  }
65 
66  /* local constants */
67  /* AYJ: see bug 2469 - RFC2616 confirms stating 'SP characters' plural! */
68  static const char *Http1StatusLineFormat = "HTTP/%d.%d %3d %s\r\n";
69  static const char *IcyStatusLineFormat = "ICY %3d %s\r\n";
70 
71  /* handle ICY protocol status line specially. Pass on the bad format. */
72  if (protocol == AnyP::PROTO_ICY) {
73  debugs(57, 9, "packing sline " << this << " using " << p << ":");
74  debugs(57, 9, "FORMAT=" << IcyStatusLineFormat );
75  debugs(57, 9, "ICY " << packedStatus << " " << packedReason);
76  p->appendf(IcyStatusLineFormat, packedStatus, packedReason);
77  return;
78  }
79 
80  debugs(57, 9, "packing sline " << this << " using " << p << ":");
81  debugs(57, 9, "FORMAT=" << Http1StatusLineFormat );
82  debugs(57, 9, "HTTP/" << version.major << "." << version.minor << " " << packedStatus << " " << packedReason);
83  p->appendf(Http1StatusLineFormat, version.major, version.minor, packedStatus, packedReason);
84 }
85 
86 bool
87 Http::StatusLine::parse(const String &protoPrefix, const char *start, const char *end)
88 {
89  status_ = Http::scInvalidHeader; /* Squid header parsing error */
90 
91  // XXX: Http::Message::parse() has a similar check but is using
92  // casesensitive comparison (which is required by HTTP errata?)
93 
94  if (protoPrefix.cmp("ICY", 3) == 0) {
95  debugs(57, 3, "Invalid HTTP identifier. Detected ICY protocol instead.");
96  protocol = AnyP::PROTO_ICY;
97  start += protoPrefix.size();
98  } else if (protoPrefix.caseCmp(start, protoPrefix.size()) == 0) {
99 
100  start += protoPrefix.size();
101 
102  if (!xisdigit(*start))
103  return false;
104 
105  // XXX: HTTPbis have defined this to be single-digit version numbers. no need to sscanf()
106  // XXX: furthermore, only HTTP/1 will be using ASCII format digits
107 
108  if (sscanf(start, "%d.%d", &version.major, &version.minor) != 2) {
109  debugs(57, 7, "Invalid HTTP identifier.");
110  return false;
111  }
112  } else
113  return false;
114 
115  if (!(start = strchr(start, ' ')))
116  return false;
117 
118  ++start; // skip SP between HTTP-version and status-code
119 
120  assert(start <= end);
121  const auto stdStatusAreaLength = 4; // status-code length plus SP
122  const auto unparsedLength = end - start;
123  const auto statusAreaLength = std::min<size_t>(stdStatusAreaLength, unparsedLength);
124 
125  static SBuf statusBuf;
126  statusBuf.assign(start, statusAreaLength);
127  Parser::Tokenizer tok(statusBuf);
128  try {
130  } catch (const Parser::InsufficientInput &) {
131  debugs(57, 7, "need more; have " << unparsedLength);
132  return false;
133  } catch (...) {
134  debugs(57, 3, "cannot parse status-code area: " << CurrentException);
135  return false;
136  }
137 
138  // XXX check if the given 'reason' is the default status string, if not save to reason_
139 
140  /* we ignore 'reason-phrase' */
141  /* Should assert start < end ? */
142  return true; /* success */
143 }
144 
int caseCmp(char const *) const
Definition: String.cc:299
void appendf(const char *fmt,...) PRINTF_FORMAT_ARG2
Append operation with printf-style arguments.
Definition: Packable.h:61
SBuf & assign(const SBuf &S)
Definition: SBuf.cc:83
@ scNone
Definition: StatusCode.h:21
const char * StatusCodeString(const Http::StatusCode status)
Definition: StatusCode.cc:14
bool parse(const String &protoPrefix, const char *start, const char *end)
Definition: StatusLine.cc:87
Definition: SBuf.h:86
void packInto(Packable *) const
pack fields into a Packable object
Definition: StatusLine.cc:51
void init()
reset this status-line back to empty state
Definition: StatusLine.cc:22
int cmp(char const *) const
Definition: String.cc:269
StatusCode
Definition: StatusCode.h:20
#define DBG_IMPORTANT
Definition: Debug.h:46
static void ParseResponseStatus(Tokenizer &, StatusCode &code)
#define NULL
Definition: types.h:166
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Debug.h:128
#define assert(EX)
Definition: assert.h:19
static int version
std::ostream & CurrentException(std::ostream &os)
prints active (i.e., thrown but not yet handled) exception
#define xisdigit(x)
Definition: xis.h:20
@ scInternalServerError
Definition: StatusCode.h:71
const char * reason() const
retrieve the reason string for this status line
Definition: StatusLine.cc:45
@ PROTO_HTTP
Definition: ProtocolType.h:25
Definition: parse.c:160
void set(const AnyP::ProtocolVersion &newVersion, Http::StatusCode newStatus, const char *newReason=NULL)
Definition: StatusLine.cc:35
size_type size() const
Definition: SquidString.h:72
@ scInvalidHeader
Definition: StatusCode.h:86
@ PROTO_ICY
Definition: ProtocolType.h:39
thrown by modern "incremental" parsers when they need more data
Definition: forward.h:18
void clean()
reset this status-line back to Internal Server Error state
Definition: StatusLine.cc:28
AnyP::ProtocolVersion ProtocolVersion(unsigned int aMajor, unsigned int aMinor)
HTTP version label information.

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors