import "contnet.org/lib/cnp-go"
Package cnp provides CNP client and server implementations.
client.go cnp.go common.go error.go header.go message.go request.go response.go server.go
const ( // DefaultPort represents the default port for the cnp:// schema. DefaultPort = 25454 // MaxHeaderLength is the maximum byte size of the header. MaxHeaderLength = 1 * 1024 * 1024 // VersionMajor is the major CNP version (X in cnp/X.Y). VersionMajor = 0 // VersionMinor is the minor CNP version (Y in cnp/X.Y). VersionMinor = 4 )
const ( // ReasonSyntax represents the "syntax" reason parameter value. ReasonSyntax = "syntax" // ReasonVersion represents the "version" reason parameter value. ReasonVersion = "version" // ReasonInvalid represents the "invalid" reason parameter value. ReasonInvalid = "invalid" // ReasonNotSupported represents the "not_supported" reason parameter value. ReasonNotSupported = "not_supported" // ReasonTooLarge represents the "too_large" reason parameter value. ReasonTooLarge = "too_large" // ReasonNotFound represents the "not_found" reason parameter value. ReasonNotFound = "not_found" // ReasonDenied represents the "denied" reason parameter value. ReasonDenied = "denied" // ReasonRejected represents the "rejected" reason parameter value. ReasonRejected = "rejected" // ReasonServerError represents the "server_error" reason parameter value. ReasonServerError = "server_error" )
const ( // IntentOK represents the "ok" response intent. IntentOK = "ok" // IntentNotModified represents the "not_modified" response intent. IntentNotModified = "not_modified" // IntentError represents the "error" response intent. IntentError = "error" // IntentRedirect represents the "redirect" response intent. IntentRedirect = "redirect" )
Clean cleans a CNP request intent path.
This works the same as path.Clean(), but preserves a trailing slash.
Escape CNP-escapes the bytestring s.
ListenAndServe creates a new Server with a listen address and a handler and calls its ListenAndServe method.
Unescape unescapes data from wire format into a bytestring.
type Error interface { // CNPError returns the value of the "error" parameter in a CNP error // response. CNPError() string Error() string }
Error represents an error as used in a CNP error response.
NewError returns a new Error based on a reason parameter value.
If the reason is blank, nil is returned. If the reason is unknown, ErrorServerError is returned.
type ErrorDenied struct { Reason string }
ErrorDenied represents the CNP "denied" error reason.
func (e ErrorDenied) CNPError() string
CNPError on ErrorDenied returns the error parameter value "denied".
func (e ErrorDenied) Error() string
type ErrorInvalid struct { Reason string }
ErrorInvalid represents the CNP "invalid" error reason.
func (e ErrorInvalid) CNPError() string
CNPError on ErrorInvalid returns the error parameter value "invalid".
func (e ErrorInvalid) Error() string
type ErrorNotFound struct { Reason string }
ErrorNotFound represents the CNP "not_found" error reason.
func (e ErrorNotFound) CNPError() string
CNPError on ErrorNotFound returns the error parameter value "not_found".
func (e ErrorNotFound) Error() string
type ErrorNotSupported struct { Reason string }
ErrorNotSupported represents the CNP "not_supported" error reason.
func (e ErrorNotSupported) CNPError() string
CNPError on ErrorNotSupported returns the error parameter value "not_supported".
func (e ErrorNotSupported) Error() string
type ErrorRejected struct { Reason string }
ErrorRejected represents the CNP "rejected" error reason.
func (e ErrorRejected) CNPError() string
CNPError on ErrorRejected returns the error parameter value "rejected".
func (e ErrorRejected) Error() string
type ErrorServerError struct { Reason string }
ErrorServerError represents the CNP "server_error" error reason.
func (e ErrorServerError) CNPError() string
CNPError on ErrorServerError returns the error parameter value "server_error".
func (e ErrorServerError) Error() string
type ErrorSyntax struct { Reason string }
ErrorSyntax represents the CNP "syntax" error reason.
func (e ErrorSyntax) CNPError() string
CNPError on ErrorSyntax returns the error parameter value "syntax".
func (e ErrorSyntax) Error() string
type ErrorTooLarge struct { Reason string }
ErrorTooLarge represents the CNP "too_large" error reason.
func (e ErrorTooLarge) CNPError() string
CNPError on ErrorTooLarge returns the error parameter value "too_large".
func (e ErrorTooLarge) Error() string
type ErrorURL struct { // Err represents the error reason. Err error // URL is the URL that triggered the error. URL string }
ErrorURL is a non-CNPError that represents an invalid CNP URL.
type ErrorVersion struct { Reason string }
ErrorVersion represents the CNP "version" error reason.
func (e ErrorVersion) CNPError() string
CNPError on ErrorVersion returns the error parameter value "version".
func (e ErrorVersion) Error() string
type Handler interface { // ServeCNP responds to a CNP request. // // This function must be safe for concurrent use. ServeCNP(resp ResponseWriter, req *Request) }
Handler handles CNP requests accepted by the server.
type HandlerFunc func(resp ResponseWriter, req *Request)
HandlerFunc allows using raw functions as handlers.
func (h HandlerFunc) ServeCNP(resp ResponseWriter, req *Request)
ServeCNP calls h(resp, req).
type Header struct { // VersionMajor is the major CNP version given in the header. VersionMajor int // VersionMinor is the minor CNP version given in the header. VersionMinor int // Intent is the intent string of the message. Intent string // Parameters is a decoded map of the message parameters. Parameters Parameters }
Header represents a CNP message header
func NewHeader(intent string, params Parameters) Header
NewHeader creates a new CNP header from an intent and optional parameter map.
ParseHeader parses a CNP header from a bytestring. The line parameter must be a single line that ends with a line feed.
Version returns the message's CNP version as a "cnp/X.Y" string.
Write writes the CNP message header line in the wire format. The written line ends with a line feed.
Message represents a CNP message.
NewMessage creates a new CNP message.
This method also calls Message.TryComputeLength().
ParseMessage parses a CNP message.
The message's Body field is set to a bufio.Reader wrapping r. If r is an io.Closer, it is also stored separately for usage with Message.Close().
Close attempts to close the message body reader.
If the message's Body field is an io.Closer, its Close() method is called. If Body is not an io.Closer and the message was created with ParseMessage provided with an io.Closer, the Close() method on the original reader will be called. Otherwise, this function does nothing.
ComputeLength sets the length header parameter based on the message body. First, msg.TryComputeLength() is attempted; if that fails, the request is fully read into a bytes.Buffer and msg.Body is set to it.
Intent retrieves the message header intent.
Length gets the length header parameter (or 0 if it's not set or invalid).
Param retrieves a header parameter. It performs no value validation.
SetIntent sets the message header intent.
SetLength sets the length header parameter to n.
If negative or zero, the parameter is unset.
SetParam sets a header parameter. If the value is empty, the parameter is unset. It performs no value validation.
TryComputeLength sets the length header parameter to the length of the message body if the body's type is one of *bytes.Buffer, *bytes.Reader or *strings.Reader and returns true. If msg.Body is nil, the length parameter is unset and the function returns true. Otherwise, false is returned and the length parameter remains unchanged.
Validate validates the message header parameter value format (length).
Write writes the message header and body to w.
Parameters represents CNP message parameter key=value pairs.
func (p Parameters) Write(w io.Writer) (err error)
Write writes the parameters encoded for inclusion in the wire format. Includes a leading space if p is nonempty.
type Request struct { Message }
Request represents a CNP request message.
NewRequest creates a new Request from a host, path and optional body data.
NewRequestURL creates a new Request from a URL and optional body data.
ParseRequest parses a request message.
Host returns the host part of the request intent.
HostPath returns the host and path parts of the request intent.
IfModified retrieves the if_modified request parameter.
If the parameter isn't a valid RFC3339 timestamp, a zero time.Time is returned.
Length gets the length request parameter (or 0 if not set or invalid).
Name retrieves the name request parameter.
If the name request parameter is not a valid filename, an empty string is returned.
Path returns the path part of the request intent.
Select retrieves the select request parameter.
If the parameter isn't a valid selector, empty strings are returned.
SetHost sets the host part of the request intent, leaving path unchanged.
SetHostPath sets the request intent.
SetIfModified sets the if_modified request parameter.
If t is the zero time value, the if_modified parameter is unset.
SetLength sets the length request parameter to n.
If n is negative or zero, the parameter is unset.
SetName sets the name request parameter.
Returns an error if the name includes characters not valid in a filename (slash, null byte).
SetPath sets the path part of the request intent, leaving host unchanged.
SetSelect sets the select request parameter.
If the selector name is empty, the select parameter is unset.
SetType sets the type request parameter.
Returns an error if typ is not a valid format for a MIME type.
Type retrieves the type request parameter.
If the type request parameter is invalid or empty, the default value "application/octet-stream" is returned.
URL returns a cnp:// URL based on this request's intent.
Validate validates the request header intent and parameter value format (length, name, type, if_modified, select)
Write ensures that the request's length parameter is set if it has body and then writes it to w.
type Response struct { Message }
Response represents a CNP response message.
Get sends a body-less request to a given URL.
NewResponse creates a new Response from a response intent and optional body data.
ParseResponse parses a response message.
Send sends a CNP request to a server and returns the response.
The TCP connection is made using net.Dial.
Length gets the length response parameter (or -1 if it's not set or invalid).
Location retrieves the host and path from the location response parameter.
If the location parameter is empty, it returns empty host and path. If the location parameter is invalid, an error is returned.
Modified retrieves the modified Response parameter.
If the parameter isn't a valid RFC3339 timestamp, a zero time.Time is returned.
Name retrieves the name response parameter.
If the name response parameter is not a valid filename, an empty string is returned.
Reason retrieves the reason response parameter.
If the reason is nonempty and unknown, "server_error" is returned.
ResponseIntent retrieves the response intent. If the intent is unknown, the "error" intent is returned.
Select retrieves the select response parameter.
If the parameter isn't a valid selector, empty strings are returned.
SetLength sets the length response parameter to n.
If negative, the parameter is unset.
SetLocation sets the location response parameter to host and path.
Returns an error if the host or path are invalid.
SetModified sets the modified response parameter.
If t is the zero time value, the modified parameter is unset. Otherwise, if the time response parameter is empty, it's set to the current time.
SetName sets the name response parameter.
Returns an error if the name includes characters not valid in a filename (slash, null byte).
SetReason sets the reason response parameter.
If the reason is nonempty and unknown, it sets "server_error" instead.
SetResponseIntent sets the response intent. If the provided intent is not a known response intent, an error is returned and the intent is set to "error".
SetSelect sets the select response parameter.
If the selector name is empty, the select parameter is unset.
SetTime sets the time response parameter.
If t is the zero time value, the time parameter is unset.
SetType sets the type response parameter.
Returns an error if typ is not a valid format for a MIME type.
Time retrieves the time response parameter.
If the parameter isn't a valid RFC3339 timestamp, a zero time.Time is returned.
Type retrieves the type response parameter.
If the type response parameter is invalid or empty, the default value "application/octet-stream" is returned.
Validate validates the response intent and header parameter value format (length, name, type, time, modified, location, reason, select)
Write writes the response to w.
type ResponseWriter interface { // Response returns a Response object whose header will be written to the // socket. The body is nil and should be ignored. Response() *Response // RemoteAddr returns the network address of the client. RemoteAddr() net.Addr // WriteHeader sends a CNP header with an intent. WriteHeader() error // Write sends data in the CNP response body. // // If WriteHeader has not been called yet, it also calls WriteHeader("ok"). Write(data []byte) (int, error) }
ResponseWriter is used by a CNP server to write responses to CNP requests.
type Server struct { // LogAccess is called for every finished response if it's non-nil. LogAccess func(resp ResponseWriter, req *Request, respIntent string, respBytes int64) // LogError is called when an error happens if it's non-nil. LogError func(err interface{}) // AccessLog is used to log finished responses if LogAccess is nil. AccessLogger *log.Logger // ErrorLogger is used to log errors if LogError is nil. ErrorLogger *log.Logger // Address is the host:port that the server listens on. Address string // Handler is used to handle received requests. Handler Handler // Validate enables request parameter value validation; invalid requests // are responded to with errors. Validate bool // contains filtered or unexported fields }
Server represents a CNP server.
func NewServer() *Server
NewServer creates a new Server with default access and errors loggers, validation enabled and the listen address set to "localhost".
HandleConn reads a CNP request from conn and runs a handler to respond.
ListenAndServe uses net.Listen to listen on TCP srv.Address for new requests, then calls srv.Serve.
Serve listens on l for new connections and dispatches HandleConn goroutines.
Package cnp imports 15 packages (graph). Updated 2017-09-18. Refresh now. Tools for package owners.