Created
February 7, 2017 09:24
-
-
Save onnimonni/03596be8884847ae023472191d1fa2bf to your computer and use it in GitHub Desktop.
Almost working http2 server push with Link headers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"log" | |
"net/http" | |
"net/http/httputil" | |
"net/url" | |
"github.com/tomnomnom/linkheader" | |
) | |
func main() { | |
log.Printf("Starting in port 8000...") | |
http.HandleFunc("/", someFunc) | |
log.Fatal(http.ListenAndServe(":8000", nil)) | |
} | |
func someFunc(w http.ResponseWriter, r *http.Request) { | |
r.Host = "localhost:8080" | |
u, _ := url.Parse("http://localhost:8080/") | |
proxy := httputil.NewSingleHostReverseProxy(u) | |
proxy.Transport = &myTransport{} | |
// TODO: add http.Pusher when response contains Link headers with 'preload' | |
proxy.ServeHTTP(w, r) | |
} | |
type myTransport struct { | |
} | |
func (t *myTransport) RoundTrip(request *http.Request) (*http.Response, error) { | |
response, err := http.DefaultTransport.RoundTrip(request) | |
links := linkheader.Parse( response.Header.Get("Link") ) | |
for _, link := range links { | |
if link.Rel == "preload" && len(link.URL) > 0 { | |
// How to return this information for someFunc() ? | |
log.Printf("Can server push: %s",link.URL) | |
} | |
} | |
return response, err | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment