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 package http2
27
28 import (
29 "bufio"
30 "bytes"
31 "context"
32 "crypto/rand"
33 "crypto/tls"
34 "errors"
35 "fmt"
36 "io"
37 "log"
38 "math"
39 "net"
40 "net/http/internal"
41 "net/http/internal/httpcommon"
42 "net/textproto"
43 "net/url"
44 "os"
45 "reflect"
46 "runtime"
47 "slices"
48 "strconv"
49 "strings"
50 "sync"
51 "time"
52
53 "golang.org/x/net/http/httpguts"
54 "golang.org/x/net/http2/hpack"
55 )
56
57 const (
58 prefaceTimeout = 10 * time.Second
59 firstSettingsTimeout = 2 * time.Second
60 handlerChunkWriteSize = 4 << 10
61 defaultMaxStreams = 250
62
63
64
65
66 maxQueuedControlFrames = 10000
67 )
68
69 var (
70 errClientDisconnected = errors.New("client disconnected")
71 errClosedBody = errors.New("body closed by handler")
72 errHandlerComplete = errors.New("http2: request body closed due to handler exiting")
73 errStreamClosed = errors.New("http2: stream closed")
74 )
75
76 var responseWriterStatePool = sync.Pool{
77 New: func() any {
78 rws := &responseWriterState{}
79 rws.bw = bufio.NewWriterSize(chunkWriter{rws}, handlerChunkWriteSize)
80 return rws
81 },
82 }
83
84
85 var (
86 testHookOnConn func()
87 testHookOnPanicMu *sync.Mutex
88 testHookOnPanic func(sc *serverConn, panicVal any) (rePanic bool)
89 )
90
91
92 type Server struct {
93 mu sync.Mutex
94 activeConns map[*serverConn]struct{}
95
96
97
98 errChanPool sync.Pool
99 }
100
101 func (s *Server) registerConn(sc *serverConn) {
102 if s == nil {
103 return
104 }
105 s.mu.Lock()
106 s.activeConns[sc] = struct{}{}
107 s.mu.Unlock()
108 }
109
110 func (s *Server) unregisterConn(sc *serverConn) {
111 if s == nil {
112 return
113 }
114 s.mu.Lock()
115 delete(s.activeConns, sc)
116 s.mu.Unlock()
117 }
118
119 func (s *Server) startGracefulShutdown() {
120 if s == nil {
121 return
122 }
123 s.mu.Lock()
124 for sc := range s.activeConns {
125 sc.startGracefulShutdown()
126 }
127 s.mu.Unlock()
128 }
129
130
131
132 var errChanPool = sync.Pool{
133 New: func() any { return make(chan error, 1) },
134 }
135
136 func (s *Server) getErrChan() chan error {
137 if s == nil {
138 return errChanPool.Get().(chan error)
139 }
140 return s.errChanPool.Get().(chan error)
141 }
142
143 func (s *Server) putErrChan(ch chan error) {
144 if s == nil {
145 errChanPool.Put(ch)
146 return
147 }
148 s.errChanPool.Put(ch)
149 }
150
151 func (s *Server) Configure(conf ServerConfig, tcfg *tls.Config) error {
152 s.activeConns = make(map[*serverConn]struct{})
153 s.errChanPool = sync.Pool{New: func() any { return make(chan error, 1) }}
154
155 if tcfg.CipherSuites != nil && tcfg.MinVersion < tls.VersionTLS13 {
156
157
158
159 haveRequired := false
160 for _, cs := range tcfg.CipherSuites {
161 switch cs {
162 case tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
163
164
165 tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256:
166 haveRequired = true
167 }
168 }
169 if !haveRequired {
170 return fmt.Errorf("http2: TLSConfig.CipherSuites is missing an HTTP/2-required AES_128_GCM_SHA256 cipher (need at least one of TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 or TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)")
171 }
172 }
173
174
175
176
177
178
179
180
181 return nil
182 }
183
184 func (s *Server) GracefulShutdown() {
185 s.startGracefulShutdown()
186 }
187
188
189 type ServeConnOpts struct {
190
191
192 Context context.Context
193
194
195
196 BaseConfig ServerConfig
197
198
199
200
201 Handler Handler
202
203
204
205 Settings []byte
206
207 UpgradeRequest *ServerRequest
208
209
210
211 SawClientPreface bool
212 }
213
214 func (o *ServeConnOpts) context() context.Context {
215 if o != nil && o.Context != nil {
216 return o.Context
217 }
218 return context.Background()
219 }
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235 func (s *Server) ServeConn(c net.Conn, opts *ServeConnOpts) {
236 if opts == nil {
237 opts = &ServeConnOpts{}
238 }
239
240 var newf func(*serverConn)
241 if inTests {
242
243 newf, _ = opts.Context.Value(NewConnContextKey).(func(*serverConn))
244 }
245
246 s.serveConn(c, opts, newf)
247 }
248
249 type contextKey string
250
251 var (
252 NewConnContextKey = new("NewConnContextKey")
253 ConnectionStateContextKey = new("ConnectionStateContextKey")
254 )
255
256 func (s *Server) serveConn(c net.Conn, opts *ServeConnOpts, newf func(*serverConn)) {
257 baseCtx, cancel := serverConnBaseContext(c, opts)
258 defer cancel()
259
260 conf := configFromServer(opts.BaseConfig)
261 sc := &serverConn{
262 srv: s,
263 hs: opts.BaseConfig,
264 conn: c,
265 baseCtx: baseCtx,
266 remoteAddrStr: c.RemoteAddr().String(),
267 bw: newBufferedWriter(c, conf.WriteByteTimeout),
268 handler: opts.Handler,
269 streams: make(map[uint32]*stream),
270 readFrameCh: make(chan readFrameResult),
271 wantWriteFrameCh: make(chan FrameWriteRequest, 8),
272 serveMsgCh: make(chan any, 8),
273 wroteFrameCh: make(chan frameWriteResult, 1),
274 bodyReadCh: make(chan bodyReadMsg),
275 doneServing: make(chan struct{}),
276 clientMaxStreams: math.MaxUint32,
277 advMaxStreams: uint32(conf.MaxConcurrentStreams),
278 initialStreamRecvWindowSize: int32(conf.MaxReceiveBufferPerStream),
279 maxFrameSize: initialMaxFrameSize,
280 pingTimeout: conf.PingTimeout,
281 countErrorFunc: conf.CountError,
282 serveG: newGoroutineLock(),
283 pushEnabled: true,
284 sawClientPreface: opts.SawClientPreface,
285 }
286 if newf != nil {
287 newf(sc)
288 }
289
290 s.registerConn(sc)
291 defer s.unregisterConn(sc)
292
293 switch {
294 case sc.hs.DisableClientPriority():
295 sc.writeSched = newRoundRobinWriteScheduler()
296 default:
297 sc.writeSched = newPriorityWriteSchedulerRFC9218()
298 }
299
300
301
302
303 sc.flow.init()
304 sc.inflow.init(initialWindowSize)
305 sc.hpackEncoder = hpack.NewEncoder(&sc.headerWriteBuf)
306 sc.hpackEncoder.SetMaxDynamicTableSizeLimit(uint32(conf.MaxEncoderHeaderTableSize))
307
308 fr := NewFramer(sc.bw, c)
309 if conf.CountError != nil {
310 fr.countError = conf.CountError
311 }
312 fr.ReadMetaHeaders = hpack.NewDecoder(uint32(conf.MaxDecoderHeaderTableSize), nil)
313 fr.MaxHeaderListSize = sc.maxHeaderListSize()
314 fr.MaxHeaderValueCount = sc.hs.MaxHeaderValueCount()
315 fr.SetMaxReadFrameSize(uint32(conf.MaxReadFrameSize))
316 sc.framer = fr
317
318 if tc, ok := c.(connectionStater); ok {
319 sc.tlsState = new(tls.ConnectionState)
320 *sc.tlsState = tc.ConnectionState()
321
322
323 if inTests {
324 f, ok := opts.Context.Value(ConnectionStateContextKey).(func() tls.ConnectionState)
325 if ok {
326 *sc.tlsState = f()
327 }
328 }
329
330
331
332
333
334
335
336
337
338
339
340 if sc.tlsState.Version < tls.VersionTLS12 {
341 sc.rejectConn(ErrCodeInadequateSecurity, "TLS version too low")
342 return
343 }
344
345 if sc.tlsState.ServerName == "" {
346
347
348
349
350
351
352
353
354
355 }
356
357 if !conf.PermitProhibitedCipherSuites && isBadCipher(sc.tlsState.CipherSuite) {
358
359
360
361
362
363
364
365
366
367
368 sc.rejectConn(ErrCodeInadequateSecurity, fmt.Sprintf("Prohibited TLS 1.2 Cipher Suite: %x", sc.tlsState.CipherSuite))
369 return
370 }
371 }
372
373 if opts.Settings != nil {
374 fr := &SettingsFrame{
375 FrameHeader: FrameHeader{valid: true},
376 p: opts.Settings,
377 }
378 if err := fr.ForeachSetting(sc.processSetting); err != nil {
379 sc.rejectConn(ErrCodeProtocol, "invalid settings")
380 return
381 }
382 opts.Settings = nil
383 }
384
385 if opts.UpgradeRequest != nil {
386 sc.upgradeRequest(opts.UpgradeRequest)
387 opts.UpgradeRequest = nil
388 }
389
390 sc.serve(conf)
391 }
392
393 func serverConnBaseContext(c net.Conn, opts *ServeConnOpts) (ctx context.Context, cancel func()) {
394 return context.WithCancel(opts.context())
395 }
396
397 func (sc *serverConn) rejectConn(err ErrCode, debug string) {
398 sc.vlogf("http2: server rejecting conn: %v, %s", err, debug)
399
400 sc.framer.WriteGoAway(0, err, []byte(debug))
401 sc.bw.Flush()
402 sc.conn.Close()
403 }
404
405 type serverConn struct {
406
407 srv *Server
408 hs ServerConfig
409 conn net.Conn
410 bw *bufferedWriter
411 handler Handler
412 baseCtx context.Context
413 framer *Framer
414 doneServing chan struct{}
415 readFrameCh chan readFrameResult
416 wantWriteFrameCh chan FrameWriteRequest
417 wroteFrameCh chan frameWriteResult
418 bodyReadCh chan bodyReadMsg
419 serveMsgCh chan any
420 flow connOutflow
421 inflow inflow
422 tlsState *tls.ConnectionState
423 remoteAddrStr string
424 writeSched WriteScheduler
425 countErrorFunc func(errType string)
426
427
428 serveG goroutineLock
429 pushEnabled bool
430 sawClientPreface bool
431 sawFirstSettings bool
432 needToSendSettingsAck bool
433 unackedSettings int
434 pendingEncoderTableSize bool
435 encoderTableSizeMin uint32
436 encoderTableSize uint32
437 queuedControlFrames int
438 clientMaxStreams uint32
439 advMaxStreams uint32
440 curClientStreams uint32
441 curPushedStreams uint32
442 curHandlers uint32
443 maxClientStreamID uint32
444 maxPushPromiseID uint32
445 streams map[uint32]*stream
446 unstartedHandlers []unstartedHandler
447 initialStreamRecvWindowSize int32
448 maxFrameSize int32
449 peerMaxHeaderListSize uint32
450 canonHeader map[string]string
451 canonHeaderKeysSize int
452 writingFrame bool
453 writingFrameAsync bool
454 needsFrameFlush bool
455 inGoAway bool
456 inFrameScheduleLoop bool
457 needToSendGoAway bool
458 pingSent bool
459 sentPingData [8]byte
460 goAwayCode ErrCode
461 shutdownTimer *time.Timer
462 idleTimer *time.Timer
463 readIdleTimeout time.Duration
464 pingTimeout time.Duration
465 readIdleTimer *time.Timer
466
467
468 headerWriteBuf bytes.Buffer
469 hpackEncoder *hpack.Encoder
470
471
472 shutdownOnce sync.Once
473
474
475 hasIntermediary bool
476 priorityAware bool
477 }
478
479 func (sc *serverConn) writeSchedIgnoresRFC7540() bool {
480 switch sc.writeSched.(type) {
481 case *priorityWriteSchedulerRFC9218:
482 return true
483 case *roundRobinWriteScheduler:
484 return true
485 default:
486 return false
487 }
488 }
489
490 const DefaultMaxHeaderBytes = 1 << 20
491
492 func (sc *serverConn) maxHeaderListSize() uint32 {
493 n := sc.hs.MaxHeaderBytes()
494 if n <= 0 {
495 n = DefaultMaxHeaderBytes
496 }
497 return uint32(adjustHTTP1MaxHeaderSize(int64(n)))
498 }
499
500 func (sc *serverConn) curOpenStreams() uint32 {
501 sc.serveG.check()
502 return sc.curClientStreams + sc.curPushedStreams
503 }
504
505
506
507
508
509
510
511
512 type stream struct {
513
514 sc *serverConn
515 id uint32
516 body *pipe
517 reqBody *requestBody
518 cw closeWaiter
519 ctx context.Context
520 cancelCtx func()
521
522
523 bodyBytes int64
524 declBodyBytes int64
525 flow outflow
526 inflow inflow
527 state streamState
528 resetQueued bool
529 gotTrailerHeader bool
530 wroteHeaders bool
531 readDeadline *time.Timer
532 writeDeadline *time.Timer
533 closeErr error
534
535 trailer Header
536 reqTrailer Header
537 }
538
539 func (sc *serverConn) Framer() *Framer { return sc.framer }
540 func (sc *serverConn) CloseConn() error { return sc.conn.Close() }
541 func (sc *serverConn) Flush() error { return sc.bw.Flush() }
542 func (sc *serverConn) HeaderEncoder() (*hpack.Encoder, *bytes.Buffer) {
543 return sc.hpackEncoder, &sc.headerWriteBuf
544 }
545
546 func (sc *serverConn) state(streamID uint32) (streamState, *stream) {
547 sc.serveG.check()
548
549 if st, ok := sc.streams[streamID]; ok {
550 return st.state, st
551 }
552
553
554
555
556
557
558 if streamID%2 == 1 {
559 if streamID <= sc.maxClientStreamID {
560 return stateClosed, nil
561 }
562 } else {
563 if streamID <= sc.maxPushPromiseID {
564 return stateClosed, nil
565 }
566 }
567 return stateIdle, nil
568 }
569
570
571
572
573 func (sc *serverConn) setConnState(state ConnState) {
574 sc.hs.ConnState(sc.conn, state)
575 }
576
577 func (sc *serverConn) vlogf(format string, args ...any) {
578 if VerboseLogs {
579 sc.logf(format, args...)
580 }
581 }
582
583 func (sc *serverConn) logf(format string, args ...any) {
584 if lg := sc.hs.ErrorLog(); lg != nil {
585 lg.Printf(format, args...)
586 } else {
587 log.Printf(format, args...)
588 }
589 }
590
591
592
593
594
595 func errno(v error) uintptr {
596 if rv := reflect.ValueOf(v); rv.Kind() == reflect.Uintptr {
597 return uintptr(rv.Uint())
598 }
599 return 0
600 }
601
602
603
604 func isClosedConnError(err error) bool {
605 if err == nil {
606 return false
607 }
608
609 if errors.Is(err, net.ErrClosed) {
610 return true
611 }
612
613
614
615
616
617 if runtime.GOOS == "windows" {
618 if oe, ok := err.(*net.OpError); ok && oe.Op == "read" {
619 if se, ok := oe.Err.(*os.SyscallError); ok && se.Syscall == "wsarecv" {
620 const WSAECONNABORTED = 10053
621 const WSAECONNRESET = 10054
622 if n := errno(se.Err); n == WSAECONNRESET || n == WSAECONNABORTED {
623 return true
624 }
625 }
626 }
627 }
628 return false
629 }
630
631 func (sc *serverConn) condlogf(err error, format string, args ...any) {
632 if err == nil {
633 return
634 }
635 if err == io.EOF || err == io.ErrUnexpectedEOF || isClosedConnError(err) || err == errPrefaceTimeout {
636
637 sc.vlogf(format, args...)
638 } else {
639 sc.logf(format, args...)
640 }
641 }
642
643
644
645
646
647
648 const maxCachedCanonicalHeadersKeysSize = 2048
649
650 func (sc *serverConn) canonicalHeader(v string) string {
651 sc.serveG.check()
652 cv, ok := httpcommon.CachedCanonicalHeader(v)
653 if ok {
654 return cv
655 }
656 cv, ok = sc.canonHeader[v]
657 if ok {
658 return cv
659 }
660 if sc.canonHeader == nil {
661 sc.canonHeader = make(map[string]string)
662 }
663 cv = textproto.CanonicalMIMEHeaderKey(v)
664 size := 100 + len(v)*2
665 if sc.canonHeaderKeysSize+size <= maxCachedCanonicalHeadersKeysSize {
666 sc.canonHeader[v] = cv
667 sc.canonHeaderKeysSize += size
668 }
669 return cv
670 }
671
672 type readFrameResult struct {
673 f Frame
674 err error
675
676
677
678
679 readMore func()
680 }
681
682
683
684
685
686 func (sc *serverConn) readFrames() {
687 gate := make(chan struct{})
688 gateDone := func() { gate <- struct{}{} }
689 for {
690 f, err := sc.framer.ReadFrame()
691 select {
692 case sc.readFrameCh <- readFrameResult{f, err, gateDone}:
693 case <-sc.doneServing:
694 return
695 }
696 select {
697 case <-gate:
698 case <-sc.doneServing:
699 return
700 }
701 if terminalReadFrameError(err) {
702 return
703 }
704 }
705 }
706
707
708 type frameWriteResult struct {
709 _ incomparable
710 wr FrameWriteRequest
711 err error
712 }
713
714
715
716
717
718 func (sc *serverConn) writeFrameAsync(wr FrameWriteRequest, wd *writeData) {
719 var err error
720 if wd == nil {
721 err = wr.write.writeFrame(sc)
722 } else {
723 err = sc.framer.endWrite()
724 }
725 sc.wroteFrameCh <- frameWriteResult{wr: wr, err: err}
726 }
727
728 func (sc *serverConn) closeAllStreamsOnConnClose() {
729 sc.serveG.check()
730 for _, st := range sc.streams {
731 sc.closeStream(st, errClientDisconnected)
732 }
733 }
734
735 func (sc *serverConn) stopShutdownTimer() {
736 sc.serveG.check()
737 if t := sc.shutdownTimer; t != nil {
738 t.Stop()
739 }
740 }
741
742 func (sc *serverConn) notePanic() {
743
744 if testHookOnPanicMu != nil {
745 testHookOnPanicMu.Lock()
746 defer testHookOnPanicMu.Unlock()
747 }
748 if testHookOnPanic != nil {
749 if e := recover(); e != nil {
750 if testHookOnPanic(sc, e) {
751 panic(e)
752 }
753 }
754 }
755 }
756
757 func (sc *serverConn) serve(conf Config) {
758 sc.serveG.check()
759 defer sc.notePanic()
760 defer sc.conn.Close()
761 defer sc.closeAllStreamsOnConnClose()
762 defer sc.stopShutdownTimer()
763 defer close(sc.doneServing)
764
765 if VerboseLogs {
766 sc.vlogf("http2: server connection from %v on %p", sc.conn.RemoteAddr(), sc.hs)
767 }
768
769 settings := writeSettings{
770 {SettingMaxFrameSize, uint32(conf.MaxReadFrameSize)},
771 {SettingMaxConcurrentStreams, sc.advMaxStreams},
772 {SettingMaxHeaderListSize, sc.maxHeaderListSize()},
773 {SettingHeaderTableSize, uint32(conf.MaxDecoderHeaderTableSize)},
774 {SettingInitialWindowSize, uint32(sc.initialStreamRecvWindowSize)},
775 }
776 if !disableExtendedConnectProtocol {
777 settings = append(settings, Setting{SettingEnableConnectProtocol, 1})
778 }
779 if sc.writeSchedIgnoresRFC7540() {
780 settings = append(settings, Setting{SettingNoRFC7540Priorities, 1})
781 }
782 sc.writeFrame(FrameWriteRequest{
783 write: settings,
784 })
785 sc.unackedSettings++
786
787
788
789 if diff := conf.MaxReceiveBufferPerConnection - initialWindowSize; diff > 0 {
790 sc.sendWindowUpdate(nil, int(diff))
791 }
792
793 if err := sc.readPreface(); err != nil {
794 sc.condlogf(err, "http2: server: error reading preface from client %v: %v", sc.conn.RemoteAddr(), err)
795 return
796 }
797
798
799
800
801 sc.setConnState(ConnStateActive)
802 sc.setConnState(ConnStateIdle)
803
804 if idle := sc.hs.IdleTimeout(); idle > 0 {
805 sc.idleTimer = time.AfterFunc(idle, sc.onIdleTimer)
806 defer sc.idleTimer.Stop()
807 }
808
809 if conf.SendPingTimeout > 0 {
810 sc.readIdleTimeout = conf.SendPingTimeout
811 sc.readIdleTimer = time.AfterFunc(conf.SendPingTimeout, sc.onReadIdleTimer)
812 defer sc.readIdleTimer.Stop()
813 }
814
815 go sc.readFrames()
816
817 settingsTimer := time.AfterFunc(firstSettingsTimeout, sc.onSettingsTimer)
818 defer settingsTimer.Stop()
819
820 lastFrameTime := time.Now()
821 loopNum := 0
822 for {
823 loopNum++
824 select {
825 case wr := <-sc.wantWriteFrameCh:
826 if se, ok := wr.write.(StreamError); ok {
827 sc.resetStream(se)
828 break
829 }
830 sc.writeFrame(wr)
831 case res := <-sc.wroteFrameCh:
832 sc.wroteFrame(res)
833 case res := <-sc.readFrameCh:
834 lastFrameTime = time.Now()
835
836
837 if sc.writingFrameAsync {
838 select {
839 case wroteRes := <-sc.wroteFrameCh:
840 sc.wroteFrame(wroteRes)
841 default:
842 }
843 }
844 if !sc.processFrameFromReader(res) {
845 return
846 }
847 res.readMore()
848 if settingsTimer != nil {
849 settingsTimer.Stop()
850 settingsTimer = nil
851 }
852 case m := <-sc.bodyReadCh:
853 sc.noteBodyRead(m.st, m.n)
854 case msg := <-sc.serveMsgCh:
855 switch v := msg.(type) {
856 case func(int):
857 v(loopNum)
858 case *serverMessage:
859 switch v {
860 case settingsTimerMsg:
861 sc.logf("timeout waiting for SETTINGS frames from %v", sc.conn.RemoteAddr())
862 return
863 case idleTimerMsg:
864 sc.vlogf("connection is idle")
865 sc.goAway(ErrCodeNo)
866 case readIdleTimerMsg:
867 sc.handlePingTimer(lastFrameTime)
868 case shutdownTimerMsg:
869 sc.vlogf("GOAWAY close timer fired; closing conn from %v", sc.conn.RemoteAddr())
870 return
871 case gracefulShutdownMsg:
872 sc.startGracefulShutdownInternal()
873 case handlerDoneMsg:
874 sc.handlerDone()
875 default:
876 panic("unknown timer")
877 }
878 case *startPushRequest:
879 sc.startPush(v)
880 case func(*serverConn):
881 v(sc)
882 default:
883 panic(fmt.Sprintf("unexpected type %T", v))
884 }
885 }
886
887
888
889
890 if sc.queuedControlFrames > maxQueuedControlFrames {
891 sc.vlogf("http2: too many control frames in send queue, closing connection")
892 return
893 }
894
895
896
897
898 sentGoAway := sc.inGoAway && !sc.needToSendGoAway && !sc.writingFrame
899 gracefulShutdownComplete := sc.goAwayCode == ErrCodeNo && sc.curOpenStreams() == 0
900 if sentGoAway && sc.shutdownTimer == nil && (sc.goAwayCode != ErrCodeNo || gracefulShutdownComplete) {
901 sc.shutDownIn(goAwayTimeout)
902 }
903 }
904 }
905
906 func (sc *serverConn) handlePingTimer(lastFrameReadTime time.Time) {
907 if sc.pingSent {
908 sc.logf("timeout waiting for PING response")
909 if f := sc.countErrorFunc; f != nil {
910 f("conn_close_lost_ping")
911 }
912 sc.conn.Close()
913 return
914 }
915
916 pingAt := lastFrameReadTime.Add(sc.readIdleTimeout)
917 now := time.Now()
918 if pingAt.After(now) {
919
920
921 sc.readIdleTimer.Reset(pingAt.Sub(now))
922 return
923 }
924
925 sc.pingSent = true
926
927
928 _, _ = rand.Read(sc.sentPingData[:])
929 sc.writeFrame(FrameWriteRequest{
930 write: &writePing{data: sc.sentPingData},
931 })
932 sc.readIdleTimer.Reset(sc.pingTimeout)
933 }
934
935 type serverMessage int
936
937
938 var (
939 settingsTimerMsg = new(serverMessage)
940 idleTimerMsg = new(serverMessage)
941 readIdleTimerMsg = new(serverMessage)
942 shutdownTimerMsg = new(serverMessage)
943 gracefulShutdownMsg = new(serverMessage)
944 handlerDoneMsg = new(serverMessage)
945 )
946
947 func (sc *serverConn) onSettingsTimer() { sc.sendServeMsg(settingsTimerMsg) }
948 func (sc *serverConn) onIdleTimer() { sc.sendServeMsg(idleTimerMsg) }
949 func (sc *serverConn) onReadIdleTimer() { sc.sendServeMsg(readIdleTimerMsg) }
950 func (sc *serverConn) onShutdownTimer() { sc.sendServeMsg(shutdownTimerMsg) }
951
952 func (sc *serverConn) sendServeMsg(msg any) {
953 sc.serveG.checkNotOn()
954 select {
955 case sc.serveMsgCh <- msg:
956 case <-sc.doneServing:
957 }
958 }
959
960 var errPrefaceTimeout = errors.New("timeout waiting for client preface")
961
962
963
964
965 func (sc *serverConn) readPreface() error {
966 if sc.sawClientPreface {
967 return nil
968 }
969 errc := make(chan error, 1)
970 go func() {
971
972 buf := make([]byte, len(ClientPreface))
973 if _, err := io.ReadFull(sc.conn, buf); err != nil {
974 errc <- err
975 } else if !bytes.Equal(buf, clientPreface) {
976 errc <- fmt.Errorf("bogus greeting %q", buf)
977 } else {
978 errc <- nil
979 }
980 }()
981 timer := time.NewTimer(prefaceTimeout)
982 defer timer.Stop()
983 select {
984 case <-timer.C:
985 return errPrefaceTimeout
986 case err := <-errc:
987 if err == nil {
988 if VerboseLogs {
989 sc.vlogf("http2: server: client %v said hello", sc.conn.RemoteAddr())
990 }
991 }
992 return err
993 }
994 }
995
996 var writeDataPool = sync.Pool{
997 New: func() any { return new(writeData) },
998 }
999
1000
1001
1002 func (sc *serverConn) writeDataFromHandler(stream *stream, data []byte, endStream bool) error {
1003 ch := sc.srv.getErrChan()
1004 writeArg := writeDataPool.Get().(*writeData)
1005 *writeArg = writeData{stream.id, data, endStream}
1006 err := sc.writeFrameFromHandler(FrameWriteRequest{
1007 write: writeArg,
1008 stream: stream,
1009 done: ch,
1010 })
1011 if err != nil {
1012 return err
1013 }
1014 var frameWriteDone bool
1015 select {
1016 case err = <-ch:
1017 frameWriteDone = true
1018 case <-sc.doneServing:
1019 return errClientDisconnected
1020 case <-stream.cw:
1021
1022
1023
1024
1025
1026
1027
1028 select {
1029 case err = <-ch:
1030 frameWriteDone = true
1031 default:
1032 return errStreamClosed
1033 }
1034 }
1035 sc.srv.putErrChan(ch)
1036 if frameWriteDone {
1037 writeDataPool.Put(writeArg)
1038 }
1039 return err
1040 }
1041
1042
1043
1044
1045
1046
1047
1048
1049 func (sc *serverConn) writeFrameFromHandler(wr FrameWriteRequest) error {
1050 sc.serveG.checkNotOn()
1051 select {
1052 case sc.wantWriteFrameCh <- wr:
1053 return nil
1054 case <-sc.doneServing:
1055
1056
1057 return errClientDisconnected
1058 }
1059 }
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069 func (sc *serverConn) writeFrame(wr FrameWriteRequest) {
1070 sc.serveG.check()
1071
1072
1073 var ignoreWrite bool
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093 if wr.StreamID() != 0 {
1094 _, isReset := wr.write.(StreamError)
1095 if state, _ := sc.state(wr.StreamID()); state == stateClosed && !isReset {
1096 ignoreWrite = true
1097 }
1098 }
1099
1100
1101
1102 switch wr.write.(type) {
1103 case *writeResHeaders:
1104 wr.stream.wroteHeaders = true
1105 case write100ContinueHeadersFrame:
1106 if wr.stream.wroteHeaders {
1107
1108
1109 if wr.done != nil {
1110 panic("wr.done != nil for write100ContinueHeadersFrame")
1111 }
1112 ignoreWrite = true
1113 }
1114 }
1115
1116 if !ignoreWrite {
1117 if wr.isControl() {
1118 sc.queuedControlFrames++
1119
1120
1121 if sc.queuedControlFrames < 0 {
1122 sc.conn.Close()
1123 }
1124 }
1125 sc.writeSched.Push(wr)
1126 }
1127 sc.scheduleFrameWrite()
1128 }
1129
1130
1131
1132
1133 func (sc *serverConn) startFrameWrite(wr FrameWriteRequest) {
1134 sc.serveG.check()
1135 if sc.writingFrame {
1136 panic("internal error: can only be writing one frame at a time")
1137 }
1138
1139 st := wr.stream
1140 if st != nil {
1141 switch st.state {
1142 case stateHalfClosedLocal:
1143 switch wr.write.(type) {
1144 case StreamError, handlerPanicRST, writeWindowUpdate:
1145
1146
1147 default:
1148 panic(fmt.Sprintf("internal error: attempt to send frame on a half-closed-local stream: %v", wr))
1149 }
1150 case stateClosed:
1151 panic(fmt.Sprintf("internal error: attempt to send frame on a closed stream: %v", wr))
1152 }
1153 }
1154 if wpp, ok := wr.write.(*writePushPromise); ok {
1155 var err error
1156 wpp.promisedID, err = wpp.allocatePromisedID()
1157 if err != nil {
1158 sc.writingFrameAsync = false
1159 wr.replyToWriter(err)
1160 return
1161 }
1162 }
1163
1164 sc.writingFrame = true
1165 sc.needsFrameFlush = true
1166 if sc.pendingEncoderTableSize {
1167
1168
1169
1170
1171
1172 sc.pendingEncoderTableSize = false
1173 sc.hpackEncoder.SetMaxDynamicTableSize(sc.encoderTableSizeMin)
1174 sc.hpackEncoder.SetMaxDynamicTableSize(sc.encoderTableSize)
1175 }
1176 if wr.write.staysWithinBuffer(sc.bw.Available()) {
1177 sc.writingFrameAsync = false
1178 err := wr.write.writeFrame(sc)
1179 sc.wroteFrame(frameWriteResult{wr: wr, err: err})
1180 } else if wd, ok := wr.write.(*writeData); ok {
1181
1182
1183
1184 sc.framer.startWriteDataPadded(wd.streamID, wd.endStream, wd.p, nil)
1185 sc.writingFrameAsync = true
1186 go sc.writeFrameAsync(wr, wd)
1187 } else {
1188 sc.writingFrameAsync = true
1189 go sc.writeFrameAsync(wr, nil)
1190 }
1191 }
1192
1193
1194
1195
1196 var errHandlerPanicked = errors.New("http2: handler panicked")
1197
1198
1199
1200 func (sc *serverConn) wroteFrame(res frameWriteResult) {
1201 sc.serveG.check()
1202 if !sc.writingFrame {
1203 panic("internal error: expected to be already writing a frame")
1204 }
1205 sc.writingFrame = false
1206 sc.writingFrameAsync = false
1207
1208 if res.err != nil {
1209 sc.conn.Close()
1210 }
1211
1212 wr := res.wr
1213
1214 if writeEndsStream(wr.write) {
1215 st := wr.stream
1216 if st == nil {
1217 panic("internal error: expecting non-nil stream")
1218 }
1219 switch st.state {
1220 case stateOpen:
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231 st.state = stateHalfClosedLocal
1232
1233
1234
1235
1236 sc.resetStream(streamError(st.id, ErrCodeNo))
1237 case stateHalfClosedRemote:
1238 sc.closeStream(st, errHandlerComplete)
1239 }
1240 } else {
1241 switch v := wr.write.(type) {
1242 case StreamError:
1243
1244 if st, ok := sc.streams[v.StreamID]; ok {
1245 sc.closeStream(st, v)
1246 }
1247 case handlerPanicRST:
1248 sc.closeStream(wr.stream, errHandlerPanicked)
1249 }
1250 }
1251
1252
1253 wr.replyToWriter(res.err)
1254
1255 sc.scheduleFrameWrite()
1256 }
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268 func (sc *serverConn) scheduleFrameWrite() {
1269 sc.serveG.check()
1270 if sc.writingFrame || sc.inFrameScheduleLoop {
1271 return
1272 }
1273 sc.inFrameScheduleLoop = true
1274 for !sc.writingFrameAsync {
1275 if sc.flow.flowErr && (!sc.inGoAway || sc.goAwayCode == ErrCodeNo) {
1276 sc.inGoAway = true
1277 sc.needToSendGoAway = true
1278 sc.goAwayCode = ErrCodeFlowControl
1279 }
1280 if sc.needToSendGoAway {
1281 sc.needToSendGoAway = false
1282 sc.startFrameWrite(FrameWriteRequest{
1283 write: &writeGoAway{
1284 maxStreamID: sc.maxClientStreamID,
1285 code: sc.goAwayCode,
1286 },
1287 })
1288 continue
1289 }
1290 if sc.needToSendSettingsAck {
1291 sc.needToSendSettingsAck = false
1292 sc.startFrameWrite(FrameWriteRequest{write: writeSettingsAck{}})
1293 continue
1294 }
1295 if !sc.inGoAway || sc.goAwayCode == ErrCodeNo {
1296 if wr, ok := sc.writeSched.Pop(); ok {
1297 if wr.isControl() {
1298 sc.queuedControlFrames--
1299 }
1300 sc.startFrameWrite(wr)
1301 continue
1302 }
1303 if sc.flow.flowErr {
1304 continue
1305 }
1306 }
1307 if sc.needsFrameFlush {
1308 sc.startFrameWrite(FrameWriteRequest{write: flushFrameWriter{}})
1309 sc.needsFrameFlush = false
1310 continue
1311 }
1312 break
1313 }
1314 sc.inFrameScheduleLoop = false
1315 }
1316
1317
1318
1319
1320
1321
1322
1323
1324 func (sc *serverConn) startGracefulShutdown() {
1325 sc.serveG.checkNotOn()
1326 sc.shutdownOnce.Do(func() { sc.sendServeMsg(gracefulShutdownMsg) })
1327 }
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345 var goAwayTimeout = 1 * time.Second
1346
1347 func (sc *serverConn) startGracefulShutdownInternal() {
1348 sc.goAway(ErrCodeNo)
1349 }
1350
1351 func (sc *serverConn) goAway(code ErrCode) {
1352 sc.serveG.check()
1353 if sc.inGoAway {
1354 if sc.goAwayCode == ErrCodeNo {
1355 sc.goAwayCode = code
1356 }
1357 return
1358 }
1359 sc.inGoAway = true
1360 sc.needToSendGoAway = true
1361 sc.goAwayCode = code
1362 sc.scheduleFrameWrite()
1363 }
1364
1365 func (sc *serverConn) shutDownIn(d time.Duration) {
1366 sc.serveG.check()
1367 sc.shutdownTimer = time.AfterFunc(d, sc.onShutdownTimer)
1368 }
1369
1370 func (sc *serverConn) resetStream(se StreamError) {
1371 sc.serveG.check()
1372 sc.writeFrame(FrameWriteRequest{write: se})
1373 if st, ok := sc.streams[se.StreamID]; ok {
1374 st.resetQueued = true
1375 }
1376 }
1377
1378
1379
1380
1381 func (sc *serverConn) processFrameFromReader(res readFrameResult) bool {
1382 sc.serveG.check()
1383 err := res.err
1384 if err != nil {
1385 if err == ErrFrameTooLarge {
1386 sc.goAway(ErrCodeFrameSize)
1387 return true
1388 }
1389 clientGone := err == io.EOF || err == io.ErrUnexpectedEOF || isClosedConnError(err)
1390 if clientGone {
1391
1392
1393
1394
1395
1396
1397
1398
1399 return false
1400 }
1401 } else {
1402 f := res.f
1403 if VerboseLogs {
1404 sc.vlogf("http2: server read frame %v", summarizeFrame(f))
1405 }
1406 err = sc.processFrame(f)
1407 if err == nil {
1408 return true
1409 }
1410 }
1411
1412 switch ev := err.(type) {
1413 case StreamError:
1414 sc.resetStream(ev)
1415 return true
1416 case goAwayFlowError:
1417 sc.goAway(ErrCodeFlowControl)
1418 return true
1419 case ConnectionError:
1420 if res.f != nil {
1421 if id := res.f.Header().StreamID; id > sc.maxClientStreamID {
1422 sc.maxClientStreamID = id
1423 }
1424 }
1425 sc.logf("http2: server connection error from %v: %v", sc.conn.RemoteAddr(), ev)
1426 sc.goAway(ErrCode(ev))
1427 return true
1428 default:
1429 if res.err != nil {
1430 sc.vlogf("http2: server closing client connection; error reading frame from client %s: %v", sc.conn.RemoteAddr(), err)
1431 } else {
1432 sc.logf("http2: server closing client connection: %v", err)
1433 }
1434 return false
1435 }
1436 }
1437
1438 func (sc *serverConn) processFrame(f Frame) error {
1439 sc.serveG.check()
1440
1441
1442 if !sc.sawFirstSettings {
1443 if _, ok := f.(*SettingsFrame); !ok {
1444 return sc.countError("first_settings", ConnectionError(ErrCodeProtocol))
1445 }
1446 sc.sawFirstSettings = true
1447 }
1448
1449
1450
1451
1452
1453 if sc.inGoAway && (sc.goAwayCode != ErrCodeNo || f.Header().StreamID > sc.maxClientStreamID) {
1454
1455 if f, ok := f.(*DataFrame); ok {
1456 if !sc.inflow.take(f.Length) {
1457 return sc.countError("data_flow", streamError(f.Header().StreamID, ErrCodeFlowControl))
1458 }
1459 sc.sendWindowUpdate(nil, int(f.Length))
1460 }
1461 return nil
1462 }
1463
1464 switch f := f.(type) {
1465 case *SettingsFrame:
1466 return sc.processSettings(f)
1467 case *MetaHeadersFrame:
1468 return sc.processHeaders(f)
1469 case *WindowUpdateFrame:
1470 return sc.processWindowUpdate(f)
1471 case *PingFrame:
1472 return sc.processPing(f)
1473 case *DataFrame:
1474 return sc.processData(f)
1475 case *RSTStreamFrame:
1476 return sc.processResetStream(f)
1477 case *PriorityFrame:
1478 return sc.processPriority(f)
1479 case *GoAwayFrame:
1480 return sc.processGoAway(f)
1481 case *PushPromiseFrame:
1482
1483
1484 return sc.countError("push_promise", ConnectionError(ErrCodeProtocol))
1485 case *PriorityUpdateFrame:
1486 return sc.processPriorityUpdate(f)
1487 default:
1488 sc.vlogf("http2: server ignoring frame: %v", f.Header())
1489 return nil
1490 }
1491 }
1492
1493 func (sc *serverConn) processPing(f *PingFrame) error {
1494 sc.serveG.check()
1495 if f.IsAck() {
1496 if sc.pingSent && sc.sentPingData == f.Data {
1497
1498 sc.pingSent = false
1499 sc.readIdleTimer.Reset(sc.readIdleTimeout)
1500 }
1501
1502
1503 return nil
1504 }
1505 if f.StreamID != 0 {
1506
1507
1508
1509
1510
1511 return sc.countError("ping_on_stream", ConnectionError(ErrCodeProtocol))
1512 }
1513 sc.writeFrame(FrameWriteRequest{write: writePingAck{f}})
1514 return nil
1515 }
1516
1517 func (sc *serverConn) processWindowUpdate(f *WindowUpdateFrame) error {
1518 sc.serveG.check()
1519 switch {
1520 case f.StreamID != 0:
1521 state, st := sc.state(f.StreamID)
1522 if state == stateIdle {
1523
1524
1525
1526
1527 return sc.countError("stream_idle", ConnectionError(ErrCodeProtocol))
1528 }
1529 if st == nil {
1530
1531
1532
1533
1534
1535 return nil
1536 }
1537 if !st.flow.add(int32(f.Increment)) {
1538 if st.flow.conn.flowErr {
1539
1540 return sc.countError("bad_flow", ConnectionError(ErrCodeFlowControl))
1541 }
1542 return sc.countError("bad_flow", streamError(f.StreamID, ErrCodeFlowControl))
1543 }
1544 default:
1545 if !sc.flow.add(int32(f.Increment)) {
1546 return goAwayFlowError{}
1547 }
1548 }
1549 sc.scheduleFrameWrite()
1550 return nil
1551 }
1552
1553 func (sc *serverConn) processResetStream(f *RSTStreamFrame) error {
1554 sc.serveG.check()
1555
1556 state, st := sc.state(f.StreamID)
1557 if state == stateIdle {
1558
1559
1560
1561
1562
1563 return sc.countError("reset_idle_stream", ConnectionError(ErrCodeProtocol))
1564 }
1565 if st != nil {
1566 st.cancelCtx()
1567 sc.closeStream(st, streamError(f.StreamID, f.ErrCode))
1568 }
1569 return nil
1570 }
1571
1572 func (sc *serverConn) closeStream(st *stream, err error) {
1573 sc.serveG.check()
1574 if st.state == stateIdle || st.state == stateClosed {
1575 panic(fmt.Sprintf("invariant; can't close stream in state %v", st.state))
1576 }
1577 st.state = stateClosed
1578 if st.readDeadline != nil {
1579 st.readDeadline.Stop()
1580 }
1581 if st.writeDeadline != nil {
1582 st.writeDeadline.Stop()
1583 }
1584 if st.isPushed() {
1585 sc.curPushedStreams--
1586 } else {
1587 sc.curClientStreams--
1588 }
1589 delete(sc.streams, st.id)
1590 if len(sc.streams) == 0 {
1591 sc.setConnState(ConnStateIdle)
1592 idleTimeout := sc.hs.IdleTimeout()
1593 if idleTimeout > 0 && sc.idleTimer != nil {
1594 sc.idleTimer.Reset(idleTimeout)
1595 }
1596 if h1ServerKeepAlivesDisabled(sc.hs) {
1597 sc.startGracefulShutdownInternal()
1598 }
1599 }
1600 if p := st.body; p != nil {
1601 p.CloseWithError(err)
1602 }
1603 if e, ok := err.(StreamError); ok {
1604 if e.Cause != nil {
1605 err = e.Cause
1606 } else {
1607 err = errStreamClosed
1608 }
1609 }
1610 st.closeErr = err
1611 st.cancelCtx()
1612 st.cw.Close()
1613 sc.writeSched.CloseStream(st.id)
1614 }
1615
1616 func (sc *serverConn) processSettings(f *SettingsFrame) error {
1617 sc.serveG.check()
1618 if f.IsAck() {
1619 sc.unackedSettings--
1620 if sc.unackedSettings < 0 {
1621
1622
1623
1624 return sc.countError("ack_mystery", ConnectionError(ErrCodeProtocol))
1625 }
1626 return nil
1627 }
1628 if f.NumSettings() > 100 || f.HasDuplicates() {
1629
1630
1631
1632 return sc.countError("settings_big_or_dups", ConnectionError(ErrCodeProtocol))
1633 }
1634 if err := f.ForeachSetting(sc.processSetting); err != nil {
1635 return err
1636 }
1637
1638
1639 sc.needToSendSettingsAck = true
1640 sc.scheduleFrameWrite()
1641 return nil
1642 }
1643
1644 func (sc *serverConn) processSetting(s Setting) error {
1645 sc.serveG.check()
1646 if err := s.Valid(); err != nil {
1647 return err
1648 }
1649 if VerboseLogs {
1650 sc.vlogf("http2: server processing setting %v", s)
1651 }
1652 switch s.ID {
1653 case SettingHeaderTableSize:
1654
1655 if !sc.pendingEncoderTableSize || s.Val < sc.encoderTableSizeMin {
1656 sc.encoderTableSizeMin = s.Val
1657 }
1658 sc.encoderTableSize = s.Val
1659 sc.pendingEncoderTableSize = true
1660 case SettingEnablePush:
1661 sc.pushEnabled = s.Val != 0
1662 case SettingMaxConcurrentStreams:
1663 sc.clientMaxStreams = s.Val
1664 case SettingInitialWindowSize:
1665 return sc.processSettingInitialWindowSize(s.Val)
1666 case SettingMaxFrameSize:
1667 sc.maxFrameSize = int32(s.Val)
1668 case SettingMaxHeaderListSize:
1669 sc.peerMaxHeaderListSize = s.Val
1670 case SettingEnableConnectProtocol:
1671
1672
1673 case SettingNoRFC7540Priorities:
1674 if s.Val > 1 {
1675 return ConnectionError(ErrCodeProtocol)
1676 }
1677 default:
1678
1679
1680
1681 if VerboseLogs {
1682 sc.vlogf("http2: server ignoring unknown setting %v", s)
1683 }
1684 }
1685 return nil
1686 }
1687
1688 func (sc *serverConn) processSettingInitialWindowSize(val uint32) error {
1689 sc.serveG.check()
1690 if !sc.flow.changeInitialWindowSize(int64(val)) {
1691
1692
1693
1694
1695
1696
1697 return sc.countError("setting_win_size", ConnectionError(ErrCodeFlowControl))
1698 }
1699 return nil
1700 }
1701
1702 func (sc *serverConn) processData(f *DataFrame) error {
1703 sc.serveG.check()
1704 id := f.Header().StreamID
1705
1706 data := f.Data()
1707 state, st := sc.state(id)
1708 if id == 0 || state == stateIdle {
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719 return sc.countError("data_on_idle", ConnectionError(ErrCodeProtocol))
1720 }
1721
1722
1723
1724
1725 if st == nil || state != stateOpen || st.gotTrailerHeader || st.resetQueued {
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735 if !sc.inflow.take(f.Length) {
1736 return sc.countError("data_flow", streamError(id, ErrCodeFlowControl))
1737 }
1738 sc.sendWindowUpdate(nil, int(f.Length))
1739
1740 if st != nil && st.resetQueued {
1741
1742 return nil
1743 }
1744 return sc.countError("closed", streamError(id, ErrCodeStreamClosed))
1745 }
1746 if st.body == nil {
1747 panic("internal error: should have a body in this state")
1748 }
1749
1750
1751 if st.declBodyBytes != -1 && st.bodyBytes+int64(len(data)) > st.declBodyBytes {
1752 if !sc.inflow.take(f.Length) {
1753 return sc.countError("data_flow", streamError(id, ErrCodeFlowControl))
1754 }
1755 sc.sendWindowUpdate(nil, int(f.Length))
1756
1757 st.body.CloseWithError(fmt.Errorf("sender tried to send more than declared Content-Length of %d bytes", st.declBodyBytes))
1758
1759
1760
1761 return sc.countError("send_too_much", streamError(id, ErrCodeProtocol))
1762 }
1763 if f.Length > 0 {
1764
1765 if !takeInflows(&sc.inflow, &st.inflow, f.Length) {
1766 return sc.countError("flow_on_data_length", streamError(id, ErrCodeFlowControl))
1767 }
1768
1769 if len(data) > 0 {
1770 st.bodyBytes += int64(len(data))
1771 wrote, err := st.body.Write(data)
1772 if err != nil {
1773
1774
1775
1776 sc.sendWindowUpdate(nil, int(f.Length)-wrote)
1777 return nil
1778 }
1779 if wrote != len(data) {
1780 panic("internal error: bad Writer")
1781 }
1782 }
1783
1784
1785
1786
1787
1788
1789 pad := int32(f.Length) - int32(len(data))
1790 sc.sendWindowUpdate32(nil, pad)
1791 sc.sendWindowUpdate32(st, pad)
1792 }
1793 if f.StreamEnded() {
1794 st.endStream()
1795 }
1796 return nil
1797 }
1798
1799 func (sc *serverConn) processGoAway(f *GoAwayFrame) error {
1800 sc.serveG.check()
1801 if f.ErrCode != ErrCodeNo {
1802 sc.logf("http2: received GOAWAY %+v, starting graceful shutdown", f)
1803 } else {
1804 sc.vlogf("http2: received GOAWAY %+v, starting graceful shutdown", f)
1805 }
1806 sc.startGracefulShutdownInternal()
1807
1808
1809 sc.pushEnabled = false
1810 return nil
1811 }
1812
1813
1814 func (st *stream) isPushed() bool {
1815 return st.id%2 == 0
1816 }
1817
1818
1819
1820 func (st *stream) endStream() {
1821 sc := st.sc
1822 sc.serveG.check()
1823
1824 if st.declBodyBytes != -1 && st.declBodyBytes != st.bodyBytes {
1825 st.body.CloseWithError(fmt.Errorf("request declared a Content-Length of %d but only wrote %d bytes",
1826 st.declBodyBytes, st.bodyBytes))
1827 } else {
1828 st.body.closeWithErrorAndCode(io.EOF, st.copyTrailersToHandlerRequest)
1829 st.body.CloseWithError(io.EOF)
1830 }
1831 st.state = stateHalfClosedRemote
1832 }
1833
1834
1835
1836 func (st *stream) copyTrailersToHandlerRequest() {
1837 for k, vv := range st.trailer {
1838 if _, ok := st.reqTrailer[k]; ok {
1839
1840 st.reqTrailer[k] = vv
1841 }
1842 }
1843 }
1844
1845
1846
1847 func (st *stream) onReadTimeout() {
1848 if st.body != nil {
1849
1850
1851 st.body.CloseWithError(fmt.Errorf("%w", os.ErrDeadlineExceeded))
1852 }
1853 }
1854
1855
1856
1857 func (st *stream) onWriteTimeout() {
1858 st.sc.writeFrameFromHandler(FrameWriteRequest{write: StreamError{
1859 StreamID: st.id,
1860 Code: ErrCodeInternal,
1861 Cause: os.ErrDeadlineExceeded,
1862 }})
1863 }
1864
1865 func (sc *serverConn) processHeaders(f *MetaHeadersFrame) error {
1866 sc.serveG.check()
1867 id := f.StreamID
1868
1869
1870
1871
1872
1873 if id%2 != 1 {
1874 return sc.countError("headers_even", ConnectionError(ErrCodeProtocol))
1875 }
1876
1877
1878
1879
1880 if st := sc.streams[f.StreamID]; st != nil {
1881 if st.resetQueued {
1882
1883
1884 return nil
1885 }
1886
1887
1888
1889
1890 if st.state == stateHalfClosedRemote {
1891 return sc.countError("headers_half_closed", streamError(id, ErrCodeStreamClosed))
1892 }
1893 return st.processTrailerHeaders(f)
1894 }
1895
1896
1897
1898
1899
1900
1901 if id <= sc.maxClientStreamID {
1902 return sc.countError("stream_went_down", ConnectionError(ErrCodeProtocol))
1903 }
1904 sc.maxClientStreamID = id
1905
1906 if sc.idleTimer != nil {
1907 sc.idleTimer.Stop()
1908 }
1909
1910
1911
1912
1913
1914
1915
1916 if sc.curClientStreams+1 > sc.advMaxStreams {
1917 if sc.unackedSettings == 0 {
1918
1919 return sc.countError("over_max_streams", streamError(id, ErrCodeProtocol))
1920 }
1921
1922
1923
1924
1925
1926 return sc.countError("over_max_streams_race", streamError(id, ErrCodeRefusedStream))
1927 }
1928
1929 initialState := stateOpen
1930 if f.StreamEnded() {
1931 initialState = stateHalfClosedRemote
1932 }
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942 initialPriority := defaultRFC9218Priority(sc.priorityAware && !sc.hasIntermediary)
1943 if _, ok := sc.writeSched.(*priorityWriteSchedulerRFC9218); ok && !sc.hasIntermediary {
1944 headerPriority, priorityAware, hasIntermediary := f.rfc9218Priority(sc.priorityAware)
1945 initialPriority = headerPriority
1946 sc.hasIntermediary = hasIntermediary
1947 if priorityAware {
1948 sc.priorityAware = true
1949 }
1950 }
1951 st := sc.newStream(id, 0, initialState, initialPriority)
1952
1953 if f.HasPriority() {
1954 if err := sc.checkPriority(f.StreamID, f.Priority); err != nil {
1955 return err
1956 }
1957 if !sc.writeSchedIgnoresRFC7540() {
1958 sc.writeSched.AdjustStream(st.id, f.Priority)
1959 }
1960 }
1961
1962 rw, req, err := sc.newWriterAndRequest(st, f)
1963 if err != nil {
1964 return err
1965 }
1966 st.reqTrailer = req.Trailer
1967 if st.reqTrailer != nil {
1968 st.trailer = make(Header)
1969 }
1970 st.body = st.reqBody.pipe
1971 st.declBodyBytes = req.ContentLength
1972
1973 handler := sc.handler.ServeHTTP
1974 if f.Truncated {
1975
1976 handler = handleHeaderListTooLong
1977 } else if err := checkValidHTTP2RequestHeaders(req.Header); err != nil {
1978 handler = serve400Handler{err}.ServeHTTP
1979 }
1980
1981 if sc.hs.ReadTimeout() > 0 {
1982 st.readDeadline = time.AfterFunc(sc.hs.ReadTimeout(), st.onReadTimeout)
1983 }
1984
1985 return sc.scheduleHandler(st, rw, req, handler)
1986 }
1987
1988 func (sc *serverConn) upgradeRequest(req *ServerRequest) {
1989 sc.serveG.check()
1990 id := uint32(1)
1991 sc.maxClientStreamID = id
1992 st := sc.newStream(id, 0, stateHalfClosedRemote, defaultRFC9218Priority(sc.priorityAware && !sc.hasIntermediary))
1993 st.reqTrailer = req.Trailer
1994 if st.reqTrailer != nil {
1995 st.trailer = make(Header)
1996 }
1997 rw := sc.newResponseWriter(st)
1998 rw.rws.req = *req
1999 req = &rw.rws.req
2000
2001
2002
2003
2004 sc.curHandlers++
2005 go sc.runHandler(rw, req, sc.handler.ServeHTTP)
2006 }
2007
2008 func (st *stream) processTrailerHeaders(f *MetaHeadersFrame) error {
2009 sc := st.sc
2010 sc.serveG.check()
2011 if st.gotTrailerHeader {
2012 return sc.countError("dup_trailers", ConnectionError(ErrCodeProtocol))
2013 }
2014 st.gotTrailerHeader = true
2015 if !f.StreamEnded() {
2016 return sc.countError("trailers_not_ended", streamError(st.id, ErrCodeProtocol))
2017 }
2018
2019 if len(f.PseudoFields()) > 0 {
2020 return sc.countError("trailers_pseudo", streamError(st.id, ErrCodeProtocol))
2021 }
2022 if f.Truncated {
2023 return sc.countError("trailers_too_large", streamError(st.id, ErrCodeProtocol))
2024 }
2025 if st.trailer != nil {
2026 for _, hf := range f.RegularFields() {
2027 key := sc.canonicalHeader(hf.Name)
2028 if !httpguts.ValidTrailerHeader(key) {
2029
2030
2031
2032 return sc.countError("trailers_bogus", streamError(st.id, ErrCodeProtocol))
2033 }
2034 st.trailer[key] = append(st.trailer[key], hf.Value)
2035 }
2036 }
2037 st.endStream()
2038 return nil
2039 }
2040
2041 func (sc *serverConn) checkPriority(streamID uint32, p PriorityParam) error {
2042 if streamID == p.StreamDep {
2043
2044
2045
2046
2047 return sc.countError("priority", streamError(streamID, ErrCodeProtocol))
2048 }
2049 return nil
2050 }
2051
2052 func (sc *serverConn) processPriority(f *PriorityFrame) error {
2053 if err := sc.checkPriority(f.StreamID, f.PriorityParam); err != nil {
2054 return err
2055 }
2056
2057
2058
2059
2060
2061 if sc.writeSchedIgnoresRFC7540() {
2062 return nil
2063 }
2064 sc.writeSched.AdjustStream(f.StreamID, f.PriorityParam)
2065 return nil
2066 }
2067
2068 func (sc *serverConn) processPriorityUpdate(f *PriorityUpdateFrame) error {
2069 sc.priorityAware = true
2070 if _, ok := sc.writeSched.(*priorityWriteSchedulerRFC9218); !ok {
2071 return nil
2072 }
2073 p, ok := parseRFC9218Priority(f.Priority, sc.priorityAware)
2074 if !ok {
2075 return sc.countError("unparsable_priority_update", streamError(f.PrioritizedStreamID, ErrCodeProtocol))
2076 }
2077 sc.writeSched.AdjustStream(f.PrioritizedStreamID, p)
2078 return nil
2079 }
2080
2081 func (sc *serverConn) newStream(id, pusherID uint32, state streamState, priority PriorityParam) *stream {
2082 sc.serveG.check()
2083 if id == 0 {
2084 panic("internal error: cannot create stream with id 0")
2085 }
2086
2087 ctx, cancelCtx := context.WithCancel(sc.baseCtx)
2088 st := &stream{
2089 sc: sc,
2090 id: id,
2091 state: state,
2092 ctx: ctx,
2093 cancelCtx: cancelCtx,
2094 }
2095 st.cw.Init()
2096 st.flow.conn = &sc.flow
2097 st.inflow.init(sc.initialStreamRecvWindowSize)
2098 if writeTimeout := sc.hs.WriteTimeout(); writeTimeout > 0 {
2099 st.writeDeadline = time.AfterFunc(writeTimeout, st.onWriteTimeout)
2100 }
2101
2102 sc.streams[id] = st
2103 sc.writeSched.OpenStream(st.id, OpenStreamOptions{PusherID: pusherID, priority: priority})
2104 if st.isPushed() {
2105 sc.curPushedStreams++
2106 } else {
2107 sc.curClientStreams++
2108 }
2109 if sc.curOpenStreams() == 1 {
2110 sc.setConnState(ConnStateActive)
2111 }
2112
2113 return st
2114 }
2115
2116 func (sc *serverConn) newWriterAndRequest(st *stream, f *MetaHeadersFrame) (*responseWriter, *ServerRequest, error) {
2117 sc.serveG.check()
2118
2119 rp := httpcommon.ServerRequestParam{
2120 Method: f.PseudoValue("method"),
2121 Scheme: f.PseudoValue("scheme"),
2122 Authority: f.PseudoValue("authority"),
2123 Path: f.PseudoValue("path"),
2124 Protocol: f.PseudoValue("protocol"),
2125 }
2126
2127
2128 if disableExtendedConnectProtocol && rp.Protocol != "" {
2129 return nil, nil, sc.countError("bad_connect", streamError(f.StreamID, ErrCodeProtocol))
2130 }
2131
2132 isConnect := rp.Method == "CONNECT"
2133 if isConnect {
2134 if rp.Protocol == "" && (rp.Path != "" || rp.Scheme != "" || rp.Authority == "") {
2135 return nil, nil, sc.countError("bad_connect", streamError(f.StreamID, ErrCodeProtocol))
2136 }
2137 } else if rp.Method == "" || rp.Path == "" || (rp.Scheme != "https" && rp.Scheme != "http") {
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148 return nil, nil, sc.countError("bad_path_method", streamError(f.StreamID, ErrCodeProtocol))
2149 }
2150
2151 header := make(Header)
2152 rp.Header = header
2153 for _, hf := range f.RegularFields() {
2154 header.Add(sc.canonicalHeader(hf.Name), hf.Value)
2155 }
2156 if rp.Authority == "" {
2157 rp.Authority = header.Get("Host")
2158 }
2159 if rp.Protocol != "" {
2160 header.Set(":protocol", rp.Protocol)
2161 }
2162
2163 rw, req, err := sc.newWriterAndRequestNoBody(st, rp)
2164 if err != nil {
2165 return nil, nil, err
2166 }
2167 bodyOpen := !f.StreamEnded()
2168 if clens, ok := rp.Header["Content-Length"]; ok {
2169 if cl, err := strconv.ParseUint(clens[0], 10, 63); err == nil {
2170 req.ContentLength = int64(cl)
2171 } else {
2172 req.ContentLength = 0
2173 }
2174 if !bodyOpen && req.ContentLength != 0 {
2175 return nil, nil, sc.countError("bodyless_content_length", streamError(f.StreamID, ErrCodeProtocol))
2176 }
2177 if len(clens) > 1 {
2178 for _, dup := range clens[1:] {
2179 if clens[0] != dup {
2180 return nil, nil, sc.countError("duplicate_content_length", streamError(f.StreamID, ErrCodeProtocol))
2181 }
2182 }
2183 rp.Header["Content-Length"] = clens[:1]
2184 }
2185 }
2186 if bodyOpen {
2187 if _, ok := rp.Header["Content-Length"]; !ok {
2188 req.ContentLength = -1
2189 }
2190 st.reqBody.pipe = &pipe{
2191 b: &dataBuffer{expected: req.ContentLength},
2192 }
2193 }
2194 return rw, req, nil
2195 }
2196
2197 func (sc *serverConn) newWriterAndRequestNoBody(st *stream, rp httpcommon.ServerRequestParam) (*responseWriter, *ServerRequest, error) {
2198 sc.serveG.check()
2199
2200 res := httpcommon.NewServerRequest(rp)
2201 if res.InvalidReason != "" {
2202 return nil, nil, sc.countError(res.InvalidReason, streamError(st.id, ErrCodeProtocol))
2203 }
2204
2205 st.reqBody = &requestBody{
2206 conn: sc,
2207 stream: st,
2208 needsContinue: res.NeedsContinue,
2209 }
2210 rw := sc.newResponseWriter(st)
2211 rw.rws.req = ServerRequest{
2212 Context: st.ctx,
2213 Method: rp.Method,
2214 URL: res.URL,
2215 RemoteAddr: sc.remoteAddrStr,
2216 Header: rp.Header,
2217 RequestURI: res.RequestURI,
2218 Proto: "HTTP/2.0",
2219 ProtoMajor: 2,
2220 ProtoMinor: 0,
2221 TLS: sc.tlsState,
2222 Host: rp.Authority,
2223 Body: st.reqBody,
2224 Trailer: res.Trailer,
2225 }
2226 return rw, &rw.rws.req, nil
2227 }
2228
2229 func (sc *serverConn) newResponseWriter(st *stream) *responseWriter {
2230 rws := responseWriterStatePool.Get().(*responseWriterState)
2231 bwSave := rws.bw
2232 *rws = responseWriterState{}
2233 rws.conn = sc
2234 rws.bw = bwSave
2235 rws.bw.Reset(chunkWriter{rws})
2236 rws.stream = st
2237 return &responseWriter{rws: rws}
2238 }
2239
2240 type unstartedHandler struct {
2241 streamID uint32
2242 rw *responseWriter
2243 req *ServerRequest
2244 handler func(*ResponseWriter, *ServerRequest)
2245 body *pipe
2246 }
2247
2248
2249
2250 func (sc *serverConn) scheduleHandler(st *stream, rw *responseWriter, req *ServerRequest, handler func(*ResponseWriter, *ServerRequest)) error {
2251 sc.serveG.check()
2252 maxHandlers := sc.advMaxStreams
2253 if sc.curHandlers < maxHandlers {
2254 sc.curHandlers++
2255 go sc.runHandler(rw, req, handler)
2256 return nil
2257 }
2258 if len(sc.unstartedHandlers) > int(4*sc.advMaxStreams) {
2259 return sc.countError("too_many_early_resets", ConnectionError(ErrCodeEnhanceYourCalm))
2260 }
2261 sc.unstartedHandlers = append(sc.unstartedHandlers, unstartedHandler{
2262 streamID: st.id,
2263 rw: rw,
2264 req: req,
2265 handler: handler,
2266 body: st.body,
2267 })
2268 return nil
2269 }
2270
2271 func (sc *serverConn) handlerDone() {
2272 sc.serveG.check()
2273 sc.curHandlers--
2274 i := 0
2275 maxHandlers := sc.advMaxStreams
2276 for ; i < len(sc.unstartedHandlers); i++ {
2277 u := sc.unstartedHandlers[i]
2278 if sc.streams[u.streamID] == nil {
2279
2280 if u.body != nil {
2281 u.body.BreakWithError(errClosedBody)
2282 sc.sendWindowUpdate(nil, u.body.Len())
2283 }
2284 continue
2285 }
2286 if sc.curHandlers >= maxHandlers {
2287 break
2288 }
2289 sc.curHandlers++
2290 go sc.runHandler(u.rw, u.req, u.handler)
2291 sc.unstartedHandlers[i] = unstartedHandler{}
2292 }
2293 sc.unstartedHandlers = sc.unstartedHandlers[i:]
2294 if len(sc.unstartedHandlers) == 0 {
2295 sc.unstartedHandlers = nil
2296 }
2297 }
2298
2299
2300 func (sc *serverConn) runHandler(rw *responseWriter, req *ServerRequest, handler func(*ResponseWriter, *ServerRequest)) {
2301 defer sc.sendServeMsg(handlerDoneMsg)
2302 didPanic := true
2303 defer func() {
2304 rw.rws.stream.cancelCtx()
2305 if req.MultipartForm != nil {
2306 req.MultipartForm.RemoveAll()
2307 }
2308 if b := rw.rws.stream.reqBody; b != nil {
2309
2310
2311
2312 b.Close()
2313 }
2314 if didPanic {
2315 e := recover()
2316 sc.writeFrameFromHandler(FrameWriteRequest{
2317 write: handlerPanicRST{rw.rws.stream.id},
2318 stream: rw.rws.stream,
2319 })
2320
2321 if e != nil && e != ErrAbortHandler {
2322 const size = 64 << 10
2323 buf := make([]byte, size)
2324 buf = buf[:runtime.Stack(buf, false)]
2325 sc.logf("http2: panic serving %v: %v\n%s", sc.conn.RemoteAddr(), e, buf)
2326 }
2327 return
2328 }
2329 rw.handlerDone()
2330 }()
2331 handler(rw, req)
2332 didPanic = false
2333 }
2334
2335 func handleHeaderListTooLong(w *ResponseWriter, r *ServerRequest) {
2336
2337
2338
2339
2340 const statusRequestHeaderFieldsTooLarge = 431
2341 w.WriteHeader(statusRequestHeaderFieldsTooLarge)
2342 io.WriteString(w, "<h1>HTTP Error 431</h1><p>Request Header Field(s) Too Large</p>")
2343 }
2344
2345
2346
2347 func (sc *serverConn) writeHeaders(st *stream, headerData *writeResHeaders) error {
2348 sc.serveG.checkNotOn()
2349 var errc chan error
2350 if headerData.h != nil {
2351
2352
2353
2354
2355 errc = sc.srv.getErrChan()
2356 }
2357 if err := sc.writeFrameFromHandler(FrameWriteRequest{
2358 write: headerData,
2359 stream: st,
2360 done: errc,
2361 }); err != nil {
2362 return err
2363 }
2364 if errc != nil {
2365 select {
2366 case err := <-errc:
2367 sc.srv.putErrChan(errc)
2368 return err
2369 case <-sc.doneServing:
2370 return errClientDisconnected
2371 case <-st.cw:
2372 return errStreamClosed
2373 }
2374 }
2375 return nil
2376 }
2377
2378
2379 func (sc *serverConn) write100ContinueHeaders(st *stream) {
2380 sc.writeFrameFromHandler(FrameWriteRequest{
2381 write: write100ContinueHeadersFrame{st.id},
2382 stream: st,
2383 })
2384 }
2385
2386
2387
2388 type bodyReadMsg struct {
2389 st *stream
2390 n int
2391 }
2392
2393
2394
2395
2396 func (sc *serverConn) noteBodyReadFromHandler(st *stream, n int, err error) {
2397 sc.serveG.checkNotOn()
2398 if n > 0 {
2399 select {
2400 case sc.bodyReadCh <- bodyReadMsg{st, n}:
2401 case <-sc.doneServing:
2402 }
2403 }
2404 }
2405
2406 func (sc *serverConn) noteBodyRead(st *stream, n int) {
2407 sc.serveG.check()
2408 sc.sendWindowUpdate(nil, n)
2409 if st != nil && st.state != stateHalfClosedRemote && st.state != stateClosed {
2410
2411
2412 sc.sendWindowUpdate(st, n)
2413 }
2414 }
2415
2416
2417 func (sc *serverConn) sendWindowUpdate32(st *stream, n int32) {
2418 sc.sendWindowUpdate(st, int(n))
2419 }
2420
2421
2422 func (sc *serverConn) sendWindowUpdate(st *stream, n int) {
2423 sc.serveG.check()
2424 var streamID uint32
2425 var send int32
2426 if st == nil {
2427 send = sc.inflow.add(n)
2428 } else {
2429 streamID = st.id
2430 send = st.inflow.add(n)
2431 }
2432 if send == 0 {
2433 return
2434 }
2435 sc.writeFrame(FrameWriteRequest{
2436 write: writeWindowUpdate{streamID: streamID, n: uint32(send)},
2437 stream: st,
2438 })
2439 }
2440
2441
2442
2443 type requestBody struct {
2444 _ incomparable
2445 stream *stream
2446 conn *serverConn
2447 closeOnce sync.Once
2448 sawEOF bool
2449 pipe *pipe
2450 needsContinue bool
2451 }
2452
2453 func (b *requestBody) Close() error {
2454 b.closeOnce.Do(func() {
2455 if b.pipe != nil {
2456 b.pipe.BreakWithError(errClosedBody)
2457 if unread := b.pipe.Len(); unread > 0 {
2458 b.conn.noteBodyReadFromHandler(nil, unread, errClosedBody)
2459 }
2460 }
2461 })
2462 return nil
2463 }
2464
2465 func (b *requestBody) Read(p []byte) (n int, err error) {
2466 if b.needsContinue {
2467 b.needsContinue = false
2468 b.conn.write100ContinueHeaders(b.stream)
2469 }
2470 if b.pipe == nil || b.sawEOF {
2471 return 0, io.EOF
2472 }
2473 n, err = b.pipe.Read(p)
2474 if err == io.EOF {
2475 b.sawEOF = true
2476 }
2477 b.conn.noteBodyReadFromHandler(b.stream, n, err)
2478 return
2479 }
2480
2481
2482
2483
2484
2485
2486
2487 type responseWriter struct {
2488 rws *responseWriterState
2489 }
2490
2491 type responseWriterState struct {
2492
2493 stream *stream
2494 req ServerRequest
2495 conn *serverConn
2496
2497
2498 bw *bufio.Writer
2499
2500
2501 handlerHeader Header
2502 snapHeader Header
2503 trailers []string
2504 status int
2505 wroteHeader bool
2506 sentHeader bool
2507 handlerDone bool
2508
2509 sentContentLen int64
2510 wroteBytes int64
2511
2512 closeNotifierMu sync.Mutex
2513 closeNotifierCh chan bool
2514 }
2515
2516 type chunkWriter struct{ rws *responseWriterState }
2517
2518 func (cw chunkWriter) Write(p []byte) (n int, err error) {
2519 n, err = cw.rws.writeChunk(p)
2520 if err == errStreamClosed {
2521
2522
2523 err = cw.rws.stream.closeErr
2524 }
2525 return n, err
2526 }
2527
2528 func (rws *responseWriterState) hasTrailers() bool { return len(rws.trailers) > 0 }
2529
2530 func (rws *responseWriterState) hasNonemptyTrailers() bool {
2531 for _, trailer := range rws.trailers {
2532 if _, ok := rws.handlerHeader[trailer]; ok {
2533 return true
2534 }
2535 }
2536 return false
2537 }
2538
2539
2540
2541
2542 func (rws *responseWriterState) declareTrailer(k string) {
2543 k = textproto.CanonicalMIMEHeaderKey(k)
2544 if !httpguts.ValidTrailerHeader(k) {
2545
2546 rws.conn.logf("ignoring invalid trailer %q", k)
2547 return
2548 }
2549 if !slices.Contains(rws.trailers, k) {
2550 rws.trailers = append(rws.trailers, k)
2551 }
2552 }
2553
2554 const TimeFormat = "Mon, 02 Jan 2006 15:04:05 GMT"
2555
2556
2557
2558
2559
2560
2561
2562 func (rws *responseWriterState) writeChunk(p []byte) (n int, err error) {
2563 if !rws.wroteHeader {
2564 rws.writeHeader(200)
2565 }
2566
2567 if rws.handlerDone {
2568 rws.promoteUndeclaredTrailers()
2569 }
2570
2571 isHeadResp := rws.req.Method == "HEAD"
2572 if !rws.sentHeader {
2573 rws.sentHeader = true
2574 var ctype, clen string
2575 if clen = rws.snapHeader.Get("Content-Length"); clen != "" {
2576 rws.snapHeader.Del("Content-Length")
2577 if cl, err := strconv.ParseUint(clen, 10, 63); err == nil {
2578 rws.sentContentLen = int64(cl)
2579 } else {
2580 clen = ""
2581 }
2582 }
2583 _, hasContentLength := rws.snapHeader["Content-Length"]
2584 if !hasContentLength && clen == "" && rws.handlerDone && bodyAllowedForStatus(rws.status) && (len(p) > 0 || !isHeadResp) {
2585 clen = strconv.Itoa(len(p))
2586 }
2587 _, hasContentType := rws.snapHeader["Content-Type"]
2588
2589
2590 ce := rws.snapHeader.Get("Content-Encoding")
2591 hasCE := len(ce) > 0
2592 if !hasCE && !hasContentType && bodyAllowedForStatus(rws.status) && len(p) > 0 {
2593 ctype = internal.DetectContentType(p)
2594 }
2595 var date string
2596 if _, ok := rws.snapHeader["Date"]; !ok {
2597
2598 date = time.Now().UTC().Format(TimeFormat)
2599 }
2600
2601 for _, v := range rws.snapHeader["Trailer"] {
2602 foreachHeaderElement(v, rws.declareTrailer)
2603 }
2604
2605
2606
2607
2608
2609
2610 if _, ok := rws.snapHeader["Connection"]; ok {
2611 v := rws.snapHeader.Get("Connection")
2612 delete(rws.snapHeader, "Connection")
2613 if v == "close" {
2614 rws.conn.startGracefulShutdown()
2615 }
2616 }
2617
2618 endStream := (rws.handlerDone && !rws.hasTrailers() && len(p) == 0) || isHeadResp
2619 err = rws.conn.writeHeaders(rws.stream, &writeResHeaders{
2620 streamID: rws.stream.id,
2621 httpResCode: rws.status,
2622 h: rws.snapHeader,
2623 endStream: endStream,
2624 contentType: ctype,
2625 contentLength: clen,
2626 date: date,
2627 })
2628 if err != nil {
2629 return 0, err
2630 }
2631 if endStream {
2632 return 0, nil
2633 }
2634 }
2635 if isHeadResp {
2636 return len(p), nil
2637 }
2638 if len(p) == 0 && !rws.handlerDone {
2639 return 0, nil
2640 }
2641
2642
2643
2644 hasNonemptyTrailers := rws.hasNonemptyTrailers()
2645 endStream := rws.handlerDone && !hasNonemptyTrailers
2646 if len(p) > 0 || endStream {
2647
2648 if err := rws.conn.writeDataFromHandler(rws.stream, p, endStream); err != nil {
2649 return 0, err
2650 }
2651 }
2652
2653 if rws.handlerDone && hasNonemptyTrailers {
2654 err = rws.conn.writeHeaders(rws.stream, &writeResHeaders{
2655 streamID: rws.stream.id,
2656 h: rws.handlerHeader,
2657 trailers: rws.trailers,
2658 endStream: true,
2659 })
2660 return len(p), err
2661 }
2662 return len(p), nil
2663 }
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678 const TrailerPrefix = "Trailer:"
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701 func (rws *responseWriterState) promoteUndeclaredTrailers() {
2702 for k, vv := range rws.handlerHeader {
2703 if !strings.HasPrefix(k, TrailerPrefix) {
2704 continue
2705 }
2706 trailerKey := strings.TrimPrefix(k, TrailerPrefix)
2707 rws.declareTrailer(trailerKey)
2708 rws.handlerHeader[textproto.CanonicalMIMEHeaderKey(trailerKey)] = vv
2709 }
2710
2711 if len(rws.trailers) > 1 {
2712 slices.Sort(rws.trailers)
2713 }
2714 }
2715
2716 func (w *responseWriter) SetReadDeadline(deadline time.Time) error {
2717 st := w.rws.stream
2718 if !deadline.IsZero() && deadline.Before(time.Now()) {
2719
2720
2721 st.onReadTimeout()
2722 return nil
2723 }
2724 w.rws.conn.sendServeMsg(func(sc *serverConn) {
2725 if st.readDeadline != nil {
2726 if !st.readDeadline.Stop() {
2727
2728 return
2729 }
2730 }
2731 if deadline.IsZero() {
2732 st.readDeadline = nil
2733 } else if st.readDeadline == nil {
2734 st.readDeadline = time.AfterFunc(deadline.Sub(time.Now()), st.onReadTimeout)
2735 } else {
2736 st.readDeadline.Reset(deadline.Sub(time.Now()))
2737 }
2738 })
2739 return nil
2740 }
2741
2742 func (w *responseWriter) SetWriteDeadline(deadline time.Time) error {
2743 st := w.rws.stream
2744 if !deadline.IsZero() && deadline.Before(time.Now()) {
2745
2746
2747 st.onWriteTimeout()
2748 return nil
2749 }
2750 w.rws.conn.sendServeMsg(func(sc *serverConn) {
2751 if st.writeDeadline != nil {
2752 if !st.writeDeadline.Stop() {
2753
2754 return
2755 }
2756 }
2757 if deadline.IsZero() {
2758 st.writeDeadline = nil
2759 } else if st.writeDeadline == nil {
2760 st.writeDeadline = time.AfterFunc(deadline.Sub(time.Now()), st.onWriteTimeout)
2761 } else {
2762 st.writeDeadline.Reset(deadline.Sub(time.Now()))
2763 }
2764 })
2765 return nil
2766 }
2767
2768 func (w *responseWriter) EnableFullDuplex() error {
2769
2770 return nil
2771 }
2772
2773 func (w *responseWriter) Flush() {
2774 w.FlushError()
2775 }
2776
2777 func (w *responseWriter) FlushError() error {
2778 rws := w.rws
2779 if rws == nil {
2780 panic("Header called after Handler finished")
2781 }
2782 var err error
2783 if rws.bw.Buffered() > 0 {
2784 err = rws.bw.Flush()
2785 } else {
2786
2787
2788
2789
2790 _, err = chunkWriter{rws}.Write(nil)
2791 if err == nil {
2792 select {
2793 case <-rws.stream.cw:
2794 err = rws.stream.closeErr
2795 default:
2796 }
2797 }
2798 }
2799 return err
2800 }
2801
2802 func (w *responseWriter) CloseNotify() <-chan bool {
2803 rws := w.rws
2804 if rws == nil {
2805 panic("CloseNotify called after Handler finished")
2806 }
2807 rws.closeNotifierMu.Lock()
2808 ch := rws.closeNotifierCh
2809 if ch == nil {
2810 ch = make(chan bool, 1)
2811 rws.closeNotifierCh = ch
2812 cw := rws.stream.cw
2813 go func() {
2814 cw.Wait()
2815 ch <- true
2816 }()
2817 }
2818 rws.closeNotifierMu.Unlock()
2819 return ch
2820 }
2821
2822 func (w *responseWriter) Header() Header {
2823 rws := w.rws
2824 if rws == nil {
2825 panic("Header called after Handler finished")
2826 }
2827 if rws.handlerHeader == nil {
2828 rws.handlerHeader = make(Header)
2829 }
2830 return rws.handlerHeader
2831 }
2832
2833
2834 func checkWriteHeaderCode(code int) {
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845 if code < 100 || code > 999 {
2846 panic(fmt.Sprintf("invalid WriteHeader code %v", code))
2847 }
2848 }
2849
2850 func (w *responseWriter) WriteHeader(code int) {
2851 rws := w.rws
2852 if rws == nil {
2853 panic("WriteHeader called after Handler finished")
2854 }
2855 rws.writeHeader(code)
2856 }
2857
2858 func (rws *responseWriterState) writeHeader(code int) {
2859 if rws.wroteHeader {
2860 return
2861 }
2862
2863 checkWriteHeaderCode(code)
2864
2865
2866 if code >= 100 && code <= 199 {
2867
2868 h := rws.handlerHeader
2869
2870 _, cl := h["Content-Length"]
2871 _, te := h["Transfer-Encoding"]
2872 if cl || te {
2873 h = cloneHeader(h)
2874 h.Del("Content-Length")
2875 h.Del("Transfer-Encoding")
2876 }
2877
2878 rws.conn.writeHeaders(rws.stream, &writeResHeaders{
2879 streamID: rws.stream.id,
2880 httpResCode: code,
2881 h: h,
2882 endStream: rws.handlerDone && !rws.hasTrailers(),
2883 })
2884
2885 return
2886 }
2887
2888 rws.wroteHeader = true
2889 rws.status = code
2890 if len(rws.handlerHeader) > 0 {
2891 rws.snapHeader = cloneHeader(rws.handlerHeader)
2892 }
2893 }
2894
2895 func cloneHeader(h Header) Header {
2896 h2 := make(Header, len(h))
2897 for k, vv := range h {
2898 vv2 := make([]string, len(vv))
2899 copy(vv2, vv)
2900 h2[k] = vv2
2901 }
2902 return h2
2903 }
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913 func (w *responseWriter) Write(p []byte) (n int, err error) {
2914 return w.write(len(p), p, "")
2915 }
2916
2917 func (w *responseWriter) WriteString(s string) (n int, err error) {
2918 return w.write(len(s), nil, s)
2919 }
2920
2921
2922 func (w *responseWriter) write(lenData int, dataB []byte, dataS string) (n int, err error) {
2923 rws := w.rws
2924 if rws == nil {
2925 panic("Write called after Handler finished")
2926 }
2927 if !rws.wroteHeader {
2928 w.WriteHeader(200)
2929 }
2930 if !bodyAllowedForStatus(rws.status) {
2931 return 0, ErrBodyNotAllowed
2932 }
2933 rws.wroteBytes += int64(len(dataB)) + int64(len(dataS))
2934 if rws.sentContentLen != 0 && rws.wroteBytes > rws.sentContentLen {
2935
2936 return 0, errors.New("http2: handler wrote more than declared Content-Length")
2937 }
2938
2939 if dataB != nil {
2940 return rws.bw.Write(dataB)
2941 } else {
2942 return rws.bw.WriteString(dataS)
2943 }
2944 }
2945
2946 func (w *responseWriter) handlerDone() {
2947 rws := w.rws
2948 rws.handlerDone = true
2949 w.Flush()
2950 w.rws = nil
2951 responseWriterStatePool.Put(rws)
2952 }
2953
2954
2955 var (
2956 ErrRecursivePush = errors.New("http2: recursive push not allowed")
2957 ErrPushLimitReached = errors.New("http2: push would exceed peer's SETTINGS_MAX_CONCURRENT_STREAMS")
2958 )
2959
2960 func (w *responseWriter) Push(target, method string, header Header) error {
2961 st := w.rws.stream
2962 sc := st.sc
2963 sc.serveG.checkNotOn()
2964
2965
2966
2967 if st.isPushed() {
2968 return ErrRecursivePush
2969 }
2970
2971
2972 if method == "" {
2973 method = "GET"
2974 }
2975 if header == nil {
2976 header = Header{}
2977 }
2978 wantScheme := "http"
2979 if w.rws.req.TLS != nil {
2980 wantScheme = "https"
2981 }
2982
2983
2984 u, err := url.Parse(target)
2985 if err != nil {
2986 return err
2987 }
2988 if u.Scheme == "" {
2989 if !strings.HasPrefix(target, "/") {
2990 return fmt.Errorf("target must be an absolute URL or an absolute path: %q", target)
2991 }
2992 u.Scheme = wantScheme
2993 u.Host = w.rws.req.Host
2994 } else {
2995 if u.Scheme != wantScheme {
2996 return fmt.Errorf("cannot push URL with scheme %q from request with scheme %q", u.Scheme, wantScheme)
2997 }
2998 if u.Host == "" {
2999 return errors.New("URL must have a host")
3000 }
3001 }
3002 for k := range header {
3003 if strings.HasPrefix(k, ":") {
3004 return fmt.Errorf("promised request headers cannot include pseudo header %q", k)
3005 }
3006
3007
3008
3009
3010 if asciiEqualFold(k, "content-length") ||
3011 asciiEqualFold(k, "content-encoding") ||
3012 asciiEqualFold(k, "trailer") ||
3013 asciiEqualFold(k, "te") ||
3014 asciiEqualFold(k, "expect") ||
3015 asciiEqualFold(k, "host") {
3016 return fmt.Errorf("promised request headers cannot include %q", k)
3017 }
3018 }
3019 if err := checkValidHTTP2RequestHeaders(header); err != nil {
3020 return err
3021 }
3022
3023
3024
3025
3026 if method != "GET" && method != "HEAD" {
3027 return fmt.Errorf("method %q must be GET or HEAD", method)
3028 }
3029
3030 msg := &startPushRequest{
3031 parent: st,
3032 method: method,
3033 url: u,
3034 header: cloneHeader(header),
3035 done: sc.srv.getErrChan(),
3036 }
3037
3038 select {
3039 case <-sc.doneServing:
3040 return errClientDisconnected
3041 case <-st.cw:
3042 return errStreamClosed
3043 case sc.serveMsgCh <- msg:
3044 }
3045
3046 select {
3047 case <-sc.doneServing:
3048 return errClientDisconnected
3049 case <-st.cw:
3050 return errStreamClosed
3051 case err := <-msg.done:
3052 sc.srv.putErrChan(msg.done)
3053 return err
3054 }
3055 }
3056
3057 type startPushRequest struct {
3058 parent *stream
3059 method string
3060 url *url.URL
3061 header Header
3062 done chan error
3063 }
3064
3065 func (sc *serverConn) startPush(msg *startPushRequest) {
3066 sc.serveG.check()
3067
3068
3069
3070
3071 if msg.parent.state != stateOpen && msg.parent.state != stateHalfClosedRemote {
3072
3073 msg.done <- errStreamClosed
3074 return
3075 }
3076
3077
3078 if !sc.pushEnabled {
3079 msg.done <- ErrNotSupported
3080 return
3081 }
3082
3083
3084
3085
3086 allocatePromisedID := func() (uint32, error) {
3087 sc.serveG.check()
3088
3089
3090
3091 if !sc.pushEnabled {
3092 return 0, ErrNotSupported
3093 }
3094
3095 if sc.curPushedStreams+1 > sc.clientMaxStreams {
3096 return 0, ErrPushLimitReached
3097 }
3098
3099
3100
3101
3102
3103 if sc.maxPushPromiseID+2 >= 1<<31 {
3104 sc.startGracefulShutdownInternal()
3105 return 0, ErrPushLimitReached
3106 }
3107 sc.maxPushPromiseID += 2
3108 promisedID := sc.maxPushPromiseID
3109
3110
3111
3112
3113
3114
3115 promised := sc.newStream(promisedID, msg.parent.id, stateHalfClosedRemote, defaultRFC9218Priority(sc.priorityAware && !sc.hasIntermediary))
3116 rw, req, err := sc.newWriterAndRequestNoBody(promised, httpcommon.ServerRequestParam{
3117 Method: msg.method,
3118 Scheme: msg.url.Scheme,
3119 Authority: msg.url.Host,
3120 Path: msg.url.RequestURI(),
3121 Header: cloneHeader(msg.header),
3122 })
3123 if err != nil {
3124
3125 panic(fmt.Sprintf("newWriterAndRequestNoBody(%+v): %v", msg.url, err))
3126 }
3127
3128 sc.curHandlers++
3129 go sc.runHandler(rw, req, sc.handler.ServeHTTP)
3130 return promisedID, nil
3131 }
3132
3133 sc.writeFrame(FrameWriteRequest{
3134 write: &writePushPromise{
3135 streamID: msg.parent.id,
3136 method: msg.method,
3137 url: msg.url,
3138 h: msg.header,
3139 allocatePromisedID: allocatePromisedID,
3140 },
3141 stream: msg.parent,
3142 done: msg.done,
3143 })
3144 }
3145
3146
3147
3148 func foreachHeaderElement(v string, fn func(string)) {
3149 v = textproto.TrimString(v)
3150 if v == "" {
3151 return
3152 }
3153 if !strings.Contains(v, ",") {
3154 fn(v)
3155 return
3156 }
3157 for f := range strings.SplitSeq(v, ",") {
3158 if f = textproto.TrimString(f); f != "" {
3159 fn(f)
3160 }
3161 }
3162 }
3163
3164
3165 var connHeaders = []string{
3166 "Connection",
3167 "Keep-Alive",
3168 "Proxy-Connection",
3169 "Transfer-Encoding",
3170 "Upgrade",
3171 }
3172
3173
3174
3175
3176 func checkValidHTTP2RequestHeaders(h Header) error {
3177 for _, k := range connHeaders {
3178 if _, ok := h[k]; ok {
3179 return fmt.Errorf("request header %q is not valid in HTTP/2", k)
3180 }
3181 }
3182 te := h["Te"]
3183 if len(te) > 0 && (len(te) > 1 || (te[0] != "trailers" && te[0] != "")) {
3184 return errors.New(`request header "TE" may only be "trailers" in HTTP/2`)
3185 }
3186 return nil
3187 }
3188
3189 type serve400Handler struct {
3190 err error
3191 }
3192
3193 func (handler serve400Handler) ServeHTTP(w *ResponseWriter, r *ServerRequest) {
3194 const statusBadRequest = 400
3195
3196
3197 h := w.Header()
3198 h.Del("Content-Length")
3199 h.Set("Content-Type", "text/plain; charset=utf-8")
3200 h.Set("X-Content-Type-Options", "nosniff")
3201 w.WriteHeader(statusBadRequest)
3202 fmt.Fprintln(w, handler.err.Error())
3203 }
3204
3205
3206
3207
3208 func h1ServerKeepAlivesDisabled(hs ServerConfig) bool {
3209 return !hs.DoKeepAlives()
3210 }
3211
3212 func (sc *serverConn) countError(name string, err error) error {
3213 if sc == nil || sc.srv == nil {
3214 return err
3215 }
3216 f := sc.countErrorFunc
3217 if f == nil {
3218 return err
3219 }
3220 var typ string
3221 var code ErrCode
3222 switch e := err.(type) {
3223 case ConnectionError:
3224 typ = "conn"
3225 code = ErrCode(e)
3226 case StreamError:
3227 typ = "stream"
3228 code = ErrCode(e.Code)
3229 default:
3230 return err
3231 }
3232 codeStr := errCodeName[code]
3233 if codeStr == "" {
3234 codeStr = strconv.Itoa(int(code))
3235 }
3236 f(fmt.Sprintf("%s_%s_%s", typ, codeStr, name))
3237 return err
3238 }
3239
View as plain text