Skip to content

Commit

Permalink
#18 - Improve host parsing (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
nsteinmetz authored and maxcnunes committed Aug 30, 2018
1 parent cb6a66b commit 46fa5bb
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import (
"strconv"
)

const regexAddressConn string = `^([a-z]{3,}):\/\/([^:]+):?([0-9]+)?$`
const regexPathAddressConn string = `^([^\/]+)(\/?.*)$`
const regexAddressConn string = `^([a-z]{3,}):\/\/([a-zA-Z0-9\.\-_]+):?([0-9]*)*([a-zA-Z0-9\/\.\-_\(\)?=&#%]*)*$`

// Connection data
type Connection struct {
Expand Down Expand Up @@ -36,12 +35,10 @@ func BuildConn(cfg *Config) *Connection {
}

res := match[0]

hostAndPath := regexp.MustCompile(regexPathAddressConn).FindAllStringSubmatch(res[2], -1)[0]
conn := &Connection{
Type: res[1],
Host: hostAndPath[1],
Path: hostAndPath[2],
Host: res[2],
Path: res[4],
}

if conn.Type != "tcp" {
Expand All @@ -50,14 +47,16 @@ func BuildConn(cfg *Config) *Connection {
}

// resolve port
if port, err := strconv.Atoi(res[3]); err != nil {
if len(res[3]) == 0 {
if conn.Scheme == "https" {
conn.Port = 443
} else {
conn.Port = 80
}
} else {
conn.Port = port
if port, err := strconv.Atoi(res[3]); err == nil {
conn.Port = port
}
}

return conn
Expand Down

0 comments on commit 46fa5bb

Please sign in to comment.