Compare commits

...
This repository has been archived on 2023-05-01. You can view files and clone it, but cannot push or open issues or pull requests.

6 Commits

12 changed files with 714 additions and 19 deletions

46
contrib/cgi/spartan.go Normal file
View File

@ -0,0 +1,46 @@
package cgi
import (
"context"
"fmt"
"strings"
"tildegit.org/tjp/gus"
"tildegit.org/tjp/gus/spartan"
)
// SpartanCGIDirectory runs executable files relative to a root directory in the file system.
//
// It will also find any run any executable _part way_ through the path, so for example a
// request for /foo/bar/baz can also run an executable found at /foo or /foo/bar. In such
// a case the PATH_INFO environment variable will include the remaining portion of the URI.
func SpartanCGIDirectory(pathRoot, fsRoot string) gus.Handler {
fsRoot = strings.TrimRight(fsRoot, "/")
return gus.HandlerFunc(func(ctx context.Context, request *gus.Request) *gus.Response {
if !strings.HasPrefix(request.Path, pathRoot) {
return nil
}
filepath, pathinfo, err := ResolveCGI(request.Path[len(pathRoot):], fsRoot)
if err != nil {
return spartan.ServerError(err)
}
if filepath == "" {
return nil
}
stdout, exitCode, err := RunCGI(ctx, request, filepath, pathinfo)
if err != nil {
return spartan.ServerError(err)
}
if exitCode != 0 {
return spartan.ServerError(fmt.Errorf("CGI process exited with status %d", exitCode))
}
response, err := spartan.ParseResponse(stdout)
if err != nil {
return spartan.ServerError(err)
}
return response
})
}

View File

@ -37,7 +37,7 @@ func GeminiFileHandler(fileSystem fs.FS) gus.Handler {
//
// When it encounters a directory path which doesn't end in a trailing slash (/) it
// redirects to a URL with the trailing slash appended. This is necessary for relative
// links inot the directory's contents to function properly.
// links not the directory's contents to function properly.
//
// It requires that files from the provided fs.FS implement fs.ReadDirFile. If they
// don't, it will produce nil responses for any directory paths.
@ -70,7 +70,7 @@ func GeminiDirectoryDefault(fileSystem fs.FS, filenames ...string) gus.Handler {
//
// When it encounters a directory path which doesn't end in a trailing slash (/) it
// redirects to a URL with the trailing slash appended. This is necessary for relative
// links inot the directory's contents to function properly.
// links not the directory's contents to function properly.
//
// It requires that files from the provided fs.FS implement fs.ReadDirFile. If they
// don't, it will produce "51 Not Found" responses for any directory paths.

124
contrib/fs/spartan.go Normal file
View File

@ -0,0 +1,124 @@
package fs
import (
"context"
"io/fs"
"strings"
"text/template"
"tildegit.org/tjp/gus"
"tildegit.org/tjp/gus/spartan"
)
// SpartanFileHandler builds a handler which serves up files from a filesystem.
//
// It only serves responses for paths which do not correspond to directories on disk.
func SpartanFileHandler(fileSystem fs.FS) gus.Handler {
return gus.HandlerFunc(func(ctx context.Context, request *gus.Request) *gus.Response {
filepath, file, err := ResolveFile(request, fileSystem)
if err != nil {
return spartan.ClientError(err)
}
if file == nil {
return nil
}
return spartan.Success(mediaType(filepath), file)
})
}
// SpartanDirectoryDefault serves up default files for directory path requests.
//
// If any of the supported filenames are found, the contents of the file is returned
// as the spartan response.
//
// It returns nil for any paths which don't correspond to a directory.
//
// When it encounters a directory path which doesn't end in a trailing slash (/) it
// redirects to the same URL with the slash appended. This is necessary for relative
// links not in the directory's contents to function properly.
//
// It requires that files from the provided fs.FS implement fs.ReadDirFile. If they
// don't, it will produce nil responses for any directory paths.
func SpartanDirectoryDefault(fileSystem fs.FS, filenames ...string) gus.Handler {
return gus.HandlerFunc(func(ctx context.Context, request *gus.Request) *gus.Response {
dirpath, dir, response := handleDirSpartan(request, fileSystem)
if response != nil {
return response
}
if dir == nil {
return nil
}
defer func() { _ = dir.Close() }()
filepath, file, err := ResolveDirectoryDefault(fileSystem, dirpath, dir, filenames)
if err != nil {
return spartan.ServerError(err)
}
if file == nil {
return nil
}
return spartan.Success(mediaType(filepath), file)
})
}
// SpartanDirectoryListing produces a listing of the contents of any requested directories.
//
// It returns "4 Resource not found" for any paths which don't correspond to a filesystem directory.
//
// When it encounters a directory path which doesn't end in a trailing slash (/) it redirects to a
// URL with the trailing slash appended. This is necessary for relative links not in the directory's
// contents to function properly.
//
// It requires that files provided by the fs.FS implement fs.ReadDirFile. If they don't, it will
// produce "4 Resource not found" responses for any directory paths.
//
// The tmeplate may be nil, in which cause DefaultSpartanDirectoryList is used instead. The
// template is then processed with RenderDirectoryListing.
func SpartanDirectoryListing(filesystem fs.FS, template *template.Template) gus.Handler {
return gus.HandlerFunc(func(ctx context.Context, request *gus.Request) *gus.Response {
dirpath, dir, response := handleDirSpartan(request, filesystem)
if response != nil {
return response
}
if dir == nil {
return nil
}
defer func() { _ = dir.Close() }()
if template == nil {
template = DefaultSpartanDirectoryList
}
body, err := RenderDirectoryListing(dirpath, dir, template, request.Server)
if err != nil {
return spartan.ServerError(err)
}
return spartan.Success("text/gemini", body)
})
}
// DefaultSpartanDirectoryList is a template which renders a reasonable gemtext dir listing.
var DefaultSpartanDirectoryList = DefaultGeminiDirectoryList
func handleDirSpartan(request *gus.Request, filesystem fs.FS) (string, fs.ReadDirFile, *gus.Response) {
path, dir, err := ResolveDirectory(request, filesystem)
if err != nil {
return "", nil, spartan.ServerError(err)
}
if dir == nil {
return "", nil, nil
}
if !strings.HasSuffix(request.Path, "/") {
_ = dir.Close()
url := *request.URL
url.Path += "/"
return "", nil, spartan.Redirect(url.String())
}
return path, dir, nil
}

View File

@ -10,10 +10,16 @@ func ParseLine(line []byte) Line {
switch line[0] {
case '=':
if len(line) == 1 || line[1] != '>' {
if len(line) == 1 {
break
}
return parseLinkLine(line)
if line[1] == '>' {
return parseLinkLine(line)
}
if line[1] == ':' {
return parsePromptLine(line)
}
break
case '`':
if len(line) < 3 || line[1] != '`' || line[2] != '`' {
break
@ -73,6 +79,39 @@ func parseLinkLine(raw []byte) LinkLine {
return line
}
func parsePromptLine(raw []byte) PromptLine {
line := PromptLine{raw: raw}
// move past =:[<whitespace>]
raw = bytes.TrimLeft(raw[2:], " \t")
// find the next space or tab
spIdx := bytes.IndexByte(raw, ' ')
tbIdx := bytes.IndexByte(raw, '\t')
idx := spIdx
if idx == -1 {
idx = tbIdx
}
if tbIdx >= 0 && tbIdx < idx {
idx = tbIdx
}
if idx < 0 {
line.url = bytes.TrimRight(raw, "\r\n")
return line
}
line.url = raw[:idx]
raw = raw[idx+1:]
label := bytes.TrimRight(bytes.TrimLeft(raw, " \t"), "\r\n")
if len(label) > 0 {
line.label = label
}
return line
}
func parsePreformatToggleLine(raw []byte) PreformatToggleLine {
line := PreformatToggleLine{raw: raw}

View File

@ -57,6 +57,57 @@ func TestParseLinkLine(t *testing.T) {
}
}
func TestParsePromptLine(t *testing.T) {
tests := []struct {
input string
url string
label string
}{
{
input: "=: gemini.ctrl-c.club/~tjp/ home page\r\n",
url: "gemini.ctrl-c.club/~tjp/",
label: "home page",
},
{
input: "=: gemi.dev/\n",
url: "gemi.dev/",
},
{
input: "=: /gemlog/foobar 2023-01-13 - Foo Bar\n",
url: "/gemlog/foobar",
label: "2023-01-13 - Foo Bar",
},
}
for _, test := range tests {
t.Run(test.input, func(t *testing.T) {
line := gemtext.ParseLine([]byte(test.input))
if line == nil {
t.Fatal("ParseLine() returned nil line")
}
if string(line.Raw()) != string(test.input) {
t.Error("Raw() does not match input")
}
if line.Type() != gemtext.LineTypePrompt{
t.Errorf("expected LineTypePrompt, got %d", line.Type())
}
link, ok := line.(gemtext.PromptLine)
if !ok {
t.Fatalf("expected a PromptLine, got %T", line)
}
if link.URL() != test.url {
t.Errorf("expected url %q, got %q", test.url, link.URL())
}
if link.Label() != test.label {
t.Errorf("expected label %q, got %q", test.label, link.Label())
}
})
}
}
func TestParsePreformatToggleLine(t *testing.T) {
tests := []struct {
input string

View File

@ -24,6 +24,8 @@ This is some non-blank regular text.
=> gemini://google.com/ as if
=: spartan://foo.bar/baz this should be a spartan prompt
> this is a quote
> -tjp
@ -37,7 +39,7 @@ This is some non-blank regular text.
doc, err := gemtext.Parse(bytes.NewBuffer(docBytes))
require.Nil(t, err)
require.Equal(t, 18, len(doc))
require.Equal(t, 20, len(doc))
assert.Equal(t, gemtext.LineTypeHeading1, doc[0].Type())
assert.Equal(t, "# top-level header line\n", string(doc[0].Raw()))
@ -74,26 +76,33 @@ This is some non-blank regular text.
assertEmptyLine(t, doc[11])
assert.Equal(t, gemtext.LineTypeQuote, doc[12].Type())
assert.Equal(t, "> this is a quote\n", string(doc[12].Raw()))
assert.Equal(t, " this is a quote", doc[12].(gemtext.QuoteLine).Body())
assert.Equal(t, gemtext.LineTypePrompt, doc[12].Type())
assert.Equal(t, "=: spartan://foo.bar/baz this should be a spartan prompt\n", string(doc[12].Raw()))
assert.Equal(t, "spartan://foo.bar/baz", doc[12].(gemtext.PromptLine).URL())
assert.Equal(t, "this should be a spartan prompt", doc[12].(gemtext.PromptLine).Label())
assert.Equal(t, gemtext.LineTypeQuote, doc[13].Type())
assert.Equal(t, "> -tjp\n", string(doc[13].Raw()))
assert.Equal(t, " -tjp", doc[13].(gemtext.QuoteLine).Body())
assertEmptyLine(t, doc[13])
assertEmptyLine(t, doc[14])
assert.Equal(t, gemtext.LineTypeQuote, doc[14].Type())
assert.Equal(t, "> this is a quote\n", string(doc[14].Raw()))
assert.Equal(t, " this is a quote", doc[14].(gemtext.QuoteLine).Body())
assert.Equal(t, gemtext.LineTypePreformatToggle, doc[15].Type())
assert.Equal(t, "```pre-formatted code\n", string(doc[15].Raw()))
assert.Equal(t, "pre-formatted code", doc[15].(gemtext.PreformatToggleLine).AltText())
assert.Equal(t, gemtext.LineTypeQuote, doc[15].Type())
assert.Equal(t, "> -tjp\n", string(doc[15].Raw()))
assert.Equal(t, " -tjp", doc[15].(gemtext.QuoteLine).Body())
assert.Equal(t, gemtext.LineTypePreformattedText, doc[16].Type())
assert.Equal(t, "doc := gemtext.Parse(req.Body)\n", string(doc[16].Raw()))
assertEmptyLine(t, doc[16])
assert.Equal(t, gemtext.LineTypePreformatToggle, doc[17].Type())
assert.Equal(t, "```ignored closing alt-text\n", string(doc[17].Raw()))
assert.Equal(t, "", doc[17].(gemtext.PreformatToggleLine).AltText())
assert.Equal(t, "```pre-formatted code\n", string(doc[17].Raw()))
assert.Equal(t, "pre-formatted code", doc[17].(gemtext.PreformatToggleLine).AltText())
assert.Equal(t, gemtext.LineTypePreformattedText, doc[18].Type())
assert.Equal(t, "doc := gemtext.Parse(req.Body)\n", string(doc[18].Raw()))
assert.Equal(t, gemtext.LineTypePreformatToggle, doc[19].Type())
assert.Equal(t, "```ignored closing alt-text\n", string(doc[19].Raw()))
assert.Equal(t, "", doc[19].(gemtext.PreformatToggleLine).AltText())
// ensure we can rebuild the original doc from all the line.Raw()s
buf := &bytes.Buffer{}

View File

@ -16,6 +16,13 @@ const (
// The line is a LinkLine.
LineTypeLink
// LineTypePrompt is a spartan =: prompt line.
//
// =:[<ws>]<url>[<ws><label>][\r]\n
//
// The line is a PromptLine.
LineTypePrompt
// LineTypePreformatToggle switches the document between pre-formatted text or not.
//
// ```[<alt-text>][\r]\n
@ -111,6 +118,25 @@ func (ll LinkLine) URL() string { return string(ll.url) }
// Label returns the label portion of the line.
func (ll LinkLine) Label() string { return string(ll.label) }
// PromptLine is a Spartan =: prompt line.
type PromptLine struct {
raw []byte
url []byte
label []byte
}
func (pl PromptLine) Type() LineType { return LineTypePrompt }
func (pl PromptLine) Raw() []byte { return pl.raw }
func (pl PromptLine) String() string { return string(pl.raw) }
// URL returns the original url portion of the line.
//
// It is not guaranteed to be a valid URL.
func (pl PromptLine) URL() string { return string(pl.url) }
// Label retrns the label portion of the line.
func (pl PromptLine) Label() string { return string(pl.label) }
// PreformatToggleLine is a preformatted text toggle line.
type PreformatToggleLine struct {
raw []byte

70
spartan/client.go Normal file
View File

@ -0,0 +1,70 @@
package spartan
import (
"bytes"
"errors"
"io"
"net"
"strconv"
"tildegit.org/tjp/gus"
)
// Client is used for sending spartan requests and receiving responses.
//
// It carries no state and is reusable simultaneously by multiple goroutines.
//
// The zero value is immediately usabble.
type Client struct{}
// RoundTrip sends a single spartan request and returns its response.
func (c Client) RoundTrip(request *gus.Request, body io.Reader) (*gus.Response, error) {
if request.Scheme != "spartan" && request.Scheme != "" {
return nil, errors.New("non-spartan protocols not supported")
}
host, port, _ := net.SplitHostPort(request.Host)
if port == "" {
host = request.Host
port = "300"
}
addr := net.JoinHostPort(host, port)
conn, err := net.Dial("tcp", addr)
if err != nil {
return nil, err
}
defer conn.Close()
request.RemoteAddr = conn.RemoteAddr()
var bodyBytes []byte = nil
if body != nil {
bodyBytes, err = io.ReadAll(body)
if err != nil {
return nil, err
}
}
requestLine := host + " " + request.EscapedPath() + " " + strconv.Itoa(len(bodyBytes)) + "\r\n"
if _, err := conn.Write([]byte(requestLine)); err != nil {
return nil, err
}
if _, err := conn.Write(bodyBytes); err != nil {
return nil, err
}
response, err := ParseResponse(conn)
if err != nil {
return nil, err
}
bodybuf, err := io.ReadAll(response.Body)
if err != nil {
return nil, err
}
response.Body = bytes.NewBuffer(bodybuf)
return response, nil
}

62
spartan/request.go Normal file
View File

@ -0,0 +1,62 @@
package spartan
import (
"bufio"
"errors"
"io"
"net/url"
"strconv"
"strings"
"tildegit.org/tjp/gus"
)
var (
// InvalidRequestLine indicates a malformed first-line of a spartan request.
InvalidRequestLine = errors.New("invalid request line")
// InvalidRequestLineEnding says that a spartan request's first line wasn't terminated with CRLF.
InvalidRequestLineEnding = errors.New("invalid request line ending")
)
// ParseRequest parses a single spartan request and the indicated content-length from a reader.
//
// If ther reader artument is a *bufio.Reader, it will only read a single line from it.
func ParseRequest(rdr io.Reader) (*gus.Request, int, error) {
bufrdr, ok := rdr.(*bufio.Reader)
if !ok {
bufrdr = bufio.NewReader(rdr)
}
line, err := bufrdr.ReadString('\n')
if err != io.EOF && err != nil {
return nil, 0, err
}
host, rest, ok := strings.Cut(line, " ")
if !ok {
return nil, 0, InvalidRequestLine
}
path, rest, ok := strings.Cut(rest, " ")
if !ok {
return nil, 0, InvalidRequestLine
}
if len(rest) < 2 || line[len(line)-2:] != "\r\n" {
return nil, 0, InvalidRequestLineEnding
}
contentlen, err := strconv.Atoi(rest[:len(rest)-2])
if err != nil {
return nil, 0, err
}
return &gus.Request{
URL: &url.URL{
Scheme: "spartan",
Host: host,
Path: path,
RawPath: path,
},
}, contentlen, nil
}

45
spartan/request_test.go Normal file
View File

@ -0,0 +1,45 @@
package spartan_test
import (
"bytes"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"tildegit.org/tjp/gus/spartan"
)
func TestParseRequest(t *testing.T) {
tests := []struct {
requestLine string
host string
path string
clen int
}{
{
requestLine: "foobar.ninja /baz/quux 0\r\n",
host: "foobar.ninja",
path: "/baz/quux",
clen: 0,
},
{
requestLine: "foo.bar / 12\r\n",
host: "foo.bar",
path: "/",
clen: 12,
},
}
for _, test := range tests {
t.Run(test.requestLine, func(t *testing.T) {
request, clen, err := spartan.ParseRequest(bytes.NewBufferString(test.requestLine))
require.Nil(t, err)
assert.Equal(t, test.host, request.Host)
assert.Equal(t, test.path, request.Path)
assert.Equal(t, test.path, request.RawPath)
assert.Equal(t, test.clen, clen)
})
}
}

128
spartan/response.go Normal file
View File

@ -0,0 +1,128 @@
package spartan
import (
"bufio"
"bytes"
"errors"
"io"
"strconv"
"sync"
"tildegit.org/tjp/gus"
)
// The spartan response types.
const (
StatusSuccess gus.Status = 2
StatusRedirect gus.Status = 3
StatusClientError gus.Status = 4
StatusServerError gus.Status = 5
)
// Success builds a successful spartan response.
func Success(mediatype string, body io.Reader) *gus.Response {
return &gus.Response{
Status: StatusSuccess,
Meta: mediatype,
Body: body,
}
}
// Redirect builds a spartan redirect response.
func Redirect(url string) *gus.Response {
return &gus.Response{
Status: StatusRedirect,
Meta: url,
}
}
// ClientError builds a "client error" spartan response.
func ClientError(err error) *gus.Response {
return &gus.Response{
Status: StatusClientError,
Meta: err.Error(),
}
}
// ServerError builds a "server error" spartan response.
func ServerError(err error) *gus.Response {
return &gus.Response{
Status: StatusServerError,
Meta: err.Error(),
}
}
// InvalidResponseHeaderLine indicates a malformed spartan response line.
var InvalidResponseHeaderLine = errors.New("Invalid response header line.")
// InvalidResponseLineEnding indicates that a spartan response header didn't end with "\r\n".
var InvalidResponseLineEnding = errors.New("Invalid response line ending.")
func ParseResponse(rdr io.Reader) (*gus.Response, error) {
bufrdr := bufio.NewReader(rdr)
hdrLine, err := bufrdr.ReadString('\n')
if err != nil {
return nil, InvalidResponseLineEnding
}
if len(hdrLine) < 2 {
return nil, InvalidResponseHeaderLine
}
status, err := strconv.Atoi(string(hdrLine[0]))
if err != nil || hdrLine[1] != ' ' || hdrLine[len(hdrLine)-2:] != "\r\n" {
return nil, InvalidResponseHeaderLine
}
return &gus.Response{
Status: gus.Status(status),
Meta: hdrLine[2 : len(hdrLine)-2],
Body: bufrdr,
}, nil
}
// NewResponseReader builds a reader for a response.
func NewResponseReader(response *gus.Response) gus.ResponseReader {
return &responseReader{
Response: response,
once: &sync.Once{},
}
}
type responseReader struct {
*gus.Response
reader io.Reader
once *sync.Once
}
func (rdr *responseReader) Read(b []byte) (int, error) {
rdr.ensureReader()
return rdr.reader.Read(b)
}
func (rdr *responseReader) WriteTo(dst io.Writer) (int64, error) {
rdr.ensureReader()
return rdr.reader.(io.WriterTo).WriteTo(dst)
}
func (rdr *responseReader) ensureReader() {
rdr.once.Do(func() {
hdr := bytes.NewBuffer(rdr.headerLine())
if rdr.Body != nil {
rdr.reader = io.MultiReader(hdr, rdr.Body)
} else {
rdr.reader = hdr
}
})
}
func (rdr *responseReader) headerLine() []byte {
meta := rdr.Meta.(string)
buf := make([]byte, len(meta)+4)
buf[0] = byte(rdr.Status) + '0'
buf[1] = ' '
copy(buf[2:], meta)
buf[len(buf)-2] = '\r'
buf[len(buf)-1] = '\n'
return buf
}

95
spartan/serve.go Normal file
View File

@ -0,0 +1,95 @@
package spartan
import (
"bufio"
"context"
"errors"
"fmt"
"io"
"net"
"strings"
"tildegit.org/tjp/gus"
"tildegit.org/tjp/gus/internal"
"tildegit.org/tjp/gus/logging"
)
type spartanRequestBodyKey struct{}
type spartanRequestBodyLenKey struct{}
// SpartanRequestBody is the key set in a handler's context for spartan request bodies.
//
// The corresponding value is a *bufio.Reader from which the request body can be read.
var SpartanRequestBody = spartanRequestBodyKey{}
// SpartanRequestBodyLen is the key set in a handler's context for the content-length of the request.
//
// The corresponding value is an int.
var SpartanRequestBodyLen = spartanRequestBodyLenKey{}
type spartanServer struct {
internal.Server
handler gus.Handler
}
func (ss spartanServer) Protocol() string { return "SPARTAN" }
// NewServer builds a spartan server.
func NewServer(
ctx context.Context,
hostname string,
network string,
address string,
handler gus.Handler,
errLog logging.Logger,
) (gus.Server, error) {
ss := &spartanServer{handler: handler}
if strings.IndexByte(hostname, ':') < 0 {
hostname = net.JoinHostPort(hostname, "300")
}
var err error
ss.Server, err = internal.NewServer(ctx, hostname, network, address, errLog, ss.handleConn)
if err != nil {
return nil, err
}
return ss, nil
}
func (ss *spartanServer) handleConn(conn net.Conn) {
buf := bufio.NewReader(conn)
var response *gus.Response
request, clen, err := ParseRequest(buf)
if err != nil {
response = ClientError(err)
} else {
request.Server = ss
request.RemoteAddr = conn.RemoteAddr()
var body *bufio.Reader = nil
if clen > 0 {
body = bufio.NewReader(io.LimitReader(buf, int64(clen)))
}
ctx := context.WithValue(ss.Ctx, SpartanRequestBody, body)
ctx = context.WithValue(ctx, SpartanRequestBodyLen, clen)
defer func() {
if r := recover(); r != nil {
err := fmt.Errorf("%s", r)
_ = ss.LogError("msg", "panic in handler", "err", err)
rdr := NewResponseReader(ServerError(errors.New("Server error")))
_, _ = io.Copy(conn, rdr)
}
}()
response = ss.handler.Handle(ctx, request)
if response == nil {
response = ClientError(errors.New("Resource does not exist."))
}
}
defer response.Close()
_, _ = io.Copy(conn, NewResponseReader(response))
}