Skip to content

Commit

Permalink
Merge pull request #563 from jacobweinstock/clean-up-logging
Browse files Browse the repository at this point in the history
Refactor ISO patching implementation:

## Description

<!--- Please describe what this PR is going to change -->
The previous implementation only worked for clients that did partial content requests. This refactor allows all requests, whether partial content or not to have patching occur.

This is particularly useful when using the redfish emulator from sushy-tools. This emulator doesn't not request the ISO as partial content but requests the whole ISO like a curl or wget client might. This implementation is very memory safe and doesn't require content length checking of any kind. The Go stdlib httputil.NewSingleHostReverseProxy file and supporting files were copied in. This is because reimplementing all the logic in func (p *ReverseProxy) ServeHTTP of the httputil package is a lot and made more sense to copy it in and modify it to be able to patch when writing the body to the client.

## Why is this needed

<!--- Link to issue you have raised -->

Fixes: #

## How Has This Been Tested?
<!--- Please describe in detail how you tested your changes. -->
<!--- Include details of your testing environment, and the tests you ran to -->
<!--- see how your change affects other areas of the code, etc. -->


## How are existing users impacted? What migration steps/scripts do we need?

<!--- Fixes a bug, unblocks installation, removes a component of the stack etc -->
<!--- Requires a DB migration script, etc. -->


## Checklist:

I have:

- [ ] updated the documentation and/or roadmap (if required)
- [ ] added unit or e2e tests
- [ ] provided instructions on how to upgrade
  • Loading branch information
jacobweinstock authored Nov 27, 2024
2 parents 40fa972 + 9614a33 commit c90e0a0
Show file tree
Hide file tree
Showing 11 changed files with 3,192 additions and 231 deletions.
5 changes: 5 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ linters:
- whitespace

issues:
exclude-files:
- internal/iso/internal/reverseproxy.go
- internal/iso/internal/reverseproxy_test.go
- internal/iso/internal/acsii.go
- internal/iso/internal/acsii_test.go
# Excluding configuration per-path, per-linter, per-text and per-source
exclude-rules:
- path: _test\.go
Expand Down
27 changes: 27 additions & 0 deletions internal/iso/internal/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Copyright 2009 The Go Authors.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the name of Google LLC nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 changes: 38 additions & 0 deletions internal/iso/internal/acsii.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package internal

// EqualFold is [strings.EqualFold], ASCII only. It reports whether s and t
// are equal, ASCII-case-insensitively.
func EqualFold(s, t string) bool {
if len(s) != len(t) {
return false
}
for i := 0; i < len(s); i++ {
if lower(s[i]) != lower(t[i]) {
return false
}
}
return true
}

// lower returns the ASCII lowercase version of b.
func lower(b byte) byte {
if 'A' <= b && b <= 'Z' {
return b + ('a' - 'A')
}
return b
}

// IsPrint returns whether s is ASCII and printable according to
// https://tools.ietf.org/html/rfc20#section-4.2.
func IsPrint(s string) bool {
for i := 0; i < len(s); i++ {
if s[i] < ' ' || s[i] > '~' {
return false
}
}
return true
}
95 changes: 95 additions & 0 deletions internal/iso/internal/acsii_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package internal

import "testing"

func TestEqualFold(t *testing.T) {
var tests = []struct {
name string
a, b string
want bool
}{
{
name: "empty",
want: true,
},
{
name: "simple match",
a: "CHUNKED",
b: "chunked",
want: true,
},
{
name: "same string",
a: "chunked",
b: "chunked",
want: true,
},
{
name: "Unicode Kelvin symbol",
a: "chunKed", // This "K" is 'KELVIN SIGN' (\u212A)
b: "chunked",
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := EqualFold(tt.a, tt.b); got != tt.want {
t.Errorf("AsciiEqualFold(%q,%q): got %v want %v", tt.a, tt.b, got, tt.want)
}
})
}
}

func TestIsPrint(t *testing.T) {
var tests = []struct {
name string
in string
want bool
}{
{
name: "empty",
want: true,
},
{
name: "ASCII low",
in: "This is a space: ' '",
want: true,
},
{
name: "ASCII high",
in: "This is a tilde: '~'",
want: true,
},
{
name: "ASCII low non-print",
in: "This is a unit separator: \x1F",
want: false,
},
{
name: "Ascii high non-print",
in: "This is a Delete: \x7F",
want: false,
},
{
name: "Unicode letter",
in: "Today it's 280K outside: it's freezing!", // This "K" is 'KELVIN SIGN' (\u212A)
want: false,
},
{
name: "Unicode emoji",
in: "Gophers like 🧀",
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := IsPrint(tt.in); got != tt.want {
t.Errorf("IsASCIIPrint(%q): got %v want %v", tt.in, got, tt.want)
}
})
}
}
19 changes: 19 additions & 0 deletions internal/iso/internal/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package internal

import "context"

type patchCtxKeyType string

const isoPatchCtxKey patchCtxKeyType = "iso-patch"

func WithPatch(ctx context.Context, patch []byte) context.Context {
return context.WithValue(ctx, isoPatchCtxKey, patch)
}

func GetPatch(ctx context.Context) []byte {
patch, ok := ctx.Value(isoPatchCtxKey).([]byte)
if !ok {
return nil
}
return patch
}
Loading

0 comments on commit c90e0a0

Please sign in to comment.