Skip to content

Commit

Permalink
Use std library mime parser instead of custom regex to extract charset
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikPelli committed Dec 20, 2024
1 parent f2928e5 commit 4d4060e
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package goproxy

import (
"crypto/tls"
"mime"
"net/http"
"regexp"
)

// ProxyCtx is the Proxy context, contains useful information about every request. It is passed to
Expand Down Expand Up @@ -79,15 +79,15 @@ func (ctx *ProxyCtx) Warnf(msg string, argv ...interface{}) {
ctx.printf("WARN: "+msg, argv...)
}

var charsetFinder = regexp.MustCompile("charset=([^ ;]*)")

// Will try to infer the character set of the request from the headers.
// Returns the empty string if we don't know which character set it used.
// Currently it will look for charset=<charset> in the Content-Type header of the request.
func (ctx *ProxyCtx) Charset() string {
charsets := charsetFinder.FindStringSubmatch(ctx.Resp.Header.Get("Content-Type"))
if charsets == nil {
return ""
contentType := ctx.Resp.Header.Get("Content-Type")
if _, params, err := mime.ParseMediaType(contentType); err == nil {
if cs, ok := params["charset"]; ok {
return cs
}
}
return charsets[1]
return ""
}

0 comments on commit 4d4060e

Please sign in to comment.