Repository navigation
Expand file tree
/
Copy pathstream.go
More file actions
128 lines (120 loc) · 3.69 KB
/
Copy pathstream.go
File metadata and controls
128 lines (120 loc) · 3.69 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
package rhttp
import (
"errors"
"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, isHead: decoded.Method == "HEAD"}
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
// Scrub any Content-Length the handler set before panicking:
// the empty 500 below would otherwise declare a body it never
// sends, which strict peers treat as a truncated message.
rw.stream.Header.Del("Content-Length")
rw.declaredCL = -1
_ = 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). A
// response that doesn't match its declared Content-Length is reset
// instead — HAProxy detects both excess data and premature END_STREAM
// (commits d12edebe4, f572cc242) and each mismatch counts against the
// tunnel's glitch budget, so an explicit RST_STREAM is the only clean
// way out.
completed := false
switch {
case rw.clViolated:
slog.Error("rhttp: handler wrote more than declared Content-Length",
"stream_id", s.ID, "declared", rw.declaredCL)
s.Reset(http2.ErrCodeInternal)
case rw.err == nil:
switch err := rw.endStream(); {
case errors.Is(err, errShortContentLength):
slog.Error("rhttp: handler wrote less than declared Content-Length",
"stream_id", s.ID, "declared", rw.declaredCL, "written", rw.written)
s.Reset(http2.ErrCodeInternal)
case err == nil:
completed = true
}
}
// 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 completed && !s.DataDone() {
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
}