1 /** @file
2   Licensed to the Apache Software Foundation (ASF) under one
3   or more contributor license agreements.  See the NOTICE file
4   distributed with this work for additional information
5   regarding copyright ownership.  The ASF licenses this file
6   to you under the Apache License, Version 2.0 (the
7   "License"); you may not use this file except in compliance
8   with the License.  You may obtain a copy of the License at
9 
10       http://www.apache.org/licenses/LICENSE-2.0
11 
12   Unless required by applicable law or agreed to in writing, software
13   distributed under the License is distributed on an "AS IS" BASIS,
14   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   See the License for the specific language governing permissions and
16   limitations under the License.
17  */
18 
19 #pragma once
20 
21 #include "ts/ts.h"
22 
23 #include "HttpHeader.h"
24 #include "Range.h"
25 #include "Stage.h"
26 
27 #include <netinet/in.h>
28 
29 struct Config;
30 
31 enum BlockState {
32   Pending,
33   PendingInt, // Pending internal refectch
34   PendingRef, // Pending reference refetch
35   Active,
36   ActiveInt, // Active internal refetch
37   ActiveRef, // Active reference refetch
38   Done,
39   Passthru, // non 206 response passthru
40   Fail,
41 };
42 
43 struct Data {
44   Data(Data const &) = delete;
45   Data &operator=(Data const &) = delete;
46 
47   Config *const m_config;
48 
49   sockaddr_storage m_client_ip;
50 
51   // transaction pointer
52   TSHttpTxn m_txnp{nullptr};
53 
54   // for pristine/effective url coming in
55   TSMBuffer m_urlbuf{nullptr};
56   TSMLoc m_urlloc{nullptr};
57 
58   char m_hostname[8192];
59   int m_hostlen{0};
60 
61   // read from slice block 0
62   char m_date[33];
63   int m_datelen{0};
64   char m_etag[8192];
65   int m_etaglen{0};
66   char m_lastmodified[33];
67   int m_lastmodifiedlen{0};
68 
69   int64_t m_contentlen{-1};
70 
71   TSHttpStatus m_statustype{TS_HTTP_STATUS_NONE}; // 200 or 206
72 
73   Range m_req_range; // converted to half open interval
74 
75   int64_t m_blocknum{-1};     // block number to work on, -1 bad/stop
76   int64_t m_blockexpected{0}; // body bytes expected
77   int64_t m_blockskip{0};     // number of bytes to skip in this block
78   int64_t m_blockconsumed{0}; // body bytes consumed
79 
80   BlockState m_blockstate{Pending}; // is there an active slice block
81 
82   int64_t m_bytestosend{0}; // header + content bytes to send
83   int64_t m_bytessent{0};   // number of bytes written to the client
84 
85   bool m_server_block_header_parsed{false};
86   bool m_server_first_header_parsed{false};
87 
88   Stage m_upstream;
89   Stage m_dnstream;
90 
91   HdrMgr m_req_hdrmgr;  // manager for server request
92   HdrMgr m_resp_hdrmgr; // manager for client response
93 
94   TSHttpParser m_http_parser{nullptr}; //!< cached for reuse
95 
DataData96   explicit Data(Config *const config) : m_config(config)
97   {
98     m_date[0]         = '\0';
99     m_hostname[0]     = '\0';
100     m_etag[0]         = '\0';
101     m_lastmodified[0] = '\0';
102   }
103 
~DataData104   ~Data()
105   {
106     if (nullptr != m_urlbuf) {
107       if (nullptr != m_urlloc) {
108         TSHandleMLocRelease(m_urlbuf, TS_NULL_MLOC, m_urlloc);
109         m_urlloc = nullptr;
110       }
111       TSMBufferDestroy(m_urlbuf);
112       m_urlbuf = nullptr;
113     }
114     if (nullptr != m_http_parser) {
115       TSHttpParserDestroy(m_http_parser);
116       m_http_parser = nullptr;
117     }
118   }
119 };
120