contentdetect

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 5, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package contentdetect provides content type detection using multiple methods: Content-Type headers, magic bytes (file signatures), and byte analysis heuristics.

This package can be used independently from OmniProxy.

Example usage:

// Quick check
if contentdetect.IsBinary("", data) {
    fmt.Println("Binary content detected")
}

// With Content-Type hint
if contentdetect.IsBinary("image/png", data) {
    fmt.Println("Binary content detected")
}

// Detailed detection
info := contentdetect.Detect("", data)
fmt.Printf("Binary: %v, MIME: %s, Method: %s\n", info.IsBinary, info.MIMEType, info.Method)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsBinary

func IsBinary(contentType string, data []byte) bool

IsBinary returns true if the content appears to be binary. contentType is optional and can be empty.

func IsBinaryWithOptions

func IsBinaryWithOptions(contentType string, data []byte, opts *Options) bool

IsBinaryWithOptions returns true if the content appears to be binary using custom options.

func IsText

func IsText(contentType string, data []byte) bool

IsText returns true if the content appears to be text. contentType is optional and can be empty.

Types

type ContentInfo

type ContentInfo struct {
	// IsBinary is true if the content appears to be binary
	IsBinary bool
	// IsText is true if the content appears to be text
	IsText bool
	// MIMEType is the detected or provided MIME type
	MIMEType string
	// Extension is the likely file extension (e.g., "png", "json")
	Extension string
	// Method indicates how the detection was made: "content-type", "magic", "heuristic"
	Method string
	// Confidence is a value from 0.0 to 1.0 indicating detection confidence
	Confidence float64
}

ContentInfo contains detailed information about detected content.

func Detect

func Detect(contentType string, data []byte) ContentInfo

Detect analyzes content and returns detailed information. contentType is optional and can be empty.

func DetectWithOptions

func DetectWithOptions(contentType string, data []byte, opts *Options) ContentInfo

DetectWithOptions analyzes content with custom options.

type Options

type Options struct {
	// TrustContentType trusts the Content-Type header if provided and recognized
	TrustContentType bool
	// HighBitThreshold is the ratio of non-printable bytes to consider content binary (default: 0.30)
	HighBitThreshold float64
	// CheckBytes is the number of bytes to examine for detection (default: 512)
	CheckBytes int
}

Options configures the detection behavior.

func DefaultOptions

func DefaultOptions() *Options

DefaultOptions returns the default detection options.

Jump to

Keyboard shortcuts

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