Skip to content

Commit

Permalink
enhance: net/netutil: enhance ModifyConnectionRequest() error han…
Browse files Browse the repository at this point in the history
…dling
  • Loading branch information
grokify committed Mar 26, 2024
1 parent ebaf3fd commit 1424fe4
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions net/netutil/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,24 @@ package netutil

import (
"bufio"
"fmt"
"errors"
"net"
"net/http"

"github.com/grokify/mogo/errors/errorsutil"
)

// ModifyConnectionRequest updates the HTTP request for network connection.
func ModifyConnectionRequest(conn net.Conn, modRequest func(r *http.Request)) {
func ModifyConnectionRequest(conn net.Conn, modRequest func(r *http.Request)) error {
// Code adapted from: https://stackoverflow.com/a/76684845/1908967
if conn == nil {
return errors.New("net.Conn cannot be nil")
}
defer conn.Close()

req, err := http.ReadRequest(bufio.NewReader(conn))
if err != nil {
fmt.Println("Error reading request:", err)
return
return errorsutil.Wrap(err, "error reading request")
}

// Modify the request as needed
Expand All @@ -25,10 +29,9 @@ func ModifyConnectionRequest(conn net.Conn, modRequest func(r *http.Request)) {

resp, err := http.DefaultTransport.RoundTrip(req)
if err != nil {
fmt.Println("Error sending request:", err)
return
return errorsutil.Wrap(err, "error sending request")
}
defer resp.Body.Close()

resp.Write(conn)
return resp.Write(conn)
}

0 comments on commit 1424fe4

Please sign in to comment.