contentviews

package
v0.0.0-...-ab2ec04 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 10, 2026 License: MIT Imports: 62 Imported by: 0

Documentation

Overview

Package contentviews formats message bodies for display and interactive editing. Views must support concurrent calls and must not modify their input or metadata.

Index

Constants

This section is empty.

Variables

View Source
var DefaultRegistry = NewRegistry()

DefaultRegistry is used by PrettifyMessage when no registry is specified.

View Source
var ErrNoView = errors.New("at least one view needs to have a working render priority")

ErrNoView indicates that no registered view has a working render priority.

View Source
var ErrUnsupportedMessage = errors.New("unsupported contentview message")

ErrUnsupportedMessage indicates a message type not supported by PrettifyMessage.

Functions

This section is empty.

Types

type CSS

type CSS struct{}

CSS reformats a style sheet's whitespace, as upstream's pure-Python CSS prettifier does: it works with any input and modifies whitespace only.

func (CSS) Name

func (CSS) Name() string

Name returns the registered view name. The upstream class is named ViewCSS and inherits the name derived from the class name, so that spelling is the registered name.

func (CSS) Prettify

func (CSS) Prettify(data []byte, metadata Metadata) (string, error)

Prettify beautifies the style sheet: rules and declarations get their own lines and indentation while quoted strings and comments stay untouched. Undecodable bytes pass through unchanged. A pattern the engine abandons at the match timeout leaves its area unmarked or its rewrite unapplied, and one warning goes to the package logger.

func (CSS) RenderPriority

func (CSS) RenderPriority(data []byte, metadata Metadata) float64

RenderPriority prefers nonempty text/css bodies.

func (CSS) SyntaxHighlight

func (CSS) SyntaxHighlight() string

SyntaxHighlight returns the view's highlighting language.

type DNS

type DNS struct{}

DNS renders DNS messages as editable YAML, including TCP framing when metadata carries a TCP or HTTP message. Calls are pure and safe to run concurrently.

func (DNS) Name

func (DNS) Name() string

Name returns the registered view name.

func (DNS) Prettify

func (DNS) Prettify(data []byte, metadata Metadata) (string, error)

Prettify decodes DNS wire bytes and returns upstream-ordered YAML. Malformed DNS or missing TCP framing returns an error.

func (DNS) Reencode

func (DNS) Reencode(prettified string, metadata Metadata) ([]byte, error)

Reencode reconstructs DNS wire bytes from YAML without changing metadata. YAML text is bounded at 1 MiB, nesting at 64, and alias expansion at 10,000; malformed fields, excessive decoding work or oversized DNS returns an error.

func (DNS) RenderPriority

func (DNS) RenderPriority(_ []byte, metadata Metadata) float64

RenderPriority prefers DNS media types and flows whose server port is 53 or 5353.

func (DNS) SyntaxHighlight

func (DNS) SyntaxHighlight() string

SyntaxHighlight returns the upstream DNS view's highlighting language.

type GRPC

type GRPC struct{}

GRPC renders length-prefixed gRPC messages as YAML documents, one protobuf message each, and encodes edited documents as uncompressed messages.

func (GRPC) Name

func (GRPC) Name() string

Name returns the view's selection name.

func (GRPC) Prettify

func (GRPC) Prettify(data []byte, metadata Metadata) (string, error)

Prettify renders every message or fails without partial output. When the matched definition does not fit, the whole body is rendered again without it on the same budget.

func (GRPC) Reencode

func (GRPC) Reencode(prettified string, metadata Metadata) ([]byte, error)

Reencode encodes each YAML document as one uncompressed message.

func (GRPC) RenderPriority

func (GRPC) RenderPriority(_ []byte, metadata Metadata) float64

RenderPriority selects the view for gRPC media types.

func (GRPC) SyntaxHighlight

func (GRPC) SyntaxHighlight() string

SyntaxHighlight returns the language of the rendered text.

type GraphQL

type GraphQL struct{}

GraphQL separates GraphQL queries from their JSON envelope.

func (GraphQL) Name

func (GraphQL) Name() string

Name returns the view's selection name.

func (GraphQL) Prettify

func (GraphQL) Prettify(data []byte, _ Metadata) (string, error)

Prettify formats query envelopes without changing member order or input.

func (GraphQL) RenderPriority

func (GraphQL) RenderPriority(data []byte, metadata Metadata) float64

RenderPriority selects GraphQL JSON objects and batches.

func (GraphQL) SyntaxHighlight

func (GraphQL) SyntaxHighlight() string

SyntaxHighlight returns the renderer's syntax name.

type HTTP3Frames

type HTTP3Frames struct{}

HTTP3Frames displays HTTP/3 stream frames without changing endpoint state.

func (HTTP3Frames) Name

func (HTTP3Frames) Name() string

Name returns the view's selection name.

func (HTTP3Frames) Prettify

func (HTTP3Frames) Prettify(data []byte, metadata Metadata) (string, error)

Prettify replays an immutable message prefix and attributes completed frames to the message that supplied the final bytes. Calls retain no flow pointers.

func (HTTP3Frames) RenderPriority

func (HTTP3Frames) RenderPriority(_ []byte, metadata Metadata) float64

RenderPriority selects TCP streams carrying an HTTP/3 ALPN.

func (HTTP3Frames) SyntaxHighlight

func (HTTP3Frames) SyntaxHighlight() string

SyntaxHighlight returns the renderer's syntax name.

type HexDump

type HexDump struct{}

HexDump renders sixteen bytes per line, with offsets and an ASCII column.

func (HexDump) Name

func (HexDump) Name() string

Name returns the display name.

func (HexDump) Prettify

func (HexDump) Prettify(data []byte, _ Metadata) (string, error)

Prettify reproduces the grouped hexadecimal format of the Rust view.

func (HexDump) RenderPriority

func (HexDump) RenderPriority(data []byte, _ Metadata) float64

RenderPriority prefers binary data over the raw text view.

func (HexDump) SyntaxHighlight

func (HexDump) SyntaxHighlight() string

SyntaxHighlight disables syntax highlighting.

type HexStream

type HexStream struct{}

HexStream renders contiguous lowercase hex and accepts edited hexadecimal.

func (HexStream) Name

func (HexStream) Name() string

Name returns the display name.

func (HexStream) Prettify

func (HexStream) Prettify(data []byte, _ Metadata) (string, error)

Prettify returns two lowercase hex characters per byte.

func (HexStream) Reencode

func (HexStream) Reencode(text string, _ Metadata) ([]byte, error)

Reencode accepts either letter case and trailing carriage returns/newlines. It returns an error for odd-length input or any non-hexadecimal character.

func (HexStream) RenderPriority

func (HexStream) RenderPriority(data []byte, _ Metadata) float64

RenderPriority prefers binary data, below the grouped hex dump view.

func (HexStream) SyntaxHighlight

func (HexStream) SyntaxHighlight() string

SyntaxHighlight disables syntax highlighting.

type Image

type Image struct{}

Image displays an image's metadata as YAML under a heading that names the detected format.

func (Image) Name

func (Image) Name() string

Name returns the registered view name.

func (Image) Prettify

func (Image) Prettify(data []byte, metadata Metadata) (string, error)

Prettify names the detected image format and lists the metadata of a PNG, GIF, JPEG or ICO image. It returns an error when such an image is malformed; a format it detects but cannot parse gets the heading alone.

func (Image) RenderPriority

func (Image) RenderPriority(data []byte, metadata Metadata) float64

RenderPriority prefers image content types other than SVG.

func (Image) SyntaxHighlight

func (Image) SyntaxHighlight() string

SyntaxHighlight returns the view's highlighting language.

type InteractiveView

type InteractiveView interface {
	View
	Reencode(prettified string, metadata Metadata) ([]byte, error)
}

InteractiveView also encodes edited text into the original data format.

type JSON

type JSON struct{}

JSON formats JSON with four-space indentation and Python's scalar semantics. Objects retain insertion order, including the position of duplicate names. Lone surrogates are preserved as WTF-8, since Go strings can hold their bytes. Input nesting is limited to 1024 containers.

func (JSON) Name

func (JSON) Name() string

Name returns the registered view name.

func (JSON) Prettify

func (JSON) Prettify(data []byte, _ Metadata) (string, error)

Prettify decodes JSON and formats it as Python's json.dumps with indent=4. Invalid encodings, malformed JSON and excessive nesting return an error.

func (JSON) RenderPriority

func (JSON) RenderPriority(data []byte, metadata Metadata) float64

RenderPriority prefers nonempty JSON media types.

func (JSON) SyntaxHighlight

func (JSON) SyntaxHighlight() string

SyntaxHighlight returns the upstream JSON view's highlighting language.

type JavaScript

type JavaScript struct{}

JavaScript reformats a script's braces, semicolons and indentation, as upstream's beautifier does, while regular-expression literals, strings, template literals, comments and for-loop heads stay untouched.

func (JavaScript) Name

func (JavaScript) Name() string

Name returns the registered view name.

func (JavaScript) Prettify

func (JavaScript) Prettify(data []byte, metadata Metadata) (string, error)

Prettify beautifies the script: blocks get their own lines and two-space indentation while the protected areas keep their text. Bytes that are not valid UTF-8 become replacement characters, as Python's replace error handler has it. A pattern the engine abandons at the match timeout leaves its area unmarked or its rewrite unapplied, and one warning goes to the package logger.

func (JavaScript) RenderPriority

func (JavaScript) RenderPriority(data []byte, metadata Metadata) float64

RenderPriority prefers nonempty bodies with a JavaScript content type.

func (JavaScript) SyntaxHighlight

func (JavaScript) SyntaxHighlight() string

SyntaxHighlight returns the view's highlighting language.

type LimitError

type LimitError struct {
	View, Stage, Resource string
	Limit, Attempted      uint64
}

LimitError identifies a contentview resource refusal without including input.

func (*LimitError) Error

func (e *LimitError) Error() string

Error reports the view, stage and exhausted resource.

type MQTT

type MQTT struct{}

MQTT renders the supported MQTT control packet summaries.

func (MQTT) Name

func (MQTT) Name() string

Name returns the view's selection name.

func (MQTT) Prettify

func (MQTT) Prettify(data []byte, _ Metadata) (string, error)

Prettify decodes one MQTT packet without retaining transport state.

func (MQTT) RenderPriority

func (MQTT) RenderPriority([]byte, Metadata) float64

RenderPriority keeps MQTT an explicitly selected display view.

func (MQTT) SyntaxHighlight

func (MQTT) SyntaxHighlight() string

SyntaxHighlight returns the renderer's syntax name.

type Metadata

type Metadata struct {
	Flow                flow.Flow
	ContentType         string
	HTTPMessage         *httpmsg.Message
	HTTPRequest         *httpmsg.Request
	TCPMessage          *tcp.Message
	UDPMessage          *udp.Message
	WebSocketMessage    *websocket.Message
	DNSMessage          *dns.Message
	Protocol            string
	ProtobufDefinitions string
	OriginalData        []byte
}

Metadata describes the data being displayed. Every field is optional. The message and flow pointers are borrowed; callers synchronise their access.

type MsgPack

type MsgPack struct{}

MsgPack renders MessagePack data as YAML and encodes edited YAML back.

The YAML representation follows the Rust view: scalars are resolved with the YAML 1.2 core rules, strings that would read back as another type are single-quoted, and map entries keep their wire order. MessagePack binary values, which the Rust view cannot display, use the standard !!binary tag so that they survive an edit as binary rather than becoming strings.

func (MsgPack) Name

func (MsgPack) Name() string

Name returns the view's selection name.

func (MsgPack) Prettify

func (MsgPack) Prettify(data []byte, _ Metadata) (string, error)

Prettify decodes one MessagePack value and renders it as YAML.

func (MsgPack) Reencode

func (MsgPack) Reencode(prettified string, _ Metadata) ([]byte, error)

Reencode parses one YAML document and encodes it as MessagePack.

func (MsgPack) RenderPriority

func (MsgPack) RenderPriority(_ []byte, metadata Metadata) float64

RenderPriority selects the view for MessagePack media types.

func (MsgPack) SyntaxHighlight

func (MsgPack) SyntaxHighlight() string

SyntaxHighlight returns the language of the rendered text.

type Multipart

type Multipart struct{}

Multipart displays a multipart/form-data body as an ordered YAML mapping.

func (Multipart) Name

func (Multipart) Name() string

Name returns the registered view name.

func (Multipart) Prettify

func (Multipart) Prettify(data []byte, metadata Metadata) (string, error)

Prettify decodes the parts named by the message's Content-Type boundary, escapes their bytes, and merges repeated names. It returns an error when metadata has no HTTP message, that message has no Content-Type header, or a part has no end of headers.

func (Multipart) RenderPriority

func (Multipart) RenderPriority(data []byte, metadata Metadata) float64

RenderPriority prefers nonempty multipart/form-data bodies.

func (Multipart) SyntaxHighlight

func (Multipart) SyntaxHighlight() string

SyntaxHighlight returns the view's highlighting language.

type Protobuf

type Protobuf struct{}

Protobuf renders protobuf messages as editable YAML. Without definitions, fields are keyed by number and their types are inferred from the wire data; with the protobuf_definitions option, the message matching the request path names known fields.

Numeric alternatives are inline comments on the YAML nodes rather than a textual rewrite of the rendered YAML, so string values that resemble tags are never changed.

func (Protobuf) Name

func (Protobuf) Name() string

Name returns the view's selection name.

func (Protobuf) Prettify

func (Protobuf) Prettify(data []byte, metadata Metadata) (string, error)

Prettify renders one message, retrying without definitions when the matched definition does not fit the wire data.

func (Protobuf) Reencode

func (Protobuf) Reencode(prettified string, metadata Metadata) ([]byte, error)

Reencode encodes one YAML mapping as a protobuf message.

func (Protobuf) RenderPriority

func (Protobuf) RenderPriority(_ []byte, metadata Metadata) float64

RenderPriority selects the view for protobuf media types.

func (Protobuf) SyntaxHighlight

func (Protobuf) SyntaxHighlight() string

SyntaxHighlight returns the language of the rendered text.

type Query

type Query struct{}

Query displays an HTTP request's query parameters as an ordered YAML mapping.

func (Query) Name

func (Query) Name() string

Name returns the registered view name.

func (Query) Prettify

func (Query) Prettify(_ []byte, metadata Metadata) (string, error)

Prettify renders query parameters, preserving key order and repeated values. It returns an error when metadata has no HTTP request or YAML encoding fails.

func (Query) RenderPriority

func (Query) RenderPriority(data []byte, metadata Metadata) float64

RenderPriority prefers request query parameters when the body is empty.

func (Query) SyntaxHighlight

func (Query) SyntaxHighlight() string

SyntaxHighlight returns the view's highlighting language.

type Raw

type Raw struct{}

Raw displays UTF-8 text, replacing invalid bytes with backslash escapes.

func (Raw) Name

func (Raw) Name() string

Name returns the display name.

func (Raw) Prettify

func (Raw) Prettify(data []byte, _ Metadata) (string, error)

Prettify decodes UTF-8 using Python's backslashreplace error policy.

func (Raw) RenderPriority

func (Raw) RenderPriority([]byte, Metadata) float64

RenderPriority supplies the default text fallback priority.

func (Raw) SyntaxHighlight

func (Raw) SyntaxHighlight() string

SyntaxHighlight disables syntax highlighting.

type Registry

type Registry struct {
	// contains filtered or unexported fields
}

Registry holds named views in registration order. Its zero value is empty. Operations are concurrency-safe; view and subscriber calls run without its lock.

func NewRegistry

func NewRegistry() *Registry

NewRegistry returns a registry containing the built-in views.

func (*Registry) AvailableViews

func (r *Registry) AvailableViews() []string

AvailableViews lists auto followed by the sorted lower-case view names.

func (*Registry) Get

func (r *Registry) Get(name string) (View, bool)

Get looks up a name case-insensitively.

func (*Registry) GetView

func (r *Registry) GetView(data []byte, metadata Metadata, name string) (View, error)

GetView selects name or, for auto, an empty name, or an unknown name, the highest-priority view. Ties retain registration order. Panicking priority implementations are logged and skipped. ErrNoView means none succeeded.

func (*Registry) Register

func (r *Registry) Register(view View)

Register adds or replaces a view, retaining its position when replaced. It panics for a nil view or an empty name.

func (*Registry) Subscribe

func (r *Registry) Subscribe(notify func(View)) func()

Subscribe registers an upstream on_change notification and returns an idempotent unsubscribe. Notifications are synchronous and may call back into the registry.

type Result

type Result struct {
	Text            string
	SyntaxHighlight string
	ViewName        string
	Description     string
	Truncated       bool
	// Err records a message-type, view-selection, or rendering failure, even
	// when Text contains a successful Raw fallback. Missing content is not an error.
	Err error
}

Result contains display text, the selected view, and content-decoding information. An empty ViewName means no view could be selected. Truncated reports line cutoff.

func PrettifyMessage

func PrettifyMessage(message any, f flow.Flow, viewName string, registry *Registry, lineCutoff int) Result

PrettifyMessage renders an HTTP, TCP, UDP, or WebSocket message. HTTP inputs may be *httpmsg.Message, *httpmsg.Request, or *httpmsg.Response. A nil registry uses DefaultRegistry. An empty viewName means auto. Positive lineCutoff limits text through that many newlines; nonpositive values leave the text uncut. Callers pass options.Manager.Int("content_view_lines_cutoff") when appropriate. A failed automatic view falls back to Raw; an explicitly requested view's failure is displayed as an error. Result.Err preserves either failure and wraps ErrUnsupportedMessage for unsupported message types. The input and its flow must not be mutated concurrently while this function runs.

func PrettifyMessageWithMetadata

func PrettifyMessageWithMetadata(message any, metadata Metadata, viewName string, registry *Registry, lineCutoff int) Result

PrettifyMessageWithMetadata renders a message using the same options and fallback behavior as PrettifyMessage. It retains the supplied flow and expands the protobuf definitions home path, deriving other metadata from the message. It does not mutate the supplied metadata. The message and its flow must not be mutated concurrently while this function runs.

type SocketIO

type SocketIO struct{}

SocketIO displays Engine.IO and Socket.IO packet names and payloads.

func (SocketIO) Name

func (SocketIO) Name() string

Name returns the view's selection name.

func (SocketIO) Prettify

func (SocketIO) Prettify(data []byte, _ Metadata) (string, error)

Prettify shows visible packet types and escaped payloads.

func (SocketIO) RenderPriority

func (SocketIO) RenderPriority(data []byte, metadata Metadata) float64

RenderPriority selects nonempty Socket.IO WebSocket messages.

func (SocketIO) SyntaxHighlight

func (SocketIO) SyntaxHighlight() string

SyntaxHighlight returns the renderer's syntax name.

type URLEncoded

type URLEncoded struct{}

URLEncoded displays form fields as an ordered YAML mapping.

func (URLEncoded) Name

func (URLEncoded) Name() string

Name returns the registered view name.

func (URLEncoded) Prettify

func (URLEncoded) Prettify(data []byte, _ Metadata) (string, error)

Prettify decodes form fields, escapes their bytes, and merges repeated keys. It returns an error if the YAML encoder cannot represent the result.

func (URLEncoded) RenderPriority

func (URLEncoded) RenderPriority(data []byte, metadata Metadata) float64

RenderPriority prefers nonempty URL-encoded form bodies.

func (URLEncoded) SyntaxHighlight

func (URLEncoded) SyntaxHighlight() string

SyntaxHighlight returns the view's highlighting language.

type View

type View interface {
	Name() string
	SyntaxHighlight() string
	Prettify(data []byte, metadata Metadata) (string, error)
	RenderPriority(data []byte, metadata Metadata) float64
}

View transforms bytes into human-readable text. SyntaxHighlight returns css, javascript, xml, yaml, none, or error. RenderPriority selects the automatic view: higher wins, negative means unsupported. Prettify returns an error when the data cannot be rendered by this view.

type WBXML

type WBXML struct{}

WBXML renders the pinned ActiveSync WBXML vocabulary as XML.

func (WBXML) Name

func (WBXML) Name() string

Name returns the view's selection name.

func (WBXML) Prettify

func (WBXML) Prettify(data []byte, _ Metadata) (string, error)

Prettify parses complete tokens with bounded work and finite truncation errors.

func (WBXML) RenderPriority

func (WBXML) RenderPriority(data []byte, metadata Metadata) float64

RenderPriority selects supported nonempty WBXML media types.

func (WBXML) SyntaxHighlight

func (WBXML) SyntaxHighlight() string

SyntaxHighlight returns the renderer's syntax name.

type XMLHTML

type XMLHTML struct{}

XMLHTML formats XML or HTML by changing whitespace only, even in malformed input.

func (XMLHTML) Name

func (XMLHTML) Name() string

Name returns the registered view name.

func (XMLHTML) Prettify

func (XMLHTML) Prettify(data []byte, metadata Metadata) (string, error)

Prettify indents markup without repairing it or changing its tags. HTTP metadata supplies decoded text when available. It never returns an error.

func (XMLHTML) RenderPriority

func (XMLHTML) RenderPriority(data []byte, metadata Metadata) float64

RenderPriority prefers XML and HTML content types, then XML-looking bytes.

func (XMLHTML) SyntaxHighlight

func (XMLHTML) SyntaxHighlight() string

SyntaxHighlight returns the view's highlighting language.

type ZIP

type ZIP struct{}

ZIP displays archive filenames without opening or extracting entries.

func (ZIP) Name

func (ZIP) Name() string

Name returns the view's selection name.

func (ZIP) Prettify

func (ZIP) Prettify(data []byte, _ Metadata) (string, error)

Prettify validates central-directory bounds before constructing a ZIP reader.

func (ZIP) RenderPriority

func (ZIP) RenderPriority(data []byte, metadata Metadata) float64

RenderPriority selects nonempty ZIP media types.

func (ZIP) SyntaxHighlight

func (ZIP) SyntaxHighlight() string

SyntaxHighlight returns the renderer's syntax name.

Directories

Path Synopsis
Package highlight supplies syntax-token adapters for content presentation.
Package highlight supplies syntax-token adapters for content presentation.
Package imagemeta extracts the display metadata of PNG, GIF, JPEG and ICO images, reading exactly the fields mitmproxy's parsers read.
Package imagemeta extracts the display metadata of PNG, GIF, JPEG and ICO images, reading exactly the fields mitmproxy's parsers read.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL