Skip to content

Commit 5c41119

Browse files
committed
Use Patch-* as header prefix
1 parent 128ce31 commit 5c41119

File tree

3 files changed

+52
-52
lines changed

3 files changed

+52
-52
lines changed

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -149,21 +149,21 @@ curl https://patchwork.example.com/public/./notifications?pubsub=true -d "broadc
149149

150150
## Passthrough Headers
151151

152-
Patchwork supports passthrough headers using the `PH-*` prefix system, allowing you to forward original request context between clients through the relay system.
152+
Patchwork supports passthrough headers using the `Patch-H-*` prefix system, allowing you to forward original request context between clients through the relay system.
153153

154154
### How It Works
155155

156-
- **Request Headers**: Headers starting with `PH-` represent original headers from the requester
157-
- **Response Headers**: `PH-*` headers are stripped of their prefix and passed through to the final receiver
158-
- **Automatic Headers**: Common headers like `User-Agent`, `Accept`, etc. are automatically converted to `PH-*` format
156+
- **Request Headers**: Headers starting with `Patch-H-` represent original headers from the requester
157+
- **Response Headers**: `Patch-H-*` headers are stripped of their prefix and passed through to the final receiver
158+
- **Automatic Headers**: Common headers like `User-Agent`, `Accept`, etc. are automatically converted to `Patch-H-*` format
159159

160160
### Example Usage
161161

162162
**Producer side** (sending headers):
163163
```bash
164164
curl -X POST \
165-
-H "PH-Original-IP: 192.168.1.100" \
166-
-H "PH-User-ID: alice123" \
165+
-H "Patch-H-Original-IP: 192.168.1.100" \
166+
-H "Patch-H-User-ID: alice123" \
167167
-H "User-Agent: MyApp/1.0" \
168168
-d "request data" \
169169
https://patchwork.example.com/public/queue/api
@@ -328,8 +328,8 @@ curl https://patchwork.example.com/public/./alerts?pubsub=true -d "system-update
328328
```bash
329329
# Send with context headers
330330
curl -X POST \
331-
-H "PH-Client-IP: 10.0.1.5" \
332-
-H "PH-Trace-ID: req-12345" \
331+
-H "Patch-H-Client-IP: 10.0.1.5" \
332+
-H "Patch-H-Trace-ID: req-12345" \
333333
-d "api-request" \
334334
https://patchwork.example.com/public/queue/api
335335

assets/index.html

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,20 +94,20 @@ <h3>Flexible Behavior</h3>
9494
curl {{.BaseURL}}/public/./notifications?pubsub=true -d "broadcast"</pre>
9595

9696
<h2>Passthrough Headers</h2>
97-
<p>Patchwork supports passthrough headers using the <code>PH-*</code> prefix system, allowing you to forward original request context between clients through the relay system.</p>
97+
<p>Patchwork supports passthrough headers using the <code>Patch-H-*</code> prefix system, allowing you to forward original request context between clients through the relay system.</p>
9898

9999
<h3>How It Works</h3>
100100
<ul>
101-
<li><strong>Request Headers:</strong> Headers starting with <code>PH-</code> represent original headers from the requester</li>
102-
<li><strong>Response Headers:</strong> <code>PH-*</code> headers are stripped of their prefix and passed through to the final receiver</li>
103-
<li><strong>Automatic Headers:</strong> Common headers like <code>User-Agent</code>, <code>Accept</code>, etc. are automatically converted to <code>PH-*</code> format</li>
101+
<li><strong>Request Headers:</strong> Headers starting with <code>Patch-H-</code> represent original headers from the requester</li>
102+
<li><strong>Response Headers:</strong> <code>Patch-H-*</code> headers are stripped of their prefix and passed through to the final receiver</li>
103+
<li><strong>Automatic Headers:</strong> Common headers like <code>User-Agent</code>, <code>Accept</code>, etc. are automatically converted to <code>Patch-H-*</code> format</li>
104104
</ul>
105105

106106
<h3>Example Usage</h3>
107107
<p><strong>Producer side</strong> (sending headers):</p>
108108
<pre>curl -X POST \
109-
-H "PH-Original-IP: 192.168.1.100" \
110-
-H "PH-User-ID: alice123" \
109+
-H "Patch-H-Original-IP: 192.168.1.100" \
110+
-H "Patch-H-User-ID: alice123" \
111111
-H "User-Agent: MyApp/1.0" \
112112
-d "request data" \
113113
{{.BaseURL}}/public/queue/api</pre>
@@ -274,8 +274,8 @@ <h3>Namespace Behavior Examples</h3>
274274
<p><strong>Using passthrough headers</strong>:</p>
275275
<pre># Send with context headers
276276
curl -X POST \
277-
-H "PH-Client-IP: 10.0.1.5" \
278-
-H "PH-Trace-ID: req-12345" \
277+
-H "Patch-H-Client-IP: 10.0.1.5" \
278+
-H "Patch-H-Trace-ID: req-12345" \
279279
-d "api-request" \
280280
{{.BaseURL}}/public/queue/api
281281

main.go

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -607,13 +607,13 @@ func (s *server) publicHandler(w http.ResponseWriter, r *http.Request) {
607607
path := vars["path"]
608608

609609
s.logRequest(r, "Public namespace access")
610-
610+
611611
// Determine namespace based on request path
612612
namespace := "p" // default for backward compatibility
613613
if strings.HasPrefix(r.URL.Path, "/public/") {
614614
namespace = "public"
615615
}
616-
616+
617617
s.handlePatch(w, r, namespace, "", path)
618618
}
619619

@@ -1145,66 +1145,66 @@ const (
11451145
func determinePathBehavior(path string, hasQueueParam bool) PathBehavior {
11461146
// Remove leading slash for consistent checking
11471147
cleanPath := strings.TrimPrefix(path, "/")
1148-
1148+
11491149
// Check for special control endpoints
11501150
if strings.HasPrefix(cleanPath, "_/") {
11511151
return BehaviorSpecial
11521152
}
1153-
1153+
11541154
// Check for explicit pubsub namespace
11551155
if strings.HasPrefix(cleanPath, "pubsub/") {
11561156
return BehaviorPubsub
11571157
}
1158-
1158+
11591159
// Check for explicit queue namespace
11601160
if strings.HasPrefix(cleanPath, "queue/") {
11611161
return BehaviorBlocking
11621162
}
1163-
1163+
11641164
// For flexible space (/./...), check query parameter
11651165
if strings.HasPrefix(cleanPath, "./") {
11661166
if hasQueueParam {
11671167
return BehaviorPubsub
11681168
}
11691169
return BehaviorBlocking
11701170
}
1171-
1171+
11721172
// Default to blocking behavior
11731173
return BehaviorBlocking
11741174
}
11751175

1176-
// processPassthroughHeaders handles PH-* headers for request/response
1176+
// processPassthroughHeaders handles Patch-H-* headers for request/response
11771177
func processPassthroughHeaders(headers http.Header, isRequest bool) map[string]string {
11781178
processed := make(map[string]string)
1179-
1179+
11801180
for key, values := range headers {
1181-
if strings.HasPrefix(key, "PH-") {
1181+
if strings.HasPrefix(key, "Patch-H-") {
11821182
if isRequest {
1183-
// For requests: PH-* headers represent original headers from requester
1184-
// Strip PH- prefix for the responder
1185-
originalKey := strings.TrimPrefix(key, "PH-")
1183+
// For requests: Patch-H-* headers represent original headers from requester
1184+
// Strip Patch-H- prefix for the responder
1185+
originalKey := strings.TrimPrefix(key, "Patch-H-")
11861186
if len(values) > 0 {
11871187
processed[originalKey] = values[0]
11881188
}
11891189
} else {
1190-
// For responses: PH-* headers should be stripped and passed through
1191-
originalKey := strings.TrimPrefix(key, "PH-")
1190+
// For responses: Patch-H-* headers should be stripped and passed through
1191+
originalKey := strings.TrimPrefix(key, "Patch-H-")
11921192
if len(values) > 0 {
11931193
processed[originalKey] = values[0]
11941194
}
11951195
}
11961196
}
11971197
}
1198-
1198+
11991199
return processed
12001200
}
12011201

1202-
// addPassthroughHeaders adds headers to the response, handling PH-* passthrough
1202+
// addPassthroughHeaders adds headers to the response, handling Patch-H-* passthrough
12031203
func addPassthroughHeaders(w http.ResponseWriter, streamHeaders map[string]string) {
12041204
for key, value := range streamHeaders {
1205-
if strings.HasPrefix(key, "PH-") {
1206-
// Strip PH- prefix and add as regular header
1207-
originalKey := strings.TrimPrefix(key, "PH-")
1205+
if strings.HasPrefix(key, "Patch-H-") {
1206+
// Strip Patch-H- prefix and add as regular header
1207+
originalKey := strings.TrimPrefix(key, "Patch-H-")
12081208
w.Header().Set(originalKey, value)
12091209
} else {
12101210
// Regular headers pass through as-is
@@ -1213,38 +1213,38 @@ func addPassthroughHeaders(w http.ResponseWriter, streamHeaders map[string]strin
12131213
}
12141214
}
12151215

1216-
// prepareRequestHeaders prepares headers for the stream, adding PH-* prefixes for passthrough
1216+
// prepareRequestHeaders prepares headers for the stream, adding Patch-H-* prefixes for passthrough
12171217
func prepareRequestHeaders(r *http.Request) map[string]string {
12181218
headers := make(map[string]string)
1219-
1219+
12201220
// Add content type if present
12211221
contentType := r.Header.Get("Content-Type")
12221222
if contentType != "" {
12231223
headers["Content-Type"] = contentType
12241224
} else {
12251225
headers["Content-Type"] = "text/plain"
12261226
}
1227-
1228-
// Process passthrough headers (add PH- prefix to headers that should be passed through)
1227+
1228+
// Process passthrough headers (add Patch-H-* prefix to headers that should be passed through)
12291229
// For now, we'll pass through common headers like User-Agent, Accept, etc.
12301230
passthroughCandidates := []string{
12311231
"User-Agent", "Accept", "Accept-Language", "Accept-Encoding",
12321232
"Referer", "Origin", "X-Forwarded-For", "X-Real-IP",
12331233
}
1234-
1234+
12351235
for _, headerName := range passthroughCandidates {
12361236
if value := r.Header.Get(headerName); value != "" {
1237-
headers["PH-"+headerName] = value
1237+
headers["Patch-H-"+headerName] = value
12381238
}
12391239
}
1240-
1241-
// Also add any explicit PH-* headers from the request
1240+
1241+
// Also add any explicit Patch-H-* headers from the request
12421242
for key, values := range r.Header {
1243-
if strings.HasPrefix(key, "PH-") && len(values) > 0 {
1243+
if strings.HasPrefix(key, "Patch-H-") && len(values) > 0 {
12441244
headers[key] = values[0]
12451245
}
12461246
}
1247-
1247+
12481248
return headers
12491249
}
12501250

@@ -1357,7 +1357,7 @@ func (s *server) handlePatch(
13571357

13581358
// Determine behavior based on path structure and query params
13591359
// (queries, hasPubsubParam, and behavior already defined above)
1360-
1360+
13611361
// For backward compatibility, also check the old pubsub query parameter
13621362
_, pubsub := queries["pubsub"]
13631363
if behavior == BehaviorPubsub || pubsub {
@@ -1851,19 +1851,19 @@ func getHTTPServer(logger *slog.Logger, ctx context.Context, port int) *http.Ser
18511851
})
18521852

18531853
router.HandleFunc("/huproxy/{user}/{host}/{port}", huproxy.HuproxyHandler(server))
1854-
1855-
// Public namespace with new structure
1854+
1855+
// Public namespace with new structure
18561856
router.HandleFunc("/public/{path:.*}", server.publicHandler)
1857-
1857+
18581858
// Backward compatibility - old /p/ routes map to /public/
18591859
router.HandleFunc("/p/{path:.*}", server.publicHandler)
1860-
1860+
18611861
// Hook namespaces (unchanged)
18621862
router.HandleFunc("/h", server.forwardHookRootHandler)
18631863
router.HandleFunc("/h/{path:.*}", server.forwardHookHandler)
18641864
router.HandleFunc("/r", server.reverseHookRootHandler)
18651865
router.HandleFunc("/r/{path:.*}", server.reverseHookHandler)
1866-
1866+
18671867
// User namespaces with new structure
18681868
router.HandleFunc("/u/{username}/_/ntfy", server.userNtfyHandler)
18691869
router.HandleFunc("/u/{username}/_/{adminPath:.*}", server.userAdminHandler)

0 commit comments

Comments
 (0)