Compare commits
11 Commits
v0.2.0-rc.
...
v0.2.1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1170e007d4 | ||
|
|
c85759d777 | ||
|
|
507773618b | ||
|
|
3bc243dd66 | ||
|
|
de93d44786 | ||
|
|
eb32c32063 | ||
|
|
e5cf345273 | ||
|
|
c68ce57488 | ||
|
|
2b161650fe | ||
|
|
dbbef1fb6d | ||
|
|
90518a01a8 |
64
LICENSE
64
LICENSE
@@ -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
27
LICENSE-GO
Normal 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.
|
||||
15
README.md
15
README.md
@@ -2,9 +2,9 @@
|
||||
|
||||
[](https://godocs.io/git.sr.ht/~adnano/go-gemini) [](https://builds.sr.ht/~adnano/go-gemini?)
|
||||
|
||||
Package gemini implements the [Gemini protocol](https://gemini.circumlunar.space) in Go.
|
||||
|
||||
It provides an API similar to that of net/http to make it easy to develop Gemini clients and servers.
|
||||
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.
|
||||
|
||||
Compatible with version v0.14.3 of the Gemini specification.
|
||||
|
||||
@@ -12,6 +12,9 @@ Compatible with version v0.14.3 of the Gemini specification.
|
||||
|
||||
import "git.sr.ht/~adnano/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
|
||||
|
||||
There are a few examples provided in the examples directory.
|
||||
@@ -24,3 +27,9 @@ To run an example:
|
||||
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).
|
||||
|
||||
## License
|
||||
|
||||
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.
|
||||
|
||||
172
fs.go
172
fs.go
@@ -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,51 +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
|
||||
}
|
||||
w.WriteHeader(StatusPermanentRedirect, 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)+"/")
|
||||
return
|
||||
}
|
||||
|
||||
// Use contents of index.gmi if present
|
||||
name = path.Join(name, indexPage)
|
||||
index, err := fsys.Open(name)
|
||||
if err == nil {
|
||||
defer index.Close()
|
||||
istat, err := index.Stat()
|
||||
if err == nil {
|
||||
f = index
|
||||
stat = istat
|
||||
}
|
||||
f = index
|
||||
} else {
|
||||
// Failed to find index file
|
||||
dirList(w, f)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if stat.IsDir() {
|
||||
// Failed to find index file
|
||||
dirList(w, f)
|
||||
// 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) {
|
||||
|
||||
2
go.mod
2
go.mod
@@ -1,5 +1,5 @@
|
||||
module git.sr.ht/~adnano/go-gemini
|
||||
|
||||
go 1.16
|
||||
go 1.15
|
||||
|
||||
require golang.org/x/net v0.0.0-20210119194325-5f4716e94777
|
||||
|
||||
@@ -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
|
||||
|
||||
4
mux.go
4
mux.go
@@ -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 (
|
||||
|
||||
@@ -2,6 +2,7 @@ package gemini
|
||||
|
||||
import (
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user