Debian Codegraph @

parser.go

raw
1 // Copyright 2012-2023 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 nats
15
16 import (
17 "fmt"
18 )
19
20 type msgArg struct {
21 subject []byte
22 reply []byte
23 sid int64
24 hdr int
25 size int
26 }
27
28 const MAX_CONTROL_LINE_SIZE = 4096
29
30 type parseState struct {
31 state int
32 as int
33 drop int
34 hdr int
35 ma msgArg
36 argBuf []byte
37 msgBuf []byte
38 msgCopied bool
39 scratch [MAX_CONTROL_LINE_SIZE]byte
40 }
41
42 const (
43 OP_START = iota
44 OP_PLUS
45 OP_PLUS_O
46 OP_PLUS_OK
47 OP_MINUS
48 OP_MINUS_E
49 OP_MINUS_ER
50 OP_MINUS_ERR
51 OP_MINUS_ERR_SPC
52 MINUS_ERR_ARG
53 OP_M
54 OP_MS
55 OP_MSG
56 OP_MSG_SPC
57 MSG_ARG
58 MSG_PAYLOAD
59 MSG_END
60 OP_H
61 OP_P
62 OP_PI
63 OP_PIN
64 OP_PING
65 OP_PO
66 OP_PON
67 OP_PONG
68 OP_I
69 OP_IN
70 OP_INF
71 OP_INFO
72 OP_INFO_SPC
73 INFO_ARG
74 )
75
76 // parse is the fast protocol parser engine.
77 func (nc *Conn) parse(buf []byte) error {
78 var i int
79 var b byte
80
81 // Move to loop instead of range syntax to allow jumping of i
82 for i = 0; i < len(buf); i++ {
83 b = buf[i]
84
85 switch nc.ps.state {
86 case OP_START:
87 switch b {
88 case 'M', 'm':
89 nc.ps.state = OP_M
90 nc.ps.hdr = -1
91 nc.ps.ma.hdr = -1
92 case 'H', 'h':
93 nc.ps.state = OP_H
94 nc.ps.hdr = 0
95 nc.ps.ma.hdr = 0
96 case 'P', 'p':
97 nc.ps.state = OP_P
98 case '+':
99 nc.ps.state = OP_PLUS
100 case '-':
101 nc.ps.state = OP_MINUS
102 case 'I', 'i':
103 nc.ps.state = OP_I
104 default:
105 goto parseErr
106 }
107 case OP_H:
108 switch b {
109 case 'M', 'm':
110 nc.ps.state = OP_M
111 default:
112 goto parseErr
113 }
114 case OP_M:
115 switch b {
116 case 'S', 's':
117 nc.ps.state = OP_MS
118 default:
119 goto parseErr
120 }
121 case OP_MS:
122 switch b {
123 case 'G', 'g':
124 nc.ps.state = OP_MSG
125 default:
126 goto parseErr
127 }
128 case OP_MSG:
129 switch b {
130 case ' ', '\t':
131 nc.ps.state = OP_MSG_SPC
132 default:
133 goto parseErr
134 }
135 case OP_MSG_SPC:
136 switch b {
137 case ' ', '\t':
138 continue
139 default:
140 nc.ps.state = MSG_ARG
141 nc.ps.as = i
142 }
143 case MSG_ARG:
144 switch b {
145 case '\r':
146 nc.ps.drop = 1
147 case '\n':
148 var arg []byte
149 if nc.ps.argBuf != nil {
150 arg = nc.ps.argBuf
151 } else {
152 arg = buf[nc.ps.as : i-nc.ps.drop]
153 }
154 if err := nc.processMsgArgs(arg); err != nil {
155 return err
156 }
157 nc.ps.drop, nc.ps.as, nc.ps.state = 0, i+1, MSG_PAYLOAD
158
159 // jump ahead with the index. If this overruns
160 // what is left we fall out and process a split buffer.
161 i = nc.ps.as + nc.ps.ma.size - 1
162 default:
163 if nc.ps.argBuf != nil {
164 nc.ps.argBuf = append(nc.ps.argBuf, b)
165 }
166 }
167 case MSG_PAYLOAD:
168 if nc.ps.msgBuf != nil {
169 if len(nc.ps.msgBuf) >= nc.ps.ma.size {
170 nc.processMsg(nc.ps.msgBuf)
171 nc.ps.argBuf, nc.ps.msgBuf, nc.ps.msgCopied, nc.ps.state = nil, nil, false, MSG_END
172 } else {
173 // copy as much as we can to the buffer and skip ahead.
174 toCopy := nc.ps.ma.size - len(nc.ps.msgBuf)
175 avail := len(buf) - i
176
177 if avail < toCopy {
178 toCopy = avail
179 }
180
181 if toCopy > 0 {
182 start := len(nc.ps.msgBuf)
183 // This is needed for copy to work.
184 nc.ps.msgBuf = nc.ps.msgBuf[:start+toCopy]
185 copy(nc.ps.msgBuf[start:], buf[i:i+toCopy])
186 // Update our index
187 i = (i + toCopy) - 1
188 } else {
189 nc.ps.msgBuf = append(nc.ps.msgBuf, b)
190 }
191 }
192 } else if i-nc.ps.as >= nc.ps.ma.size {
193 nc.processMsg(buf[nc.ps.as:i])
194 nc.ps.argBuf, nc.ps.msgBuf, nc.ps.msgCopied, nc.ps.state = nil, nil, false, MSG_END
195 }
196 case MSG_END:
197 switch b {
198 case '\n':
199 nc.ps.drop, nc.ps.as, nc.ps.state = 0, i+1, OP_START
200 default:
201 continue
202 }
203 case OP_PLUS:
204 switch b {
205 case 'O', 'o':
206 nc.ps.state = OP_PLUS_O
207 default:
208 goto parseErr
209 }
210 case OP_PLUS_O:
211 switch b {
212 case 'K', 'k':
213 nc.ps.state = OP_PLUS_OK
214 default:
215 goto parseErr
216 }
217 case OP_PLUS_OK:
218 switch b {
219 case '\n':
220 nc.processOK()
221 nc.ps.drop, nc.ps.state = 0, OP_START
222 }
223 case OP_MINUS:
224 switch b {
225 case 'E', 'e':
226 nc.ps.state = OP_MINUS_E
227 default:
228 goto parseErr
229 }
230 case OP_MINUS_E:
231 switch b {
232 case 'R', 'r':
233 nc.ps.state = OP_MINUS_ER
234 default:
235 goto parseErr
236 }
237 case OP_MINUS_ER:
238 switch b {
239 case 'R', 'r':
240 nc.ps.state = OP_MINUS_ERR
241 default:
242 goto parseErr
243 }
244 case OP_MINUS_ERR:
245 switch b {
246 case ' ', '\t':
247 nc.ps.state = OP_MINUS_ERR_SPC
248 default:
249 goto parseErr
250 }
251 case OP_MINUS_ERR_SPC:
252 switch b {
253 case ' ', '\t':
254 continue
255 default:
256 nc.ps.state = MINUS_ERR_ARG
257 nc.ps.as = i
258 }
259 case MINUS_ERR_ARG:
260 switch b {
261 case '\r':
262 nc.ps.drop = 1
263 case '\n':
264 var arg []byte
265 if nc.ps.argBuf != nil {
266 arg = nc.ps.argBuf
267 nc.ps.argBuf = nil
268 } else {
269 arg = buf[nc.ps.as : i-nc.ps.drop]
270 }
271 nc.processErr(string(arg))
272 nc.ps.drop, nc.ps.as, nc.ps.state = 0, i+1, OP_START
273 default:
274 if nc.ps.argBuf != nil {
275 nc.ps.argBuf = append(nc.ps.argBuf, b)
276 }
277 }
278 case OP_P:
279 switch b {
280 case 'I', 'i':
281 nc.ps.state = OP_PI
282 case 'O', 'o':
283 nc.ps.state = OP_PO
284 default:
285 goto parseErr
286 }
287 case OP_PO:
288 switch b {
289 case 'N', 'n':
290 nc.ps.state = OP_PON
291 default:
292 goto parseErr
293 }
294 case OP_PON:
295 switch b {
296 case 'G', 'g':
297 nc.ps.state = OP_PONG
298 default:
299 goto parseErr
300 }
301 case OP_PONG:
302 switch b {
303 case '\n':
304 nc.processPong()
305 nc.ps.drop, nc.ps.state = 0, OP_START
306 }
307 case OP_PI:
308 switch b {
309 case 'N', 'n':
310 nc.ps.state = OP_PIN
311 default:
312 goto parseErr
313 }
314 case OP_PIN:
315 switch b {
316 case 'G', 'g':
317 nc.ps.state = OP_PING
318 default:
319 goto parseErr
320 }
321 case OP_PING:
322 switch b {
323 case '\n':
324 nc.processPing()
325 nc.ps.drop, nc.ps.state = 0, OP_START
326 }
327 case OP_I:
328 switch b {
329 case 'N', 'n':
330 nc.ps.state = OP_IN
331 default:
332 goto parseErr
333 }
334 case OP_IN:
335 switch b {
336 case 'F', 'f':
337 nc.ps.state = OP_INF
338 default:
339 goto parseErr
340 }
341 case OP_INF:
342 switch b {
343 case 'O', 'o':
344 nc.ps.state = OP_INFO
345 default:
346 goto parseErr
347 }
348 case OP_INFO:
349 switch b {
350 case ' ', '\t':
351 nc.ps.state = OP_INFO_SPC
352 default:
353 goto parseErr
354 }
355 case OP_INFO_SPC:
356 switch b {
357 case ' ', '\t':
358 continue
359 default:
360 nc.ps.state = INFO_ARG
361 nc.ps.as = i
362 }
363 case INFO_ARG:
364 switch b {
365 case '\r':
366 nc.ps.drop = 1
367 case '\n':
368 var arg []byte
369 if nc.ps.argBuf != nil {
370 arg = nc.ps.argBuf
371 nc.ps.argBuf = nil
372 } else {
373 arg = buf[nc.ps.as : i-nc.ps.drop]
374 }
375 nc.processAsyncInfo(arg)
376 nc.ps.drop, nc.ps.as, nc.ps.state = 0, i+1, OP_START
377 default:
378 if nc.ps.argBuf != nil {
379 nc.ps.argBuf = append(nc.ps.argBuf, b)
380 }
381 }
382 default:
383 goto parseErr
384 }
385 }
386 // Check for split buffer scenarios
387 if (nc.ps.state == MSG_ARG || nc.ps.state == MINUS_ERR_ARG || nc.ps.state == INFO_ARG) && nc.ps.argBuf == nil {
388 nc.ps.argBuf = nc.ps.scratch[:0]
389 nc.ps.argBuf = append(nc.ps.argBuf, buf[nc.ps.as:i-nc.ps.drop]...)
390 // FIXME, check max len
391 }
392 // Check for split msg
393 if nc.ps.state == MSG_PAYLOAD && nc.ps.msgBuf == nil {
394 // We need to clone the msgArg if it is still referencing the
395 // read buffer and we are not able to process the msg.
396 if nc.ps.argBuf == nil {
397 nc.cloneMsgArg()
398 }
399
400 // If we will overflow the scratch buffer, just create a
401 // new buffer to hold the split message.
402 if nc.ps.ma.size > cap(nc.ps.scratch)-len(nc.ps.argBuf) {
403 lrem := len(buf[nc.ps.as:])
404
405 nc.ps.msgBuf = make([]byte, lrem, nc.ps.ma.size)
406 copy(nc.ps.msgBuf, buf[nc.ps.as:])
407 nc.ps.msgCopied = true
408 } else {
409 nc.ps.msgBuf = nc.ps.scratch[len(nc.ps.argBuf):len(nc.ps.argBuf)]
410 nc.ps.msgBuf = append(nc.ps.msgBuf, (buf[nc.ps.as:])...)
411 }
412 }
413
414 return nil
415
416 parseErr:
417 return fmt.Errorf("nats: Parse Error [%d]: '%s'", nc.ps.state, buf[i:])
418 }
419
420 // cloneMsgArg is used when the split buffer scenario has the pubArg in the existing read buffer, but
421 // we need to hold onto it into the next read.
422 func (nc *Conn) cloneMsgArg() {
423 nc.ps.argBuf = nc.ps.scratch[:0]
424 nc.ps.argBuf = append(nc.ps.argBuf, nc.ps.ma.subject...)
425 nc.ps.argBuf = append(nc.ps.argBuf, nc.ps.ma.reply...)
426 nc.ps.ma.subject = nc.ps.argBuf[:len(nc.ps.ma.subject)]
427 if nc.ps.ma.reply != nil {
428 nc.ps.ma.reply = nc.ps.argBuf[len(nc.ps.ma.subject):]
429 }
430 }
431
432 const argsLenMax = 4
433 const hdrArgsLenMax = 5
434
435 func (nc *Conn) processMsgArgs(arg []byte) error {
436 // Use separate function for header based messages.
437 if nc.ps.hdr >= 0 {
438 return nc.processHeaderMsgArgs(arg)
439 }
440
441 // Unroll splitArgs to avoid runtime/heap issues
442 a := [argsLenMax][]byte{}
443 args := a[:0]
444 start := -1
445 for i, b := range arg {
446 switch b {
447 case ' ', '\t', '\r', '\n':
448 if start >= 0 {
449 args = append(args, arg[start:i])
450 start = -1
451 }
452 default:
453 if start < 0 {
454 start = i
455 }
456 }
457 }
458 if start >= 0 {
459 args = append(args, arg[start:])
460 }
461
462 switch len(args) {
463 case 3:
464 nc.ps.ma.subject = args[0]
465 nc.ps.ma.sid = parseInt64(args[1])
466 nc.ps.ma.reply = nil
467 nc.ps.ma.size = int(parseInt64(args[2]))
468 case 4:
469 nc.ps.ma.subject = args[0]
470 nc.ps.ma.sid = parseInt64(args[1])
471 nc.ps.ma.reply = args[2]
472 nc.ps.ma.size = int(parseInt64(args[3]))
473 default:
474 return fmt.Errorf("nats: processMsgArgs Parse Error: '%s'", arg)
475 }
476 if nc.ps.ma.sid < 0 {
477 return fmt.Errorf("nats: processMsgArgs Bad or Missing Sid: '%s'", arg)
478 }
479 if nc.ps.ma.size < 0 {
480 return fmt.Errorf("nats: processMsgArgs Bad or Missing Size: '%s'", arg)
481 }
482 return nil
483 }
484
485 // processHeaderMsgArgs is for a header based message.
486 func (nc *Conn) processHeaderMsgArgs(arg []byte) error {
487 // Unroll splitArgs to avoid runtime/heap issues
488 a := [hdrArgsLenMax][]byte{}
489 args := a[:0]
490 start := -1
491 for i, b := range arg {
492 switch b {
493 case ' ', '\t', '\r', '\n':
494 if start >= 0 {
495 args = append(args, arg[start:i])
496 start = -1
497 }
498 default:
499 if start < 0 {
500 start = i
501 }
502 }
503 }
504 if start >= 0 {
505 args = append(args, arg[start:])
506 }
507
508 switch len(args) {
509 case 4:
510 nc.ps.ma.subject = args[0]
511 nc.ps.ma.sid = parseInt64(args[1])
512 nc.ps.ma.reply = nil
513 nc.ps.ma.hdr = int(parseInt64(args[2]))
514 nc.ps.ma.size = int(parseInt64(args[3]))
515 case 5:
516 nc.ps.ma.subject = args[0]
517 nc.ps.ma.sid = parseInt64(args[1])
518 nc.ps.ma.reply = args[2]
519 nc.ps.ma.hdr = int(parseInt64(args[3]))
520 nc.ps.ma.size = int(parseInt64(args[4]))
521 default:
522 return fmt.Errorf("nats: processHeaderMsgArgs Parse Error: '%s'", arg)
523 }
524 if nc.ps.ma.sid < 0 {
525 return fmt.Errorf("nats: processHeaderMsgArgs Bad or Missing Sid: '%s'", arg)
526 }
527 if nc.ps.ma.hdr < 0 || nc.ps.ma.hdr > nc.ps.ma.size {
528 return fmt.Errorf("nats: processHeaderMsgArgs Bad or Missing Header Size: '%s'", arg)
529 }
530 if nc.ps.ma.size < 0 {
531 return fmt.Errorf("nats: processHeaderMsgArgs Bad or Missing Size: '%s'", arg)
532 }
533 return nil
534 }
535
536 // ASCII numbers 0-9
537 const (
538 ascii_0 = 48
539 ascii_9 = 57
540 )
541
542 // parseInt64 expects decimal positive numbers. We
543 // return -1 to signal error
544 func parseInt64(d []byte) (n int64) {
545 if len(d) == 0 {
546 return -1
547 }
548 for _, dec := range d {
549 if dec < ascii_0 || dec > ascii_9 {
550 return -1
551 }
552 n = n*10 + (int64(dec) - ascii_0)
553 }
554 return n
555 }