Skip to content

Commit c90e0a0

Browse files
Merge pull request #563 from jacobweinstock/clean-up-logging
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
2 parents 40fa972 + 9614a33 commit c90e0a0

File tree

11 files changed

+3192
-231
lines changed

11 files changed

+3192
-231
lines changed

.golangci.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,11 @@ linters:
146146
- whitespace
147147

148148
issues:
149+
exclude-files:
150+
- internal/iso/internal/reverseproxy.go
151+
- internal/iso/internal/reverseproxy_test.go
152+
- internal/iso/internal/acsii.go
153+
- internal/iso/internal/acsii_test.go
149154
# Excluding configuration per-path, per-linter, per-text and per-source
150155
exclude-rules:
151156
- path: _test\.go

internal/iso/internal/LICENSE

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Copyright 2009 The Go Authors.
2+
3+
Redistribution and use in source and binary forms, with or without
4+
modification, are permitted provided that the following conditions are
5+
met:
6+
7+
* Redistributions of source code must retain the above copyright
8+
notice, this list of conditions and the following disclaimer.
9+
* Redistributions in binary form must reproduce the above
10+
copyright notice, this list of conditions and the following disclaimer
11+
in the documentation and/or other materials provided with the
12+
distribution.
13+
* Neither the name of Google LLC nor the names of its
14+
contributors may be used to endorse or promote products derived from
15+
this software without specific prior written permission.
16+
17+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21+
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

internal/iso/internal/acsii.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2021 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package internal
6+
7+
// EqualFold is [strings.EqualFold], ASCII only. It reports whether s and t
8+
// are equal, ASCII-case-insensitively.
9+
func EqualFold(s, t string) bool {
10+
if len(s) != len(t) {
11+
return false
12+
}
13+
for i := 0; i < len(s); i++ {
14+
if lower(s[i]) != lower(t[i]) {
15+
return false
16+
}
17+
}
18+
return true
19+
}
20+
21+
// lower returns the ASCII lowercase version of b.
22+
func lower(b byte) byte {
23+
if 'A' <= b && b <= 'Z' {
24+
return b + ('a' - 'A')
25+
}
26+
return b
27+
}
28+
29+
// IsPrint returns whether s is ASCII and printable according to
30+
// https://tools.ietf.org/html/rfc20#section-4.2.
31+
func IsPrint(s string) bool {
32+
for i := 0; i < len(s); i++ {
33+
if s[i] < ' ' || s[i] > '~' {
34+
return false
35+
}
36+
}
37+
return true
38+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// Copyright 2021 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package internal
6+
7+
import "testing"
8+
9+
func TestEqualFold(t *testing.T) {
10+
var tests = []struct {
11+
name string
12+
a, b string
13+
want bool
14+
}{
15+
{
16+
name: "empty",
17+
want: true,
18+
},
19+
{
20+
name: "simple match",
21+
a: "CHUNKED",
22+
b: "chunked",
23+
want: true,
24+
},
25+
{
26+
name: "same string",
27+
a: "chunked",
28+
b: "chunked",
29+
want: true,
30+
},
31+
{
32+
name: "Unicode Kelvin symbol",
33+
a: "chunKed", // This "K" is 'KELVIN SIGN' (\u212A)
34+
b: "chunked",
35+
want: false,
36+
},
37+
}
38+
for _, tt := range tests {
39+
t.Run(tt.name, func(t *testing.T) {
40+
if got := EqualFold(tt.a, tt.b); got != tt.want {
41+
t.Errorf("AsciiEqualFold(%q,%q): got %v want %v", tt.a, tt.b, got, tt.want)
42+
}
43+
})
44+
}
45+
}
46+
47+
func TestIsPrint(t *testing.T) {
48+
var tests = []struct {
49+
name string
50+
in string
51+
want bool
52+
}{
53+
{
54+
name: "empty",
55+
want: true,
56+
},
57+
{
58+
name: "ASCII low",
59+
in: "This is a space: ' '",
60+
want: true,
61+
},
62+
{
63+
name: "ASCII high",
64+
in: "This is a tilde: '~'",
65+
want: true,
66+
},
67+
{
68+
name: "ASCII low non-print",
69+
in: "This is a unit separator: \x1F",
70+
want: false,
71+
},
72+
{
73+
name: "Ascii high non-print",
74+
in: "This is a Delete: \x7F",
75+
want: false,
76+
},
77+
{
78+
name: "Unicode letter",
79+
in: "Today it's 280K outside: it's freezing!", // This "K" is 'KELVIN SIGN' (\u212A)
80+
want: false,
81+
},
82+
{
83+
name: "Unicode emoji",
84+
in: "Gophers like 🧀",
85+
want: false,
86+
},
87+
}
88+
for _, tt := range tests {
89+
t.Run(tt.name, func(t *testing.T) {
90+
if got := IsPrint(tt.in); got != tt.want {
91+
t.Errorf("IsASCIIPrint(%q): got %v want %v", tt.in, got, tt.want)
92+
}
93+
})
94+
}
95+
}

internal/iso/internal/context.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package internal
2+
3+
import "context"
4+
5+
type patchCtxKeyType string
6+
7+
const isoPatchCtxKey patchCtxKeyType = "iso-patch"
8+
9+
func WithPatch(ctx context.Context, patch []byte) context.Context {
10+
return context.WithValue(ctx, isoPatchCtxKey, patch)
11+
}
12+
13+
func GetPatch(ctx context.Context) []byte {
14+
patch, ok := ctx.Value(isoPatchCtxKey).([]byte)
15+
if !ok {
16+
return nil
17+
}
18+
return patch
19+
}

0 commit comments

Comments
 (0)