Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions caddyhttp/httpserver/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func init() {
flag.StringVar(&Port, "port", DefaultPort, "Default port")
flag.StringVar(&Root, "root", DefaultRoot, "Root path of default site")
flag.BoolVar(&HTTP2, "http2", true, "Use HTTP/2")
flag.BoolVar(&QUIC, "quic", false, "Use experimental QUIC")

caddy.RegisterServerType(serverType, caddy.ServerType{
Directives: directives,
Expand Down Expand Up @@ -322,4 +323,7 @@ var (

// HTTP2 indicates whether HTTP2 is enabled or not.
HTTP2 bool

// QUIC indicates whether QUIC is enabled or not.
QUIC bool
)
29 changes: 28 additions & 1 deletion caddyhttp/httpserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"sync"
"time"

"github.com/lucas-clemente/quic-go/h2quic"
"github.com/mholt/caddy2/caddyhttp/staticfiles"
"github.com/mholt/caddy2/caddytls"
)
Expand Down Expand Up @@ -68,6 +69,7 @@ func (s SiteConfig) Port() string {
// Server is the HTTP server implementation.
type Server struct {
Server *http.Server
quicServer *h2quic.Server
listener net.Listener
listenerMu sync.Mutex // TODO: How necessary is this?
connTimeout time.Duration // max time to wait for a connection before force stop
Expand Down Expand Up @@ -96,6 +98,13 @@ func NewServer(addr string, group []*SiteConfig) (*Server, error) {
s.Server.TLSNextProto = make(map[string]func(*http.Server, *tls.Conn, http.Handler))
}

// Enable QUIC if desired
if QUIC {
s.quicServer = &h2quic.Server{
Server: s.Server,
}
}

// We have to bound our wg with one increment
// to prevent a "race condition" that is hard-coded
// into sync.WaitGroup.Wait() - basically, an add
Expand Down Expand Up @@ -187,7 +196,20 @@ func (s *Server) Serve(ln net.Listener) error {
go runTLSTicketKeyRotation(s.Server.TLSConfig, timer, s.tlsGovChan)
}

return s.Server.Serve(ln)
if QUIC {
go func() {
err := s.quicServer.ListenAndServe()
if err != nil {
fmt.Printf("Error listening for QUIC connections: %s", err.Error())
Copy link
Member

@mholt mholt May 31, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed this to log.Printf in my version.

But since this PR will not actually be merged (I'll be deleting this branch closer to when 0.9 betas roll out) it doesn't matter! 😉

}
}()
}

err := s.Server.Serve(ln)
if QUIC {
s.quicServer.Close()
}
return err
}

// ServeHTTP is the entry point of all HTTP requests.
Expand All @@ -203,6 +225,11 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {

w.Header().Set("Server", "Caddy")

if QUIC {
// Set the Alternate-Protocol and Alt-Svc headers for QUIC
s.quicServer.SetQuicHeaders(w.Header())
}

sanitizePath(r)

status, _ := s.serveHTTP(w, r)
Expand Down