forked from MetaCubeX/quic-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.go
165 lines (140 loc) · 3.02 KB
/
server.go
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package http09
import (
"context"
"errors"
"io"
"log"
"net"
"net/http"
"net/url"
"runtime"
"strings"
"sync"
"github.com/daeuniverse/quic-go"
)
const h09alpn = "hq-interop"
type responseWriter struct {
io.Writer
headers http.Header
}
var _ http.ResponseWriter = &responseWriter{}
func (w *responseWriter) Header() http.Header {
if w.headers == nil {
w.headers = make(http.Header)
}
return w.headers
}
func (w *responseWriter) WriteHeader(int) {}
// Server is a HTTP/0.9 server listening for QUIC connections.
type Server struct {
*http.Server
ForceRetry bool
QuicConfig *quic.Config
mutex sync.Mutex
listener *quic.EarlyListener
}
// Close closes the server.
func (s *Server) Close() error {
s.mutex.Lock()
defer s.mutex.Unlock()
return s.listener.Close()
}
// ListenAndServe listens and serves HTTP/0.9 over QUIC.
func (s *Server) ListenAndServe() error {
if s.Server == nil {
return errors.New("use of http3.Server without http.Server")
}
udpAddr, err := net.ResolveUDPAddr("udp", s.Addr)
if err != nil {
return err
}
conn, err := net.ListenUDP("udp", udpAddr)
if err != nil {
return err
}
tlsConf := s.TLSConfig.Clone()
tlsConf.NextProtos = []string{h09alpn}
tr := quic.Transport{Conn: conn}
if s.ForceRetry {
tr.VerifySourceAddress = func(net.Addr) bool { return true }
}
ln, err := tr.ListenEarly(tlsConf, s.QuicConfig)
if err != nil {
return err
}
s.mutex.Lock()
s.listener = ln
s.mutex.Unlock()
for {
conn, err := ln.Accept(context.Background())
if err != nil {
return err
}
go s.handleConn(conn)
}
}
func (s *Server) handleConn(conn quic.Connection) {
for {
str, err := conn.AcceptStream(context.Background())
if err != nil {
log.Printf("Error accepting stream: %s\n", err.Error())
return
}
go func() {
if err := s.handleStream(str); err != nil {
log.Printf("Handling stream failed: %s\n", err.Error())
}
}()
}
}
func (s *Server) handleStream(str quic.Stream) error {
reqBytes, err := io.ReadAll(str)
if err != nil {
return err
}
request := string(reqBytes)
request = strings.TrimRight(request, "\r\n")
request = strings.TrimRight(request, " ")
log.Printf("Received request: %s\n", request)
if request[:5] != "GET /" {
str.CancelWrite(42)
return nil
}
u, err := url.Parse(request[4:])
if err != nil {
return err
}
u.Scheme = "https"
req := &http.Request{
Method: http.MethodGet,
Proto: "HTTP/0.9",
ProtoMajor: 0,
ProtoMinor: 9,
Body: str,
URL: u,
}
handler := s.Handler
if handler == nil {
handler = http.DefaultServeMux
}
var panicked bool
func() {
defer func() {
if p := recover(); p != nil {
// Copied from net/http/server.go
const size = 64 << 10
buf := make([]byte, size)
buf = buf[:runtime.Stack(buf, false)]
log.Printf("http: panic serving: %v\n%s", p, buf)
panicked = true
}
}()
handler.ServeHTTP(&responseWriter{Writer: str}, req)
}()
if panicked {
if _, err := str.Write([]byte("500")); err != nil {
return err
}
}
return str.Close()
}