push

Push web resources recursively to the client

  • 所有者: tdewolff/push
  • 平台:
  • 许可证: MIT License
  • 分类:
  • 主题:
  • 喜欢:
    0
      比较:

Github星跟踪图

Push Build Status GoDoc Coverage Status

Push is a package that uses HTTP2 to push resources to the client as it parses content. By parsing HTML, CSS and SVG it extracts referenced resource URIs and pushes them towards the client, which is quicker than waiting for the client to parse and request those resources.

Installation

You need Go1.8 (from tip for example).

Run the following command

go get github.com/tdewolff/push

or add the following import and run the project with go get

import (
	"github.com/tdewolff/push"
)

Parsers

HTML

Parses

  • <style>...</style> as CSS
  • <x style="..."> as inline CSS
  • <iframe>...</iframe> as HTML
  • <svg>...</svg> as SVG

Extracts URIs from

  • <link href="...">
  • <script src="...">
  • <img src="...">
  • <img srcset="..., ...">
  • <object data="...">
  • <source src="...">
  • <audio src="...">
  • <video src="...">
  • <track src="...">
  • <embed src="...">
  • <input src="...">
  • <iframe src="...">

CSS

Parses

  • url(data:image/svg+xml,...) as SVG data URI SVGs are not allowed to load external resources

Extracts URIs from

  • url("...")

SVG

Parses

  • <style>...</style> as CSS
  • <x style="..."> as inline CSS

Extracts URIs from

  • <script href="..." xlink:href="...">
  • <image href="..." xlink:href="...">
  • <feImage href="..." xlink:href="...">
  • <color-profile href="..." xlink:href="...">
  • <use href="..." xlink:href="...">

Usage

Middleware

fileOpener := push.DefaultFileOpener("resources/")
cache := push.DefaultCache()
p := push.New("example.com/", fileOpener, cache)
http.HandleFunc("/", p.Middleware(func(w http.ResponseWriter, r *http.Request) {
	// ...
}))

Pass nil for fileOpener and cache to disable recursive parsing and URI caching respectively.

ResponseWriter

Wrap an existing http.ResponseWriter so that it pushes resources automatically:

fileOpener := push.DefaultFileOpener("resources/")
cache := push.DefaultCache()
p := push.New("example.com/", fileOpener, cache)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
	if pushWriter, err := p.ResponseWriter(w, r); err == nil {
		defer pushWriter.Close() // Close returns an error...
		w = pushWriter
	}

	// ...
})

Pass nil for fileOpener and cache to disable recursive parsing and URI caching respectively.

Reader

Wrap a reader and obtain the URIs from a channel:

func openIndex() io.Reader {
	r, _ := os.Open("index.html")

	uriHandler := push.URIHandlerFunc(func(uri string) error {
		// is called concurrently when using a recursive parser
		return nil
	})
	fileOpener := push.FileOpenerFunc(func(uri string) (io.Reader, string, error) {
		// open file for uri
		return r, mimetype, nil
	})
	parser := push.NewParser("example.com/", fileOpener, uriHandler)

	return p.Reader(r, parser, "text/html", "/index.html")
}

List

List the resource URIs found:

r, _ := os.Open("index.html")

uris, err := push.List("example.com/", fileOpener, r, "text/html", "/index.html")
if err != nil {
	panic(err)
}

Low-level usage

Push pushes resources to pusher. It is the underlying functionality of ResponseWriter.

httpPusher, ok := w.(http.Pusher)
if ok {
	pusher := NewPusher(httpPusher, &http.PushOptions{"", http.Header{}})

	parser, err := push.NewParser("example.com/", nil, pusher)
	if err != nil {
		panic(err)
	}

	tr := io.TeeReader(r, w)
	err := parser.Parse(r, "text/html", "/index.html")
}

Example

See example, it shows how a webserver with artificial 50ms delay per request can have the page load time reduced from 1.6s (http) to 0.4s (https).

License

Released under the MIT license.

主要指标

概览
名称与所有者tdewolff/push
主编程语言Go
编程语言Go (语言数: 4)
平台
许可证MIT License
所有者活动
创建于2016-12-16 22:55:22
推送于2017-03-25 09:22:09
最后一次提交2017-03-25 10:22:02
发布数0
用户参与
星数103
关注者数6
派生数2
提交数26
已启用问题?
问题数4
打开的问题数0
拉请求数0
打开的拉请求数0
关闭的拉请求数0
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?