]>
Commit | Line | Data |
---|---|---|
9b769c67 | 1 | /* |
bbc27441 | 2 | * Copyright (C) 1996-2014 The Squid Software Foundation and contributors |
9b769c67 | 3 | * |
bbc27441 AJ |
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. | |
9b769c67 AJ |
7 | */ |
8 | ||
bbc27441 AJ |
9 | /* DEBUG: section 57 HTTP Status-line */ |
10 | ||
9b769c67 AJ |
11 | #include "squid.h" |
12 | #include "Debug.h" | |
13 | #include "http/StatusLine.h" | |
14 | #include "Packer.h" | |
15 | ||
16 | void | |
17 | Http::StatusLine::init() | |
18 | { | |
19 | set(Http::ProtocolVersion(), Http::scNone, NULL); | |
20 | } | |
21 | ||
22 | void | |
23 | Http::StatusLine::clean() | |
24 | { | |
25 | set(Http::ProtocolVersion(), Http::scInternalServerError, NULL); | |
26 | } | |
27 | ||
28 | /* set values */ | |
29 | void | |
2592bc70 | 30 | Http::StatusLine::set(const AnyP::ProtocolVersion &newVersion, const Http::StatusCode newStatus, const char *newReason) |
9b769c67 AJ |
31 | { |
32 | protocol = AnyP::PROTO_HTTP; | |
33 | version = newVersion; | |
34 | status_ = newStatus; | |
35 | /* Note: no xstrdup for 'reason', assumes constant 'reasons' */ | |
36 | reason_ = newReason; | |
37 | } | |
38 | ||
39 | const char * | |
40 | Http::StatusLine::reason() const | |
41 | { | |
42 | return reason_ ? reason_ : Http::StatusCodeString(status()); | |
43 | } | |
44 | ||
45 | void | |
46 | Http::StatusLine::packInto(Packer * p) const | |
47 | { | |
48 | assert(p); | |
49 | ||
50 | /* local constants */ | |
51 | /* AYJ: see bug 2469 - RFC2616 confirms stating 'SP characters' plural! */ | |
52 | static const char *Http1StatusLineFormat = "HTTP/%d.%d %3d %s\r\n"; | |
53 | static const char *IcyStatusLineFormat = "ICY %3d %s\r\n"; | |
54 | ||
55 | /* handle ICY protocol status line specially. Pass on the bad format. */ | |
56 | if (protocol == AnyP::PROTO_ICY) { | |
57 | debugs(57, 9, "packing sline " << this << " using " << p << ":"); | |
58 | debugs(57, 9, "FORMAT=" << IcyStatusLineFormat ); | |
59 | debugs(57, 9, "ICY " << status() << " " << reason()); | |
60 | packerPrintf(p, IcyStatusLineFormat, status(), reason()); | |
61 | return; | |
62 | } | |
63 | ||
64 | debugs(57, 9, "packing sline " << this << " using " << p << ":"); | |
65 | debugs(57, 9, "FORMAT=" << Http1StatusLineFormat ); | |
66 | debugs(57, 9, "HTTP/" << version.major << "." << version.minor << " " << status() << " " << reason()); | |
67 | packerPrintf(p, Http1StatusLineFormat, version.major, version.minor, status(), reason()); | |
68 | } | |
69 | ||
70 | /* | |
71 | * Parse character string. | |
72 | * XXX: Note 'end' currently unused, so NULL-termination assumed. | |
73 | */ | |
74 | bool | |
75 | Http::StatusLine::parse(const String &protoPrefix, const char *start, const char *end) | |
76 | { | |
f53969cc | 77 | status_ = Http::scInvalidHeader; /* Squid header parsing error */ |
9b769c67 AJ |
78 | |
79 | // XXX: HttpMsg::parse() has a similar check but is using | |
80 | // casesensitive comparison (which is required by HTTP errata?) | |
81 | ||
82 | if (protoPrefix.cmp("ICY", 3) == 0) { | |
83 | debugs(57, 3, "Invalid HTTP identifier. Detected ICY protocol istead."); | |
84 | protocol = AnyP::PROTO_ICY; | |
85 | start += protoPrefix.size(); | |
86 | } else if (protoPrefix.caseCmp(start, protoPrefix.size()) == 0) { | |
87 | ||
88 | start += protoPrefix.size(); | |
89 | ||
90 | if (!xisdigit(*start)) | |
91 | return false; | |
92 | ||
93 | // XXX: HTTPbis have defined this to be single-digit version numbers. no need to sscanf() | |
94 | // XXX: furthermore, only HTTP/1 will be using ASCII format digits | |
95 | ||
96 | if (sscanf(start, "%d.%d", &version.major, &version.minor) != 2) { | |
97 | debugs(57, 7, "Invalid HTTP identifier."); | |
98 | return false; | |
99 | } | |
100 | } else | |
101 | return false; | |
102 | ||
103 | if (!(start = strchr(start, ' '))) | |
104 | return false; | |
105 | ||
106 | // XXX: should we be using xstrtoui() or xatoui() ? | |
107 | status_ = static_cast<Http::StatusCode>(atoi(++start)); | |
108 | ||
109 | // XXX check if the given 'reason' is the default status string, if not save to reason_ | |
110 | ||
111 | /* we ignore 'reason-phrase' */ | |
112 | /* Should assert start < end ? */ | |
f53969cc | 113 | return true; /* success */ |
9b769c67 | 114 | } |
f53969cc | 115 |