-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
66 lines (55 loc) · 1.64 KB
/
Copy pathserver.go
File metadata and controls
66 lines (55 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright (c) 2013, Daniel Morsing
// For more information, see the LICENSE file
// package spdy implements a HTTP server on top of code.google.com/p/go.net/spdy.
//
// This package provides a http server that uses the TLSNextProto feature in net/http to serve connections that where negotiated to be SPDY 3.
// The HTTPS fallback feature can be disabled by providing a TLS config which does not include
// http/1.1 in its valid NPN protocols.
//
// TODO: Server Push, Client Certificate validation
package spdy
import (
"crypto/tls"
"log"
"net/http"
)
// Server defines the parameters for running a SPDY server.
type Server struct {
http.Server
}
// ListenAndServeTLS starts a SPDY forwarding server, using the parameters in the embedded
// http.Server.
//
// certFile and keyFile must be filenames to a pair of valid certificate and key.
func (srv *Server) ListenAndServeTLS(certFile, keyFile string) error {
hs := &srv.Server
config := &tls.Config{}
if hs.TLSConfig == nil {
hs.TLSConfig = config
} else {
config = hs.TLSConfig
}
srv.makeNPN(config)
if hs.TLSNextProto == nil {
hs.TLSNextProto = make(map[string]func(*http.Server, *tls.Conn, http.Handler))
}
if _, ok := hs.TLSNextProto["spdy/3"]; !ok {
hs.TLSNextProto["spdy/3"] = srv.servespdy
}
return hs.ListenAndServeTLS(certFile, keyFile)
}
func (srv *Server) makeNPN(config *tls.Config) {
np := config.NextProtos
if np == nil {
config.NextProtos = []string{"spdy/3", "http/1.1"}
}
return
}
func (srv *Server) servespdy(s *http.Server, conn *tls.Conn, hnd http.Handler) {
sess, err := newSession(s, conn, hnd)
if err != nil {
log.Println(err)
return
}
sess.serve()
}