diff options
author | tjpcc <tjp@ctrl-c.club> | 2023-08-12 09:40:39 -0600 |
---|---|---|
committer | tjpcc <tjp@ctrl-c.club> | 2023-08-12 09:40:39 -0600 |
commit | 23bc5f4fb7542e64c94eaa7fe2c7a6aa55010898 (patch) | |
tree | ec8113d3aa2379e3ca9cb3c6e13a5531895ea8c0 /gemini | |
parent | 57a31a9b2cd549174d839b9b91b47db337f174cc (diff) |
move common types to an internal package
This helps avoid import cycles.
Diffstat (limited to 'gemini')
-rw-r--r-- | gemini/client.go | 8 | ||||
-rw-r--r-- | gemini/request.go | 8 | ||||
-rw-r--r-- | gemini/response.go | 100 | ||||
-rw-r--r-- | gemini/response_test.go | 18 | ||||
-rw-r--r-- | gemini/roundtrip_test.go | 8 | ||||
-rw-r--r-- | gemini/serve.go | 16 |
6 files changed, 79 insertions, 79 deletions
diff --git a/gemini/client.go b/gemini/client.go index e81e64a..34d5839 100644 --- a/gemini/client.go +++ b/gemini/client.go @@ -8,7 +8,7 @@ import ( "net" neturl "net/url" - sr "tildegit.org/tjp/sliderule" + "tildegit.org/tjp/sliderule/internal/types" ) // Client is used for sending gemini requests and parsing gemini responses. @@ -43,7 +43,7 @@ var ExceededMaxRedirects = errors.New("gemini.Client: exceeded MaxRedirects") // // This method will not automatically follow redirects or cache permanent failures or // redirects. -func (client Client) RoundTrip(request *sr.Request) (*sr.Response, error) { +func (client Client) RoundTrip(request *types.Request) (*types.Response, error) { if request.Scheme != "gemini" && request.Scheme != "" { return nil, errors.New("non-gemini protocols not supported") } @@ -91,14 +91,14 @@ func (client Client) RoundTrip(request *sr.Request) (*sr.Response, error) { // Fetch parses a URL string and fetches the gemini resource. // // It will resolve any redirects along the way, up to client.MaxRedirects. -func (c Client) Fetch(url string) (*sr.Response, error) { +func (c Client) Fetch(url string) (*types.Response, error) { u, err := neturl.Parse(url) if err != nil { return nil, err } for i := 0; i <= c.MaxRedirects; i += 1 { - response, err := c.RoundTrip(&sr.Request{URL: u}) + response, err := c.RoundTrip(&types.Request{URL: u}) if err != nil { return nil, err } diff --git a/gemini/request.go b/gemini/request.go index 51aaae5..4eb7cf0 100644 --- a/gemini/request.go +++ b/gemini/request.go @@ -6,7 +6,7 @@ import ( "io" "net/url" - sr "tildegit.org/tjp/sliderule" + "tildegit.org/tjp/sliderule/internal/types" ) // InvalidRequestLineEnding indicates that a gemini request didn't end with "\r\n". @@ -15,7 +15,7 @@ var InvalidRequestLineEnding = errors.New("invalid request line ending") // ParseRequest parses a single gemini request from a reader. // // If the reader argument is a *bufio.Reader, it will only read a single line from it. -func ParseRequest(rdr io.Reader) (*sr.Request, error) { +func ParseRequest(rdr io.Reader) (*types.Request, error) { bufrdr, ok := rdr.(*bufio.Reader) if !ok { bufrdr = bufio.NewReader(rdr) @@ -39,14 +39,14 @@ func ParseRequest(rdr io.Reader) (*sr.Request, error) { u.Scheme = "gemini" } - return &sr.Request{URL: u}, nil + return &types.Request{URL: u}, nil } // GetTitanRequestBody fetches the request body from a titan request. // // It returns nil if the argument is not a titan request or it otherwise // does not have a request body set. -func GetTitanRequestBody(request *sr.Request) io.Reader { +func GetTitanRequestBody(request *types.Request) io.Reader { if request.Scheme != "titan" { return nil } diff --git a/gemini/response.go b/gemini/response.go index 9d4ede1..13f493a 100644 --- a/gemini/response.go +++ b/gemini/response.go @@ -8,7 +8,7 @@ import ( "strconv" "sync" - sr "tildegit.org/tjp/sliderule" + "tildegit.org/tjp/sliderule/internal/types" ) // ResponseCategory represents the various types of gemini responses. @@ -43,32 +43,32 @@ const ( ResponseCategoryCertificateRequired ) -func ResponseCategoryForStatus(status sr.Status) ResponseCategory { +func ResponseCategoryForStatus(status types.Status) ResponseCategory { return ResponseCategory(status / 10) } const ( // StatusInput indicates a required query parameter at the requested URL. - StatusInput sr.Status = sr.Status(ResponseCategoryInput) + iota + StatusInput types.Status = types.Status(ResponseCategoryInput) + iota // StatusSensitiveInput indicates a sensitive query parameter is required. StatusSensitiveInput ) const ( // StatusSuccess is a successful response. - StatusSuccess = sr.Status(ResponseCategorySuccess) + iota + StatusSuccess = types.Status(ResponseCategorySuccess) + iota ) const ( // StatusTemporaryRedirect indicates a temporary redirect to another URL. - StatusTemporaryRedirect = sr.Status(ResponseCategoryRedirect) + iota + StatusTemporaryRedirect = types.Status(ResponseCategoryRedirect) + iota // StatusPermanentRedirect indicates that the resource should always be requested at the new URL. StatusPermanentRedirect ) const ( // StatusTemporaryFailure indicates that the request failed and there is no response body. - StatusTemporaryFailure = sr.Status(ResponseCategoryTemporaryFailure) + iota + StatusTemporaryFailure = types.Status(ResponseCategoryTemporaryFailure) + iota // StatusServerUnavailable occurs when the server is unavailable due to overload or maintenance. StatusServerUnavailable // StatusCGIError is the result of a failure of a CGI script. @@ -84,7 +84,7 @@ const ( const ( // StatusPermanentFailure is a server failure which should be expected to continue indefinitely. - StatusPermanentFailure = sr.Status(ResponseCategoryPermanentFailure) + iota + StatusPermanentFailure = types.Status(ResponseCategoryPermanentFailure) + iota // StatusNotFound means the resource doesn't exist but it may in the future. StatusNotFound // StatusGone occurs when a resource will not be available any longer. @@ -92,12 +92,12 @@ const ( // StatusProxyRequestRefused means the server is unwilling to act as a proxy for the resource. StatusProxyRequestRefused // StatusBadRequest indicates that the request was malformed somehow. - StatusBadRequest = sr.Status(ResponseCategoryPermanentFailure) + 9 + StatusBadRequest = types.Status(ResponseCategoryPermanentFailure) + 9 ) const ( // StatusClientCertificateRequired is returned when a certificate was required but not provided. - StatusClientCertificateRequired = sr.Status(ResponseCategoryCertificateRequired) + iota + StatusClientCertificateRequired = types.Status(ResponseCategoryCertificateRequired) + iota // StatusCertificateNotAuthorized means the certificate doesn't grant access to the requested resource. StatusCertificateNotAuthorized // StatusCertificateNotValid means the provided client certificate is invalid. @@ -105,24 +105,24 @@ const ( ) // Input builds an input-prompting response. -func Input(prompt string) *sr.Response { - return &sr.Response{ +func Input(prompt string) *types.Response { + return &types.Response{ Status: StatusInput, Meta: prompt, } } // SensitiveInput builds a password-prompting response. -func SensitiveInput(prompt string) *sr.Response { - return &sr.Response{ +func SensitiveInput(prompt string) *types.Response { + return &types.Response{ Status: StatusSensitiveInput, Meta: prompt, } } // Success builds a success response with resource body. -func Success(mediatype string, body io.Reader) *sr.Response { - return &sr.Response{ +func Success(mediatype string, body io.Reader) *types.Response { + return &types.Response{ Status: StatusSuccess, Meta: mediatype, Body: body, @@ -130,120 +130,120 @@ func Success(mediatype string, body io.Reader) *sr.Response { } // Redirect builds a redirect response. -func Redirect(url string) *sr.Response { - return &sr.Response{ +func Redirect(url string) *types.Response { + return &types.Response{ Status: StatusTemporaryRedirect, Meta: url, } } // PermanentRedirect builds a response with a permanent redirect. -func PermanentRedirect(url string) *sr.Response { - return &sr.Response{ +func PermanentRedirect(url string) *types.Response { + return &types.Response{ Status: StatusPermanentRedirect, Meta: url, } } // Failure builds a temporary failure response from an error. -func Failure(err error) *sr.Response { - return &sr.Response{ +func Failure(err error) *types.Response { + return &types.Response{ Status: StatusTemporaryFailure, Meta: err.Error(), } } // Unavailable build a "server unavailable" response. -func Unavailable(msg string) *sr.Response { - return &sr.Response{ +func Unavailable(msg string) *types.Response { + return &types.Response{ Status: StatusServerUnavailable, Meta: msg, } } // CGIError builds a "cgi error" response. -func CGIError(err string) *sr.Response { - return &sr.Response{ +func CGIError(err string) *types.Response { + return &types.Response{ Status: StatusCGIError, Meta: err, } } // ProxyError builds a proxy error response. -func ProxyError(msg string) *sr.Response { - return &sr.Response{ +func ProxyError(msg string) *types.Response { + return &types.Response{ Status: StatusProxyError, Meta: msg, } } // SlowDown builds a "slow down" response with the number of seconds until the resource is available. -func SlowDown(seconds int) *sr.Response { - return &sr.Response{ +func SlowDown(seconds int) *types.Response { + return &types.Response{ Status: StatusSlowDown, Meta: strconv.Itoa(seconds), } } // PermanentFailure builds a "permanent failure" from an error. -func PermanentFailure(err error) *sr.Response { - return &sr.Response{ +func PermanentFailure(err error) *types.Response { + return &types.Response{ Status: StatusPermanentFailure, Meta: err.Error(), } } // NotFound builds a "resource not found" response. -func NotFound(msg string) *sr.Response { - return &sr.Response{ +func NotFound(msg string) *types.Response { + return &types.Response{ Status: StatusNotFound, Meta: msg, } } // Gone builds a "resource gone" response. -func Gone(msg string) *sr.Response { - return &sr.Response{ +func Gone(msg string) *types.Response { + return &types.Response{ Status: StatusGone, Meta: msg, } } // RefuseProxy builds a "proxy request refused" response. -func RefuseProxy(msg string) *sr.Response { - return &sr.Response{ +func RefuseProxy(msg string) *types.Response { + return &types.Response{ Status: StatusProxyRequestRefused, Meta: msg, } } // BadRequest builds a "bad request" response. -func BadRequest(msg string) *sr.Response { - return &sr.Response{ +func BadRequest(msg string) *types.Response { + return &types.Response{ Status: StatusBadRequest, Meta: msg, } } // RequireCert builds a "client certificate required" response. -func RequireCert(msg string) *sr.Response { - return &sr.Response{ +func RequireCert(msg string) *types.Response { + return &types.Response{ Status: StatusClientCertificateRequired, Meta: msg, } } // CertAuthFailure builds a "certificate not authorized" response. -func CertAuthFailure(msg string) *sr.Response { - return &sr.Response{ +func CertAuthFailure(msg string) *types.Response { + return &types.Response{ Status: StatusCertificateNotAuthorized, Meta: msg, } } // CertInvalid builds a "client certificate not valid" response. -func CertInvalid(msg string) *sr.Response { - return &sr.Response{ +func CertInvalid(msg string) *types.Response { + return &types.Response{ Status: StatusCertificateNotValid, Meta: msg, } @@ -258,7 +258,7 @@ var InvalidResponseHeaderLine = errors.New("Invalid response header line.") // ParseResponse parses a complete gemini response from a reader. // // The reader must contain only one gemini response. -func ParseResponse(rdr io.Reader) (*sr.Response, error) { +func ParseResponse(rdr io.Reader) (*types.Response, error) { bufrdr := bufio.NewReader(rdr) hdrLine, err := bufrdr.ReadBytes('\n') @@ -278,14 +278,14 @@ func ParseResponse(rdr io.Reader) (*sr.Response, error) { return nil, InvalidResponseHeaderLine } - return &sr.Response{ - Status: sr.Status(status), + return &types.Response{ + Status: types.Status(status), Meta: string(hdrLine[3:]), Body: bufrdr, }, nil } -func NewResponseReader(response *sr.Response) sr.ResponseReader { +func NewResponseReader(response *types.Response) types.ResponseReader { return &responseReader{ Response: response, once: &sync.Once{}, @@ -293,7 +293,7 @@ func NewResponseReader(response *sr.Response) sr.ResponseReader { } type responseReader struct { - *sr.Response + *types.Response reader io.Reader once *sync.Once } diff --git a/gemini/response_test.go b/gemini/response_test.go index 00166dd..784e00a 100644 --- a/gemini/response_test.go +++ b/gemini/response_test.go @@ -6,15 +6,15 @@ import ( "io" "testing" - sr "tildegit.org/tjp/sliderule" + "tildegit.org/tjp/sliderule/internal/types" "tildegit.org/tjp/sliderule/gemini" ) func TestBuildResponses(t *testing.T) { table := []struct { name string - response *sr.Response - status sr.Status + response *types.Response + status types.Status meta string body string }{ @@ -154,7 +154,7 @@ func TestBuildResponses(t *testing.T) { func TestParseResponses(t *testing.T) { table := []struct { input string - status sr.Status + status types.Status meta string body string err error @@ -233,7 +233,7 @@ func TestParseResponses(t *testing.T) { func TestResponseClose(t *testing.T) { body := &rdCloser{Buffer: bytes.NewBufferString("the body here")} - resp := &sr.Response{ + resp := &types.Response{ Status: gemini.StatusSuccess, Meta: "text/gemini", Body: body, @@ -247,7 +247,7 @@ func TestResponseClose(t *testing.T) { t.Error("response body was not closed by response.Close()") } - resp = &sr.Response{ + resp = &types.Response{ Status: gemini.StatusInput, Meta: "give me more", } @@ -270,8 +270,8 @@ func (rc *rdCloser) Close() error { func TestResponseWriteTo(t *testing.T) { // invariant under test: WriteTo() sends the same bytes as Read() - clone := func(resp *sr.Response) *sr.Response { - other := &sr.Response{ + clone := func(resp *types.Response) *types.Response { + other := &types.Response{ Status: resp.Status, Meta: resp.Meta, } @@ -297,7 +297,7 @@ func TestResponseWriteTo(t *testing.T) { table := []struct { name string - response *sr.Response + response *types.Response }{ { name: "simple success", diff --git a/gemini/roundtrip_test.go b/gemini/roundtrip_test.go index 0aa5648..50c1962 100644 --- a/gemini/roundtrip_test.go +++ b/gemini/roundtrip_test.go @@ -12,7 +12,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - sr "tildegit.org/tjp/sliderule" + "tildegit.org/tjp/sliderule/internal/types" "tildegit.org/tjp/sliderule/gemini" ) @@ -20,7 +20,7 @@ func TestRoundTrip(t *testing.T) { tlsConf, err := gemini.FileTLS("./testdata/server.crt", "./testdata/server.key") require.Nil(t, err) - handler := sr.HandlerFunc(func(ctx context.Context, req *sr.Request) *sr.Response { + handler := types.HandlerFunc(func(ctx context.Context, req *types.Request) *types.Response { return gemini.Success("text/gemini", bytes.NewBufferString("you've found my page")) }) @@ -36,7 +36,7 @@ func TestRoundTrip(t *testing.T) { require.Nil(t, err) cli := gemini.NewClient(testClientTLS()) - response, err := cli.RoundTrip(&sr.Request{URL: u}) + response, err := cli.RoundTrip(&types.Request{URL: u}) require.Nil(t, err) assert.Equal(t, gemini.StatusSuccess, response.Status) @@ -54,7 +54,7 @@ func TestTitanRequest(t *testing.T) { require.Nil(t, err) invoked := false - handler := sr.HandlerFunc(func(ctx context.Context, request *sr.Request) *sr.Response { + handler := types.HandlerFunc(func(ctx context.Context, request *types.Request) *types.Response { invoked = true body := gemini.GetTitanRequestBody(request) diff --git a/gemini/serve.go b/gemini/serve.go index 9173212..6fee458 100644 --- a/gemini/serve.go +++ b/gemini/serve.go @@ -11,7 +11,7 @@ import ( "strconv" "strings" - sr "tildegit.org/tjp/sliderule" + "tildegit.org/tjp/sliderule/internal/types" "tildegit.org/tjp/sliderule/internal" "tildegit.org/tjp/sliderule/logging" ) @@ -19,7 +19,7 @@ import ( type server struct { internal.Server - handler sr.Handler + handler types.Handler } func (s server) Protocol() string { return "GEMINI" } @@ -30,10 +30,10 @@ func NewServer( hostname string, network string, address string, - handler sr.Handler, + handler types.Handler, errorLog logging.Logger, tlsConfig *tls.Config, -) (sr.Server, error) { +) (types.Server, error) { s := &server{handler: handler} hostname = internal.JoinDefaultPort(hostname, "1965") @@ -53,7 +53,7 @@ func NewServer( func (s *server) handleConn(conn net.Conn) { buf := bufio.NewReader(conn) - var response *sr.Response + var response *types.Response request, err := ParseRequest(buf) if err != nil { response = BadRequest(err.Error()) @@ -112,9 +112,9 @@ func sizeParam(path string) (int, error) { // Optionally, it will also allow through titan:// requests. // // Filtered requests will be turned away with a 53 response "proxy request refused". -func GeminiOnly(allowTitan bool) sr.Middleware { - return func(inner sr.Handler) sr.Handler { - return sr.HandlerFunc(func(ctx context.Context, request *sr.Request) *sr.Response { +func GeminiOnly(allowTitan bool) types.Middleware { + return func(inner types.Handler) types.Handler { + return types.HandlerFunc(func(ctx context.Context, request *types.Request) *types.Response { if request.Scheme == "gemini" || (allowTitan && request.Scheme == "titan") { return inner.Handle(ctx, request) } |