| 1 |
// Copyright 2020-2022 The NATS Authors |
| 2 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
// you may not use this file except in compliance with the License. |
| 4 |
// You may obtain a copy of the License at |
| 5 |
// |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package parser |
| 15 |
|
| 16 |
import ( |
| 17 |
"errors" |
| 18 |
"fmt" |
| 19 |
) |
| 20 |
|
| 21 |
const ( |
| 22 |
AckDomainTokenPos = iota + 2 |
| 23 |
AckAccHashTokenPos |
| 24 |
AckStreamTokenPos |
| 25 |
AckConsumerTokenPos |
| 26 |
AckNumDeliveredTokenPos |
| 27 |
AckStreamSeqTokenPos |
| 28 |
AckConsumerSeqTokenPos |
| 29 |
AckTimestampSeqTokenPos |
| 30 |
AckNumPendingTokenPos |
| 31 |
) |
| 32 |
|
| 33 |
var ErrInvalidSubjectFormat = errors.New("invalid format of ACK subject") |
| 34 |
|
| 35 |
// Quick parser for positive numbers in ack reply encoding. |
| 36 |
// NOTE: This parser does not detect uint64 overflow |
| 37 |
func ParseNum(d string) (n uint64) { |
| 38 |
if len(d) == 0 { |
| 39 |
return 0 |
| 40 |
} |
| 41 |
|
| 42 |
// ASCII numbers 0-9 |
| 43 |
const ( |
| 44 |
asciiZero = 48 |
| 45 |
asciiNine = 57 |
| 46 |
) |
| 47 |
|
| 48 |
for _, dec := range d { |
| 49 |
if dec < asciiZero || dec > asciiNine { |
| 50 |
return 0 |
| 51 |
} |
| 52 |
n = n*10 + uint64(dec) - asciiZero |
| 53 |
} |
| 54 |
return |
| 55 |
} |
| 56 |
|
| 57 |
func GetMetadataFields(subject string) ([]string, error) { |
| 58 |
v1TokenCounts, v2TokenCounts := 9, 12 |
| 59 |
|
| 60 |
var start int |
| 61 |
tokens := make([]string, 0, v2TokenCounts) |
| 62 |
for i := 0; i < len(subject); i++ { |
| 63 |
if subject[i] == '.' { |
| 64 |
tokens = append(tokens, subject[start:i]) |
| 65 |
start = i + 1 |
| 66 |
} |
| 67 |
} |
| 68 |
tokens = append(tokens, subject[start:]) |
| 69 |
// |
| 70 |
// Newer server will include the domain name and account hash in the subject, |
| 71 |
// and a token at the end. |
| 72 |
// |
| 73 |
// Old subject was: |
| 74 |
// $JS.ACK.<stream>.<consumer>.<delivered>.<sseq>.<cseq>.<tm>.<pending> |
| 75 |
// |
| 76 |
// New subject would be: |
| 77 |
// $JS.ACK.<domain>.<account hash>.<stream>.<consumer>.<delivered>.<sseq>.<cseq>.<tm>.<pending>.<a token with a random value> |
| 78 |
// |
| 79 |
// v1 has 9 tokens, v2 has 12, but we must not be strict on the 12th since |
| 80 |
// it may be removed in the future. Also, the library has no use for it. |
| 81 |
// The point is that a v2 ACK subject is valid if it has at least 11 tokens. |
| 82 |
// |
| 83 |
tokensLen := len(tokens) |
| 84 |
// If lower than 9 or more than 9 but less than 11, report an error |
| 85 |
if tokensLen < v1TokenCounts || (tokensLen > v1TokenCounts && tokensLen < v2TokenCounts-1) { |
| 86 |
return nil, ErrInvalidSubjectFormat |
| 87 |
} |
| 88 |
if tokens[0] != "$JS" || tokens[1] != "ACK" { |
| 89 |
return nil, fmt.Errorf("%w: subject should start with $JS.ACK", ErrInvalidSubjectFormat) |
| 90 |
} |
| 91 |
// For v1 style, we insert 2 empty tokens (domain and hash) so that the |
| 92 |
// rest of the library references known fields at a constant location. |
| 93 |
if tokensLen == v1TokenCounts { |
| 94 |
// Extend the array (we know the backend is big enough) |
| 95 |
tokens = append(tokens[:AckDomainTokenPos+2], tokens[AckDomainTokenPos:]...) |
| 96 |
// Clear the domain and hash tokens |
| 97 |
tokens[AckDomainTokenPos], tokens[AckAccHashTokenPos] = "", "" |
| 98 |
|
| 99 |
} else if tokens[AckDomainTokenPos] == "_" { |
| 100 |
// If domain is "_", replace with empty value. |
| 101 |
tokens[AckDomainTokenPos] = "" |
| 102 |
} |
| 103 |
return tokens, nil |
| 104 |
} |