]> git.ipfire.org Git - thirdparty/squid.git/blob - src/http/one/RequestParser.h
02f0bac5e761a04c8a8f6a6ce92993b945d64535
[thirdparty/squid.git] / src / http / one / RequestParser.h
1 /*
2 * Copyright (C) 1996-2022 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 #ifndef _SQUID_SRC_HTTP_ONE_REQUESTPARSER_H
10 #define _SQUID_SRC_HTTP_ONE_REQUESTPARSER_H
11
12 #include "http/one/Parser.h"
13 #include "http/RequestMethod.h"
14
15 namespace Parser {
16 class Tokenizer;
17 }
18
19 namespace Http {
20 namespace One {
21
22 /** HTTP/1.x protocol request parser
23 *
24 * Works on a raw character I/O buffer and tokenizes the content into
25 * the major CRLF delimited segments of an HTTP/1 request message:
26 *
27 * \li request-line (method, URL, protocol, version)
28 * \li mime-header (set of RFC2616 syntax header fields)
29 */
30 class RequestParser : public Http1::Parser
31 {
32 public:
33 RequestParser() = default;
34 RequestParser(bool preserveParsed) { preserveParsed_ = preserveParsed; }
35 RequestParser(const RequestParser &) = default;
36 RequestParser &operator =(const RequestParser &) = default;
37 RequestParser(RequestParser &&) = default;
38 RequestParser &operator =(RequestParser &&) = default;
39 ~RequestParser() override {}
40
41 /* Http::One::Parser API */
42 void clear() override {*this = RequestParser();}
43 Http1::Parser::size_type firstLineSize() const override;
44 bool parse(const SBuf &aBuf) override;
45
46 /// the HTTP method if this is a request message
47 const HttpRequestMethod & method() const {return method_;}
48
49 /// the request-line URI if this is a request message, or an empty string.
50 const SBuf &requestUri() const {return uri_;}
51
52 /// the accumulated parsed bytes
53 const SBuf &parsed() const { Must(preserveParsed_); return parsed_; }
54
55 private:
56 void skipGarbageLines();
57 int parseRequestFirstLine();
58 /// called from parse() to do the parsing
59 bool doParse(const SBuf &aBuf);
60
61 /* all these return false and set parseStatusCode on parsing failures */
62 bool parseMethodField(Tokenizer &);
63 bool parseUriField(Tokenizer &);
64 bool parseHttpVersionField(Tokenizer &);
65 bool skipDelimiter(const size_t count, const char *where);
66 bool skipTrailingCrs(Tokenizer &tok);
67
68 bool http0() const {return !msgProtocol_.major;}
69 static const CharacterSet &RequestTargetCharacters();
70
71 /// what request method has been found on the first line
72 HttpRequestMethod method_;
73
74 /// raw copy of the original client request-line URI field
75 SBuf uri_;
76
77 /// all parsed bytes (i.e., input prefix consumed by parse() calls)
78 /// meaningless unless preserveParsed_ is true
79 SBuf parsed_;
80 bool preserveParsed_ = false; ///< whether to accumulate parsed bytes (in parsed_)
81 };
82
83 } // namespace One
84 } // namespace Http
85
86 #endif /* _SQUID_SRC_HTTP_ONE_REQUESTPARSER_H */
87