rocketmq-protocol 1.0.0

Runtime-neutral RocketMQ wire protocol contracts
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
// Copyright 2026 The RocketMQ Rust Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::fmt;

use bytes::Bytes;
use cheetah_string::CheetahString;
use memchr::memchr;
use serde::de::DeserializeSeed;
use serde::de::MapAccess;
use serde::de::Visitor;
use serde::Deserialize;
use serde::Deserializer;

use super::private::FieldSourceSealed;
use super::HeaderFieldSource;
use crate::HeaderMap;

const FIELD_LENGTH_BYTES: usize = 4;
const ESTIMATED_FIELD_BYTES: usize = 32;
const MIN_INITIAL_PAYLOAD_CAPACITY: usize = 256;
const MAX_INITIAL_MAP_CAPACITY: usize = 1024;

/// A compact, immutable JSON extension-field payload.
///
/// Serde writes decoded key/value text into one length-prefixed byte buffer.
/// This preserves JSON empty strings and duplicate input order without
/// allocating one owned string or hash-table entry per field.
#[derive(Clone)]
pub(crate) struct JsonHeaderFields {
    payload: Bytes,
    entry_count: usize,
    encoding: JsonFieldEncoding,
}

#[derive(Clone, Copy)]
enum JsonFieldEncoding {
    LengthPrefixed,
    CanonicalUnescapedObject,
    UnescapedObject,
}

impl JsonHeaderFields {
    pub(crate) const fn len(&self) -> usize {
        self.entry_count
    }

    pub(crate) fn from_length_prefixed(payload: Vec<u8>, entry_count: usize) -> Self {
        Self {
            payload: Bytes::from(payload),
            entry_count,
            encoding: JsonFieldEncoding::LengthPrefixed,
        }
    }

    pub(crate) fn from_unescaped_object(payload: Bytes, entry_count: usize) -> Self {
        Self {
            payload,
            entry_count,
            encoding: JsonFieldEncoding::UnescapedObject,
        }
    }

    pub(crate) fn from_canonical_unescaped_object(payload: Bytes, entry_count: usize) -> Self {
        Self {
            payload,
            entry_count,
            encoding: JsonFieldEncoding::CanonicalUnescapedObject,
        }
    }

    pub(crate) fn materialize(&self) -> HeaderMap {
        let mut map = HeaderMap::with_capacity(self.entry_count.min(MAX_INITIAL_MAP_CAPACITY));
        for (key, value) in self.iter() {
            map.insert(CheetahString::from_slice(key), CheetahString::from_slice(value));
        }
        map
    }

    #[inline]
    fn iter(&self) -> JsonHeaderFieldIter<'_> {
        JsonHeaderFieldIter {
            payload: &self.payload,
            cursor: 0,
            encoding: self.encoding,
        }
    }
}

impl<'de> Deserialize<'de> for JsonHeaderFields {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        deserializer.deserialize_map(JsonHeaderFieldsVisitor)
    }
}

struct JsonHeaderFieldsVisitor;

impl<'de> Visitor<'de> for JsonHeaderFieldsVisitor {
    type Value = JsonHeaderFields;

    fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str("a JSON object whose extension-field values are strings")
    }

    fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
    where
        A: MapAccess<'de>,
    {
        let initial_capacity = map
            .size_hint()
            .unwrap_or_default()
            .min(MAX_INITIAL_MAP_CAPACITY)
            .saturating_mul(ESTIMATED_FIELD_BYTES)
            .max(MIN_INITIAL_PAYLOAD_CAPACITY);
        let mut payload = Vec::with_capacity(initial_capacity);
        let mut entry_count = 0usize;

        while map
            .next_key_seed(JsonFieldTextSeed { payload: &mut payload })?
            .is_some()
        {
            map.next_value_seed(JsonFieldTextSeed { payload: &mut payload })?;
            entry_count = entry_count.saturating_add(1);
        }

        Ok(JsonHeaderFields {
            payload: Bytes::from(payload),
            entry_count,
            encoding: JsonFieldEncoding::LengthPrefixed,
        })
    }
}

struct JsonFieldTextSeed<'a> {
    payload: &'a mut Vec<u8>,
}

impl<'de> DeserializeSeed<'de> for JsonFieldTextSeed<'_> {
    type Value = ();

    fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
    where
        D: Deserializer<'de>,
    {
        deserializer.deserialize_str(JsonFieldTextVisitor { payload: self.payload })
    }
}

struct JsonFieldTextVisitor<'a> {
    payload: &'a mut Vec<u8>,
}

impl JsonFieldTextVisitor<'_> {
    fn append<E>(self, value: &str) -> Result<(), E>
    where
        E: serde::de::Error,
    {
        let length = u32::try_from(value.len()).map_err(|_| E::custom("JSON extension-field text exceeds u32"))?;
        let additional = FIELD_LENGTH_BYTES
            .checked_add(value.len())
            .ok_or_else(|| E::custom("JSON extension-field length overflow"))?;
        self.payload.reserve(additional);
        self.payload.extend_from_slice(&length.to_be_bytes());
        self.payload.extend_from_slice(value.as_bytes());
        Ok(())
    }
}

impl<'de> Visitor<'de> for JsonFieldTextVisitor<'_> {
    type Value = ();

    fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str("a JSON extension-field string")
    }

    #[inline]
    fn visit_borrowed_str<E>(self, value: &'de str) -> Result<Self::Value, E>
    where
        E: serde::de::Error,
    {
        self.append(value)
    }

    #[inline]
    fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
    where
        E: serde::de::Error,
    {
        self.append(value)
    }

    #[inline]
    fn visit_string<E>(self, value: String) -> Result<Self::Value, E>
    where
        E: serde::de::Error,
    {
        self.append(&value)
    }
}

impl FieldSourceSealed for JsonHeaderFields {}

impl HeaderFieldSource for JsonHeaderFields {
    #[inline]
    fn visit_fields_while<'a>(&'a self, visitor: &mut dyn FnMut(&'a str, &'a str) -> bool) {
        let mut iter = self.iter();
        match self.encoding {
            JsonFieldEncoding::CanonicalUnescapedObject => {
                while let Some((key, value)) = iter.next_canonical_unescaped_object() {
                    if !visitor(key, value) {
                        break;
                    }
                }
            }
            JsonFieldEncoding::UnescapedObject => {
                while let Some((key, value)) = iter.next_unescaped_object() {
                    if !visitor(key, value) {
                        break;
                    }
                }
            }
            JsonFieldEncoding::LengthPrefixed => {
                while let Some((key, value)) = iter.next_length_prefixed() {
                    if !visitor(key, value) {
                        break;
                    }
                }
            }
        }
    }

    #[inline]
    fn to_header_map(&self) -> HeaderMap {
        self.materialize()
    }
}

struct JsonHeaderFieldIter<'a> {
    payload: &'a [u8],
    cursor: usize,
    encoding: JsonFieldEncoding,
}

impl<'a> JsonHeaderFieldIter<'a> {
    #[inline]
    fn take(&mut self, length: usize) -> Option<&'a [u8]> {
        let start = self.cursor;
        let end = start.checked_add(length)?;
        let bytes = self.payload.get(start..end)?;
        self.cursor = end;
        Some(bytes)
    }

    #[inline]
    fn read_u32(&mut self) -> Option<usize> {
        let bytes: [u8; FIELD_LENGTH_BYTES] = self.take(FIELD_LENGTH_BYTES)?.try_into().ok()?;
        Some(u32::from_be_bytes(bytes) as usize)
    }

    #[inline]
    fn read_utf8(&mut self, length: usize) -> Option<&'a str> {
        std::str::from_utf8(self.take(length)?).ok()
    }

    #[inline]
    fn skip_json_whitespace(&mut self) {
        while matches!(self.payload.get(self.cursor), Some(b' ' | b'\n' | b'\r' | b'\t')) {
            self.cursor += 1;
        }
    }

    #[inline]
    fn read_unescaped_json_string(&mut self) -> Option<&'a str> {
        self.skip_json_whitespace();
        if self.payload.get(self.cursor).copied()? != b'"' {
            return None;
        }
        self.cursor += 1;
        let start = self.cursor;
        let relative_end = memchr(b'"', self.payload.get(start..)?)?;
        let end = start.checked_add(relative_end)?;
        let value = std::str::from_utf8(self.payload.get(start..end)?).ok()?;
        self.cursor = end.checked_add(1)?;
        Some(value)
    }

    #[inline]
    fn next_unescaped_object(&mut self) -> Option<(&'a str, &'a str)> {
        self.skip_json_whitespace();
        if self.cursor >= self.payload.len() {
            return None;
        }
        if self.payload.get(self.cursor) == Some(&b',') {
            self.cursor += 1;
        }
        let key = self.read_unescaped_json_string()?;
        self.skip_json_whitespace();
        if self.payload.get(self.cursor).copied()? != b':' {
            return None;
        }
        self.cursor += 1;
        let value = self.read_unescaped_json_string()?;
        self.skip_json_whitespace();
        Some((key, value))
    }

    #[inline]
    fn next_canonical_unescaped_object(&mut self) -> Option<(&'a str, &'a str)> {
        if self.cursor >= self.payload.len() {
            return None;
        }
        if self.cursor != 0 {
            if self.payload.get(self.cursor).copied()? != b',' {
                return None;
            }
            self.cursor += 1;
        }
        if self.payload.get(self.cursor).copied()? != b'"' {
            return None;
        }
        self.cursor += 1;
        let key_start = self.cursor;
        let key_length = self.canonical_string_length()?;
        self.cursor = self.cursor.checked_add(key_length)?;
        let key_end = self.cursor;
        let value_prefix_end = self.cursor.checked_add(3)?;
        if self.payload.get(self.cursor..value_prefix_end)? != b"\":\"" {
            return None;
        }
        self.cursor = value_prefix_end;
        let value_start = self.cursor;
        let value_length = self.canonical_string_length()?;
        self.cursor = self.cursor.checked_add(value_length)?;
        let value_end = self.cursor;
        self.cursor = self.cursor.checked_add(1)?;
        Some((
            std::str::from_utf8(self.payload.get(key_start..key_end)?).ok()?,
            std::str::from_utf8(self.payload.get(value_start..value_end)?).ok()?,
        ))
    }

    #[inline(always)]
    fn canonical_string_length(&self) -> Option<usize> {
        memchr(b'"', self.payload.get(self.cursor..)?)
    }

    #[inline]
    fn next_length_prefixed(&mut self) -> Option<(&'a str, &'a str)> {
        if self.cursor >= self.payload.len() {
            return None;
        }
        let key_length = self.read_u32()?;
        let key = self.read_utf8(key_length)?;
        let value_length = self.read_u32()?;
        let value = self.read_utf8(value_length)?;
        Some((key, value))
    }
}

impl<'a> Iterator for JsonHeaderFieldIter<'a> {
    type Item = (&'a str, &'a str);

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        match self.encoding {
            JsonFieldEncoding::CanonicalUnescapedObject => return self.next_canonical_unescaped_object(),
            JsonFieldEncoding::UnescapedObject => return self.next_unescaped_object(),
            JsonFieldEncoding::LengthPrefixed => {}
        }
        self.next_length_prefixed()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn iterates_all_valid_internal_encodings() {
        let mut length_prefixed = Vec::new();
        length_prefixed.extend_from_slice(&3u32.to_be_bytes());
        length_prefixed.extend_from_slice(b"key");
        length_prefixed.extend_from_slice(&5u32.to_be_bytes());
        length_prefixed.extend_from_slice(b"value");
        let fields = [
            JsonHeaderFields::from_length_prefixed(length_prefixed, 1),
            JsonHeaderFields::from_unescaped_object(Bytes::from_static(b" \"key\" : \"value\" "), 1),
            JsonHeaderFields::from_canonical_unescaped_object(Bytes::from_static(b"\"key\":\"value\""), 1),
        ];

        for fields in fields {
            assert_eq!(fields.iter().collect::<Vec<_>>(), vec![("key", "value")]);
        }
    }

    #[test]
    fn iterators_fail_closed_on_truncated_invalid_utf8_and_missing_terminators() {
        let fields = [
            JsonHeaderFields::from_length_prefixed(vec![0, 0, 0, 4, b'k'], 1),
            JsonHeaderFields::from_length_prefixed(vec![0, 0, 0, 1, 0xff, 0, 0, 0, 1, b'v'], 1),
            JsonHeaderFields::from_unescaped_object(Bytes::from_static(b"\"key\":\"value"), 1),
            JsonHeaderFields::from_unescaped_object(Bytes::from_static(b"\"key\"?\"value\""), 1),
            JsonHeaderFields::from_canonical_unescaped_object(Bytes::from_static(b"\"unterminated"), 1),
            JsonHeaderFields::from_canonical_unescaped_object(Bytes::from_static(b"\"key\"?\"value\""), 1),
        ];

        for fields in fields {
            assert!(fields.iter().next().is_none());
        }
    }
}