💾 Archived View for source.community › ckaznocha › gemini › raw › main › status.go captured on 2021-12-17 at 13:26:06.

View Raw

More Information

-=-=-=-=-=-=-

//go:generate stringer -linecomment -output status_string.gen.go -type StatusCodeCategory,StatusCode

package gemini

const (
	maxCategory = 9
	maxCode     = 99
	codeDivisor = 10
)

// StatusCodeCategory identifies a class of Gemini status codes.
type StatusCodeCategory uint8

// ToCode converts a Categoy to the base code for that category.
func (c StatusCodeCategory) ToCode() StatusCode {
	if c > maxCategory {
		return 0
	}

	return StatusCode(c * codeDivisor)
}

// The status code categories as defined in `Project Gemini - Speculative
// specification - Section 3.2`.
const (
	StatusCategoryInput                     StatusCodeCategory = 1 // INPUT
	StatusCategorySuccess                   StatusCodeCategory = 2 // SUCCESS
	StatusCategoryRedirect                  StatusCodeCategory = 3 // REDIRECT
	StatusCategoryTemporaryFailure          StatusCodeCategory = 4 // TEMPORARY FAILURE
	StatusCategoryPermanentFailure          StatusCodeCategory = 5 // PERMANENT FAILURE
	StatusCategoryClientCertificateRequired StatusCodeCategory = 6 // CLIENT CERTIFICATE REQUIRED

	StatusCategoryUndefined    StatusCodeCategory = 0 // UNDEFINED
	StatusCategoryUndefinedX   StatusCodeCategory = 7 // UNDEFINED
	StatusCategoryUndefinedXX  StatusCodeCategory = 8 // UNDEFINED
	StatusCategoryUndefinedXXX StatusCodeCategory = 9 // UNDEFINED
)

// StatusCode represents a Gemini status code. Gemini status codes are two digit
// values. See `Project Gemini - Speculative specification - Section 3.2`.
type StatusCode uint8

//nolint:gochecknoglobals // un exported lookup table.
var asciiNumbers = [10]byte{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}

// MarshalText implements the encoding.TextMarshaler interface. It returns the
// the code as an ASCII byte slice. It returns "00" if the code is invalid.
func (c StatusCode) MarshalText() ([]byte, error) {
	if c > maxCode {
		return []byte{'0', '0'}, nil
	}

	return []byte{asciiNumbers[c/codeDivisor], asciiNumbers[c%codeDivisor]}, nil
}

// ToCategory returns which category the status code belongs to.
func (c StatusCode) ToCategory() StatusCodeCategory {
	if c > maxCode {
		return StatusCategoryUndefined
	}

	return StatusCodeCategory(c / codeDivisor)
}

// Description returns the description of the code as described in `Project
// Gemini - Speculative specification`. Some but not all of the descriptions may
// be appropriate to return to a client as a failure description.
func (c StatusCode) Description() string {
	return statusCodeToText[c]
}

// The status codes as defined in `Project Gemini - Speculative
// specification - Appendix 1. Full two digit status codes`.
const (
	// 1x - INPUT
	StatusInput          StatusCode = 10 // INPUT
	StatusSensitiveInput StatusCode = 11 // SENSITIVE INPUT

	// 2x - SUCCESS
	StatusSuccess StatusCode = 20 // SUCCESS

	// 3x - REDIRECT
	StatusTemporaryRedirect StatusCode = 30 // REDIRECT - TEMPORARY
	StatusPermanentRedirect StatusCode = 31 // REDIRECT - PERMANENT

	// 4x - TEMPORARY FAILURE
	StatusTemporaryFailure StatusCode = 40 // TEMPORARY FAILURE
	StatusServerFailure    StatusCode = 41 // SERVER UNAVAILABLE
	StatusCGIError         StatusCode = 42 // CGI ERROR
	StatusProxyError       StatusCode = 43 // PROXY ERROR
	StatusSlowDown         StatusCode = 44 // SLOW DOWN

	// 5x - PERMANENT FAILURE
	StatusPermanentFailure    StatusCode = 50 // PERMANENT FAILURE
	StatusNotFound            StatusCode = 51 // NOT FOUND
	StatusGone                StatusCode = 52 // GONE
	StatusProxyRequestRefused StatusCode = 53 // PROXY REQUEST REFUSED
	StatusBadRequest          StatusCode = 59 // BAD REQUEST

	// 6x - CLIENT CERTIFICATE REQUIRED
	StatusClientCertificateRequired StatusCode = 60 // CLIENT CERTIFICATE REQUIRED
	StatusCertificateNotAuthorised  StatusCode = 61 // CERTIFICATE NOT AUTHORIZED
	StatusCertificateNotValid       StatusCode = 62 // CERTIFICATE NOT VALID
)

//nolint:lll,gochecknoglobals // un exported lookup table of very long strings.
var statusCodeToText = map[StatusCode]string{
	// 1x - INPUT
	StatusInput:          `The requested resource accepts a line of textual user input. The <META> line is a prompt which should be displayed to the user. The same resource should then be requested again with the user's input included as a query component. Queries are included in requests as per the usual generic URL definition in RFC3986, i.e. separated from the path by a ?. Reserved characters used in the user's input must be "percent-encoded" as per RFC3986, and space characters should also be percent-encoded.`,
	StatusSensitiveInput: `As per status code 10, but for use with sensitive input such as passwords. Clients should present the prompt as per status code 10, but the user's input should not be echoed to the screen to prevent it being read by "shoulder surfers".`,

	// 2x - SUCCESS
	StatusSuccess: `The request was handled successfully and a response body will follow the response header. The <META> line is a MIME media type which applies to the response body.`,

	// 3x - REDIRECT
	StatusTemporaryRedirect: `The server is redirecting the client to a new location for the requested resource. There is no response body. <META> is a new URL for the requested resource. The URL may be absolute or relative. The redirect should be considered temporary, i.e. clients should continue to request the resource at the original address and should not performance convenience actions like automatically updating bookmarks. There is no response body.`,
	StatusPermanentRedirect: `The requested resource should be consistently requested from the new URL provided in future. Tools like search engine indexers or content aggregators should update their configurations to avoid requesting the old URL, and end-user clients may automatically update bookmarks, etc. Note that clients which only pay attention to the initial digit of status codes will treat this as a temporary redirect. They will still end up at the right place, they just won't be able to make use of the knowledge that this redirect is permanent, so they'll pay a small performance penalty by having to follow the redirect each time.`,

	// 4x - TEMPORARY FAILURE
	StatusTemporaryFailure: `The request has failed. There is no response body. The nature of the failure is temporary, i.e. an identical request MAY succeed in the future.`,
	StatusServerFailure:    `The server is unavailable due to overload or maintenance.`,
	StatusCGIError:         `A CGI process, or similar system for generating dynamic content, died unexpectedly or timed out.`,
	StatusProxyError:       `A proxy request failed because the server was unable to successfully complete a transaction with the remote host.)`,
	StatusSlowDown:         `Rate limiting is in effect.`,

	// 5x - PERMANENT FAILURE
	StatusPermanentFailure:    `The request has failed. There is no response body. The nature of the failure is permanent, i.e. identical future requests will reliably fail for the same reason.`,
	StatusNotFound:            `The requested resource could not be found but may be available in the future.`,
	StatusGone:                `The resource requested is no longer available and will not be available again. Search engines and similar tools should remove this resource from their indices. Content aggregators should stop requesting the resource and convey to their human users that the subscribed resource is gone.`,
	StatusProxyRequestRefused: `The request was for a resource at a domain not served by the server and the server does not accept proxy requests.`,
	StatusBadRequest:          `The server was unable to parse the client's request, presumably due to a malformed request.`,

	// 6x - CLIENT CERTIFICATE REQUIRED
	StatusClientCertificateRequired: `The requested resource requires a client certificate to access. If the request was made without a certificate, it should be repeated with one. If the request was made with a certificate, the server did not accept it and the request should be repeated with a different certificate.`,
	StatusCertificateNotAuthorised:  `The supplied client certificate is not authorized for accessing the particular requested resource. The problem is not with the certificate itself, which may be authorised for other resources.`,
	StatusCertificateNotValid:       `The supplied client certificate was not accepted because it is not valid. This indicates a problem with the certificate in and of itself, with no consideration of the particular requested resource. The most likely cause is that the certificate's validity start date is in the future or its expiry date has passed, but this code may also indicate an invalid signature, or a violation of a X509 standard requirements.`,
}