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 /spartan | |
parent | 57a31a9b2cd549174d839b9b91b47db337f174cc (diff) |
move common types to an internal package
This helps avoid import cycles.
Diffstat (limited to 'spartan')
-rw-r--r-- | spartan/client.go | 8 | ||||
-rw-r--r-- | spartan/request.go | 12 | ||||
-rw-r--r-- | spartan/response.go | 36 | ||||
-rw-r--r-- | spartan/serve.go | 10 |
4 files changed, 33 insertions, 33 deletions
diff --git a/spartan/client.go b/spartan/client.go index 9547f74..e3025ee 100644 --- a/spartan/client.go +++ b/spartan/client.go @@ -8,7 +8,7 @@ import ( neturl "net/url" "strconv" - sr "tildegit.org/tjp/sliderule" + "tildegit.org/tjp/sliderule/internal/types" ) // Client is used for sending spartan requests and receiving responses. @@ -32,7 +32,7 @@ const DefaultMaxRedirects int = 2 var ExceededMaxRedirects = errors.New("spartan.Client: exceeded MaxRedirects") // RoundTrip sends a single spartan request and returns its response. -func (c Client) RoundTrip(request *sr.Request) (*sr.Response, error) { +func (c Client) RoundTrip(request *types.Request) (*types.Response, error) { if request.Scheme != "spartan" && request.Scheme != "" { return nil, errors.New("non-spartan protocols not supported") } @@ -89,14 +89,14 @@ func (c Client) RoundTrip(request *sr.Request) (*sr.Response, error) { // Fetch parses a URL string and fetches the spartan 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/spartan/request.go b/spartan/request.go index c056af0..c88a20b 100644 --- a/spartan/request.go +++ b/spartan/request.go @@ -9,7 +9,7 @@ import ( "strconv" "strings" - sr "tildegit.org/tjp/sliderule" + "tildegit.org/tjp/sliderule/internal/types" ) var ( @@ -23,7 +23,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) (*sr.Request, int, error) { +func ParseRequest(rdr io.Reader) (*types.Request, int, error) { bufrdr, ok := rdr.(*bufio.Reader) if !ok { bufrdr = bufio.NewReader(rdr) @@ -52,7 +52,7 @@ func ParseRequest(rdr io.Reader) (*sr.Request, int, error) { return nil, 0, err } - return &sr.Request{ + return &types.Request{ URL: &url.URL{ Scheme: "spartan", Host: host, @@ -65,7 +65,7 @@ func ParseRequest(rdr io.Reader) (*sr.Request, int, error) { // 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 { +func GetRequestContentLength(request *types.Request) int { if lr, ok := request.Meta.(*io.LimitedReader); ok { return int(lr.N) } @@ -75,7 +75,7 @@ func GetRequestContentLength(request *sr.Request) int { // 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 { +func GetRequestBody(request *types.Request) io.Reader { if rdr, ok := request.Meta.(io.Reader); ok { return rdr } @@ -88,7 +88,7 @@ func GetRequestBody(request *sr.Request) io.Reader { // // This function will read the entire contents into memory unless // the reader is already an *io.LimitedReader. -func SetRequestBody(request *sr.Request, body io.Reader) error { +func SetRequestBody(request *types.Request, body io.Reader) error { if rdr, ok := body.(*io.LimitedReader); ok { request.Meta = rdr return nil diff --git a/spartan/response.go b/spartan/response.go index aeddd68..df565f9 100644 --- a/spartan/response.go +++ b/spartan/response.go @@ -8,20 +8,20 @@ import ( "strconv" "sync" - sr "tildegit.org/tjp/sliderule" + "tildegit.org/tjp/sliderule/internal/types" ) // The spartan response types. const ( - StatusSuccess sr.Status = 2 - StatusRedirect sr.Status = 3 - StatusClientError sr.Status = 4 - StatusServerError sr.Status = 5 + StatusSuccess types.Status = 2 + StatusRedirect types.Status = 3 + StatusClientError types.Status = 4 + StatusServerError types.Status = 5 ) // Success builds a successful spartan response. -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, @@ -29,24 +29,24 @@ func Success(mediatype string, body io.Reader) *sr.Response { } // Redirect builds a spartan redirect response. -func Redirect(url string) *sr.Response { - return &sr.Response{ +func Redirect(url string) *types.Response { + return &types.Response{ Status: StatusRedirect, Meta: url, } } // ClientError builds a "client error" spartan response. -func ClientError(err error) *sr.Response { - return &sr.Response{ +func ClientError(err error) *types.Response { + return &types.Response{ Status: StatusClientError, Meta: err.Error(), } } // ServerError builds a "server error" spartan response. -func ServerError(err error) *sr.Response { - return &sr.Response{ +func ServerError(err error) *types.Response { + return &types.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) (*sr.Response, error) { +func ParseResponse(rdr io.Reader) (*types.Response, error) { bufrdr := bufio.NewReader(rdr) hdrLine, err := bufrdr.ReadString('\n') @@ -74,15 +74,15 @@ 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: hdrLine[2 : len(hdrLine)-2], Body: bufrdr, }, nil } // NewResponseReader builds a reader for a response. -func NewResponseReader(response *sr.Response) sr.ResponseReader { +func NewResponseReader(response *types.Response) types.ResponseReader { return &responseReader{ Response: response, once: &sync.Once{}, @@ -90,7 +90,7 @@ func NewResponseReader(response *sr.Response) sr.ResponseReader { } type responseReader struct { - *sr.Response + *types.Response reader io.Reader once *sync.Once } diff --git a/spartan/serve.go b/spartan/serve.go index a477a45..4b37f6a 100644 --- a/spartan/serve.go +++ b/spartan/serve.go @@ -8,14 +8,14 @@ import ( "io" "net" - sr "tildegit.org/tjp/sliderule" + "tildegit.org/tjp/sliderule/internal/types" "tildegit.org/tjp/sliderule/internal" "tildegit.org/tjp/sliderule/logging" ) type spartanServer struct { internal.Server - handler sr.Handler + handler types.Handler } func (ss spartanServer) Protocol() string { return "SPARTAN" } @@ -26,9 +26,9 @@ func NewServer( hostname string, network string, address string, - handler sr.Handler, + handler types.Handler, errLog logging.Logger, -) (sr.Server, error) { +) (types.Server, error) { ss := &spartanServer{handler: handler} hostname = internal.JoinDefaultPort(hostname, "300") @@ -46,7 +46,7 @@ func NewServer( func (ss *spartanServer) handleConn(conn net.Conn) { buf := bufio.NewReader(conn) - var response *sr.Response + var response *types.Response request, clen, err := ParseRequest(buf) if err != nil { response = ClientError(err) |