mirror of
https://github.com/taigrr/wasm-experiments
synced 2025-01-18 04:03:21 -08:00
Add experimental gRPC-Web folder
This commit is contained in:
24
vendor/github.com/lpar/gzipped/.gitignore
generated
vendored
Normal file
24
vendor/github.com/lpar/gzipped/.gitignore
generated
vendored
Normal file
@@ -0,0 +1,24 @@
|
||||
# Compiled Object files, Static and Dynamic libs (Shared Objects)
|
||||
*.o
|
||||
*.a
|
||||
*.so
|
||||
|
||||
# Folders
|
||||
_obj
|
||||
_test
|
||||
|
||||
# Architecture specific extensions/prefixes
|
||||
*.[568vq]
|
||||
[568vq].out
|
||||
|
||||
*.cgo1.go
|
||||
*.cgo2.c
|
||||
_cgo_defun.c
|
||||
_cgo_gotypes.go
|
||||
_cgo_export.*
|
||||
|
||||
_testmain.go
|
||||
|
||||
*.exe
|
||||
*.test
|
||||
*.prof
|
||||
26
vendor/github.com/lpar/gzipped/LICENSE
generated
vendored
Normal file
26
vendor/github.com/lpar/gzipped/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,26 @@
|
||||
Copyright (c) 2016, IBM Corporation. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
|
||||
* Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
* Neither the name of IBM nor the names of project contributors may be used
|
||||
to endorse or promote products derived from this software without specific
|
||||
prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
73
vendor/github.com/lpar/gzipped/README.md
generated
vendored
Normal file
73
vendor/github.com/lpar/gzipped/README.md
generated
vendored
Normal file
@@ -0,0 +1,73 @@
|
||||
# gzipped.FileServer
|
||||
|
||||
Drop-in replacement for golang http.FileServer which supports gzipped static
|
||||
content.
|
||||
|
||||
This allows major bandwidth savings for CSS, JavaScript libraries, fonts, and
|
||||
other static compressible web content. It also means you can compress the
|
||||
content with zopfli without significant runtime penalty.
|
||||
|
||||
## Example
|
||||
|
||||
Suppose `/var/www/assets/css` contains your style sheets, and you want to make them available as `/css/*.css`:
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/lpar/gzipped"
|
||||
)
|
||||
|
||||
func main() {
|
||||
log.Fatal(http.ListenAndServe(":8080", http.StripPrefix("/css",
|
||||
gzipped.FileServer(http.Dir("/var/www/assets/css")))))
|
||||
}
|
||||
// curl localhost:8080/css/styles.css
|
||||
|
||||
|
||||
Using [httprouter](https://github.com/julienschmidt/httprouter)?
|
||||
|
||||
router := httprouter.New()
|
||||
router.Handler("GET", "/css/*filepath",
|
||||
gzipped.FileServer(http.Dir("/var/www/assets/css"))))
|
||||
log.Fatal(http.ListenAndServe(":8080", router)
|
||||
|
||||
## Detail
|
||||
|
||||
For any given request at `/path/filename.ext`, if:
|
||||
|
||||
1. the client will accept gzipped content, and
|
||||
2. there exists a file named `/path/filename.ext.gz` (starting from the
|
||||
appropriate base directory), and
|
||||
3. the file can be opened,
|
||||
|
||||
then the compressed file will be served as `/path/filename.ext`, with a
|
||||
`Content-Encoding` header set so that the client transparently decompresses it.
|
||||
Otherwise, the request is passed through and handled unchanged.
|
||||
|
||||
Unlike other similar code I found, this package has a license, parses
|
||||
Accept-Encoding headers properly, and has unit tests.
|
||||
|
||||
## Caveats
|
||||
|
||||
All requests are passed to Go's standard `http.ServeContent` method for
|
||||
fulfilment. MIME type sniffing, accept ranges, content negotiation and other
|
||||
tricky details are handled by that method.
|
||||
|
||||
It is up to you to ensure that your compressed and uncompressed resources are
|
||||
kept in sync.
|
||||
|
||||
Directory browsing isn't supported. (You probably don't want it on your
|
||||
application anyway, and if you do then you probably don't want Go's default
|
||||
implementation.)
|
||||
|
||||
## Related
|
||||
|
||||
* You might consider precompressing your CSS with [minify](https://github.com/tdewolff/minify).
|
||||
|
||||
* If you want to get the best possible compression, use [zopfli](https://github.com/google/zopfli).
|
||||
|
||||
* To compress your dynamically-generated HTML pages on the fly, I suggest [gziphandler](https://github.com/NYTimes/gziphandler).
|
||||
|
||||
106
vendor/github.com/lpar/gzipped/fileserver.go
generated
vendored
Normal file
106
vendor/github.com/lpar/gzipped/fileserver.go
generated
vendored
Normal file
@@ -0,0 +1,106 @@
|
||||
package gzipped
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"github.com/golang/gddo/httputil/header"
|
||||
)
|
||||
|
||||
type fileHandler struct {
|
||||
root http.FileSystem
|
||||
}
|
||||
|
||||
// FileServer is a drop-in replacement for Go's standard http.FileServer
|
||||
// which adds support for static resources precompressed with gzip, at
|
||||
// the cost of removing the support for directory browsing.
|
||||
//
|
||||
// If file filename.ext has a compressed version filename.ext.gz alongside
|
||||
// it, if the client indicates that it accepts gzip-compressed data, and
|
||||
// if the .gz file can be opened, then the compressed version of the file
|
||||
// will be sent to the client. Otherwise the request is passed on to
|
||||
// http.ServeContent, and the raw (uncompressed) version is used.
|
||||
//
|
||||
// It is up to you to ensure that the compressed and uncompressed versions
|
||||
// of files match and have sensible timestamps.
|
||||
//
|
||||
// Compressed or not, requests are fulfilled using http.ServeContent, and
|
||||
// details like accept ranges and content-type sniffing are handled by that
|
||||
// method.
|
||||
func FileServer(root http.FileSystem) http.Handler {
|
||||
return &fileHandler{root}
|
||||
}
|
||||
|
||||
func gzipAcceptable(r *http.Request) bool {
|
||||
for _, aspec := range header.ParseAccept(r.Header, "Accept-Encoding") {
|
||||
if aspec.Value == "gzip" && aspec.Q == 0.0 {
|
||||
return false
|
||||
}
|
||||
if (aspec.Value == "gzip" || aspec.Value == "*") && aspec.Q > 0.0 {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (f *fileHandler) openAndStat(path string) (http.File, os.FileInfo, error) {
|
||||
file, err := f.root.Open(path)
|
||||
var info os.FileInfo
|
||||
// This slightly weird variable reuse is so we can get 100% test coverage
|
||||
// without having to come up with a test file that can be opened, yet
|
||||
// fails to stat.
|
||||
if err == nil {
|
||||
info, err = file.Stat()
|
||||
}
|
||||
if err != nil {
|
||||
return file, nil, err
|
||||
}
|
||||
if info.IsDir() {
|
||||
return file, nil, fmt.Errorf("%s is directory", path)
|
||||
}
|
||||
return file, info, nil
|
||||
}
|
||||
|
||||
func (f *fileHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
upath := r.URL.Path
|
||||
if !strings.HasPrefix(upath, "/") {
|
||||
upath = "/" + upath
|
||||
r.URL.Path = upath
|
||||
}
|
||||
fpath := path.Clean(upath)
|
||||
if strings.HasSuffix(fpath, "/") {
|
||||
// If you wanted to put back directory browsing support, this is
|
||||
// where you'd do it.
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
// Try for a compressed version if appropriate
|
||||
var file http.File
|
||||
var err error
|
||||
var info os.FileInfo
|
||||
var gzip bool
|
||||
if gzipAcceptable(r) {
|
||||
gzpath := fpath + ".gz"
|
||||
file, info, err = f.openAndStat(gzpath)
|
||||
if err == nil {
|
||||
gzip = true
|
||||
}
|
||||
}
|
||||
// If we didn't manage to open a compressed version, try for uncompressed
|
||||
if !gzip {
|
||||
file, info, err = f.openAndStat(fpath)
|
||||
}
|
||||
if err != nil {
|
||||
// Doesn't exist compressed or uncompressed
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
if gzip {
|
||||
w.Header().Set("Content-Encoding", "gzip")
|
||||
}
|
||||
defer file.Close()
|
||||
http.ServeContent(w, r, fpath, info.ModTime(), file)
|
||||
}
|
||||
Reference in New Issue
Block a user