urlrewrite

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2022 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package urlrewrite reproduces server-side URL rewrite logic.

Web servers sometimes "rewrite" request URLs internally, without issuing redirects to clients. For example, many web servers append the index file (e.g. "index.html" and "default.asp") to URL paths pointing to a directory. "https://www.example.com/" thus may be rewritten, say, to "https://www.example.com/index.html". Another usage is content negotiation: some web sites are configured, for instance, to rewrite "*.jpg" to "*.webp" and serve WebP images to supported clients.

Package urlrewrite implements some simple rewrite rules, aiming to provide reasonable approximates for static web servers (which simply serve static files under the document root). It also defines interface that allows you to (re)implement custom rules and combine them with the existing ones.

The rewritten URLs are used to determine the file to write signed exchanges to in the filesystem, determine the validity URL (where the validity data is served), and so on. It especially helps support multiple resources served at a single URL, e.g. with content negotiation. It also helps the URL path better reflect the physical location of the resource on the server, which in turn helps signed exchanges and validity data produced in the same directory as the original resource.

References

https://www.igvita.com/2013/05/01/deploying-webp-via-accept-content-negotiation/ (Deploying WebP via Accept Content Negotiation)

Index

Examples

Constants

This section is empty.

Variables

View Source
var DefaultRules = RuleSequence{
	CleanPath(),
	IndexRule("index.html"),
}

DefaultRules contains a set of rules to reproduce the URL rewriting on static web servers. It should serve as a good approximate to Apache with typical configurations in particular.

Functions

This section is empty.

Types

type Rule

type Rule interface {
	// Rewrite mutates the provided URL u, applying the rewrite rule.
	// respHeader is the response header and used, for example, to replace
	// the file extension based on Content-Type.
	Rewrite(u *url.URL, respHeader http.Header)
}

Rule implements a URL rewrite rule.

func CleanPath

func CleanPath() Rule

CleanPath normalizes the URL path. It basically applies path.Clean to eliminate multiple slashes, "." (current directory), and ".." (parent directory) in the path, but adds the trailing slash back.

Example
package main

import (
	"fmt"
	"net/http"
	"net/url"

	"github.com/layer0-platform/webpackager/urlrewrite"
)

func main() {
	examples := []string{
		"https://example.com/foo/../index.html",
		"https://example.com/foo/./bar/",
	}

	for _, example := range examples {
		u, err := url.Parse(example)
		if err != nil {
			panic(err)
		}
		urlrewrite.CleanPath().Rewrite(u, make(http.Header))
		fmt.Println(u)
	}

}
Output:

https://example.com/index.html
https://example.com/foo/bar/

func IndexRule

func IndexRule(indexFile string) Rule

IndexRule appends indexFile to the path when it ends with a slash ("/") or otherwise likely represents a directory. indexFile is supposed to match the index file on the target server (typically "index.html").

type RuleSequence

type RuleSequence []Rule

RuleSequence represents a series of rewrite rules.

func (RuleSequence) Rewrite

func (rs RuleSequence) Rewrite(u *url.URL, respHeader http.Header)

Rewrite applies all rewrite rules in order.

Jump to

Keyboard shortcuts

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