pingora_core/utils/mod.rs
1// Copyright 2024 Cloudflare, Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! This module contains various types that make it easier to work with bytes and X509
16//! certificates.
17
18#[cfg(feature = "any_tls")]
19pub mod tls;
20
21#[cfg(not(feature = "any_tls"))]
22pub use crate::tls::utils as tls;
23
24use bytes::Bytes;
25
26/// A `BufRef` is a reference to a buffer of bytes. It removes the need for self-referential data
27/// structures. It is safe to use as long as the underlying buffer does not get mutated.
28///
29/// # Panics
30///
31/// This will panic if an index is out of bounds.
32#[derive(Clone, PartialEq, Eq, Debug)]
33pub struct BufRef(pub usize, pub usize);
34
35impl BufRef {
36 /// Return a sub-slice of `buf`.
37 pub fn get<'a>(&self, buf: &'a [u8]) -> &'a [u8] {
38 &buf[self.0..self.1]
39 }
40
41 /// Return a slice of `buf`. This operation is O(1) and increases the reference count of `buf`.
42 pub fn get_bytes(&self, buf: &Bytes) -> Bytes {
43 buf.slice(self.0..self.1)
44 }
45
46 /// Return the size of the slice reference.
47 pub fn len(&self) -> usize {
48 self.1 - self.0
49 }
50
51 /// Return true if the length is zero.
52 pub fn is_empty(&self) -> bool {
53 self.1 == self.0
54 }
55}
56
57impl BufRef {
58 /// Initialize a `BufRef` that can reference a slice beginning at index `start` and has a
59 /// length of `len`.
60 pub fn new(start: usize, len: usize) -> Self {
61 BufRef(start, start + len)
62 }
63}
64
65/// A `KVRef` contains a key name and value pair, stored as two [BufRef] types.
66#[derive(Clone)]
67pub struct KVRef {
68 name: BufRef,
69 value: BufRef,
70}
71
72impl KVRef {
73 /// Like [BufRef::get] for the name.
74 pub fn get_name<'a>(&self, buf: &'a [u8]) -> &'a [u8] {
75 self.name.get(buf)
76 }
77
78 /// Like [BufRef::get] for the value.
79 pub fn get_value<'a>(&self, buf: &'a [u8]) -> &'a [u8] {
80 self.value.get(buf)
81 }
82
83 /// Like [BufRef::get_bytes] for the name.
84 pub fn get_name_bytes(&self, buf: &Bytes) -> Bytes {
85 self.name.get_bytes(buf)
86 }
87
88 /// Like [BufRef::get_bytes] for the value.
89 pub fn get_value_bytes(&self, buf: &Bytes) -> Bytes {
90 self.value.get_bytes(buf)
91 }
92
93 /// Return a new `KVRef` with name and value start indices and lengths.
94 pub fn new(name_s: usize, name_len: usize, value_s: usize, value_len: usize) -> Self {
95 KVRef {
96 name: BufRef(name_s, name_s + name_len),
97 value: BufRef(value_s, value_s + value_len),
98 }
99 }
100
101 /// Return a reference to the value.
102 pub fn value(&self) -> &BufRef {
103 &self.value
104 }
105}
106
107/// A [KVRef] which contains empty sub-slices.
108pub const EMPTY_KV_REF: KVRef = KVRef {
109 name: BufRef(0, 0),
110 value: BufRef(0, 0),
111};