-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstream.go
More file actions
104 lines (96 loc) · 2.63 KB
/
Copy pathstream.go
File metadata and controls
104 lines (96 loc) · 2.63 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
package rhttp
import (
"log/slog"
"net/http"
"net/url"
"runtime/debug"
"strings"
"github.com/oktalz/reverse-http/internal/h2"
"golang.org/x/net/http2"
)
// serveStream is the worker's per-stream goroutine: it builds an http.Request
// from the decoded HEADERS block + h2.Stream body, runs the configured
// http.Handler under panic recovery, and cleans up. The h2.Stream is
// registered before this function is called and is removed on exit.
func (c *connection) serveStream(s *h2.Stream, decoded *h2.DecodedHeaders) {
// Cleanup defer runs LAST.
defer func() {
c.RemoveStream(s.ID)
h2.HeaderPool.Put(decoded.Header)
h2.Release(s)
}()
// Panic-recovery defer runs FIRST. A panic in a handler is contained
// here and surfaced to the peer as 500 (or RST_STREAM(INTERNAL_ERROR)
// if response headers were already on the wire). The connection stays
// usable for subsequent streams.
rw := responseWriter{stream: s, conn: c.Conn}
defer func() {
if rec := recover(); rec != nil {
slog.Error(
"rhttp: panic serving stream",
"stream_id", s.ID,
"panic", rec,
"stack", string(debug.Stack()),
)
if !rw.wroteHeaders {
rw.status = http.StatusInternalServerError
_ = rw.writeHeaders(true)
} else {
s.Reset(http2.ErrCodeInternal)
}
}
}()
uri, ok := parseRequestURI(decoded.Path)
if !ok {
s.Reset(http2.ErrCodeProtocol)
return
}
body := s.Body()
req := &http.Request{
Method: decoded.Method,
URL: uri,
RequestURI: decoded.Path,
Proto: "HTTP/2.0",
ProtoMajor: 2,
Header: decoded.Header,
Host: decoded.Authority,
Body: body,
}
req = req.WithContext(s.Ctx)
c.handler.ServeHTTP(&rw, req)
// Close out our half of the stream (END_STREAM on response side).
if rw.err == nil {
_ = rw.endStream()
}
// If the peer hasn't finished sending the request body, tell them to
// stop rather than waiting indefinitely for END_STREAM (RFC 7540 §8.1).
if !s.DataDone() && rw.err == nil {
s.Reset(http2.ErrCodeNo)
}
// Unblock anything still waiting on the body.
s.CloseData()
}
// parseRequestURI is a minimal zero-alloc URL parser for HTTP/2 :path values.
// Returns a freshly-allocated *url.URL or (nil, false) on malformed paths.
func parseRequestURI(raw string) (*url.URL, bool) {
if raw == "" || (raw[0] != '/' && raw != "*") {
return nil, false
}
u := &url.URL{}
path := raw
if before, after, ok := strings.Cut(raw, "?"); ok {
u.RawQuery = after
path = before
}
decoded, err := url.PathUnescape(path)
if err != nil {
return nil, false
}
if decoded != path {
u.RawPath = path
u.Path = decoded
} else {
u.Path = path
}
return u, true
}