]> git.ipfire.org Git - thirdparty/squid.git/blob - src/http/one/TeChunkedParser.h
Source Format Enforcement (#763)
[thirdparty/squid.git] / src / http / one / TeChunkedParser.h
1 /*
2 * Copyright (C) 1996-2021 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_TeChunkedParser_H
10 #define SQUID_SRC_HTTP_ONE_TeChunkedParser_H
11
12 #include "http/one/Parser.h"
13
14 class MemBuf;
15
16 namespace Http
17 {
18 namespace One
19 {
20
21 using ::Parser::InsufficientInput;
22
23 // TODO: Move this class into http/one/ChunkExtensionValueParser.*
24 /// A customizable parser of a single chunk extension value (chunk-ext-val).
25 /// From RFC 7230 section 4.1.1 and its Errata #4667:
26 /// chunk-ext = *( BWS ";" BWS chunk-ext-name [ BWS "=" BWS chunk-ext-val ] )
27 /// chunk-ext-name = token
28 /// chunk-ext-val = token / quoted-string
29 class ChunkExtensionValueParser
30 {
31 public:
32 typedef ::Parser::Tokenizer Tokenizer;
33
34 /// extracts and ignores the value of a named extension
35 static void Ignore(Tokenizer &tok, const SBuf &extName);
36
37 /// extracts and then interprets (or ignores) the extension value
38 virtual void parse(Tokenizer &tok, const SBuf &extName) = 0;
39 };
40
41 /**
42 * An incremental parser for chunked transfer coding
43 * defined in RFC 7230 section 4.1.
44 * http://tools.ietf.org/html/rfc7230#section-4.1
45 *
46 * The parser shovels content bytes from the raw
47 * input buffer into the content output buffer, both caller-supplied.
48 * Chunk extensions like use-original-body are handled via parseExtensionValuesWith().
49 * Trailers are available via mimeHeader() if wanted.
50 */
51 class TeChunkedParser : public Http1::Parser
52 {
53 public:
54 TeChunkedParser();
55 virtual ~TeChunkedParser() { theOut=nullptr; /* we do not own this object */ }
56
57 /// set the buffer to be used to store decoded chunk data
58 void setPayloadBuffer(MemBuf *parsedContent) {theOut = parsedContent;}
59
60 /// Instead of ignoring all chunk extension values, give the supplied
61 /// parser a chance to handle them. Only applied to last-chunk (for now).
62 void parseExtensionValuesWith(ChunkExtensionValueParser *parser) { customExtensionValueParser = parser; }
63
64 bool needsMoreSpace() const;
65
66 /* Http1::Parser API */
67 virtual void clear();
68 virtual bool parse(const SBuf &);
69 virtual Parser::size_type firstLineSize() const {return 0;} // has no meaning with multiple chunks
70
71 private:
72 bool parseChunkSize(Tokenizer &tok);
73 bool parseChunkMetadataSuffix(Tokenizer &);
74 void parseChunkExtensions(Tokenizer &);
75 void parseOneChunkExtension(Tokenizer &);
76 bool parseChunkBody(Tokenizer &tok);
77 bool parseChunkEnd(Tokenizer &tok);
78
79 MemBuf *theOut;
80 uint64_t theChunkSize;
81 uint64_t theLeftBodySize;
82
83 /// An optional plugin for parsing and interpreting custom chunk-ext-val.
84 /// This "visitor" object is owned by our creator.
85 ChunkExtensionValueParser *customExtensionValueParser;
86 };
87
88 } // namespace One
89 } // namespace Http
90
91 #endif /* SQUID_SRC_HTTP_ONE_TeChunkedParser_H */
92