From 479cd8d24eda5edf63235210112e4c5ddfa6f370 Mon Sep 17 00:00:00 2001 From: aqua Date: Wed, 4 Aug 2021 11:57:26 +0300 Subject: Move gemini code into its own module --- gemini.go | 86 --------------------------------------------------- gemini/gemini.go | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++ gemini/gemini_test.go | 43 ++++++++++++++++++++++++++ gemini/go.mod | 3 ++ gemini_test.go | 43 -------------------------- go.mod | 8 +++++ main.go | 8 ++--- 7 files changed, 144 insertions(+), 133 deletions(-) delete mode 100644 gemini.go create mode 100644 gemini/gemini.go create mode 100644 gemini/gemini_test.go create mode 100644 gemini/go.mod delete mode 100644 gemini_test.go diff --git a/gemini.go b/gemini.go deleted file mode 100644 index 1cdef8b..0000000 --- a/gemini.go +++ /dev/null @@ -1,86 +0,0 @@ -package main - -import ( - "crypto/tls" - "fmt" - "net/url" - "os" - "strconv" - "strings" -) - -const GeminiNetwork = "tcp" -const GeminiPort = 1965 -const GeminiPrefix = "gemini://" - -func FormatRequest(request string) []byte { - if !strings.HasPrefix(request, GeminiPrefix) { - request = GeminiPrefix + request - } - request = request + "\r\n" - return []byte(request) -} - -type GeminiConn struct { - host string - config *tls.Config -} - -func NewGeminiConn(host string, port int) *GeminiConn { - conf := tls.Config{ - MinVersion: tls.VersionTLS12, - InsecureSkipVerify: true, - } - - if strings.Contains(host, ":") { - return &GeminiConn{host, &conf} - } else { - return &GeminiConn{host + ":" + strconv.Itoa(port), &conf} - } -} - -func NewGeminiConnFromRequest(request string, port int) (*GeminiConn, error) { - if !strings.HasPrefix(request, GeminiPrefix) { - request = GeminiPrefix + request - } - - u, err := url.Parse(request) - if err != nil { - return nil, err - } - return NewGeminiConn(u.Host, port), nil -} - -func (c *GeminiConn) Get(request []byte, out *os.File) error { - conn, err := tls.Dial(GeminiNetwork, c.host, c.config) - if err != nil { - return err - } - defer conn.Close() - - n, err := conn.Write(request) - if err != nil { - return err - } - - buf := make([]byte, 1024) - - for { - n, err = conn.Read(buf) - if err != nil { - return err - } - written, err := out.Write(buf[:n]) - if written != n { - return fmt.Errorf("Got %d bytes, but only wrote %d bytes", n, written) - } - if err != nil { - return err - } - - if n == 0 { - break - } - } - return nil -} diff --git a/gemini/gemini.go b/gemini/gemini.go new file mode 100644 index 0000000..8f4c6ef --- /dev/null +++ b/gemini/gemini.go @@ -0,0 +1,86 @@ +package gemini + +import ( + "crypto/tls" + "fmt" + "net/url" + "os" + "strconv" + "strings" +) + +const GeminiNetwork = "tcp" +const GeminiPort = 1965 +const GeminiPrefix = "gemini://" + +func FormatRequest(request string) []byte { + if !strings.HasPrefix(request, GeminiPrefix) { + request = GeminiPrefix + request + } + request = request + "\r\n" + return []byte(request) +} + +type GeminiConn struct { + host string + config *tls.Config +} + +func NewGeminiConn(host string, port int) *GeminiConn { + conf := tls.Config{ + MinVersion: tls.VersionTLS12, + InsecureSkipVerify: true, + } + + if strings.Contains(host, ":") { + return &GeminiConn{host, &conf} + } else { + return &GeminiConn{host + ":" + strconv.Itoa(port), &conf} + } +} + +func NewGeminiConnFromRequest(request string, port int) (*GeminiConn, error) { + if !strings.HasPrefix(request, GeminiPrefix) { + request = GeminiPrefix + request + } + + u, err := url.Parse(request) + if err != nil { + return nil, err + } + return NewGeminiConn(u.Host, port), nil +} + +func (c *GeminiConn) Get(request []byte, out *os.File) error { + conn, err := tls.Dial(GeminiNetwork, c.host, c.config) + if err != nil { + return err + } + defer conn.Close() + + n, err := conn.Write(request) + if err != nil { + return err + } + + buf := make([]byte, 1024) + + for { + n, err = conn.Read(buf) + if err != nil { + return err + } + written, err := out.Write(buf[:n]) + if written != n { + return fmt.Errorf("Got %d bytes, but only wrote %d bytes", n, written) + } + if err != nil { + return err + } + + if n == 0 { + break + } + } + return nil +} diff --git a/gemini/gemini_test.go b/gemini/gemini_test.go new file mode 100644 index 0000000..ffd2588 --- /dev/null +++ b/gemini/gemini_test.go @@ -0,0 +1,43 @@ +package gemini + +import ( + "bytes" + "strconv" + "testing" +) + +func TestNewGeminiConn(t *testing.T) { + tables := []struct { + url string + port int + host string + }{ + {"hostname.com", GeminiPort, "hostname.com:" + strconv.Itoa(GeminiPort)}, + {"hostname.com", 1234, "hostname.com:1234"}, + } + + for _, table := range tables { + conn, err := NewGeminiConnFromRequest(table.url, table.port) + if err != nil { + t.Fatalf("NewGeminiConn error: %s", err.Error()) + } + if conn.host != table.host { + t.Fatalf("NewGeminiConn error: wrong hostname %s", conn.host) + } + } +} + +func TestFormatRequest(t *testing.T) { + tables := []struct { + input string + output []byte + }{ + {"hostname.com", []byte("gemini://hostname.com\r\n")}, + } + + for _, table := range tables { + if !bytes.Equal(FormatRequest(table.input), table.output) { + t.Fatalf("FormatRequest failed on: %s\n", table.input) + } + } +} diff --git a/gemini/go.mod b/gemini/go.mod new file mode 100644 index 0000000..228a120 --- /dev/null +++ b/gemini/go.mod @@ -0,0 +1,3 @@ +module gemini + +go 1.16 diff --git a/gemini_test.go b/gemini_test.go deleted file mode 100644 index efa34b9..0000000 --- a/gemini_test.go +++ /dev/null @@ -1,43 +0,0 @@ -package main - -import ( - "bytes" - "strconv" - "testing" -) - -func TestNewGeminiConn(t *testing.T) { - tables := []struct { - url string - port int - host string - }{ - {"hostname.com", GeminiPort, "hostname.com:" + strconv.Itoa(GeminiPort)}, - {"hostname.com", 1234, "hostname.com:1234"}, - } - - for _, table := range tables { - conn, err := NewGeminiConnFromRequest(table.url, table.port) - if err != nil { - t.Fatalf("NewGeminiConn error: %s", err.Error()) - } - if conn.host != table.host { - t.Fatalf("NewGeminiConn error: wrong hostname %s", conn.host) - } - } -} - -func TestFormatRequest(t *testing.T) { - tables := []struct { - input string - output []byte - }{ - {"hostname.com", []byte("gemini://hostname.com\r\n")}, - } - - for _, table := range tables { - if !bytes.Equal(FormatRequest(table.input), table.output) { - t.Fatalf("FormatRequest failed on: %s\n", table.input) - } - } -} diff --git a/go.mod b/go.mod index 51942f3..8604a2a 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,11 @@ module gemcat go 1.16 + +require ( + gemini v0.0.0 +) + +replace ( + gemini => ./gemini +) diff --git a/main.go b/main.go index 2079568..3cfacac 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,7 @@ import ( "bufio" "flag" "fmt" + "gemini" "log" "os" ) @@ -48,7 +49,7 @@ func main() { output string }{ host: "", - port: GeminiPort, + port: gemini.GeminiPort, input: "", output: "", } @@ -60,7 +61,6 @@ func main() { flag.StringVar(&flags.output, "o", flags.output, "write output to path") flag.Parse() - requests, err := read_requests(flags.input) if err != nil { log.Panic(err) @@ -79,9 +79,9 @@ func main() { log.Printf("Got %d requests\n", len(requests)) for _, request := range requests { log.Println(request) - if conn, err := NewGeminiConnFromRequest(request, flags.port); err != nil { + if conn, err := gemini.NewGeminiConnFromRequest(request, flags.port); err != nil { log.Print(err) - } else if err := conn.Get(FormatRequest(request), output); err != nil { + } else if err := conn.Get(gemini.FormatRequest(request), output); err != nil { log.Print(err) } } -- cgit v1.2.1