h2demo.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. // Copyright 2014 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // +build h2demo
  5. package main
  6. import (
  7. "bytes"
  8. "context"
  9. "crypto/tls"
  10. "flag"
  11. "fmt"
  12. "hash/crc32"
  13. "image"
  14. "image/jpeg"
  15. "io"
  16. "io/ioutil"
  17. "log"
  18. "net"
  19. "net/http"
  20. "path"
  21. "regexp"
  22. "runtime"
  23. "strconv"
  24. "strings"
  25. "sync"
  26. "time"
  27. "cloud.google.com/go/storage"
  28. "go4.org/syncutil/singleflight"
  29. "golang.org/x/build/autocertcache"
  30. "golang.org/x/crypto/acme/autocert"
  31. "golang.org/x/net/http2"
  32. )
  33. var (
  34. prod = flag.Bool("prod", false, "Whether to configure itself to be the production http2.golang.org server.")
  35. httpsAddr = flag.String("https_addr", "localhost:4430", "TLS address to listen on ('host:port' or ':port'). Required.")
  36. httpAddr = flag.String("http_addr", "", "Plain HTTP address to listen on ('host:port', or ':port'). Empty means no HTTP.")
  37. hostHTTP = flag.String("http_host", "", "Optional host or host:port to use for http:// links to this service. By default, this is implied from -http_addr.")
  38. hostHTTPS = flag.String("https_host", "", "Optional host or host:port to use for http:// links to this service. By default, this is implied from -https_addr.")
  39. )
  40. func homeOldHTTP(w http.ResponseWriter, r *http.Request) {
  41. io.WriteString(w, `<html>
  42. <body>
  43. <h1>Go + HTTP/2</h1>
  44. <p>Welcome to <a href="https://golang.org/">the Go language</a>'s <a href="https://http2.github.io/">HTTP/2</a> demo & interop server.</p>
  45. <p>Unfortunately, you're <b>not</b> using HTTP/2 right now. To do so:</p>
  46. <ul>
  47. <li>Use Firefox Nightly or go to <b>about:config</b> and enable "network.http.spdy.enabled.http2draft"</li>
  48. <li>Use Google Chrome Canary and/or go to <b>chrome://flags/#enable-spdy4</b> to <i>Enable SPDY/4</i> (Chrome's name for HTTP/2)</li>
  49. </ul>
  50. <p>See code & instructions for connecting at <a href="https://github.com/golang/net/tree/master/http2">https://github.com/golang/net/tree/master/http2</a>.</p>
  51. </body></html>`)
  52. }
  53. func home(w http.ResponseWriter, r *http.Request) {
  54. if r.URL.Path != "/" {
  55. http.NotFound(w, r)
  56. return
  57. }
  58. io.WriteString(w, `<html>
  59. <body>
  60. <h1>Go + HTTP/2</h1>
  61. <p>Welcome to <a href="https://golang.org/">the Go language</a>'s <a
  62. href="https://http2.github.io/">HTTP/2</a> demo & interop server.</p>
  63. <p>Congratulations, <b>you're using HTTP/2 right now</b>.</p>
  64. <p>This server exists for others in the HTTP/2 community to test their HTTP/2 client implementations and point out flaws in our server.</p>
  65. <p>
  66. The code is at <a href="https://golang.org/x/net/http2">golang.org/x/net/http2</a> and
  67. is used transparently by the Go standard library from Go 1.6 and later.
  68. </p>
  69. <p>Contact info: <i>bradfitz@golang.org</i>, or <a
  70. href="https://golang.org/s/http2bug">file a bug</a>.</p>
  71. <h2>Handlers for testing</h2>
  72. <ul>
  73. <li>GET <a href="/reqinfo">/reqinfo</a> to dump the request + headers received</li>
  74. <li>GET <a href="/clockstream">/clockstream</a> streams the current time every second</li>
  75. <li>GET <a href="/gophertiles">/gophertiles</a> to see a page with a bunch of images</li>
  76. <li>GET <a href="/serverpush">/serverpush</a> to see a page with server push</li>
  77. <li>GET <a href="/file/gopher.png">/file/gopher.png</a> for a small file (does If-Modified-Since, Content-Range, etc)</li>
  78. <li>GET <a href="/file/go.src.tar.gz">/file/go.src.tar.gz</a> for a larger file (~10 MB)</li>
  79. <li>GET <a href="/redirect">/redirect</a> to redirect back to / (this page)</li>
  80. <li>GET <a href="/goroutines">/goroutines</a> to see all active goroutines in this server</li>
  81. <li>PUT something to <a href="/crc32">/crc32</a> to get a count of number of bytes and its CRC-32</li>
  82. <li>PUT something to <a href="/ECHO">/ECHO</a> and it will be streamed back to you capitalized</li>
  83. </ul>
  84. </body></html>`)
  85. }
  86. func reqInfoHandler(w http.ResponseWriter, r *http.Request) {
  87. w.Header().Set("Content-Type", "text/plain")
  88. fmt.Fprintf(w, "Method: %s\n", r.Method)
  89. fmt.Fprintf(w, "Protocol: %s\n", r.Proto)
  90. fmt.Fprintf(w, "Host: %s\n", r.Host)
  91. fmt.Fprintf(w, "RemoteAddr: %s\n", r.RemoteAddr)
  92. fmt.Fprintf(w, "RequestURI: %q\n", r.RequestURI)
  93. fmt.Fprintf(w, "URL: %#v\n", r.URL)
  94. fmt.Fprintf(w, "Body.ContentLength: %d (-1 means unknown)\n", r.ContentLength)
  95. fmt.Fprintf(w, "Close: %v (relevant for HTTP/1 only)\n", r.Close)
  96. fmt.Fprintf(w, "TLS: %#v\n", r.TLS)
  97. fmt.Fprintf(w, "\nHeaders:\n")
  98. r.Header.Write(w)
  99. }
  100. func crcHandler(w http.ResponseWriter, r *http.Request) {
  101. if r.Method != "PUT" {
  102. http.Error(w, "PUT required.", 400)
  103. return
  104. }
  105. crc := crc32.NewIEEE()
  106. n, err := io.Copy(crc, r.Body)
  107. if err == nil {
  108. w.Header().Set("Content-Type", "text/plain")
  109. fmt.Fprintf(w, "bytes=%d, CRC32=%x", n, crc.Sum(nil))
  110. }
  111. }
  112. type capitalizeReader struct {
  113. r io.Reader
  114. }
  115. func (cr capitalizeReader) Read(p []byte) (n int, err error) {
  116. n, err = cr.r.Read(p)
  117. for i, b := range p[:n] {
  118. if b >= 'a' && b <= 'z' {
  119. p[i] = b - ('a' - 'A')
  120. }
  121. }
  122. return
  123. }
  124. type flushWriter struct {
  125. w io.Writer
  126. }
  127. func (fw flushWriter) Write(p []byte) (n int, err error) {
  128. n, err = fw.w.Write(p)
  129. if f, ok := fw.w.(http.Flusher); ok {
  130. f.Flush()
  131. }
  132. return
  133. }
  134. func echoCapitalHandler(w http.ResponseWriter, r *http.Request) {
  135. if r.Method != "PUT" {
  136. http.Error(w, "PUT required.", 400)
  137. return
  138. }
  139. io.Copy(flushWriter{w}, capitalizeReader{r.Body})
  140. }
  141. var (
  142. fsGrp singleflight.Group
  143. fsMu sync.Mutex // guards fsCache
  144. fsCache = map[string]http.Handler{}
  145. )
  146. // fileServer returns a file-serving handler that proxies URL.
  147. // It lazily fetches URL on the first access and caches its contents forever.
  148. func fileServer(url string, latency time.Duration) http.Handler {
  149. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  150. if latency > 0 {
  151. time.Sleep(latency)
  152. }
  153. hi, err := fsGrp.Do(url, func() (interface{}, error) {
  154. fsMu.Lock()
  155. if h, ok := fsCache[url]; ok {
  156. fsMu.Unlock()
  157. return h, nil
  158. }
  159. fsMu.Unlock()
  160. res, err := http.Get(url)
  161. if err != nil {
  162. return nil, err
  163. }
  164. defer res.Body.Close()
  165. slurp, err := ioutil.ReadAll(res.Body)
  166. if err != nil {
  167. return nil, err
  168. }
  169. modTime := time.Now()
  170. var h http.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  171. http.ServeContent(w, r, path.Base(url), modTime, bytes.NewReader(slurp))
  172. })
  173. fsMu.Lock()
  174. fsCache[url] = h
  175. fsMu.Unlock()
  176. return h, nil
  177. })
  178. if err != nil {
  179. http.Error(w, err.Error(), 500)
  180. return
  181. }
  182. hi.(http.Handler).ServeHTTP(w, r)
  183. })
  184. }
  185. func clockStreamHandler(w http.ResponseWriter, r *http.Request) {
  186. clientGone := w.(http.CloseNotifier).CloseNotify()
  187. w.Header().Set("Content-Type", "text/plain")
  188. ticker := time.NewTicker(1 * time.Second)
  189. defer ticker.Stop()
  190. fmt.Fprintf(w, "# ~1KB of junk to force browsers to start rendering immediately: \n")
  191. io.WriteString(w, strings.Repeat("# xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n", 13))
  192. for {
  193. fmt.Fprintf(w, "%v\n", time.Now())
  194. w.(http.Flusher).Flush()
  195. select {
  196. case <-ticker.C:
  197. case <-clientGone:
  198. log.Printf("Client %v disconnected from the clock", r.RemoteAddr)
  199. return
  200. }
  201. }
  202. }
  203. func registerHandlers() {
  204. tiles := newGopherTilesHandler()
  205. push := newPushHandler()
  206. mux2 := http.NewServeMux()
  207. http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  208. switch {
  209. case r.URL.Path == "/gophertiles":
  210. tiles.ServeHTTP(w, r) // allow HTTP/2 + HTTP/1.x
  211. return
  212. case strings.HasPrefix(r.URL.Path, "/serverpush"):
  213. push.ServeHTTP(w, r) // allow HTTP/2 + HTTP/1.x
  214. return
  215. case r.TLS == nil: // do not allow HTTP/1.x for anything else
  216. http.Redirect(w, r, "https://"+httpsHost()+"/", http.StatusFound)
  217. return
  218. }
  219. if r.ProtoMajor == 1 {
  220. if r.URL.Path == "/reqinfo" {
  221. reqInfoHandler(w, r)
  222. return
  223. }
  224. homeOldHTTP(w, r)
  225. return
  226. }
  227. mux2.ServeHTTP(w, r)
  228. })
  229. mux2.HandleFunc("/", home)
  230. mux2.Handle("/file/gopher.png", fileServer("https://golang.org/doc/gopher/frontpage.png", 0))
  231. mux2.Handle("/file/go.src.tar.gz", fileServer("https://storage.googleapis.com/golang/go1.4.1.src.tar.gz", 0))
  232. mux2.HandleFunc("/reqinfo", reqInfoHandler)
  233. mux2.HandleFunc("/crc32", crcHandler)
  234. mux2.HandleFunc("/ECHO", echoCapitalHandler)
  235. mux2.HandleFunc("/clockstream", clockStreamHandler)
  236. mux2.Handle("/gophertiles", tiles)
  237. mux2.HandleFunc("/redirect", func(w http.ResponseWriter, r *http.Request) {
  238. http.Redirect(w, r, "/", http.StatusFound)
  239. })
  240. stripHomedir := regexp.MustCompile(`/(Users|home)/\w+`)
  241. mux2.HandleFunc("/goroutines", func(w http.ResponseWriter, r *http.Request) {
  242. w.Header().Set("Content-Type", "text/plain; charset=utf-8")
  243. buf := make([]byte, 2<<20)
  244. w.Write(stripHomedir.ReplaceAll(buf[:runtime.Stack(buf, true)], nil))
  245. })
  246. }
  247. var pushResources = map[string]http.Handler{
  248. "/serverpush/static/jquery.min.js": fileServer("https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js", 100*time.Millisecond),
  249. "/serverpush/static/godocs.js": fileServer("https://golang.org/lib/godoc/godocs.js", 100*time.Millisecond),
  250. "/serverpush/static/playground.js": fileServer("https://golang.org/lib/godoc/playground.js", 100*time.Millisecond),
  251. "/serverpush/static/style.css": fileServer("https://golang.org/lib/godoc/style.css", 100*time.Millisecond),
  252. }
  253. func newPushHandler() http.Handler {
  254. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  255. for path, handler := range pushResources {
  256. if r.URL.Path == path {
  257. handler.ServeHTTP(w, r)
  258. return
  259. }
  260. }
  261. cacheBust := time.Now().UnixNano()
  262. if pusher, ok := w.(http.Pusher); ok {
  263. for path := range pushResources {
  264. url := fmt.Sprintf("%s?%d", path, cacheBust)
  265. if err := pusher.Push(url, nil); err != nil {
  266. log.Printf("Failed to push %v: %v", path, err)
  267. }
  268. }
  269. }
  270. time.Sleep(100 * time.Millisecond) // fake network latency + parsing time
  271. if err := pushTmpl.Execute(w, struct {
  272. CacheBust int64
  273. HTTPSHost string
  274. HTTPHost string
  275. }{
  276. CacheBust: cacheBust,
  277. HTTPSHost: httpsHost(),
  278. HTTPHost: httpHost(),
  279. }); err != nil {
  280. log.Printf("Executing server push template: %v", err)
  281. }
  282. })
  283. }
  284. func newGopherTilesHandler() http.Handler {
  285. const gopherURL = "https://blog.golang.org/go-programming-language-turns-two_gophers.jpg"
  286. res, err := http.Get(gopherURL)
  287. if err != nil {
  288. log.Fatal(err)
  289. }
  290. if res.StatusCode != 200 {
  291. log.Fatalf("Error fetching %s: %v", gopherURL, res.Status)
  292. }
  293. slurp, err := ioutil.ReadAll(res.Body)
  294. res.Body.Close()
  295. if err != nil {
  296. log.Fatal(err)
  297. }
  298. im, err := jpeg.Decode(bytes.NewReader(slurp))
  299. if err != nil {
  300. if len(slurp) > 1024 {
  301. slurp = slurp[:1024]
  302. }
  303. log.Fatalf("Failed to decode gopher image: %v (got %q)", err, slurp)
  304. }
  305. type subImager interface {
  306. SubImage(image.Rectangle) image.Image
  307. }
  308. const tileSize = 32
  309. xt := im.Bounds().Max.X / tileSize
  310. yt := im.Bounds().Max.Y / tileSize
  311. var tile [][][]byte // y -> x -> jpeg bytes
  312. for yi := 0; yi < yt; yi++ {
  313. var row [][]byte
  314. for xi := 0; xi < xt; xi++ {
  315. si := im.(subImager).SubImage(image.Rectangle{
  316. Min: image.Point{xi * tileSize, yi * tileSize},
  317. Max: image.Point{(xi + 1) * tileSize, (yi + 1) * tileSize},
  318. })
  319. buf := new(bytes.Buffer)
  320. if err := jpeg.Encode(buf, si, &jpeg.Options{Quality: 90}); err != nil {
  321. log.Fatal(err)
  322. }
  323. row = append(row, buf.Bytes())
  324. }
  325. tile = append(tile, row)
  326. }
  327. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  328. ms, _ := strconv.Atoi(r.FormValue("latency"))
  329. const nanosPerMilli = 1e6
  330. if r.FormValue("x") != "" {
  331. x, _ := strconv.Atoi(r.FormValue("x"))
  332. y, _ := strconv.Atoi(r.FormValue("y"))
  333. if ms <= 1000 {
  334. time.Sleep(time.Duration(ms) * nanosPerMilli)
  335. }
  336. if x >= 0 && x < xt && y >= 0 && y < yt {
  337. http.ServeContent(w, r, "", time.Time{}, bytes.NewReader(tile[y][x]))
  338. return
  339. }
  340. }
  341. io.WriteString(w, "<html><body onload='showtimes()'>")
  342. fmt.Fprintf(w, "A grid of %d tiled images is below. Compare:<p>", xt*yt)
  343. for _, ms := range []int{0, 30, 200, 1000} {
  344. d := time.Duration(ms) * nanosPerMilli
  345. fmt.Fprintf(w, "[<a href='https://%s/gophertiles?latency=%d'>HTTP/2, %v latency</a>] [<a href='http://%s/gophertiles?latency=%d'>HTTP/1, %v latency</a>]<br>\n",
  346. httpsHost(), ms, d,
  347. httpHost(), ms, d,
  348. )
  349. }
  350. io.WriteString(w, "<p>\n")
  351. cacheBust := time.Now().UnixNano()
  352. for y := 0; y < yt; y++ {
  353. for x := 0; x < xt; x++ {
  354. fmt.Fprintf(w, "<img width=%d height=%d src='/gophertiles?x=%d&y=%d&cachebust=%d&latency=%d'>",
  355. tileSize, tileSize, x, y, cacheBust, ms)
  356. }
  357. io.WriteString(w, "<br/>\n")
  358. }
  359. io.WriteString(w, `<p><div id='loadtimes'></div></p>
  360. <script>
  361. function showtimes() {
  362. var times = 'Times from connection start:<br>'
  363. times += 'DOM loaded: ' + (window.performance.timing.domContentLoadedEventEnd - window.performance.timing.connectStart) + 'ms<br>'
  364. times += 'DOM complete (images loaded): ' + (window.performance.timing.domComplete - window.performance.timing.connectStart) + 'ms<br>'
  365. document.getElementById('loadtimes').innerHTML = times
  366. }
  367. </script>
  368. <hr><a href='/'>&lt;&lt Back to Go HTTP/2 demo server</a></body></html>`)
  369. })
  370. }
  371. func httpsHost() string {
  372. if *hostHTTPS != "" {
  373. return *hostHTTPS
  374. }
  375. if v := *httpsAddr; strings.HasPrefix(v, ":") {
  376. return "localhost" + v
  377. } else {
  378. return v
  379. }
  380. }
  381. func httpHost() string {
  382. if *hostHTTP != "" {
  383. return *hostHTTP
  384. }
  385. if v := *httpAddr; strings.HasPrefix(v, ":") {
  386. return "localhost" + v
  387. } else {
  388. return v
  389. }
  390. }
  391. func serveProdTLS(autocertManager *autocert.Manager) error {
  392. srv := &http.Server{
  393. TLSConfig: &tls.Config{
  394. GetCertificate: autocertManager.GetCertificate,
  395. },
  396. }
  397. http2.ConfigureServer(srv, &http2.Server{
  398. NewWriteScheduler: func() http2.WriteScheduler {
  399. return http2.NewPriorityWriteScheduler(nil)
  400. },
  401. })
  402. ln, err := net.Listen("tcp", ":443")
  403. if err != nil {
  404. return err
  405. }
  406. return srv.Serve(tls.NewListener(tcpKeepAliveListener{ln.(*net.TCPListener)}, srv.TLSConfig))
  407. }
  408. type tcpKeepAliveListener struct {
  409. *net.TCPListener
  410. }
  411. func (ln tcpKeepAliveListener) Accept() (c net.Conn, err error) {
  412. tc, err := ln.AcceptTCP()
  413. if err != nil {
  414. return
  415. }
  416. tc.SetKeepAlive(true)
  417. tc.SetKeepAlivePeriod(3 * time.Minute)
  418. return tc, nil
  419. }
  420. func serveProd() error {
  421. log.Printf("running in production mode")
  422. storageClient, err := storage.NewClient(context.Background())
  423. if err != nil {
  424. log.Fatalf("storage.NewClient: %v", err)
  425. }
  426. autocertManager := &autocert.Manager{
  427. Prompt: autocert.AcceptTOS,
  428. HostPolicy: autocert.HostWhitelist("http2.golang.org"),
  429. Cache: autocertcache.NewGoogleCloudStorageCache(storageClient, "golang-h2demo-autocert"),
  430. }
  431. errc := make(chan error, 2)
  432. go func() { errc <- http.ListenAndServe(":80", autocertManager.HTTPHandler(http.DefaultServeMux)) }()
  433. go func() { errc <- serveProdTLS(autocertManager) }()
  434. return <-errc
  435. }
  436. const idleTimeout = 5 * time.Minute
  437. const activeTimeout = 10 * time.Minute
  438. // TODO: put this into the standard library and actually send
  439. // PING frames and GOAWAY, etc: golang.org/issue/14204
  440. func idleTimeoutHook() func(net.Conn, http.ConnState) {
  441. var mu sync.Mutex
  442. m := map[net.Conn]*time.Timer{}
  443. return func(c net.Conn, cs http.ConnState) {
  444. mu.Lock()
  445. defer mu.Unlock()
  446. if t, ok := m[c]; ok {
  447. delete(m, c)
  448. t.Stop()
  449. }
  450. var d time.Duration
  451. switch cs {
  452. case http.StateNew, http.StateIdle:
  453. d = idleTimeout
  454. case http.StateActive:
  455. d = activeTimeout
  456. default:
  457. return
  458. }
  459. m[c] = time.AfterFunc(d, func() {
  460. log.Printf("closing idle conn %v after %v", c.RemoteAddr(), d)
  461. go c.Close()
  462. })
  463. }
  464. }
  465. func main() {
  466. var srv http.Server
  467. flag.BoolVar(&http2.VerboseLogs, "verbose", false, "Verbose HTTP/2 debugging.")
  468. flag.Parse()
  469. srv.Addr = *httpsAddr
  470. srv.ConnState = idleTimeoutHook()
  471. registerHandlers()
  472. if *prod {
  473. *hostHTTP = "http2.golang.org"
  474. *hostHTTPS = "http2.golang.org"
  475. log.Fatal(serveProd())
  476. }
  477. url := "https://" + httpsHost() + "/"
  478. log.Printf("Listening on " + url)
  479. http2.ConfigureServer(&srv, &http2.Server{})
  480. if *httpAddr != "" {
  481. go func() {
  482. log.Printf("Listening on http://" + httpHost() + "/ (for unencrypted HTTP/1)")
  483. log.Fatal(http.ListenAndServe(*httpAddr, nil))
  484. }()
  485. }
  486. go func() {
  487. log.Fatal(srv.ListenAndServeTLS("server.crt", "server.key"))
  488. }()
  489. select {}
  490. }