From fcd51e57717f249d420030dfc2e8ac5533c8655d Mon Sep 17 00:00:00 2001 From: Lefteris Zafiris Date: Tue, 26 Jul 2022 18:56:08 +0300 Subject: [PATCH] Use bufio.Scanner in parseEnv() --- agi.go | 4 ++++ parsers.go | 25 ++++++++++--------------- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/agi.go b/agi.go index e084d61..e3bafa9 100644 --- a/agi.go +++ b/agi.go @@ -38,6 +38,10 @@ import ( "strings" ) +const ( + envMin = 18 // Minimum number of AGI environment args +) + // Session is a struct holding AGI environment vars and the I/O handlers. type Session struct { Env map[string]string //AGI environment variables. diff --git a/parsers.go b/parsers.go index e776dd7..046c51b 100644 --- a/parsers.go +++ b/parsers.go @@ -7,6 +7,7 @@ package agi import ( + "bufio" "bytes" "errors" "fmt" @@ -14,25 +15,18 @@ import ( "strings" ) -const ( - envMin = 18 // Minimum number of AGI environment args - envMax = 150 // Maximum number of AGI environment args -) - // parseEnv reads and stores AGI environment. func (a *Session) parseEnv() error { var err error var line []byte - for i := 0; i <= envMax; i++ { - line, err = a.buf.ReadBytes(10) - if err != nil || len(line) <= len("\r\n") { - break + s := bufio.NewScanner(a.buf) + for s.Scan() { + line = s.Bytes() + if len(line) <= len("\r\n") { + break // end of input } - // Strip trailing newline - line = line[:len(line)-1] ind := bytes.IndexByte(line, ':') - // "agi_type" is the shortest length key, "agi_network_script" the longest, anything outside these boundaries is invalid. - if ind < len("agi_type") || ind > len("agi_network_script") || ind == len(line)-1 { + if ind == -1 || ind == len(line)-1 { err = fmt.Errorf("malformed environment input: %s", string(line)) a.Env = nil return err @@ -42,9 +36,10 @@ func (a *Session) parseEnv() error { value := string(line[ind:]) a.Env[key] = value } - if len(a.Env) < envMin { - err = fmt.Errorf("incomplete environment with only %d env vars", len(a.Env)) + if err := s.Err(); err != nil { + err = fmt.Errorf("error reading environment input: %s", err) a.Env = nil + return err } return err }