Skip to content

Instantly share code, notes, and snippets.

@suconghou
Created February 6, 2018 03:12
Show Gist options
  • Save suconghou/50dd30df10bd6faa2abe1805afe3216f to your computer and use it in GitHub Desktop.
Save suconghou/50dd30df10bd6faa2abe1805afe3216f to your computer and use it in GitHub Desktop.
golang http2 invalid Connection request header
package main
import (
"fmt"
"io"
"net/http"
"os"
"time"
)
func main() {
port := 8080
http.HandleFunc("/", routeMatch)
http.ListenAndServe(fmt.Sprintf("%s:%d", os.Getenv("HOST"), port), nil)
}
func routeMatch(w http.ResponseWriter, r *http.Request) {
url := "https://http2.akamai.com/demo/" // this changes according to request
var timeout int64 = 5
fmt.Println(r.Header)
resp, err := NewClient(url, r.Method, r.Header, timeout, r.Body, nil)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer resp.Body.Close()
out := w.Header()
for key, value := range resp.Header {
out.Add(key, value[0])
}
w.WriteHeader(resp.StatusCode)
io.Copy(w, resp.Body)
}
// NewClient is a http client and do a request
func NewClient(url string, method string, reqHeader http.Header, timeout int64, body io.Reader, transport *http.Transport) (*http.Response, error) {
var client *http.Client
if transport != nil {
client = &http.Client{Timeout: time.Duration(timeout) * time.Second, Transport: transport}
} else {
client = &http.Client{Timeout: time.Duration(timeout) * time.Second}
}
req, err := http.NewRequest(method, url, body)
if err != nil {
return nil, err
}
req.Header = reqHeader
return client.Do(req)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment