Compare commits
3 Commits
039c58c9d0
...
3ff04cf885
Author | SHA1 | Date |
---|---|---|
tjpcc | 3ff04cf885 | |
tjpcc | 9e09825537 | |
tjpcc | fcea3099cb |
|
@ -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}
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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{}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
}
|
|
@ -37,7 +37,7 @@ func ParseRequest(rdr io.Reader) (*gus.Request, int, error) {
|
|||
if !ok {
|
||||
return nil, 0, InvalidRequestLine
|
||||
}
|
||||
path, rest, ok := strings.Cut(line, " ")
|
||||
path, rest, ok := strings.Cut(rest, " ")
|
||||
if !ok {
|
||||
return nil, 0, InvalidRequestLine
|
||||
}
|
||||
|
@ -46,7 +46,7 @@ func ParseRequest(rdr io.Reader) (*gus.Request, int, error) {
|
|||
return nil, 0, InvalidRequestLineEnding
|
||||
}
|
||||
|
||||
contentlen, err := strconv.Atoi(line[:len(line)-2])
|
||||
contentlen, err := strconv.Atoi(rest[:len(rest)-2])
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
})
|
||||
}
|
||||
}
|
|
@ -1,8 +1,11 @@
|
|||
package spartan
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"errors"
|
||||
"io"
|
||||
"strconv"
|
||||
"sync"
|
||||
|
||||
"tildegit.org/tjp/gus"
|
||||
|
@ -37,7 +40,7 @@ func Redirect(url string) *gus.Response {
|
|||
func ClientError(err error) *gus.Response {
|
||||
return &gus.Response{
|
||||
Status: StatusClientError,
|
||||
Meta: err.Error(),
|
||||
Meta: err.Error(),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -45,10 +48,39 @@ func ClientError(err error) *gus.Response {
|
|||
func ServerError(err error) *gus.Response {
|
||||
return &gus.Response{
|
||||
Status: StatusServerError,
|
||||
Meta: err.Error(),
|
||||
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{
|
||||
|
|
Reference in New Issue