@@ -187,6 +187,7 @@ func (p *Server) handleHTTPConnection(conn net.Conn) {
187187 p .auditor .AuditRequest (audit.Request {
188188 Method : req .Method ,
189189 URL : req .URL .String (),
190+ Host : req .Host ,
190191 Allowed : result .Allowed ,
191192 Rule : result .Rule ,
192193 })
@@ -237,6 +238,7 @@ func (p *Server) handleTLSConnection(conn net.Conn) {
237238 p .auditor .AuditRequest (audit.Request {
238239 Method : req .Method ,
239240 URL : req .URL .String (),
241+ Host : req .Host ,
240242 Allowed : result .Allowed ,
241243 Rule : result .Rule ,
242244 })
@@ -270,6 +272,23 @@ func (p *Server) forwardRequest(conn net.Conn, req *http.Request, https bool) {
270272 Path : req .URL .Path ,
271273 RawQuery : req .URL .RawQuery ,
272274 }
275+
276+ //var requestBodyBytes []byte
277+ //{
278+ // var err error
279+ // requestBodyBytes, err = io.ReadAll(req.Body)
280+ // if err != nil {
281+ // p.logger.Error("can't read response body", "error", err)
282+ // return
283+ // }
284+ // err = req.Body.Close()
285+ // if err != nil {
286+ // p.logger.Error("Failed to close HTTP response body", "error", err)
287+ // return
288+ // }
289+ // req.Body = io.NopCloser(bytes.NewBuffer(requestBodyBytes))
290+ //}
291+
273292 var body = req .Body
274293 if req .Method == http .MethodGet || req .Method == http .MethodHead {
275294 body = nil
@@ -300,6 +319,16 @@ func (p *Server) forwardRequest(conn net.Conn, req *http.Request, https bool) {
300319
301320 p .logger .Debug ("🔒 HTTPS Response" , "status code" , resp .StatusCode , "status" , resp .Status )
302321
322+ p .logger .Debug ("Forwarded Request" ,
323+ "method" , newReq .Method ,
324+ "host" , newReq .Host ,
325+ //"requestBodyBytes", string(requestBodyBytes),
326+ "URL" , newReq .URL ,
327+ )
328+ //for hKey, hVal := range newReq.Header {
329+ // p.logger.Debug("Forwarded Request Header", hKey, hVal)
330+ //}
331+
303332 // Read the body and explicitly set Content-Length header, otherwise client can hung up on the request.
304333 bodyBytes , err := io .ReadAll (resp .Body )
305334 if err != nil {
@@ -315,10 +344,26 @@ func (p *Server) forwardRequest(conn net.Conn, req *http.Request, https bool) {
315344 }
316345 resp .Body = io .NopCloser (bytes .NewBuffer (bodyBytes ))
317346
347+ // The downstream client (Claude) always communicates over HTTP/1.1.
348+ // However, Go's default HTTP client may negotiate an HTTP/2 connection
349+ // with the upstream server via ALPN during TLS handshake.
350+ // This can cause the response's Proto field to be set to "HTTP/2.0",
351+ // which would produce an invalid response for an HTTP/1.1 client.
352+ // To prevent this mismatch, we explicitly normalize the response
353+ // to HTTP/1.1 before writing it back to the client.
354+ resp .Proto = "HTTP/1.1"
355+ resp .ProtoMajor = 1
356+ resp .ProtoMinor = 1
357+
318358 // Copy response back to client
319359 err = resp .Write (conn )
320360 if err != nil {
321- p .logger .Error ("Failed to forward HTTP request" , "error" , err )
361+ p .logger .Error ("Failed to forward back HTTP response" ,
362+ "error" , err ,
363+ "host" , req .Host ,
364+ "method" , req .Method ,
365+ //"bodyBytes", string(bodyBytes),
366+ )
322367 return
323368 }
324369
0 commit comments