@@ -9,6 +9,15 @@ import (
9
9
)
10
10
11
11
// ResponseWrapper captures response data for transformation
12
+ //
13
+ // This is a simple example for demonstration purposes and is not intended for
14
+ // production use. Limitations:
15
+ // - Does not preserve optional HTTP interfaces (http.Hijacker, http.Flusher, http.Pusher)
16
+ // - Not safe for concurrent writes from multiple goroutines within the same request
17
+ // - No memory limits on buffered content
18
+ //
19
+ // Each request gets its own ResponseWrapper instance, so different requests won't
20
+ // interfere with each other.
12
21
type ResponseWrapper struct {
13
22
original httpserver.ResponseWriter
14
23
buffer * bytes.Buffer
@@ -63,9 +72,8 @@ func (rw *ResponseWrapper) Size() int {
63
72
64
73
// transformToJSON wraps non-JSON content in a JSON response
65
74
func transformToJSON (data []byte ) ([]byte , error ) {
66
- // First check if the data is valid JSON regardless of content type
67
- var test any
68
- if json .Unmarshal (data , & test ) == nil {
75
+ // Use json.Valid for efficient validation without unmarshaling
76
+ if json .Valid (data ) {
69
77
return data , nil // Valid JSON, return as-is
70
78
}
71
79
@@ -91,6 +99,25 @@ func New() httpserver.HandlerFunc {
91
99
92
100
// RESPONSE PHASE: Transform response to JSON
93
101
originalData := wrapper .buffer .Bytes ()
102
+ statusCode := wrapper .Status ()
103
+ if statusCode == 0 {
104
+ statusCode = http .StatusOK
105
+ }
106
+
107
+ // Copy headers to original writer
108
+ originalWriter := wrapper .original
109
+ for key , values := range wrapper .Header () {
110
+ for _ , value := range values {
111
+ originalWriter .Header ().Add (key , value )
112
+ }
113
+ }
114
+
115
+ // Check if this status code should have no body per HTTP spec
116
+ // 204 No Content and 304 Not Modified MUST NOT have a message body
117
+ if statusCode == http .StatusNoContent || statusCode == http .StatusNotModified {
118
+ originalWriter .WriteHeader (statusCode )
119
+ return
120
+ }
94
121
95
122
// Transform captured data to JSON
96
123
if len (originalData ) == 0 && wrapper .status == 0 {
@@ -104,22 +131,10 @@ func New() httpserver.HandlerFunc {
104
131
jsonData = []byte (`{"error":"Unable to encode response"}` )
105
132
}
106
133
107
- // Copy headers to original writer
108
- originalWriter := wrapper .original
109
- for key , values := range wrapper .Header () {
110
- for _ , value := range values {
111
- originalWriter .Header ().Add (key , value )
112
- }
113
- }
114
-
115
134
// Ensure JSON content type
116
135
originalWriter .Header ().Set ("Content-Type" , "application/json" )
117
136
118
137
// Write status and transformed data
119
- statusCode := wrapper .Status ()
120
- if statusCode == 0 {
121
- statusCode = http .StatusOK
122
- }
123
138
originalWriter .WriteHeader (statusCode )
124
139
if _ , err := originalWriter .Write (jsonData ); err != nil {
125
140
// Response is already committed, cannot recover from write error
0 commit comments