refactor: centralize raw HTTP parsing and header serialization

- Add internal/util/rawhttp.go with ParseRawRequest and SortedHeaderLines
- Refactor intercept/format.go and ui/intercept/helpers.go to use them
- Eliminates duplicated bufio.Reader + textproto parsing spread across 3+ files

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Hadi
2026-05-19 13:38:30 +02:00
parent 172a77e13b
commit 1a1c0cff30
3 changed files with 105 additions and 52 deletions
+5 -19
View File
@@ -3,9 +3,9 @@ package intercept
import (
"fmt"
"net/http"
"sort"
"strings"
"github.com/anotherhadi/spilltea/internal/util"
"github.com/lqqyt2423/go-mitmproxy/proxy"
)
@@ -14,15 +14,8 @@ func FormatRawRequest(f *proxy.Flow) string {
r := f.Request
var sb strings.Builder
fmt.Fprintf(&sb, "%s %s %s\n", r.Method, r.URL.RequestURI(), r.Proto)
keys := make([]string, 0, len(r.Header))
for k := range r.Header {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
for _, v := range r.Header[k] {
fmt.Fprintf(&sb, "%s: %s\n", k, v)
}
for _, line := range util.SortedHeaderLines(r.Header) {
sb.WriteString(line)
}
sb.WriteString("\n")
if len(r.Body) > 0 {
@@ -43,15 +36,8 @@ func FormatRawResponse(f *proxy.Flow) string {
proto = "HTTP/1.1"
}
fmt.Fprintf(&sb, "%s %d %s\n", proto, r.StatusCode, http.StatusText(r.StatusCode))
keys := make([]string, 0, len(r.Header))
for k := range r.Header {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
for _, v := range r.Header[k] {
fmt.Fprintf(&sb, "%s: %s\n", k, v)
}
for _, line := range util.SortedHeaderLines(r.Header) {
sb.WriteString(line)
}
sb.WriteString("\n")
if len(r.Body) > 0 {