Compare commits

...

35 Commits

Author SHA1 Message Date
Sasha Koshka 4985bf0bc8 Add link to ogirinal in readme 2023-05-26 00:45:08 -04:00
Sasha Koshka f6d3c47816 Export TLS & remove conn in request struct
This makes it possible to fully create another request outside of this module
(which Hnakra will need) and has better parity with net/http.
2023-05-26 00:38:12 -04:00
Sasha Koshka 24d70951c9 Change package URL in go.mod 2023-05-26 00:37:56 -04:00
Sasha Koshka b436ec8cb5 Change import paths 2023-05-25 18:45:53 -04:00
Sasha Koshka 54f7209f13 Update readme 2023-05-25 18:27:52 -04:00
Adnan Maolood b0f27c6f74 fs: Prevent invalid directory links
A file with a name like "gemini:example" would previously result in the
following invalid link:

    => gemini:example gemini:example

Fix by prepending a "./" before each filename, so that the resulting
link looks like:

    => ./gemini:example gemini:example
2022-05-07 13:54:56 -04:00
Yujiri 8c0af18617 Fix parsing of list item lines
According to section 5.5.2 of the Gemini specification (v0.16.1), the
space is mandatory.
2022-03-15 11:07:04 -04:00
Adnan Maolood 353416685a doc: Fix Mux documentation 2022-02-16 12:01:55 -05:00
Adnan Maolood 0ceec22705 readme: Update Gemini specification version 2021-12-18 12:51:04 -05:00
Adnan Maolood 3d2110d90f mux: Tweak documentation 2021-06-26 20:26:30 -04:00
Adnan Maolood b5c47a5ef0 mux: Add more tests 2021-06-26 20:16:38 -04:00
Adnan Maolood fb0d4d24bd mux: Remove support for handling schemes
Also fix redirection to subtree roots for wildcard patterns and patterns
without a host name.
2021-06-26 18:50:09 -04:00
Adnan Maolood 1170e007d4 fs: Avoid equality check if lengths don't match 2021-04-21 12:48:27 -04:00
Adnan Maolood c85759d777 fs: Improve redirect behavior 2021-04-21 12:41:56 -04:00
Adnan Maolood 507773618b fs: Refactor 2021-04-21 12:18:52 -04:00
Adnan Maolood 3bc243dd66 fs: Remove ServeContent function 2021-04-21 11:41:40 -04:00
Adnan Maolood de93d44786 LoggingMiddleware: Prevent writing empty meta 2021-04-21 11:38:34 -04:00
Adnan Maolood eb32c32063 fs: Fix panic on indexing URL of zero length 2021-04-21 11:36:43 -04:00
Adnan Maolood e5cf345273 Update README.md 2021-03-24 13:30:46 -04:00
Adnan Maolood c68ce57488 mux: Add copyright notice 2021-03-24 13:09:53 -04:00
Adnan Maolood 2b161650fe Split LICENSE into two files 2021-03-24 13:08:32 -04:00
Adnan Maolood dbbef1fb6d Revert "Require Go 1.16"
This reverts commit 0e87d64ffc.
2021-03-23 22:05:12 -04:00
Adnan Maolood 90518a01a8 Revert "Replace uses of ioutil with io"
This reverts commit 19f1d6693e.
2021-03-23 22:02:32 -04:00
Adnan Maolood 056e55abbb response: Remove unnecessary length check 2021-03-20 18:29:40 -04:00
Adnan Maolood 72d437c82e response: Limit response header size 2021-03-20 14:01:45 -04:00
Adnan Maolood 3dca29eb41 response: Don't use bufReadCloser 2021-03-20 13:41:53 -04:00
Adnan Maolood a40b5dcd0b fs: Fix empty media type for directory index pages 2021-03-20 13:33:15 -04:00
Adnan Maolood fffe86680e client: Only get cert if TrustCertificate is set 2021-03-20 12:54:41 -04:00
Adnan Maolood d5af32e121 client: Close connection on error 2021-03-20 12:49:27 -04:00
Adnan Maolood 5141eaafaa Tweak request and response parsing 2021-03-20 12:27:20 -04:00
Adnan Maolood e5c0afa013 response: Treat empty meta as invalid 2021-03-20 12:07:24 -04:00
Adnan Maolood 4c7c200f92 Remove unused field 2021-03-20 12:05:21 -04:00
Adnan Maolood 0a709da439 Remove charset=utf-8 from default media type 2021-03-20 12:04:42 -04:00
Adnan Maolood 1fdef9b608 Rename ServeMux to Mux 2021-03-15 15:44:35 -04:00
Adnan Maolood 2144e2c2f2 status: Reintroduce StatusSensitiveInput 2021-03-15 15:19:43 -04:00
25 changed files with 554 additions and 579 deletions

64
LICENSE
View File

@ -1,51 +1,19 @@
go-gemini is available under the terms of the MIT license:
Copyright (c) 2020 Adnan Maolood
Copyright (c) 2020 Adnan Maolood
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Portions of this program were taken from Go:
Copyright (c) 2009 The Go Authors. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the name of Google Inc. nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

27
LICENSE-GO Normal file
View File

@ -0,0 +1,27 @@
Copyright (c) 2009 The Go Authors. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the name of Google Inc. nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

View File

@ -1,16 +1,21 @@
# go-gemini
[![godocs.io](https://godocs.io/git.sr.ht/~adnano/go-gemini?status.svg)](https://godocs.io/git.sr.ht/~adnano/go-gemini) [![builds.sr.ht status](https://builds.sr.ht/~adnano/go-gemini.svg)](https://builds.sr.ht/~adnano/go-gemini?)
This repository is a fork of [go-gemini](https://godocs.io/git.sr.ht/~adnano/go-gemini)
implementing better parity with net/http and some tweaks required for use in
[Hnakra](https://git.tebibyte.media/sashakoshka/hnakra).
Package gemini implements the [Gemini protocol](https://gemini.circumlunar.space) in Go.
Package gemini implements the [Gemini protocol](https://gemini.circumlunar.space)
in Go. It provides an API similar to that of net/http to facilitate the
development of Gemini clients and servers.
It provides an API similar to that of net/http to make it easy to develop Gemini clients and servers.
Compatible with version v0.14.3 of the Gemini specification.
Compatible with version v0.16.0 of the Gemini specification.
## Usage
import "git.sr.ht/~adnano/go-gemini"
import "git.tebibyte.media/sashakoshka/go-gemini"
Note that some filesystem-related functionality is only available on Go 1.16
or later as it relies on the io/fs package.
## Examples
@ -19,8 +24,8 @@ To run an example:
go run examples/server.go
## Contributing
## License
Send patches and questions to [~adnano/go-gemini-devel](https://lists.sr.ht/~adnano/go-gemini-devel).
Subscribe to release announcements on [~adnano/go-gemini-announce](https://lists.sr.ht/~adnano/go-gemini-announce).
go-gemini is licensed under the terms of the MIT license (see LICENSE).
Portions of this library were adapted from Go and are governed by a BSD-style
license (see LICENSE-GO). Those files are marked accordingly.

View File

@ -131,6 +131,9 @@ func (c *Client) Do(ctx context.Context, req *Request) (*Response, error) {
conn.Close()
return nil, ctx.Err()
case r := <-res:
if r.err != nil {
conn.Close()
}
return r.resp, r.err
}
}
@ -174,9 +177,9 @@ func (c *Client) dialContext(ctx context.Context, network, addr string) (net.Con
}
func (c *Client) verifyConnection(cs tls.ConnectionState, hostname string) error {
cert := cs.PeerCertificates[0]
// See if the client trusts the certificate
if c.TrustCertificate != nil {
cert := cs.PeerCertificates[0]
return c.TrustCertificate(hostname, cert)
}
return nil

10
doc.go
View File

@ -29,18 +29,18 @@ Servers should be configured with certificates:
}
server.GetCertificate = certificates.Get
ServeMux is a Gemini request multiplexer.
ServeMux can handle requests for multiple hosts and schemes.
Mux is a Gemini request multiplexer.
Mux can handle requests for multiple hosts and paths.
mux := &gemini.ServeMux{}
mux := &gemini.Mux{}
mux.HandleFunc("example.com", func(ctx context.Context, w gemini.ResponseWriter, r *gemini.Request) {
fmt.Fprint(w, "Welcome to example.com")
})
mux.HandleFunc("example.org/about.gmi", func(ctx context.Context, w gemini.ResponseWriter, r *gemini.Request) {
fmt.Fprint(w, "About example.org")
})
mux.HandleFunc("http://example.net", func(ctx context.Context, w gemini.ResponseWriter, r *gemini.Request) {
fmt.Fprint(w, "Proxied content from http://example.net")
mux.HandleFunc("/images/", func(ctx context.Context, w gemini.ResponseWriter, r *gemini.Request) {
w.WriteHeader(gemini.StatusGone, "Gone forever")
})
server.Handler = mux

View File

@ -10,8 +10,8 @@ import (
"log"
"time"
"git.sr.ht/~adnano/go-gemini"
"git.sr.ht/~adnano/go-gemini/certificate"
"git.tebibyte.media/sashakoshka/go-gemini"
"git.tebibyte.media/sashakoshka/go-gemini/certificate"
)
type User struct {
@ -30,7 +30,7 @@ func main() {
log.Fatal(err)
}
mux := &gemini.ServeMux{}
mux := &gemini.Mux{}
mux.HandleFunc("/", profile)
mux.HandleFunc("/username", changeUsername)
@ -52,7 +52,7 @@ func fingerprint(cert *x509.Certificate) string {
}
func profile(ctx context.Context, w gemini.ResponseWriter, r *gemini.Request) {
tls := r.TLS()
tls := r.TLS
if len(tls.PeerCertificates) == 0 {
w.WriteHeader(gemini.StatusCertificateRequired, "Certificate required")
return
@ -68,7 +68,7 @@ func profile(ctx context.Context, w gemini.ResponseWriter, r *gemini.Request) {
}
func changeUsername(ctx context.Context, w gemini.ResponseWriter, r *gemini.Request) {
tls := r.TLS()
tls := r.TLS
if len(tls.PeerCertificates) == 0 {
w.WriteHeader(gemini.StatusCertificateRequired, "Certificate required")
return

View File

@ -11,7 +11,7 @@ import (
"os"
"time"
"git.sr.ht/~adnano/go-gemini/certificate"
"git.tebibyte.media/sashakoshka/go-gemini/certificate"
)
func main() {

View File

@ -16,8 +16,8 @@ import (
"os"
"path/filepath"
"git.sr.ht/~adnano/go-gemini"
"git.sr.ht/~adnano/go-gemini/tofu"
"git.tebibyte.media/sashakoshka/go-gemini"
"git.tebibyte.media/sashakoshka/go-gemini/tofu"
)
var (

View File

@ -10,7 +10,7 @@ import (
"io"
"os"
"git.sr.ht/~adnano/go-gemini"
"git.tebibyte.media/sashakoshka/go-gemini"
)
func main() {

View File

@ -11,8 +11,8 @@ import (
"os/signal"
"time"
"git.sr.ht/~adnano/go-gemini"
"git.sr.ht/~adnano/go-gemini/certificate"
"git.tebibyte.media/sashakoshka/go-gemini"
"git.tebibyte.media/sashakoshka/go-gemini/certificate"
)
func main() {
@ -22,7 +22,7 @@ func main() {
log.Fatal(err)
}
mux := &gemini.ServeMux{}
mux := &gemini.Mux{}
mux.Handle("/", gemini.FileServer(os.DirFS("/var/www")))
server := &gemini.Server{

View File

@ -10,8 +10,8 @@ import (
"log"
"time"
"git.sr.ht/~adnano/go-gemini"
"git.sr.ht/~adnano/go-gemini/certificate"
"git.tebibyte.media/sashakoshka/go-gemini"
"git.tebibyte.media/sashakoshka/go-gemini/certificate"
)
func main() {
@ -21,7 +21,7 @@ func main() {
log.Fatal(err)
}
mux := &gemini.ServeMux{}
mux := &gemini.Mux{}
mux.HandleFunc("/", stream)
server := &gemini.Server{

187
fs.go
View File

@ -1,3 +1,5 @@
// +build go1.16
package gemini
import (
@ -27,88 +29,22 @@ type fileServer struct {
fs.FS
}
func (fs fileServer) ServeGemini(ctx context.Context, w ResponseWriter, r *Request) {
serveFile(w, r, fs, path.Clean(r.URL.Path), true)
}
// ServeContent replies to the request using the content in the
// provided Reader. The main benefit of ServeContent over io.Copy
// is that it sets the MIME type of the response.
//
// ServeContent tries to deduce the type from name's file extension.
// The name is otherwise unused; it is never sent in the response.
func ServeContent(w ResponseWriter, r *Request, name string, content io.Reader) {
serveContent(w, name, content)
}
func serveContent(w ResponseWriter, name string, content io.Reader) {
// Detect mimetype from file extension
ext := path.Ext(name)
mimetype := mime.TypeByExtension(ext)
w.SetMediaType(mimetype)
io.Copy(w, content)
}
// ServeFile responds to the request with the contents of the named file
// or directory.
//
// If the provided file or directory name is a relative path, it is interpreted
// relative to the current directory and may ascend to parent directories. If
// the provided name is constructed from user input, it should be sanitized
// before calling ServeFile.
//
// As a precaution, ServeFile will reject requests where r.URL.Path contains a
// ".." path element; this protects against callers who might unsafely use
// path.Join on r.URL.Path without sanitizing it and then use that
// path.Join result as the name argument.
//
// As another special case, ServeFile redirects any request where r.URL.Path
// ends in "/index.gmi" to the same path, without the final "index.gmi". To
// avoid such redirects either modify the path or use ServeContent.
//
// Outside of those two special cases, ServeFile does not use r.URL.Path for
// selecting the file or directory to serve; only the file or directory
// provided in the name argument is used.
func ServeFile(w ResponseWriter, r *Request, fsys fs.FS, name string) {
if containsDotDot(r.URL.Path) {
// Too many programs use r.URL.Path to construct the argument to
// serveFile. Reject the request under the assumption that happened
// here and ".." may not be wanted.
// Note that name might not contain "..", for example if code (still
// incorrectly) used path.Join(myDir, r.URL.Path).
w.WriteHeader(StatusBadRequest, "invalid URL path")
return
}
serveFile(w, r, fsys, name, false)
}
func containsDotDot(v string) bool {
if !strings.Contains(v, "..") {
return false
}
for _, ent := range strings.FieldsFunc(v, isSlashRune) {
if ent == ".." {
return true
}
}
return false
}
func isSlashRune(r rune) bool { return r == '/' || r == '\\' }
func serveFile(w ResponseWriter, r *Request, fsys fs.FS, name string, redirect bool) {
func (fsys fileServer) ServeGemini(ctx context.Context, w ResponseWriter, r *Request) {
const indexPage = "/index.gmi"
url := path.Clean(r.URL.Path)
// Redirect .../index.gmi to .../
if strings.HasSuffix(r.URL.Path, indexPage) {
w.WriteHeader(StatusPermanentRedirect, "./")
if strings.HasSuffix(url, indexPage) {
w.WriteHeader(StatusPermanentRedirect, strings.TrimSuffix(url, "index.gmi"))
return
}
name := url
if name == "/" {
name = "."
} else {
name = strings.Trim(name, "/")
name = strings.TrimPrefix(name, "/")
}
f, err := fsys.Open(name)
@ -125,50 +61,89 @@ func serveFile(w ResponseWriter, r *Request, fsys fs.FS, name string, redirect b
}
// Redirect to canonical path
if redirect {
url := r.URL.Path
if len(r.URL.Path) != 0 {
if stat.IsDir() {
// Add trailing slash
if url[len(url)-1] != '/' {
w.WriteHeader(StatusPermanentRedirect, path.Base(url)+"/")
target := url
if target != "/" {
target += "/"
}
if len(r.URL.Path) != len(target) || r.URL.Path != target {
w.WriteHeader(StatusPermanentRedirect, target)
return
}
} else {
} else if r.URL.Path[len(r.URL.Path)-1] == '/' {
// Remove trailing slash
if url[len(url)-1] == '/' {
w.WriteHeader(StatusPermanentRedirect, "../"+path.Base(url))
return
}
}
}
if stat.IsDir() {
// Redirect if the directory name doesn't end in a slash
url := r.URL.Path
if url[len(url)-1] != '/' {
w.WriteHeader(StatusRedirect, path.Base(url)+"/")
w.WriteHeader(StatusPermanentRedirect, url)
return
}
// Use contents of index.gmi if present
index, err := fsys.Open(path.Join(name, indexPage))
if err == nil {
defer index.Close()
istat, err := index.Stat()
if err == nil {
f = index
stat = istat
}
}
}
if stat.IsDir() {
// Failed to find index file
dirList(w, f)
// Use contents of index.gmi if present
name = path.Join(name, indexPage)
index, err := fsys.Open(name)
if err == nil {
defer index.Close()
f = index
} else {
// Failed to find index file
dirList(w, f)
return
}
}
// Detect mimetype from file extension
ext := path.Ext(name)
mimetype := mime.TypeByExtension(ext)
w.SetMediaType(mimetype)
io.Copy(w, f)
}
// ServeFile responds to the request with the contents of the named file
// or directory. If the provided name is constructed from user input, it
// should be sanitized before calling ServeFile.
func ServeFile(w ResponseWriter, fsys fs.FS, name string) {
const indexPage = "/index.gmi"
// Ensure name is relative
if name == "/" {
name = "."
} else {
name = strings.TrimLeft(name, "/")
}
f, err := fsys.Open(name)
if err != nil {
w.WriteHeader(toGeminiError(err))
return
}
defer f.Close()
stat, err := f.Stat()
if err != nil {
w.WriteHeader(toGeminiError(err))
return
}
serveContent(w, name, f)
if stat.IsDir() {
// Use contents of index file if present
name = path.Join(name, indexPage)
index, err := fsys.Open(name)
if err == nil {
defer index.Close()
f = index
} else {
// Failed to find index file
dirList(w, f)
return
}
}
// Detect mimetype from file extension
ext := path.Ext(name)
mimetype := mime.TypeByExtension(ext)
w.SetMediaType(mimetype)
io.Copy(w, f)
}
func dirList(w ResponseWriter, f fs.File) {
@ -194,7 +169,7 @@ func dirList(w ResponseWriter, f fs.File) {
}
link := LineLink{
Name: name,
URL: (&url.URL{Path: name}).EscapedPath(),
URL: "./" + url.PathEscape(name),
}
fmt.Fprintln(w, link.String())
}

View File

@ -11,8 +11,6 @@ func init() {
mime.AddExtensionType(".gemini", "text/gemini")
}
var crlf = []byte("\r\n")
// Errors.
var (
ErrInvalidRequest = errors.New("gemini: invalid request")
@ -22,3 +20,15 @@ var (
// when the response status code does not permit a body.
ErrBodyNotAllowed = errors.New("gemini: response status code does not allow body")
)
var crlf = []byte("\r\n")
func trimCRLF(b []byte) ([]byte, bool) {
// Check for CR
if len(b) < 2 || b[len(b)-2] != '\r' {
return nil, false
}
// Trim CRLF
b = b[:len(b)-2]
return b, true
}

4
go.mod
View File

@ -1,5 +1,5 @@
module git.sr.ht/~adnano/go-gemini
module git.tebibyte.media/sashakoshka/go-gemini
go 1.16
go 1.15
require golang.org/x/net v0.0.0-20210119194325-5f4716e94777

28
io.go
View File

@ -1,7 +1,6 @@
package gemini
import (
"bufio"
"context"
"io"
)
@ -75,30 +74,3 @@ func (nopReadCloser) Read(p []byte) (int, error) {
func (nopReadCloser) Close() error {
return nil
}
type bufReadCloser struct {
br *bufio.Reader // used until empty
io.ReadCloser
}
func newBufReadCloser(br *bufio.Reader, rc io.ReadCloser) io.ReadCloser {
body := &bufReadCloser{ReadCloser: rc}
if br.Buffered() != 0 {
body.br = br
}
return body
}
func (b *bufReadCloser) Read(p []byte) (n int, err error) {
if b.br != nil {
if n := b.br.Buffered(); len(p) > n {
p = p[:n]
}
n, err = b.br.Read(p)
if b.br.Buffered() == 0 {
b.br = nil
}
return n, err
}
return b.ReadCloser.Read(p)
}

View File

@ -31,7 +31,12 @@ func (w *logResponseWriter) SetMediaType(mediatype string) {
func (w *logResponseWriter) Write(b []byte) (int, error) {
if !w.wroteHeader {
w.WriteHeader(StatusSuccess, w.mediatype)
meta := w.mediatype
if meta == "" {
// Use default media type
meta = defaultMediaType
}
w.WriteHeader(StatusSuccess, meta)
}
n, err := w.rw.Write(b)
w.Wrote += n

172
mux.go
View File

@ -1,3 +1,7 @@
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE-GO file.
package gemini
import (
@ -10,7 +14,7 @@ import (
"sync"
)
// ServeMux is a Gemini request multiplexer.
// Mux is a Gemini request multiplexer.
// It matches the URL of each incoming request against a list of registered
// patterns and calls the handler for the pattern that
// most closely matches the URL.
@ -28,58 +32,42 @@ import (
// the pattern "/" matches all paths not matched by other registered
// patterns, not just the URL with Path == "/".
//
// Patterns may also contain schemes and hostnames.
// Wildcard patterns can be used to match multiple hostnames (e.g. "*.example.com").
// Patterns may optionally begin with a host name, restricting matches to
// URLs on that host only. Host-specific patterns take precedence over
// general patterns, so that a handler might register for the two patterns
// "/search" and "search.example.com/" without also taking over requests
// for "gemini://example.com/".
//
// The following are examples of valid patterns, along with the scheme,
// hostname, and path that they match.
//
// Pattern │ Scheme │ Hostname │ Path
// ─────────────────────────────┼────────┼──────────┼─────────────
// /file │ gemini │ * │ /file
// /directory/ │ gemini │ * │ /directory/*
// hostname/file │ gemini │ hostname │ /file
// hostname/directory/ │ gemini │ hostname │ /directory/*
// scheme://hostname/file │ scheme │ hostname │ /file
// scheme://hostname/directory/ │ scheme │ hostname │ /directory/*
// //hostname/file │ * │ hostname │ /file
// //hostname/directory/ │ * │ hostname │ /directory/*
// scheme:///file │ scheme │ * │ /file
// scheme:///directory/ │ scheme │ * │ /directory/*
// ///file │ * │ * │ /file
// ///directory/ │ * │ * │ /directory/*
//
// A pattern without a hostname will match any hostname.
// If a pattern begins with "//", it will match any scheme.
// Otherwise, a pattern with no scheme is treated as though it has a
// scheme of "gemini".
// Wildcard patterns can be used to match multiple hostnames. For example,
// the pattern "*.example.com" will match requests for "blog.example.com"
// and "gemini.example.com", but not "example.org".
//
// If a subtree has been registered and a request is received naming the
// subtree root without its trailing slash, ServeMux redirects that
// subtree root without its trailing slash, Mux redirects that
// request to the subtree root (adding the trailing slash). This behavior can
// be overridden with a separate registration for the path without
// the trailing slash. For example, registering "/images/" causes ServeMux
// the trailing slash. For example, registering "/images/" causes Mux
// to redirect a request for "/images" to "/images/", unless "/images" has
// been registered separately.
//
// ServeMux also takes care of sanitizing the URL request path and
// Mux also takes care of sanitizing the URL request path and
// redirecting any request containing . or .. elements or repeated slashes
// to an equivalent, cleaner URL.
type ServeMux struct {
type Mux struct {
mu sync.RWMutex
m map[muxKey]Handler
m map[hostpath]Handler
es []muxEntry // slice of entries sorted from longest to shortest
}
type muxKey struct {
scheme string
host string
path string
type hostpath struct {
host string
path string
}
type muxEntry struct {
handler Handler
key muxKey
host string
path string
}
// cleanPath returns the canonical path for p, eliminating . and .. elements.
@ -106,24 +94,17 @@ func cleanPath(p string) string {
// Find a handler on a handler map given a path string.
// Most-specific (longest) pattern wins.
func (mux *ServeMux) match(key muxKey) Handler {
func (mux *Mux) match(host, path string) Handler {
// Check for exact match first.
if r, ok := mux.m[key]; ok {
return r
} else if r, ok := mux.m[muxKey{"", key.host, key.path}]; ok {
return r
} else if r, ok := mux.m[muxKey{key.scheme, "", key.path}]; ok {
return r
} else if r, ok := mux.m[muxKey{"", "", key.path}]; ok {
return r
if h, ok := mux.m[hostpath{host, path}]; ok {
return h
}
// Check for longest valid match. mux.es contains all patterns
// that end in / sorted from longest to shortest.
for _, e := range mux.es {
if (e.key.scheme == "" || key.scheme == e.key.scheme) &&
(e.key.host == "" || key.host == e.key.host) &&
strings.HasPrefix(key.path, e.key.path) {
if len(e.host) == len(host) && e.host == host &&
strings.HasPrefix(path, e.path) {
return e.handler
}
}
@ -134,31 +115,32 @@ func (mux *ServeMux) match(key muxKey) Handler {
// This occurs when a handler for path + "/" was already registered, but
// not for path itself. If the path needs appending to, it creates a new
// URL, setting the path to u.Path + "/" and returning true to indicate so.
func (mux *ServeMux) redirectToPathSlash(key muxKey, u *url.URL) (*url.URL, bool) {
func (mux *Mux) redirectToPathSlash(host, path string, u *url.URL) (*url.URL, bool) {
mux.mu.RLock()
shouldRedirect := mux.shouldRedirectRLocked(key)
shouldRedirect := mux.shouldRedirectRLocked(host, path)
mux.mu.RUnlock()
if !shouldRedirect {
return u, false
}
return u.ResolveReference(&url.URL{Path: key.path + "/"}), true
return u.ResolveReference(&url.URL{Path: path + "/"}), true
}
// shouldRedirectRLocked reports whether the given path and host should be redirected to
// path+"/". This should happen if a handler is registered for path+"/" but
// not path -- see comments at ServeMux.
func (mux *ServeMux) shouldRedirectRLocked(key muxKey) bool {
if _, exist := mux.m[key]; exist {
// not path -- see comments at Mux.
func (mux *Mux) shouldRedirectRLocked(host, path string) bool {
if _, exist := mux.m[hostpath{host, path}]; exist {
return false
}
n := len(key.path)
n := len(path)
if n == 0 {
return false
}
if _, exist := mux.m[muxKey{key.scheme, key.host, key.path + "/"}]; exist {
return key.path[n-1] != '/'
if _, exist := mux.m[hostpath{host, path + "/"}]; exist {
return path[n-1] != '/'
}
return false
}
@ -177,14 +159,18 @@ func getWildcard(hostname string) (string, bool) {
// the path is not in its canonical form, the handler will be an
// internally-generated handler that redirects to the canonical path. If the
// host contains a port, it is ignored when matching handlers.
func (mux *ServeMux) Handler(r *Request) Handler {
scheme := r.URL.Scheme
func (mux *Mux) Handler(r *Request) Handler {
// Disallow non-Gemini schemes
if r.URL.Scheme != "gemini" {
return NotFoundHandler()
}
host := r.URL.Hostname()
path := cleanPath(r.URL.Path)
// If the given path is /tree and its handler is not registered,
// redirect for /tree/.
if u, ok := mux.redirectToPathSlash(muxKey{scheme, host, path}, r.URL); ok {
if u, ok := mux.redirectToPathSlash(host, path, r.URL); ok {
return StatusHandler(StatusPermanentRedirect, u.String())
}
@ -197,29 +183,43 @@ func (mux *ServeMux) Handler(r *Request) Handler {
mux.mu.RLock()
defer mux.mu.RUnlock()
h := mux.match(muxKey{scheme, host, path})
h := mux.match(host, path)
if h == nil {
// Try wildcard
if wildcard, ok := getWildcard(host); ok {
h = mux.match(muxKey{scheme, wildcard, path})
if u, ok := mux.redirectToPathSlash(wildcard, path, r.URL); ok {
return StatusHandler(StatusPermanentRedirect, u.String())
}
h = mux.match(wildcard, path)
}
}
if h == nil {
// Try empty host
if u, ok := mux.redirectToPathSlash("", path, r.URL); ok {
return StatusHandler(StatusPermanentRedirect, u.String())
}
h = mux.match("", path)
}
if h == nil {
h = NotFoundHandler()
}
return h
}
// ServeGemini dispatches the request to the handler whose
// pattern most closely matches the request URL.
func (mux *ServeMux) ServeGemini(ctx context.Context, w ResponseWriter, r *Request) {
func (mux *Mux) ServeGemini(ctx context.Context, w ResponseWriter, r *Request) {
h := mux.Handler(r)
h.ServeGemini(ctx, w, r)
}
// Handle registers the handler for the given pattern.
// If a handler already exists for pattern, Handle panics.
func (mux *ServeMux) Handle(pattern string, handler Handler) {
func (mux *Mux) Handle(pattern string, handler Handler) {
if pattern == "" {
panic("gemini: invalid pattern")
}
@ -230,48 +230,32 @@ func (mux *ServeMux) Handle(pattern string, handler Handler) {
mux.mu.Lock()
defer mux.mu.Unlock()
var key muxKey
if strings.HasPrefix(pattern, "//") {
// match any scheme
key.scheme = ""
pattern = pattern[2:]
} else {
// extract scheme
cut := strings.Index(pattern, "://")
if cut == -1 {
// default scheme of gemini
key.scheme = "gemini"
} else {
key.scheme = pattern[:cut]
pattern = pattern[cut+3:]
}
}
var host, path string
// extract hostname and path
cut := strings.Index(pattern, "/")
if cut == -1 {
key.host = pattern
key.path = "/"
host = pattern
path = "/"
} else {
key.host = pattern[:cut]
key.path = pattern[cut:]
host = pattern[:cut]
path = pattern[cut:]
}
// strip port from hostname
if hostname, _, err := net.SplitHostPort(key.host); err == nil {
key.host = hostname
if hostname, _, err := net.SplitHostPort(host); err == nil {
host = hostname
}
if _, exist := mux.m[key]; exist {
if _, exist := mux.m[hostpath{host, path}]; exist {
panic("gemini: multiple registrations for " + pattern)
}
if mux.m == nil {
mux.m = make(map[muxKey]Handler)
mux.m = make(map[hostpath]Handler)
}
mux.m[key] = handler
e := muxEntry{handler, key}
if key.path[len(key.path)-1] == '/' {
mux.m[hostpath{host, path}] = handler
e := muxEntry{handler, host, path}
if path[len(path)-1] == '/' {
mux.es = appendSorted(mux.es, e)
}
}
@ -279,9 +263,7 @@ func (mux *ServeMux) Handle(pattern string, handler Handler) {
func appendSorted(es []muxEntry, e muxEntry) []muxEntry {
n := len(es)
i := sort.Search(n, func(i int) bool {
return len(es[i].key.scheme) < len(e.key.scheme) ||
len(es[i].key.host) < len(es[i].key.host) ||
len(es[i].key.path) < len(e.key.path)
return len(es[i].path) < len(e.path)
})
if i == n {
return append(es, e)
@ -294,6 +276,6 @@ func appendSorted(es []muxEntry, e muxEntry) []muxEntry {
}
// HandleFunc registers the handler function for the given pattern.
func (mux *ServeMux) HandleFunc(pattern string, handler HandlerFunc) {
func (mux *Mux) HandleFunc(pattern string, handler HandlerFunc) {
mux.Handle(pattern, handler)
}

View File

@ -2,6 +2,7 @@ package gemini
import (
"context"
"io"
"net/url"
"testing"
)
@ -10,7 +11,222 @@ type nopHandler struct{}
func (*nopHandler) ServeGemini(context.Context, ResponseWriter, *Request) {}
func TestServeMuxMatch(t *testing.T) {
type nopResponseWriter struct {
Status Status
Meta string
}
func (w *nopResponseWriter) WriteHeader(status Status, meta string) {
w.Status = status
w.Meta = meta
}
func (nopResponseWriter) SetMediaType(mediatype string) {}
func (nopResponseWriter) Write(b []byte) (int, error) { return 0, io.EOF }
func (nopResponseWriter) Flush() error { return nil }
func TestMux(t *testing.T) {
type Test struct {
URL string
Pattern string
Redirect string
NotFound bool
}
tests := []struct {
Patterns []string
Tests []Test
}{
{
Patterns: []string{"/a", "/b/", "/b/c/d", "/b/c/d/"},
Tests: []Test{
{
URL: "gemini://example.com",
Redirect: "gemini://example.com/",
},
{
URL: "gemini://example.com/",
NotFound: true,
},
{
URL: "gemini://example.com/c",
NotFound: true,
},
{
URL: "gemini://example.com/a",
Pattern: "/a",
},
{
URL: "gemini://example.com/a/",
NotFound: true,
},
{
URL: "gemini://example.com/b",
Redirect: "gemini://example.com/b/",
},
{
URL: "gemini://example.com/b/",
Pattern: "/b/",
},
{
URL: "gemini://example.com/b/c",
Pattern: "/b/",
},
{
URL: "gemini://example.com/b/c/d",
Pattern: "/b/c/d",
},
{
URL: "gemini://example.com/b/c/d/e/",
Pattern: "/b/c/d/",
},
},
},
{
Patterns: []string{
"/", "/a", "/b/",
"example.com", "example.com/a", "example.com/b/",
"*.example.com", "*.example.com/a", "*.example.com/b/",
},
Tests: []Test{
{
URL: "gemini://example.net/",
Pattern: "/",
},
{
URL: "gemini://example.net/a",
Pattern: "/a",
},
{
URL: "gemini://example.net/b",
Redirect: "gemini://example.net/b/",
},
{
URL: "gemini://example.net/b/",
Pattern: "/b/",
},
{
URL: "gemini://example.com/",
Pattern: "example.com",
},
{
URL: "gemini://example.com/b",
Redirect: "gemini://example.com/b/",
},
{
URL: "gemini://example.com/b/",
Pattern: "example.com/b/",
},
{
URL: "gemini://a.example.com/",
Pattern: "*.example.com",
},
{
URL: "gemini://b.example.com/a",
Pattern: "*.example.com/a",
},
{
URL: "gemini://c.example.com/b",
Redirect: "gemini://c.example.com/b/",
},
{
URL: "gemini://d.example.com/b/",
Pattern: "*.example.com/b/",
},
},
},
{
Patterns: []string{"example.net", "*.example.org"},
Tests: []Test{
{
// The following redirect occurs as a result of cleaning
// the path provided to the Mux. This happens even if there
// are no matching handlers.
URL: "gemini://example.com",
Redirect: "gemini://example.com/",
},
{
URL: "gemini://example.com/",
NotFound: true,
},
{
URL: "gemini://example.net",
Redirect: "gemini://example.net/",
},
{
URL: "gemini://example.org/",
NotFound: true,
},
{
URL: "gemini://gemini.example.org",
Redirect: "gemini://gemini.example.org/",
},
},
},
}
for _, test := range tests {
type handler struct {
nopHandler
Pattern string
}
mux := &Mux{}
for _, pattern := range test.Patterns {
mux.Handle(pattern, &handler{
Pattern: pattern,
})
}
for _, test := range test.Tests {
u, err := url.Parse(test.URL)
if err != nil {
panic(err)
}
req := &Request{URL: u}
h := mux.Handler(req)
if h, ok := h.(*handler); ok {
if h.Pattern != test.Pattern {
t.Errorf("wrong pattern for %q: expected %q, got %q", test.URL, test.Pattern, h.Pattern)
}
continue
}
// Check redirects and NotFounds
w := &nopResponseWriter{}
h.ServeGemini(context.Background(), w, req)
switch w.Status {
case StatusNotFound:
if !test.NotFound {
t.Errorf("expected pattern for %q, got NotFound", test.URL)
}
case StatusPermanentRedirect:
if test.Redirect == "" {
t.Errorf("expected pattern for %q, got redirect to %q", test.URL, w.Meta)
break
}
res, err := url.Parse(test.Redirect)
if err != nil {
panic(err)
}
if w.Meta != res.String() {
t.Errorf("bad redirect for %q: expected %q, got %q", test.URL, res.String(), w.Meta)
}
default:
t.Errorf("unexpected response for %q: %d %s", test.URL, w.Status, w.Meta)
}
}
}
}
func TestMuxMatch(t *testing.T) {
type Match struct {
URL string
Ok bool
@ -21,7 +237,7 @@ func TestServeMuxMatch(t *testing.T) {
Matches []Match
}{
{
// scheme: gemini, hostname: *, path: /*
// hostname: *, path: /*
Pattern: "/",
Matches: []Match{
{"gemini://example.com/path", true},
@ -34,7 +250,7 @@ func TestServeMuxMatch(t *testing.T) {
},
},
{
// scheme: gemini, hostname: *, path: /path
// hostname: *, path: /path
Pattern: "/path",
Matches: []Match{
{"gemini://example.com/path", true},
@ -47,7 +263,7 @@ func TestServeMuxMatch(t *testing.T) {
},
},
{
// scheme: gemini, hostname: *, path: /subtree/*
// hostname: *, path: /subtree/*
Pattern: "/subtree/",
Matches: []Match{
{"gemini://example.com/subtree/", true},
@ -62,7 +278,7 @@ func TestServeMuxMatch(t *testing.T) {
},
},
{
// scheme: gemini, hostname: example.com, path: /*
// hostname: example.com, path: /*
Pattern: "example.com",
Matches: []Match{
{"gemini://example.com/path", true},
@ -75,7 +291,7 @@ func TestServeMuxMatch(t *testing.T) {
},
},
{
// scheme: gemini, hostname: example.com, path: /path
// hostname: example.com, path: /path
Pattern: "example.com/path",
Matches: []Match{
{"gemini://example.com/path", true},
@ -88,7 +304,7 @@ func TestServeMuxMatch(t *testing.T) {
},
},
{
// scheme: gemini, hostname: example.com, path: /subtree/*
// hostname: example.com, path: /subtree/*
Pattern: "example.com/subtree/",
Matches: []Match{
{"gemini://example.com/subtree/", true},
@ -102,170 +318,6 @@ func TestServeMuxMatch(t *testing.T) {
{"http://example.com/subtree/", false},
},
},
{
// scheme: http, hostname: example.com, path: /*
Pattern: "http://example.com",
Matches: []Match{
{"http://example.com/path", true},
{"http://example.com/", true},
{"http://example.com/path.gmi", true},
{"http://example.com/path/", true},
{"http://example.org/path", false},
{"gemini://example.com/path", false},
{"gemini://example.org/path", false},
},
},
{
// scheme: http, hostname: example.com, path: /path
Pattern: "http://example.com/path",
Matches: []Match{
{"http://example.com/path", true},
{"http://example.com/", false},
{"http://example.com/path.gmi", false},
{"http://example.com/path/", false},
{"http://example.org/path", false},
{"gemini://example.com/path", false},
{"gemini://example.org/path", false},
},
},
{
// scheme: http, hostname: example.com, path: /subtree/*
Pattern: "http://example.com/subtree/",
Matches: []Match{
{"http://example.com/subtree/", true},
{"http://example.com/subtree/nested/", true},
{"http://example.com/subtree/nested/file", true},
{"http://example.org/subtree/", false},
{"http://example.org/subtree/nested/", false},
{"http://example.org/subtree/nested/file", false},
{"http://example.com/subtree", false},
{"http://www.example.com/subtree/", false},
{"gemini://example.com/subtree/", false},
},
},
{
// scheme: *, hostname: example.com, path: /*
Pattern: "//example.com",
Matches: []Match{
{"gemini://example.com/path", true},
{"gemini://example.com/", true},
{"gemini://example.com/path.gmi", true},
{"gemini://example.com/path/", true},
{"gemini://example.org/path", false},
{"http://example.com/path", true},
{"http://example.org/path", false},
},
},
{
// scheme: *, hostname: example.com, path: /path
Pattern: "//example.com/path",
Matches: []Match{
{"gemini://example.com/path", true},
{"gemini://example.com/", false},
{"gemini://example.com/path.gmi", false},
{"gemini://example.com/path/", false},
{"gemini://example.org/path", false},
{"http://example.com/path", true},
{"http://example.org/path", false},
},
},
{
// scheme: *, hostname: example.com, path: /subtree/*
Pattern: "//example.com/subtree/",
Matches: []Match{
{"gemini://example.com/subtree/", true},
{"gemini://example.com/subtree/nested/", true},
{"gemini://example.com/subtree/nested/file", true},
{"gemini://example.org/subtree/", false},
{"gemini://example.org/subtree/nested/", false},
{"gemini://example.org/subtree/nested/file", false},
{"gemini://example.com/subtree", false},
{"gemini://www.example.com/subtree/", false},
{"http://example.com/subtree/", true},
},
},
{
// scheme: http, hostname: *, path: /*
Pattern: "http://",
Matches: []Match{
{"http://example.com/path", true},
{"http://example.com/", true},
{"http://example.com/path.gmi", true},
{"http://example.com/path/", true},
{"http://example.org/path", true},
{"gemini://example.com/path", false},
{"gemini://example.org/path", false},
},
},
{
// scheme: http, hostname: *, path: /path
Pattern: "http:///path",
Matches: []Match{
{"http://example.com/path", true},
{"http://example.com/", false},
{"http://example.com/path.gmi", false},
{"http://example.com/path/", false},
{"http://example.org/path", true},
{"gemini://example.com/path", false},
{"gemini://example.org/path", false},
},
},
{
// scheme: http, hostname: *, path: /subtree/*
Pattern: "http:///subtree/",
Matches: []Match{
{"http://example.com/subtree/", true},
{"http://example.com/subtree/nested/", true},
{"http://example.com/subtree/nested/file", true},
{"http://example.org/subtree/", true},
{"http://example.org/subtree/nested/", true},
{"http://example.org/subtree/nested/file", true},
{"http://example.com/subtree", false},
{"http://www.example.com/subtree/", true},
{"gemini://example.com/subtree/", false},
},
},
{
// scheme: *, hostname: *, path: /*
Pattern: "//",
Matches: []Match{
{"gemini://example.com/path", true},
{"gemini://example.com/", true},
{"gemini://example.com/path.gmi", true},
{"gemini://example.com/path/", true},
{"gemini://example.org/path", true},
{"http://example.com/path", true},
{"http://example.org/path", true},
},
},
{
// scheme: *, hostname: *, path: /path
Pattern: "///path",
Matches: []Match{
{"gemini://example.com/path", true},
{"gemini://example.com/", false},
{"gemini://example.com/path.gmi", false},
{"gemini://example.com/path/", false},
{"gemini://example.org/path", true},
{"http://example.com/path", true},
{"http://example.org/path", true},
},
},
{
// scheme: *, hostname: *, path: /subtree/*
Pattern: "///subtree/",
Matches: []Match{
{"gemini://example.com/subtree/", true},
{"gemini://example.com/subtree/nested/", true},
{"gemini://example.com/subtree/nested/file", true},
{"gemini://example.org/subtree/", true},
{"gemini://example.org/subtree/nested/", true},
{"gemini://example.org/subtree/nested/file", true},
{"gemini://example.com/subtree", false},
{"gemini://www.example.com/subtree/", true},
{"http://example.com/subtree/", true},
},
},
{
// scheme: gemini, hostname: *.example.com, path: /*
Pattern: "*.example.com",
@ -277,25 +329,14 @@ func TestServeMuxMatch(t *testing.T) {
{"http://www.example.com/", false},
},
},
{
// scheme: http, hostname: *.example.com, path: /*
Pattern: "http://*.example.com",
Matches: []Match{
{"http://mail.example.com/", true},
{"http://www.example.com/index.gmi", true},
{"http://example.com/", false},
{"http://a.b.example.com/", false},
{"gemini://www.example.com/", false},
},
},
}
for i, test := range tests {
for _, test := range tests {
h := &nopHandler{}
var mux ServeMux
var mux Mux
mux.Handle(test.Pattern, h)
for _, match := range tests[i].Matches {
for _, match := range test.Matches {
u, err := url.Parse(match.URL)
if err != nil {
panic(err)

View File

@ -4,7 +4,6 @@ import (
"bufio"
"crypto/tls"
"io"
"net"
"net/url"
)
@ -28,8 +27,7 @@ type Request struct {
// This field is ignored by the Gemini server.
Certificate *tls.Certificate
conn net.Conn
tls *tls.ConnectionState
TLS *tls.ConnectionState
}
// NewRequest returns a new request.
@ -51,26 +49,25 @@ func NewRequest(rawurl string) (*Request, error) {
// for specialized applications; most code should use the Server
// to read requests and handle them via the Handler interface.
func ReadRequest(r io.Reader) (*Request, error) {
// Read URL
// Limit request size
r = io.LimitReader(r, 1026)
br := bufio.NewReaderSize(r, 1026)
rawurl, err := br.ReadString('\r')
b, err := br.ReadBytes('\n')
if err != nil {
if err == io.EOF {
return nil, ErrInvalidRequest
}
return nil, err
}
// Read terminating line feed
if b, err := br.ReadByte(); err != nil {
return nil, err
} else if b != '\n' {
// Read URL
rawurl, ok := trimCRLF(b)
if !ok {
return nil, ErrInvalidRequest
}
// Trim carriage return
rawurl = rawurl[:len(rawurl)-1]
// Validate URL
if len(rawurl) > 1024 {
if len(rawurl) == 0 {
return nil, ErrInvalidRequest
}
u, err := url.Parse(rawurl)
u, err := url.Parse(string(rawurl))
if err != nil {
return nil, err
}
@ -99,30 +96,11 @@ func (r *Request) WriteTo(w io.Writer) (int64, error) {
return wrote, bw.Flush()
}
// Conn returns the network connection on which the request was received.
// Conn returns nil for client requests.
func (r *Request) Conn() net.Conn {
return r.conn
}
// TLS returns information about the TLS connection on which the
// request was received.
// TLS returns nil for client requests.
func (r *Request) TLS() *tls.ConnectionState {
if r.tls == nil {
if tlsConn, ok := r.conn.(*tls.Conn); ok {
state := tlsConn.ConnectionState()
r.tls = &state
}
}
return r.tls
}
// ServerName returns the value of the TLS Server Name Indication extension
// sent by the client.
// ServerName returns an empty string for client requests.
func (r *Request) ServerName() string {
if tls := r.TLS(); tls != nil {
if tls := r.TLS; tls != nil {
return tls.ServerName
}
return ""

View File

@ -2,7 +2,6 @@ package gemini
import (
"bufio"
"io"
"net/url"
"strings"
"testing"
@ -36,25 +35,25 @@ func TestReadRequest(t *testing.T) {
},
{
Raw: "\r\n",
URL: &url.URL{},
Err: ErrInvalidRequest,
},
{
Raw: "gemini://example.com\n",
Err: io.EOF,
Err: ErrInvalidRequest,
},
{
Raw: "gemini://example.com",
Err: io.EOF,
Err: ErrInvalidRequest,
},
{
// 1030 bytes
Raw: maxURL + "xxxxxx",
Err: io.EOF,
Err: ErrInvalidRequest,
},
{
// 1027 bytes
Raw: maxURL + "x" + "\r\n",
Err: io.EOF,
Err: ErrInvalidRequest,
},
{
// 1024 bytes

View File

@ -10,7 +10,7 @@ import (
)
// The default media type for responses.
const defaultMediaType = "text/gemini; charset=utf-8"
const defaultMediaType = "text/gemini"
// Response represents the response from a Gemini request.
//
@ -44,52 +44,56 @@ type Response struct {
// ReadResponse reads a Gemini response from the provided io.ReadCloser.
func ReadResponse(r io.ReadCloser) (*Response, error) {
resp := &Response{}
br := bufio.NewReader(r)
// Read the status
statusB := make([]byte, 2)
if _, err := br.Read(statusB); err != nil {
// Limit response header size
lr := io.LimitReader(r, 1029)
// Wrap the reader to remove the limit later on
wr := &struct{ io.Reader }{lr}
br := bufio.NewReader(wr)
// Read response header
b, err := br.ReadBytes('\n')
if err != nil {
if err == io.EOF {
return nil, ErrInvalidResponse
}
return nil, err
}
status, err := strconv.Atoi(string(statusB))
if len(b) < 3 {
return nil, ErrInvalidResponse
}
// Read the status
status, err := strconv.Atoi(string(b[:2]))
if err != nil {
return nil, ErrInvalidResponse
}
resp.Status = Status(status)
// Read one space
if b, err := br.ReadByte(); err != nil {
return nil, err
} else if b != ' ' {
if b[2] != ' ' {
return nil, ErrInvalidResponse
}
// Read the meta
meta, err := br.ReadString('\r')
if err != nil {
return nil, err
}
// Trim carriage return
meta = meta[:len(meta)-1]
// Ensure meta is less than or equal to 1024 bytes
if len(meta) > 1024 {
meta, ok := trimCRLF(b[3:])
if !ok {
return nil, ErrInvalidResponse
}
if resp.Status.Class() == StatusSuccess && meta == "" {
// Use default media type
meta = defaultMediaType
}
resp.Meta = meta
// Read terminating newline
if b, err := br.ReadByte(); err != nil {
return nil, err
} else if b != '\n' {
if len(meta) == 0 {
return nil, ErrInvalidResponse
}
resp.Meta = string(meta)
if resp.Status.Class() == StatusSuccess {
resp.Body = newBufReadCloser(br, r)
// Use unlimited reader
wr.Reader = r
type readCloser struct {
io.Reader
io.Closer
}
resp.Body = readCloser{br, r}
} else {
resp.Body = nopReadCloser{}
r.Close()
@ -142,8 +146,8 @@ func (r *Response) WriteTo(w io.Writer) (int64, error) {
// has returned.
type ResponseWriter interface {
// SetMediaType sets the media type that will be sent by Write for a
// successful response. If no media type is set, a default of
// "text/gemini; charset=utf-8" will be used.
// successful response. If no media type is set, a default media type of
// "text/gemini" will be used.
//
// Setting the media type after a call to Write or WriteHeader has
// no effect.
@ -154,7 +158,7 @@ type ResponseWriter interface {
// If WriteHeader has not yet been called, Write calls WriteHeader with
// StatusSuccess and the media type set in SetMediaType before writing the data.
// If no media type was set, Write uses a default media type of
// "text/gemini; charset=utf-8".
// "text/gemini".
Write([]byte) (int, error)
// WriteHeader sends a Gemini response header with the provided
@ -175,7 +179,6 @@ type ResponseWriter interface {
type responseWriter struct {
bw *bufio.Writer
cl io.Closer
mediatype string
wroteHeader bool
bodyAllowed bool

View File

@ -2,6 +2,7 @@ package gemini
import (
"io"
"io/ioutil"
"strings"
"testing"
)
@ -65,15 +66,15 @@ func TestReadWriteResponse(t *testing.T) {
},
{
Raw: "",
Err: io.EOF,
Err: ErrInvalidResponse,
},
{
Raw: "10 Search query",
Err: io.EOF,
Err: ErrInvalidResponse,
},
{
Raw: "20 text/gemini\nHello, world!",
Err: io.EOF,
Err: ErrInvalidResponse,
},
{
Raw: "20 text/gemini\rHello, world!",
@ -81,7 +82,7 @@ func TestReadWriteResponse(t *testing.T) {
},
{
Raw: "20 text/gemini\r",
Err: io.EOF,
Err: ErrInvalidResponse,
},
{
Raw: "abcdefghijklmnopqrstuvwxyz",
@ -91,7 +92,7 @@ func TestReadWriteResponse(t *testing.T) {
for _, test := range tests {
t.Logf("%#v", test.Raw)
resp, err := ReadResponse(io.NopCloser(strings.NewReader(test.Raw)))
resp, err := ReadResponse(ioutil.NopCloser(strings.NewReader(test.Raw)))
if err != test.Err {
t.Errorf("expected err = %v, got %v", test.Err, err)
}
@ -105,7 +106,7 @@ func TestReadWriteResponse(t *testing.T) {
if resp.Meta != test.Meta {
t.Errorf("expected meta = %s, got %s", test.Meta, resp.Meta)
}
b, _ := io.ReadAll(resp.Body)
b, _ := ioutil.ReadAll(resp.Body)
body := string(b)
if body != test.Body {
t.Errorf("expected body = %#v, got %#v", test.Body, body)

View File

@ -371,7 +371,10 @@ func (srv *Server) goServeConn(ctx context.Context, conn net.Conn) error {
w.WriteHeader(StatusBadRequest, "Bad request")
return w.Flush()
}
req.conn = conn
if tlsConn, ok := conn.(*tls.Conn); ok {
state := tlsConn.ConnectionState()
req.TLS = &state
}
h := srv.Handler
if h == nil {

View File

@ -6,6 +6,7 @@ type Status int
// Gemini status codes.
const (
StatusInput Status = 10
StatusSensitiveInput Status = 11
StatusSuccess Status = 20
StatusRedirect Status = 30
StatusPermanentRedirect Status = 31
@ -36,6 +37,8 @@ func (s Status) String() string {
switch s {
case StatusInput:
return "Input"
case StatusSensitiveInput:
return "Sensitive input"
case StatusSuccess:
return "Success"
case StatusRedirect:

View File

@ -125,8 +125,8 @@ func ParseLines(r io.Reader, handler func(Line)) error {
name = strings.TrimLeft(name, spacetab)
line = LineLink{url, name}
}
} else if strings.HasPrefix(text, "*") {
text = text[1:]
} else if strings.HasPrefix(text, "* ") {
text = text[2:]
text = strings.TrimLeft(text, spacetab)
line = LineListItem(text)
} else if strings.HasPrefix(text, "###") {