From 9a2da81b11ad0064cca24ce7974827d032309369 Mon Sep 17 00:00:00 2001 From: tjpcc Date: Mon, 1 May 2023 07:56:25 -0600 Subject: name change gus -> sliderule --- spartan/client.go | 4 ++-- spartan/request.go | 26 +++++++++++++++++++++++--- spartan/request_test.go | 2 +- spartan/response.go | 36 ++++++++++++++++++------------------ spartan/serve.go | 36 +++++++++++------------------------- 5 files changed, 55 insertions(+), 49 deletions(-) (limited to 'spartan') diff --git a/spartan/client.go b/spartan/client.go index 154b18a..e571c14 100644 --- a/spartan/client.go +++ b/spartan/client.go @@ -7,7 +7,7 @@ import ( "net" "strconv" - "tildegit.org/tjp/gus" + sr "tildegit.org/tjp/sliderule" ) // Client is used for sending spartan requests and receiving responses. @@ -18,7 +18,7 @@ import ( 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) { +func (c Client) RoundTrip(request *sr.Request, body io.Reader) (*sr.Response, error) { if request.Scheme != "spartan" && request.Scheme != "" { return nil, errors.New("non-spartan protocols not supported") } diff --git a/spartan/request.go b/spartan/request.go index ca1159b..a9b2815 100644 --- a/spartan/request.go +++ b/spartan/request.go @@ -8,7 +8,7 @@ import ( "strconv" "strings" - "tildegit.org/tjp/gus" + sr "tildegit.org/tjp/sliderule" ) var ( @@ -22,7 +22,7 @@ var ( // 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) { +func ParseRequest(rdr io.Reader) (*sr.Request, int, error) { bufrdr, ok := rdr.(*bufio.Reader) if !ok { bufrdr = bufio.NewReader(rdr) @@ -51,7 +51,7 @@ func ParseRequest(rdr io.Reader) (*gus.Request, int, error) { return nil, 0, err } - return &gus.Request{ + return &sr.Request{ URL: &url.URL{ Scheme: "spartan", Host: host, @@ -60,3 +60,23 @@ func ParseRequest(rdr io.Reader) (*gus.Request, int, error) { }, }, contentlen, nil } + +// GetRequestContentLength reads the remaining un-read number of bytes in a request body. +// +// It will immediately return 0 if there is no request body. +func GetRequestContentLength(request *sr.Request) int { + if lr, ok := request.Meta.(*io.LimitedReader); ok { + return int(lr.N) + } + return 0 +} + +// GetRequestBody returns a reader of the spartan request body. +// +// It will return nil if the request has no body. +func GetRequestBody(request *sr.Request) io.Reader { + if rdr, ok := request.Meta.(io.Reader); ok { + return rdr + } + return nil +} diff --git a/spartan/request_test.go b/spartan/request_test.go index bffecef..89326f1 100644 --- a/spartan/request_test.go +++ b/spartan/request_test.go @@ -7,7 +7,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "tildegit.org/tjp/gus/spartan" + "tildegit.org/tjp/sliderule/spartan" ) func TestParseRequest(t *testing.T) { diff --git a/spartan/response.go b/spartan/response.go index bd906e0..aeddd68 100644 --- a/spartan/response.go +++ b/spartan/response.go @@ -8,20 +8,20 @@ import ( "strconv" "sync" - "tildegit.org/tjp/gus" + sr "tildegit.org/tjp/sliderule" ) // The spartan response types. const ( - StatusSuccess gus.Status = 2 - StatusRedirect gus.Status = 3 - StatusClientError gus.Status = 4 - StatusServerError gus.Status = 5 + StatusSuccess sr.Status = 2 + StatusRedirect sr.Status = 3 + StatusClientError sr.Status = 4 + StatusServerError sr.Status = 5 ) // Success builds a successful spartan response. -func Success(mediatype string, body io.Reader) *gus.Response { - return &gus.Response{ +func Success(mediatype string, body io.Reader) *sr.Response { + return &sr.Response{ Status: StatusSuccess, Meta: mediatype, Body: body, @@ -29,24 +29,24 @@ func Success(mediatype string, body io.Reader) *gus.Response { } // Redirect builds a spartan redirect response. -func Redirect(url string) *gus.Response { - return &gus.Response{ +func Redirect(url string) *sr.Response { + return &sr.Response{ Status: StatusRedirect, Meta: url, } } // ClientError builds a "client error" spartan response. -func ClientError(err error) *gus.Response { - return &gus.Response{ +func ClientError(err error) *sr.Response { + return &sr.Response{ Status: StatusClientError, Meta: err.Error(), } } // ServerError builds a "server error" spartan response. -func ServerError(err error) *gus.Response { - return &gus.Response{ +func ServerError(err error) *sr.Response { + return &sr.Response{ Status: StatusServerError, Meta: err.Error(), } @@ -58,7 +58,7 @@ 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) { +func ParseResponse(rdr io.Reader) (*sr.Response, error) { bufrdr := bufio.NewReader(rdr) hdrLine, err := bufrdr.ReadString('\n') @@ -74,15 +74,15 @@ func ParseResponse(rdr io.Reader) (*gus.Response, error) { return nil, InvalidResponseHeaderLine } - return &gus.Response{ - Status: gus.Status(status), + return &sr.Response{ + Status: sr.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 { +func NewResponseReader(response *sr.Response) sr.ResponseReader { return &responseReader{ Response: response, once: &sync.Once{}, @@ -90,7 +90,7 @@ func NewResponseReader(response *gus.Response) gus.ResponseReader { } type responseReader struct { - *gus.Response + *sr.Response reader io.Reader once *sync.Once } diff --git a/spartan/serve.go b/spartan/serve.go index 677d76c..61199b1 100644 --- a/spartan/serve.go +++ b/spartan/serve.go @@ -9,27 +9,14 @@ import ( "net" "strings" - "tildegit.org/tjp/gus" - "tildegit.org/tjp/gus/internal" - "tildegit.org/tjp/gus/logging" + sr "tildegit.org/tjp/sliderule" + "tildegit.org/tjp/sliderule/internal" + "tildegit.org/tjp/sliderule/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 + handler sr.Handler } func (ss spartanServer) Protocol() string { return "SPARTAN" } @@ -40,9 +27,9 @@ func NewServer( hostname string, network string, address string, - handler gus.Handler, + handler sr.Handler, errLog logging.Logger, -) (gus.Server, error) { +) (sr.Server, error) { ss := &spartanServer{handler: handler} if strings.IndexByte(hostname, ':') < 0 { @@ -61,7 +48,7 @@ func NewServer( func (ss *spartanServer) handleConn(conn net.Conn) { buf := bufio.NewReader(conn) - var response *gus.Response + var response *sr.Response request, clen, err := ParseRequest(buf) if err != nil { response = ClientError(err) @@ -69,12 +56,11 @@ func (ss *spartanServer) handleConn(conn net.Conn) { request.Server = ss request.RemoteAddr = conn.RemoteAddr() - var body *bufio.Reader = nil + var body io.Reader = nil if clen > 0 { - body = bufio.NewReader(io.LimitReader(buf, int64(clen))) + body = io.LimitReader(buf, int64(clen)) } - ctx := context.WithValue(ss.Ctx, SpartanRequestBody, body) - ctx = context.WithValue(ctx, SpartanRequestBodyLen, clen) + request.Meta = body defer func() { if r := recover(); r != nil { @@ -84,7 +70,7 @@ func (ss *spartanServer) handleConn(conn net.Conn) { _, _ = io.Copy(conn, rdr) } }() - response = ss.handler.Handle(ctx, request) + response = ss.handler.Handle(ss.Ctx, request) if response == nil { response = ClientError(errors.New("Resource does not exist.")) } -- cgit v1.2.3