mirror of
https://github.com/go-i2p/go-connfilter.git
synced 2025-06-08 01:09:15 -04:00
HTTP part
This commit is contained in:
44
http/example/example.go
Normal file
44
http/example/example.go
Normal file
@ -0,0 +1,44 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
|
||||
httpinspector "github.com/go-i2p/go-connfilter/http"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Create a regular TCP listener
|
||||
listener, err := net.Listen("tcp", ":8080")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// Create inspector with custom configuration
|
||||
config := httpinspector.Config{
|
||||
OnRequest: func(req *http.Request) error {
|
||||
// Add custom header to all requests
|
||||
req.Header.Set("X-Inspected", "true")
|
||||
return nil
|
||||
},
|
||||
OnResponse: func(resp *http.Response) error {
|
||||
// Log all response status codes
|
||||
log.Printf("Response status: %s", resp.Status)
|
||||
return nil
|
||||
},
|
||||
LoggingEnabled: true,
|
||||
}
|
||||
|
||||
inspector := httpinspector.New(listener, config)
|
||||
defer inspector.Close()
|
||||
|
||||
// Use the inspector with http.Server
|
||||
server := &http.Server{
|
||||
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte("Hello, World!"))
|
||||
}),
|
||||
}
|
||||
|
||||
log.Fatal(server.Serve(inspector))
|
||||
}
|
@ -5,7 +5,6 @@ package httpinspector
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
@ -153,9 +152,6 @@ func (c *inspectedConn) Write(b []byte) (int, error) {
|
||||
|
||||
// handleHTTPRequest processes incoming HTTP requests.
|
||||
func (c *inspectedConn) handleHTTPRequest(b []byte) (int, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), c.config.ReadTimeout)
|
||||
defer cancel()
|
||||
|
||||
// Peek to verify HTTP request
|
||||
peek, err := c.reader.Peek(4)
|
||||
if err != nil {
|
||||
@ -191,9 +187,6 @@ func (c *inspectedConn) handleHTTPRequest(b []byte) (int, error) {
|
||||
|
||||
// handleHTTPResponse processes outgoing HTTP responses.
|
||||
func (c *inspectedConn) handleHTTPResponse(b []byte) (int, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), c.config.ReadTimeout)
|
||||
defer cancel()
|
||||
|
||||
// Parse the response
|
||||
resp, err := http.ReadResponse(bufio.NewReader(bytes.NewReader(b)), nil)
|
||||
if err != nil {
|
||||
|
@ -6,6 +6,7 @@ import (
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestInspector(t *testing.T) {
|
||||
@ -78,4 +79,39 @@ type mockConn struct {
|
||||
readPos int
|
||||
}
|
||||
|
||||
// Implement net.Conn interface methods for mockConn...
|
||||
func (m *mockConn) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *mockConn) Read(b []byte) (n int, err error) {
|
||||
if m.readPos >= len(m.readData) {
|
||||
return 0, io.EOF
|
||||
}
|
||||
n = copy(b, m.readData[m.readPos:])
|
||||
m.readPos += n
|
||||
return n, nil
|
||||
}
|
||||
|
||||
func (m *mockConn) Write(b []byte) (n int, err error) {
|
||||
return len(b), nil
|
||||
}
|
||||
|
||||
func (m *mockConn) LocalAddr() net.Addr {
|
||||
return &net.TCPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 8080}
|
||||
}
|
||||
|
||||
func (m *mockConn) RemoteAddr() net.Addr {
|
||||
return &net.TCPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 8080}
|
||||
}
|
||||
|
||||
func (m *mockConn) SetDeadline(t time.Time) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *mockConn) SetReadDeadline(t time.Time) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *mockConn) SetWriteDeadline(t time.Time) error {
|
||||
return nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user