httphandler

package
v0.0.0-...-f68f6f9 Latest Latest
Warning

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

Go to latest
Published: Feb 6, 2024 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package httphandler provides a basic net/http http.Handler implementation that resolves URLs.

The handler expects a ?url=URL_TO_RESOLVE query parameter, and responds with a JSON object containing the resolved URL and the resolved title:

$ curl -s localhost:8080/resolve?url=https://nyti.ms/2FVHq9v | jq .
{
    "given_url": "https://nyti.ms/2FVHq9v",
    "resolved_url": "https://www.nytimes.com/tips",
    "title": "Tips - The New York Times"
}

If an error occurs during resolution, the response status code will be 203 Non-Authoritative Information (to indicate partial response), an additional error field will be added, and a partial result will be returned, including the canonicalized and potentially partially-resolved URL:

$ curl -s localhost:8080/resolve?url=https://i-do-not-exist.xyz?utm_tag=tracking-code | jq .
{
    "given_url": "https://i-do-not-exist.xyz?utm_tag=tracking-code",
    "resolved_url": "https://i-do-not-exist.xyz",
    "title": "",
    "error": "resolve error"
}

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidURL     = errors.New("invalid arg url")
	ErrMissingURL     = errors.New("missing arg url")
	ErrRequestTimeout = errors.New("request timeout")
	ErrResolveError   = errors.New("resolve error")
	ErrUnsafeURL      = errors.New("unsafe URL")
)

Errors that might be returned by the HTTP handler.

Functions

This section is empty.

Types

type Handler

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

Handler is an HTTP request handler that can resolve URLs.

func New

func New(resolver urlresolver.Interface) *Handler

New creates a new Handler.

func (*Handler) ServeHTTP

func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)

type ResolveResponse

type ResolveResponse struct {
	GivenURL         string   `json:"given_url"`
	ResolvedURL      string   `json:"resolved_url"`
	Title            string   `json:"title"`
	IntermediateURLs []string `json:"intermediate_urls"`
	Error            string   `json:"error,omitempty"`
}

ResolveResponse defines the HTTP handler's response structure.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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