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?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?