@@ -6,74 +6,94 @@ import (
66 "io"
77 "os"
88 "strings"
9+ "sync"
910)
1011
12+ // User defined protocols
13+ var protocols sync.Map
14+
15+ // Handler is the function type for user defined input protocols.
16+ type Handler func (arg string ) (io.ReadCloser , error )
17+
1118// Buffer size used for incoming messages to servers
1219const bufferSize = 4 * 1024
1320
1421// New parses an option string and returns a new ReadCloser.
1522func New (opts string ) (io.ReadCloser , error ) {
1623 parts := strings .SplitN (opts , "://" , 2 )
1724 proto := parts [0 ]
25+ arg := ""
26+ if len (parts ) > 1 {
27+ arg = parts [1 ]
28+ }
29+ fn , ok := protocols .Load (proto )
30+ if ok {
31+ return fn .(Handler )(arg )
32+ }
1833 switch proto {
1934 case "stdin" :
2035 return os .Stdin , nil
2136 case "file" :
22- if len ( parts ) < 2 {
37+ if arg == "" {
2338 return nil , errors .New ("file: no path supplied" )
2439 }
25- return file (parts [ 1 ] )
40+ return file (arg )
2641 case "http" :
27- if len ( parts ) < 2 {
42+ if arg == "" {
2843 return nil , errors .New ("http client: no address supplied" )
2944 }
30- return httpClient ("http://" + parts [ 1 ] )
45+ return httpClient ("http://" + arg )
3146 case "https" :
32- if len ( parts ) < 2 {
47+ if arg == "" {
3348 return nil , errors .New ("https client: no address supplied" )
3449 }
35- return httpClient ("https://" + parts [ 1 ] )
50+ return httpClient ("https://" + arg )
3651 case "http-server" :
37- if len ( parts ) < 2 {
52+ if arg == "" {
3853 return nil , errors .New ("http server: no address supplied" )
3954 }
40- return httpServer (parts [ 1 ] )
55+ return httpServer (arg )
4156 case "tcp" :
42- if len ( parts ) < 2 {
57+ if arg == "" {
4358 return nil , errors .New ("tcp client: no address supplied" )
4459 }
45- return tcpClient (parts [ 1 ] )
60+ return tcpClient (arg )
4661 case "tcp-server" :
47- if len ( parts ) < 2 {
62+ if arg == "" {
4863 return nil , errors .New ("tcp server: no address supplied" )
4964 }
50- return tcpServer (parts [ 1 ] )
65+ return tcpServer (arg )
5166 case "unix" :
52- if len ( parts ) < 2 {
67+ if arg == "" {
5368 return nil , errors .New ("unix client: no address supplied" )
5469 }
55- return unixClient (parts [ 1 ] )
70+ return unixClient (arg )
5671 case "unix-server" :
57- if len ( parts ) < 2 {
72+ if arg == "" {
5873 return nil , errors .New ("unix server: no address supplied" )
5974 }
60- return unixServer (parts [ 1 ] )
75+ return unixServer (arg )
6176 case "ws" :
62- if len ( parts ) < 2 {
77+ if arg == "" {
6378 return nil , errors .New ("ws client: no address supplied" )
6479 }
65- return wsClient ("ws://" + parts [ 1 ] )
80+ return wsClient ("ws://" + arg )
6681 case "wss" :
67- if len ( parts ) < 2 {
82+ if arg == "" {
6883 return nil , errors .New ("wss client: no address supplied" )
6984 }
70- return wsClient ("wss://" + parts [ 1 ] )
85+ return wsClient ("wss://" + arg )
7186 case "ws-server" :
72- if len ( parts ) < 2 {
87+ if arg == "" {
7388 return nil , errors .New ("ws server: no address supplied" )
7489 }
75- return wsServer (parts [ 1 ] )
90+ return wsServer (arg )
7691 default :
7792 return nil , errors .New ("unknown in protocol: " + proto )
7893 }
7994}
95+
96+ // Register a new input protocol.
97+ func Register (proto string , fn Handler ) {
98+ protocols .Store (proto , fn )
99+ }
0 commit comments