-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support scrubbing gzip encoded bundles #36
Changes from all commits
f68c9b6
dc153b5
94b54c2
a18311e
0d11296
627b047
a52dacd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,7 +25,7 @@ package content_blocker_plugin | |
import ( | ||
"bytes" | ||
"fmt" | ||
"io/ioutil" | ||
"io" | ||
"log" | ||
"net/http" | ||
"os" | ||
|
@@ -78,7 +78,7 @@ func (f contentBlockerPluginFactory) New(configSection *config.Section) (traffic | |
} | ||
|
||
if regexp, err := regexp.Compile(pattern); err != nil { | ||
return fmt.Errorf(`Could not compile regular expression "%v": %v`, pattern, err) | ||
return fmt.Errorf(`could not compile regular expression "%v": %v`, pattern, err) | ||
} else { | ||
logger.Printf("Added rule: %s %s content matching \"%s\"", mode, contentKind, regexp) | ||
blockers = append(blockers, &contentBlocker{ | ||
|
@@ -94,7 +94,7 @@ func (f contentBlockerPluginFactory) New(configSection *config.Section) (traffic | |
case "header": | ||
plugin.headerBlockers = append(plugin.headerBlockers, blockers...) | ||
default: | ||
return fmt.Errorf(`Unexpected content kind %s`, contentKind) | ||
return fmt.Errorf(`unexpected content kind %s`, contentKind) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note capitalization of error messages is a style lint warning for Golang so fixed these as well generally. https://google.github.io/styleguide/go/decisions.html#error-strings |
||
} | ||
|
||
return nil | ||
|
@@ -222,28 +222,26 @@ func (plug contentBlockerPlugin) blockBodyContent(response http.ResponseWriter, | |
return false | ||
} | ||
|
||
processedBody, err := ioutil.ReadAll(request.Body) | ||
processedBody, err := io.ReadAll(request.Body) | ||
if err != nil { | ||
http.Error(response, fmt.Sprintf("Error reading request body: %s", err), 500) | ||
request.Body = http.NoBody | ||
return true | ||
} | ||
initialLength := len(processedBody) | ||
|
||
for _, blocker := range plug.bodyBlockers { | ||
processedBody = blocker.Block(processedBody) | ||
} | ||
|
||
// If the length of the body has changed, we should update the | ||
// Content-Length header too. | ||
finalLength := len(processedBody) | ||
if finalLength != initialLength { | ||
contentLength := int64(finalLength) | ||
contentLength := int64(len(processedBody)) | ||
if contentLength != request.ContentLength { | ||
Comment on lines
+238
to
+239
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Small change to directly verify the content length is valid instead of indirectly through the existing body content. |
||
request.ContentLength = contentLength | ||
request.Header.Set("Content-Length", strconv.FormatInt(contentLength, 10)) | ||
} | ||
|
||
request.Body = ioutil.NopCloser(bytes.NewBuffer(processedBody)) | ||
request.Body = io.NopCloser(bytes.NewBuffer(processedBody)) | ||
return false | ||
} | ||
|
||
|
@@ -283,7 +281,7 @@ func (b *contentBlocker) Block(content []byte) []byte { | |
case excludeMode: | ||
return b.regexp.ReplaceAllLiteral(content, []byte{}) | ||
default: | ||
panic(fmt.Errorf("Invalid content blocking mode: %v", b.mode)) | ||
panic(fmt.Errorf("invalid content blocking mode: %v", b.mode)) | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note
ioutil
is deprecated and internally just calls theio
package, so I've updated all usages ofioutil
.