Repository navigation
Expand file tree
/
Copy pathcontext.go
More file actions
1323 lines (1259 loc) · 45.3 KB
/
Copy pathcontext.go
File metadata and controls
1323 lines (1259 loc) · 45.3 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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//go:build linux || darwin || dragonfly || freebsd || netbsd || openbsd || windows
package http
import (
"context"
"io"
"net"
stdhttp "net/http"
"net/url"
"strings"
"sync"
"sync/atomic"
"time"
)
// serverState is what a Parser keeps for the server driving it: the request
// being served, reachable without the parser's lock so that a close can
// cancel a request whose handler is still working on it, and when its
// response has to be written by.
type serverState struct {
liveContext atomic.Pointer[Context]
// writeDeadline is when the response being written stops being allowed
// to, under Config.WriteTimeout or as the handler set it with
// SetWriteDeadline, or zero. It is set before the handler runs and read
// by whatever writes the response.
writeDeadline time.Time
// bodyDeadline is what a handler's SetReadDeadline shares with the
// connection for a body that streams, allocated with the connection's
// first such body, and bodyArmed tells armRead to consult it. bodyArmed
// is guarded by the parser's mu.
bodyDeadline *bodyDeadline
bodyArmed bool
// quietSince is when the engine's shutdown first found the connection
// without a byte from its client, in Unix nanoseconds, or zero; see
// ServerHandler.OnShutdown. Guarded by the parser's mu.
quietSince int64
// earlyPending is how many bytes of the parser's input, from the front of
// what it has not parsed, arrived as TLS early data, 0-RTT (RFC 8446
// section 2.3): a request that begins while it is positive is one, and
// carries Early-Data, with only a safe method served; see
// Config.EarlyData. Early data is a prefix of the connection's
// application data, so a count tells a request pipelined behind a
// retained one apart from what arrives once the handshake is over.
// Guarded by the parser's mu.
earlyPending int64
// idleOverride is the idle timeout a handler gave the connection with
// Context.SetIdleTimeout, in nanoseconds, which stands in for Config's
// whenever the connection waits for its next request, or zero for
// Config's. It is atomic because a handler that retained its request may
// set it from any goroutine, while the connection's worker reads it.
idleOverride atomic.Int64
}
func (p *Parser) resetServerState() {
p.liveContext.Store(nil)
p.idleOverride.Store(0)
p.writeDeadline = time.Time{}
p.bodyDeadline, p.bodyArmed = nil, false
p.quietSince = 0
p.earlyPending = 0
}
// cancelLive cancels the request being served, whose streamed body has
// failed with err, and then fails the body. The request is marked cancelled
// first because OnBody's last call may be the last hold on it, and a request
// let go with that call would end unanswered rather than cancelled, leaving
// OnCancel's callbacks unheard; they run on the engine's handler pool, as a
// close's do. It reports false, leaving the body for the caller to fail, when
// no request is live.
func (p *Parser) cancelLive(stream *BodyStream, err error) bool {
context := p.liveContext.Load()
if context == nil {
return false
}
// As in OnClose: the generation is read while the Context is still this
// connection's, which the second load confirms.
gen := context.generation()
if p.liveContext.Load() != context {
return false
}
rest := context.markCancelled(err, gen)
if rest == nil {
return false
}
stream.fail(err, true)
cancelElsewhere(p.conn, rest)
return true
}
// bodyDeadline is a deadline a handler set with SetReadDeadline for the rest
// of its request's body to arrive by, which stands in for the one
// Config.ReadTimeout puts on it. The handler may set it from any goroutine,
// while the connection's worker re-arms the connection's read deadline at
// the end of every round it runs (see armRead); mu orders the two, so that
// neither undoes the other.
type bodyDeadline struct {
mu sync.Mutex
// open is the request whose body is still arriving and whose response is
// still open, the only one a deadline set now can apply to, or nil.
open *Context
// owner is the request the handler set at for, or nil.
owner *Context
at time.Time
// applied records that the connection's read deadline is the handler's,
// which armRead replaces once it no longer applies.
applied bool
}
// openBody records that context's body streams and is still arriving, for
// SetReadDeadline to bound. Callers hold the parser's mu.
func (p *Parser) openBody(context *Context) {
b := p.bodyDeadline
if b == nil {
b = new(bodyDeadline)
p.bodyDeadline = b
}
b.mu.Lock()
b.open, b.owner = context, nil
b.mu.Unlock()
p.bodyArmed = true
}
// closeBody forgets a deadline context's handler set, once the connection is
// done with its request: a Context the Reuse options recycle comes back as
// another request's, which must not find it. Callers hold the parser's mu.
func (p *Parser) closeBody(context *Context) {
if b := p.bodyDeadline; b != nil {
b.mu.Lock()
if b.owner == context {
b.owner = nil
}
b.mu.Unlock()
}
}
// set gives c's body until t to arrive, or as long as it takes when t is
// zero, applying it to the connection at once if the body is still
// arriving; otherwise armRead applies it, or it has nothing left to bound.
func (b *bodyDeadline) set(c *Context, t time.Time) error {
b.mu.Lock()
defer b.mu.Unlock()
if ctxState(c.word.Load()) != ctxOpen {
return ErrResponseWritten
}
b.owner, b.at = c, t
if b.open != c {
return nil
}
b.applied = true
return c.Conn.SetReadDeadline(t)
}
// blockServerState is the part of a requestBlock a server serves the request
// with: the Context it is answered through.
type blockServerState struct {
context Context
}
func (b *requestBlock) recycleServerState() { b.context.recycle() }
// A Context's lifetime is one atomic word, which the ordinary path through a
// request — begin, the handler's return, settle, and any Retain and Release —
// moves on with a compare-and-swap rather than under a lock. mu is left
// guarding what hands a callback from one goroutine to another: err, body,
// bodyDone and cancel.
//
// Two counts live in it, since a response and the objects it is written from
// do not end together. The response is held open by the handler's share and
// by every Retain not yet released, and is written when the last of them goes,
// as Retain describes. The Context, and the request and body it serves, stay
// the handler's for as long as anyone may still be using them: until the
// handler has returned, every Retain has been released — including one taken
// before the connection went and released after, when the response is long
// past — the connection has done with the request, and a cancellation or a
// response in the middle of being written has finished. Only then does the
// body's buffer go back, and do the Reuse options recycle anything.
const (
// ctxHolds counts the Retains not yet released.
ctxHolds uint64 = 1<<16 - 1
// ctxServed marks a handler that has not returned yet, and ctxShare its
// hold on the response, which its return gives back unless a Release too
// many gave it back first.
ctxServed uint64 = 1 << 16
ctxShare uint64 = 1 << 17
// ctxConn marks a request the connection has not finished with yet.
ctxConn uint64 = 1 << 18
// ctxCancelling and ctxFinishing mark a cancellation and the writing of a
// finished response that are still going on, on whichever goroutine is
// doing them.
ctxCancelling uint64 = 1 << 19
ctxFinishing uint64 = 1 << 20
// ctxResume marks the connection's duty to carry on, left by settle for
// whoever finishes the response, and ctxDelivering a body callback
// running on the connection's worker, which cannot take that duty itself.
ctxResume uint64 = 1 << 21
ctxDelivering uint64 = 1 << 22
// ctxHandled marks a handler that has returned, once the body it asked
// for while it ran has been handed over, and ctxHandover a handover owed
// then; see later.
ctxHandled uint64 = 1 << 23
ctxHandover uint64 = 1 << 24
// ctxReader marks a handler running on the goroutine that reads its
// multiplexed connection, until it returns; see onReader.
ctxReader uint64 = 1 << 25
// The state takes two bits, and the generation the rest.
ctxStateShift = 26
ctxStateMask uint64 = 3 << ctxStateShift
ctxGenShift = 28
// ctxAlive is what keeps the Context the request's.
ctxAlive = ctxHolds | ctxServed | ctxConn | ctxCancelling | ctxFinishing
)
// The states a response is in: ctxOpen is still being written, ctxDone has
// been, and ctxCancelled is one whose connection went first. A fresh Context,
// whose word is zero, is open.
const (
ctxOpen uint64 = iota
ctxDone
ctxCancelled
)
func ctxState(w uint64) uint64 { return w & ctxStateMask >> ctxStateShift }
func withState(w, state uint64) uint64 { return w&^ctxStateMask | state<<ctxStateShift }
// generation is the one the Context is serving, which a Context recycled
// since has moved past; see recycle.
func (c *Context) generation() uint64 { return c.word.Load() >> ctxGenShift }
// drop clears bits from the word, and ends the Context if nothing is left
// keeping it.
func (c *Context) drop(bits uint64) {
for {
w := c.word.Load()
next := w &^ bits
if c.word.CompareAndSwap(w, next) {
if w&ctxAlive != 0 && next&ctxAlive == 0 {
c.ended()
}
return
}
}
}
// ended runs once nothing can reach the Context any more but the server: the
// body's buffer goes back, and whatever the Reuse options recycle goes too.
func (c *Context) ended() {
if w := c.w; w != nil && w.hooks != nil && w.hooks.filters != nil {
// Filters the response never closed: it failed, or never ended.
err := w.err
if err == nil {
err = errBodyUnended
}
abortFilters(w.hooks.filters, err)
w.hooks.filters = nil
}
if w := c.w; w != nil && w.hooks != nil && w.hooks.body != nil {
err := w.err
if err == nil {
err = errBodyUnended
}
w.hooks.abortAsync(err)
}
if h := c.cancel.Load(); h != nil && h.stop != nil {
stop := h.stop
h.stop = nil
stop(nil)
}
if c.whole != nil {
c.whole.release()
}
if c.server != nil {
c.server.recycle(c)
} else if st := c.stream; st != nil {
st.unref()
} else if ender, ok := c.external.(StreamEnder); ok {
ender.RequestEnded()
}
}
// contextPool recycles Contexts on their own, for a server that recycles its
// Contexts but not its requests; see Config.ReuseContexts.
var contextPool = sync.Pool{New: func() any { return &Context{pooled: true} }}
// recycle clears the Context for another request. Its generation moves on,
// so that a cancellation meant for the request it served, which OnClose makes
// from a goroutine of its own, finds it serving another and leaves it alone.
// Every field but mu, deliverMu, word and pooled is reset, w to a cleared writer, which
// TestContextRecycleResetsEveryField holds it to.
//
// A recycled Context waiting for its next request reads as finished, so that
// a handler that wrongly holds on to it past its end finds that Retain,
// Release and the rest do nothing, as they do on any finished response,
// rather than acting on a Context with no connection; reopen hands it out.
func (c *Context) recycle() {
gen := c.generation() + 1
c.word.Store(withState(gen<<ctxGenShift, ctxDone))
c.Conn, c.Request = nil, nil
c.wrote, c.ends, c.streamed, c.status = true, 0, false, 0
if w := c.w; w != nil {
// Nothing reaches the writer but through the Context, so it is as
// free to serve another request, once cleared, as the Context is.
// The Context keeps it for its next request, which is as likely to
// want one.
w.reset()
}
c.stream, c.external = nil, nil
c.err, c.body, c.bodyDone = nil, nil, false
c.bodyHeld, c.handover = false, nil
if h := c.cancel.Load(); h != nil {
// Kept for the next request, which is as likely to want it.
h.reset()
}
c.server, c.parser, c.whole, c.block = nil, nil, nil, nil
if st := c.route; st != nil {
if st.shared {
c.route = nil
} else {
st.reset()
}
}
}
// SetValue keeps v with the request, for whatever serves it to read back
// with Value: an access log's entry, say, which middleware would otherwise
// attach with Request.WithContext, a copy of the request and a context value
// on every request. There is one slot, cleared when the request is done and
// its Context recycled, so code that shares it agrees on what it holds;
// several values go in a struct of the caller's. The slot lives with the
// Context's cancellation hooks, which a recycled Context keeps for its next
// request, so that the Context itself, which an HTTP/2 or HTTP/3 stream is
// allocated with, does not grow by it.
//
// The slot takes no lock, so that keeping a value costs a request a store
// and no more: SetValue is for whatever serves the request to call before
// it hands the request to any other goroutine, as a middleware does before
// it calls the next handler, and Value for whatever runs after that, the
// OnFinish hooks included, wherever they run. Two goroutines calling
// SetValue, or one calling SetValue while another calls Value, race.
func (c *Context) SetValue(v any) {
h := c.cancel.Load()
if h == nil {
c.mu.Lock()
h = c.hooks()
c.mu.Unlock()
}
h.value = v
}
// Value returns what SetValue last set for this request, or nil.
func (c *Context) Value() any {
if h := c.cancel.Load(); h != nil {
return h.value
}
return nil
}
// reopen readies a recycled Context for the request it is about to serve.
func (c *Context) reopen() {
c.word.Store(c.generation() << ctxGenShift)
c.wrote = false
}
// Retain keeps the response open past the handler's return, so that the
// request can be answered later, from another goroutine or from the body
// callbacks OnBody delivers. Every Retain needs a Release; the response is
// finished and handed to the connection when the last hold goes, which is
// the handler's own return when it retained nothing.
//
// On an HTTP/1 connection a retained request holds the ones pipelined behind
// it: they are parsed and served once it is released, so responses keep their
// order. Nothing bounds how long a request may be retained, so a handler that
// never releases holds its connection until the peer or a timeout ends it.
//
// Retaining a request whose response is already written does nothing, and
// neither does retaining one whose connection has gone, once the request has
// ended. A request whose connection went while its handler was still at it
// is held as one retained a moment before would be: nothing more is written,
// but the request and its Context stay the handler's until the Release, so a
// handler that retains and answers from elsewhere does not find its Context
// recycled under it, whenever the cancellation lands.
//
// A handler may hold at most 65535 Retains on one request at a time.
func (c *Context) Retain() { c.hold() }
// hold is Retain, reporting whether it took a hold.
func (c *Context) hold() bool {
for {
w := c.word.Load()
if ctxState(w) == ctxOpen || ctxState(w) == ctxCancelled && w&ctxAlive != 0 {
if w&ctxHolds == ctxHolds {
panic("http: too many Retains on one request")
}
if c.word.CompareAndSwap(w, w+1) {
return true
}
continue
}
return false
}
}
// Async retains the request and returns a func that runs fn and then
// releases it, for the handler to hand to a goroutine or a pool of its own:
//
// pool.Go(c.Async(func() {
// _ = c.Respond(200, "text/plain", slowQuery())
// }))
//
// The returned func runs fn once; calling it again does nothing, since the
// request may be another one's by then. The release is deferred, so a fn
// that panics still gives the hold back. If the request could not be
// retained, because its response is already written or its connection has
// gone and the request ended, fn still runs but nothing is released, so the
// func never gives back a hold it did not take. A func that is never called
// holds the request as a Retain never released does.
func (c *Context) Async(fn func()) func() {
held := c.hold()
var ran atomic.Bool
return func() {
if !ran.CompareAndSwap(false, true) {
return
}
if held {
defer c.Release()
}
fn()
}
}
// retain takes a hold on an open response, as Retain does, reporting whether
// it took one; a cancelled request it leaves alone, which OnBody relies on.
func (c *Context) retain() bool {
for {
w := c.word.Load()
if ctxState(w) != ctxOpen {
return false
}
if w&ctxHolds == ctxHolds {
panic("http: too many Retains on one request")
}
if c.word.CompareAndSwap(w, w+1) {
return true
}
}
}
// Release gives back one hold taken by Retain. The last one to go finishes
// the response, hands it to the connection and lets the connection carry on
// with whatever is pipelined behind it.
//
// Releasing more often than retaining ends the response early: the hold that
// serving the request took is given back by the handler's return, so a
// release that is not undoing a Retain is undoing that one. Releasing a
// response that is already written, or one whose connection has gone, writes
// nothing, so a Release on a failed request is safe and can be repeated. It
// still counts, though: a request whose connection went is the handler's
// until it has released every Retain it took, and no longer.
func (c *Context) Release() { c.giveBack(false) }
// Retained reports whether the response is still being held open by a Retain
// not yet released, including once the handler has returned.
func (c *Context) Retained() bool {
w := c.word.Load()
return ctxState(w) == ctxOpen && w&ctxHolds != 0
}
// Status returns the status of the response sent, or begun through the
// ResponseWriter methods, so far: 0 before any, and the 101 or 2xx of a
// tunnel opened by Upgrade or Connect. Together with Retained, it lets
// whatever called the handler tell, once it returns, that the response is
// settled, and what it was, without an OnFinish hook on every request: a
// response that Status says is sent and that is not Retained is finished,
// with that status. Call it on the goroutine that answers, or once that one
// is known to be done.
func (c *Context) Status() int {
if c.status != 0 {
return int(c.status)
}
if w := c.w; w != nil {
return w.status
}
return 0
}
// Err is why the request ended before its response did — the connection
// closing, a read timeout, or a body that could not be read — or nil while it
// is still going. Writing to a Context whose connection has gone fails with
// the same reason, so a handler that does not check Err does not corrupt
// anything; it only finds out later.
func (c *Context) Err() error {
c.mu.Lock()
defer c.mu.Unlock()
return c.err
}
// OnCancel registers fn to run if the request ends before its response does:
// the connection closing under it, a read timeout, or a body that failed. It
// is for a handler that retained the request and is working on it elsewhere,
// so it can stop and give back what it holds, and for one that waits in place,
// on a database or another server, so it can stop waiting: a peer that hangs
// up on it is heard while it waits, rather than once it returns, where the
// engine's backend can tell (see fib.HangupHandler). A body that fails part
// way cancels the request too, whether or not its handler takes the body
// through OnBody: the callback OnBody registered hears the body's failure, so
// a handler that takes the body there needs no OnCancel of its own, and every
// OnCancel callback runs as well, so that a middleware which registered one
// hears of it whatever the handler did with the body. A body refused for its
// size is the exception, being answered with 413; see LimitBody.
//
// Every callback registered is kept, and they run in the reverse of the order
// they were registered, as the OnFinish hooks do, so that a middleware which
// registers one before calling the handler hears of the cancellation after
// the handler's own, as it would have left the request.
//
// Each fn runs once, on the engine's handler pool rather than in the
// connection's round, and never after the response has been written. It still
// has to Release what it retained. Nothing more is written then, but the
// request, its body and the Context stay the handler's until it has, and are
// recycled or given back only after its last Release.
// Registering it on a request that has already ended runs it at once, and
// registering it once the response has been written does nothing.
func (c *Context) OnCancel(fn func(error)) {
if fn == nil {
return
}
c.mu.Lock()
// cancelWith moves the state on under mu, so what is read here is either
// before it, and fn is left for it to run, or after it, with err set.
switch ctxState(c.word.Load()) {
case ctxCancelled:
err := c.err
c.mu.Unlock()
fn(err)
return
case ctxDone:
c.mu.Unlock()
return
}
if h := c.hooks(); h.fn == nil {
h.fn = fn
} else {
h.add(fn)
}
c.mu.Unlock()
}
// cancelHooks is what hears of a request ending: OnCancel's callbacks, and
// the context Context handed out with what cancels it, once. They share a
// pointer so that a Context, which every HTTP/2 and HTTP/3 stream carries,
// grows by no more than one word for them.
type cancelHooks struct {
// fn is the first OnCancel callback, and more those registered after it,
// so that a request with one callback, the common case, keeps it in a
// field and makes no slice for it. more is a pointer so that the hooks,
// which a recycled Context clears on every request, grow by a word only.
fn func(error)
more *[]func(error)
ctx context.Context
stop context.CancelCauseFunc
// value is what SetValue set, which lives here rather than in Context;
// see SetValue.
value any
}
// reset clears the hooks for the Context's next request, keeping the room
// more grew to for one that registers as many callbacks.
func (h *cancelHooks) reset() {
more := h.more
*h = cancelHooks{}
if more != nil {
clear(*more)
*more = (*more)[:0]
h.more = more
}
}
// add keeps a callback past the first.
func (h *cancelHooks) add(fn func(error)) {
if h.more == nil {
h.more = &[]func(error){fn}
return
}
*h.more = append(*h.more, fn)
}
// runCancel runs the callbacks OnCancel registered, in the reverse of the
// order they were registered.
func runCancel(fn func(error), more *[]func(error), err error) {
if more != nil {
for i := len(*more) - 1; i >= 0; i-- {
(*more)[i](err)
}
}
if fn != nil {
fn(err)
}
}
// hooks returns the Context's cancelHooks, made the first time they are
// needed. Callers hold mu.
func (c *Context) hooks() *cancelHooks {
h := c.cancel.Load()
if h == nil {
h = new(cancelHooks)
c.cancel.Store(h)
}
return h
}
// Context is a context.Context for the request, for code that hears of a
// request ending only through one, as httputil.ReverseProxy and
// database/sql do. Request.Context() is not cancelled; this one is, as
// net/http's request context is: when the request is cancelled, for any of
// the reasons OnCancel is told of, and otherwise once it has ended — the
// handler has returned and every Retain has been released. From then on its
// Done channel is closed, its Err is context.Canceled, and context.Cause
// reports the reason OnCancel was given, or context.Canceled for a request
// that ended in the ordinary way. Its values are Request.Context()'s.
//
// It is made the first time it is asked for, so a handler that does not ask
// pays nothing for it. It holds nothing of the Context, so it can outlive the
// request and goes on reporting how the request ended, even once the Reuse
// options have the Context serving another. Asked for once the request has
// ended, it returns a context that is already cancelled.
func (c *Context) Context() context.Context {
c.mu.Lock()
defer c.mu.Unlock()
if h := c.cancel.Load(); h != nil && h.ctx != nil {
return h.ctx
}
// cancelWith moves the state on under mu, as OnCancel relies on too.
w := c.word.Load()
if ctxState(w) != ctxOpen && w&ctxAlive == 0 {
return endedContext
}
parent := context.Background()
if c.Request != nil {
parent = c.Request.Context()
}
ctx, stop := context.WithCancelCause(parent)
h := c.hooks()
if ctxState(w) == ctxCancelled {
stop(c.err)
} else {
h.stop = stop
}
h.ctx = ctx
return ctx
}
// endedContext is what Context returns for a request that has ended already.
var endedContext = func() context.Context {
ctx, stop := context.WithCancel(context.Background())
stop()
return ctx
}()
// BodyComplete reports whether the whole request body has arrived: always
// for a body read whole before the handler ran, and for a streamed one once
// its last byte has been taken off the connection; see
// Config.StreamRequestBody. A handler that finds it complete can
// read Request.Body to the end without meeting ErrWouldBlock, and one that
// does not can take the body through OnBody as it arrives. A body that
// streams is never in Body, complete or not: whether Body holds the body is
// whether RequestBody is nil.
func (c *Context) BodyComplete() bool {
if stream := c.RequestBody(); stream != nil {
return stream.Complete()
}
return true
}
// Body returns the request body that arrived whole before the handler ran, as
// every body does that does not stream (see Config.StreamRequestBody): the
// server's own buffer, rather than a copy read out of Request.Body, which
// io.ReadAll makes by growing a buffer of its own a piece at a time. It is
// the whole body whatever has been read from Request.Body already, and
// reading Request.Body afterwards still returns it.
//
// The bytes stay the handler's for as long as the body does: until the
// response is finished, at the handler's return or at the last Release of a
// request it retained. They must not be changed, nor kept past that; keep a
// copy instead. They can be answered with as they are: a body that is the
// request's own is the one buffer the server recycles, so a protocol that
// goes on reading the body after the response — HTTP/3, until the peer
// acknowledges it — copies it. A scratch buffer the handler fills again goes
// through RespondCopy or WriteResponseCopy instead; see Response.Body.
//
// It returns nil for a request without a body, and for one whose body streams,
// which RequestBody and OnBody read instead.
func (c *Context) Body() []byte {
if b := c.whole; b != nil && !b.released {
return b.data
}
return nil
}
// Query returns the first value of the query parameter name, as
// Request.URL.Query().Get(name) does, decoded the same way and skipping the
// same malformed pairs, but without building the map Query builds on every
// call: a value that needs no decoding is a slice of the request's raw query,
// so looking one up allocates nothing. It returns "" when the parameter is
// absent, as Get does.
func (c *Context) Query(name string) string {
if c.Request == nil || c.Request.URL == nil {
return ""
}
value, _ := lookupQuery(c.Request.URL.RawQuery, name)
return value
}
// lookupQuery finds name's first value in query, as url.ParseQuery followed
// by Get finds it: pairs are split at '&', a pair holding a ';' is skipped,
// and so is one whose key or value does not decode.
func lookupQuery(query, name string) (string, bool) {
for query != "" {
var pair string
pair, query, _ = strings.Cut(query, "&")
if pair == "" || strings.IndexByte(pair, ';') >= 0 {
continue
}
key, value, _ := strings.Cut(pair, "=")
if !queryKeyIs(key, name) {
continue
}
if strings.IndexByte(value, '%') < 0 && strings.IndexByte(value, '+') < 0 {
return value, true
}
if decoded, err := url.QueryUnescape(value); err == nil {
return decoded, true
}
}
return "", false
}
// queryKeyIs reports whether the raw key of a query pair decodes to name. A
// key that does not decode is no key at all, as url.ParseQuery skips it.
func queryKeyIs(key, name string) bool {
if strings.IndexByte(key, '%') < 0 && strings.IndexByte(key, '+') < 0 {
return key == name
}
decoded, err := url.QueryUnescape(key)
return err == nil && decoded == name
}
// OnBody delivers the request body to fn as it arrives, rather than through
// Request.Body, the way a websocket handler's OnFrame is given a message
// frame by frame. fin marks the last call, and err a body that will not be
// finished — the connection went, it outgrew MaxStreamedBodyBytes, its
// framing was broken, or the body was closed — which is also a last call.
// data is only valid for the duration of the call.
//
// OnBody never calls fn itself and never waits: it registers fn and returns,
// and no goroutine is started for fn. What of the body has already arrived —
// all of it, when BodyComplete says so — is handed to fn once the handler
// returns, on the goroutine that ran it; when OnBody is called after the
// handler has returned, it is handed over on the engine's handler pool, since
// no read of the connection will bring it again. The rest follows as the
// connection is read: on the worker that reads an HTTP/1 connection, and on
// the engine's handler pool for a multiplexed one, whose reader serves every
// stream on it.
// The calls are one at a time and in order, so a handler can take a body of
// any size without a goroutine and without it being buffered: what holds the
// connection back is fn itself.
//
// OnBody retains the request, and the server releases it once fn has
// returned from its last call. A handler that answers from fn therefore
// needs neither Retain nor Release; it answers when fin arrives, and the
// response is written after that call:
//
// c.OnBody(func(data []byte, fin bool, err error) {
// if err != nil {
// file.Close()
// return
// }
// file.Write(data)
// if fin {
// c.Respond(200, "text/plain", []byte("stored"))
// }
// })
//
// A handler that answers elsewhere, after the body has arrived, takes a
// Retain of its own in fn and releases it when it has answered.
//
// Request.Body is not readable once OnBody has taken it over. Calling OnBody
// twice replaces the callback, and calling it after the body has ended, or
// after the response has been written, delivers nothing, except on a request
// that has already failed, whose error it reports.
func (c *Context) OnBody(fn BodyFunc) {
if fn == nil {
return
}
c.mu.Lock()
if c.bodyDone {
err := c.failureLocked()
c.mu.Unlock()
if err != nil {
c.later(func(bool) { fn(nil, true, err) })
}
return
}
if !c.bodyHeld && !c.retain() {
// The response is written already, and the body with it, or the
// request was cancelled, which a handler running beside the
// connection's reader may find it was a moment ago: cancelWith has
// then already looked for a callback and found none, and the error
// is reported here instead.
err := c.failureLocked()
c.mu.Unlock()
if err != nil {
c.later(func(bool) { fn(nil, true, err) })
}
return
}
c.body, c.bodyHeld = fn, true
c.mu.Unlock()
if stream := c.RequestBody(); stream != nil {
// The body is still arriving; the stream hands it on from here,
// starting with whatever it has already taken off the connection.
if stream.setSink(c.deliverBody, c.handling()) {
c.later(stream.drain)
}
return
}
// The body was read whole before the handler ran, so it is all here. The
// callback may keep none of it past its call, so a body still in the
// server's buffer is handed over from there rather than copied out; the
// hold taken above keeps the buffer until then.
var data []byte
if whole, ok := c.Request.Body.(*wholeBody); ok && !whole.released {
data = whole.data[whole.read:]
whole.read = len(whole.data)
} else if c.Request.Body != nil {
data, _ = io.ReadAll(c.Request.Body)
}
if c.Request.Body != nil {
c.Request.Body = emptyBody()
}
c.later(func(worker bool) { c.deliverBody(data, true, nil, worker) })
}
// failureLocked is what a body callback registered after the request failed
// is told, or nil for a request that has not failed: the error its streamed
// body failed with, which may be what ended the connection, as a body that
// outgrew MaxStreamedBodyBytes does, and otherwise why the request was
// cancelled. Callers hold mu.
func (c *Context) failureLocked() error {
if c.err == nil {
return nil
}
if stream := c.RequestBody(); stream != nil {
if err := stream.failure(); err != nil {
return err
}
}
return c.err
}
// handling reports whether the handler is still running, which later then
// leaves a handover to its return.
func (c *Context) handling() bool {
w := c.word.Load()
return w&ctxServed != 0 && w&ctxHandled == 0
}
// later runs handover once the handler has returned: on the handler's own
// goroutine, after it returns, when it is still running, and on the engine's
// handler pool when it is not, so that OnBody never calls a body callback
// itself and no goroutine is started for one. handover is told whether it
// runs on the connection's worker.
func (c *Context) later(handover func(worker bool)) {
c.mu.Lock()
for {
w := c.word.Load()
if w&ctxServed == 0 || w&ctxHandled != 0 {
break
}
if w&ctxHandover != 0 {
// Only one handover is ever owed: a second OnBody replaces the
// callback, which the first handover delivers to.
c.mu.Unlock()
return
}
c.handover = handover
if c.word.CompareAndSwap(w, w|ctxHandover) {
c.mu.Unlock()
return
}
c.handover = nil
}
c.mu.Unlock()
runOnStreams(c.Conn, func() { handover(false) })
}
// handled marks the handler's return, and hands over the body it asked for
// while it ran, on the handler's goroutine, which holds the connection's
// parser. It runs before the handler's hold
// is given back, so the response the body callbacks answer is still open.
func (c *Context) handled() {
var w uint64
for {
w = c.word.Load()
if c.word.CompareAndSwap(w, w|ctxHandled) {
break
}
}
if w&ctxHandover == 0 {
return
}
c.mu.Lock()
handover := c.handover
c.handover = nil
c.mu.Unlock()
handover(true)
}
// begin takes the hold that serving a request stands on, which the handler's
// return gives back, and reports whether the handler has it: taken now, or
// before, when the request was handed to a pool. conn says the connection
// has the request too, until endRequestLocked is done with it, and reader
// that the handler runs on the goroutine reading its multiplexed connection.
//
// A request cancelled before anything held it has ended already: its body
// has gone back and its Context may be serving another request, so its
// handler must not run.
func (c *Context) begin(conn, reader bool) bool {
add := ctxServed | ctxShare
if conn {
add |= ctxConn
}
if reader {
add |= ctxReader
}
for {
w := c.word.Load()
if w&ctxServed != 0 {
return true
}
if ctxState(w) != ctxOpen {
return false
}
if c.word.CompareAndSwap(w, w|add) {
return true
}
}
}
// onReader reports whether the handler runs on the goroutine that reads its
// multiplexed connection, which the stream pool makes it do once the
// connection has as many handlers running as it may. A response written
// there must not wait for the client to take what was sent before, since the
// goroutine that would read the client's go-ahead is the one waiting. Once
// the handler has returned, a response it retained is written from
// elsewhere, and may wait.
func (c *Context) onReader() bool { return c.word.Load()&ctxReader != 0 }
// returned is the handler's return, which gives back the hold serving the
// request took.
func (c *Context) returned() { c.giveBack(true) }
// giveBack gives back the handler's hold, when returning is set, or one taken
// by Retain, and when that was the last hold on an open response, writes it.
func (c *Context) giveBack(returning bool) {
for {
w := c.word.Load()
next := w
if returning {
// The handler is done with the goroutine it ran on too.
next &^= ctxServed | ctxReader
}
if ctxState(w) != ctxOpen {
// Nothing more is written, but the hold still counts for how long
// the request is the handler's.
if !returning {
if w&ctxHolds == 0 {
return
}
next--
}
if c.word.CompareAndSwap(w, next) {
if w&ctxAlive != 0 && next&ctxAlive == 0 {
c.ended()
}
return
}
continue
}
switch {
case returning:
next &^= ctxShare
case w&ctxHolds != 0:
next--
case w&ctxShare != 0:
// A Release too many takes the handler's share.
next &^= ctxShare
default:
return