1 //--------------------------------------------------------------------------
2 // Copyright (C) 2014-2021 Cisco and/or its affiliates. All rights reserved.
3 //
4 // This program is free software; you can redistribute it and/or modify it
5 // under the terms of the GNU General Public License Version 2 as published
6 // by the Free Software Foundation.  You may not use, modify or distribute
7 // this program under any other version of the GNU General Public License.
8 //
9 // This program is distributed in the hope that it will be useful, but
10 // WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 // General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License along
15 // with this program; if not, write to the Free Software Foundation, Inc.,
16 // 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17 //--------------------------------------------------------------------------
18 // http_field.h author Tom Peters <thopeter@cisco.com>
19 
20 #ifndef HTTP_FIELD_H
21 #define HTTP_FIELD_H
22 
23 #include <cstdint>
24 #include <cstdio>
25 #include <cassert>
26 
27 #include "http_common.h"
28 #include "http_enum.h"
29 
30 // Individual pieces of the message found during parsing.
31 // Length values <= 0 are StatusCode values and imply that the start pointer is meaningless.
32 // Never use the start pointer without verifying that length > 0.
33 class Field
34 {
35 public:
36     static const Field FIELD_NULL;
37 
38     Field(int32_t length, const uint8_t* start, bool own_the_buffer_ = false);
Field(int32_t length)39     explicit Field(int32_t length) : len(length) { assert(length<=0); }
40     Field() = default;
41     Field& operator=(const Field& rhs);
~Field()42     ~Field() { if (own_the_buffer) delete[] strt; }
length()43     int32_t length() const { return len; }
start()44     const uint8_t* start() const { return strt; }
45     void set(int32_t length, const uint8_t* start, bool own_the_buffer_ = false);
46     void set(const Field& f);
47     void set(HttpCommon::StatusCode stat_code);
set(int32_t length)48     void set(int32_t length) { set(static_cast<HttpCommon::StatusCode>(length)); }
49 
50 #ifdef REG_TEST
51     void print(FILE* output, const char* name) const;
52 #endif
53 
54 private:
55     const uint8_t* strt = nullptr;
56     int32_t len = HttpCommon::STAT_NOT_COMPUTE;
57     bool own_the_buffer = false;
58 };
59 
60 #endif
61 
62