Repository navigation
Expand file tree
/
Copy pathhelpers.go
More file actions
148 lines (136 loc) · 4.36 KB
/
Copy pathhelpers.go
File metadata and controls
148 lines (136 loc) · 4.36 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
package main
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"log/slog"
"net/http"
"net/url"
"path"
"github.com/hekmon/httplog/v3"
)
// rewriteRequestURL rewrites request URL and Host header for proxying to backend
func rewriteRequestURL(req *http.Request, target *url.URL) {
req.URL.Scheme = target.Scheme
req.URL.Host = target.Host
req.Host = target.Host
req.URL.Path = path.Join(target.Path, req.URL.Path)
req.URL.RawPath = ""
targetQuery := target.RawQuery
if targetQuery == "" || req.URL.RawQuery == "" {
req.URL.RawQuery = targetQuery + req.URL.RawQuery
} else {
req.URL.RawQuery = targetQuery + "&" + req.URL.RawQuery
}
}
// maxRequestBodySize is the maximum allowed request body size (100 MB).
// This is a DoS safeguard, not a functional limit — conversation histories
// with base64-encoded images can legitimately be very large.
const maxRequestBodySize = 100 << 20 // 100 MB
// maxSSEEventSize is the maximum allowed size for a single buffered SSE event (10 MB).
// This prevents unbounded memory growth if the backend sends a malformed stream
// without proper event delimiters.
const maxSSEEventSize = 10 << 20 // 10 MB
// hopByHopHeaders lists headers that must not be forwarded by proxies (RFC 7230 §6.1)
var hopByHopHeaders = []string{
"Connection",
"Keep-Alive",
"Proxy-Authenticate",
"Proxy-Authorization",
"Te",
"Trailer",
"Transfer-Encoding",
"Upgrade",
}
// stripHopByHopHeaders removes hop-by-hop headers from the request before proxying
func stripHopByHopHeaders(req *http.Request) {
for _, h := range hopByHopHeaders {
req.Header.Del(h)
}
}
// applySamplingParams applies sampling parameters to request data
// If enforce is true, parameters are always set, overriding any client-provided values
func applySamplingParams(data map[string]any, samplingParams map[string]any, logger *slog.Logger, enforce bool) {
for k, v := range samplingParams {
if existing, ok := data[k]; ok && existing != nil {
if enforce {
logger.Debug("enforcing sampling parameter",
slog.Any("key", k),
slog.Any("old_value", data[k]),
slog.Any("new_value", v),
)
data[k] = v
} else {
logger.Debug("key already set in request, not modifying",
slog.Any("key", k),
slog.Any("value", data[k]),
slog.Any("default_value", v),
)
}
continue
}
data[k] = v
}
}
// copyHeaders copies response headers from backend response to the client ResponseWriter,
// stripping hop-by-hop headers that must not be forwarded by proxies.
func copyHeaders(w http.ResponseWriter, resp *http.Response) {
for _, h := range hopByHopHeaders {
resp.Header.Del(h)
}
for header, values := range resp.Header {
for _, value := range values {
w.Header().Add(header, value)
}
}
}
// httpError writes an OpenAI-compatible JSON error response
func httpError(ctx context.Context, w http.ResponseWriter, statusCode int) {
reqID := ctx.Value(httplog.ReqIDKey)
message := fmt.Sprintf("%s - check kimi-rp logs for more details (request id #%v)",
http.StatusText(statusCode),
reqID,
)
errResp := map[string]any{
"error": map[string]any{
"message": message,
"type": http.StatusText(statusCode),
"code": statusCode,
},
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(statusCode)
if err := json.NewEncoder(w).Encode(errResp); err != nil {
logger.Error("failed to write error response", slog.Any("error", err))
}
}
// readBodyStatusCode returns the appropriate HTTP status code for a body read error.
// Returns 413 for MaxBytesError, 500 for everything else.
func readBodyStatusCode(err error) int {
var maxBytesErr *http.MaxBytesError
if errors.As(err, &maxBytesErr) {
return http.StatusRequestEntityTooLarge
}
return http.StatusInternalServerError
}
// extractSSEDataJSON extracts the JSON payload from an SSE event.
// It handles multi-line events by scanning all lines for the first "data: " line
// that contains parseable JSON (skipping [DONE] and empty data lines).
// Returns nil if no valid JSON data line is found.
func extractSSEDataJSON(event []byte) []byte {
trimmed := bytes.TrimRight(event, "\n\r ")
lines := bytes.Split(trimmed, []byte("\n"))
for _, line := range lines {
if !bytes.HasPrefix(line, []byte("data: ")) {
continue
}
jsonPart := bytes.TrimSpace(line[6:])
if len(jsonPart) == 0 || bytes.Equal(jsonPart, []byte("[DONE]")) {
continue
}
return jsonPart
}
return nil
}