| 1 |
/* |
| 2 |
Copyright The ORAS Authors. |
| 3 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
you may not use this file except in compliance with the License. |
| 5 |
You may obtain a copy of the License at |
| 6 |
|
| 7 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
|
| 9 |
Unless required by applicable law or agreed to in writing, software |
| 10 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
See the License for the specific language governing permissions and |
| 13 |
limitations under the License. |
| 14 |
*/ |
| 15 |
|
| 16 |
package remote |
| 17 |
|
| 18 |
import ( |
| 19 |
"errors" |
| 20 |
"fmt" |
| 21 |
"strconv" |
| 22 |
"strings" |
| 23 |
) |
| 24 |
|
| 25 |
const ( |
| 26 |
// headerWarning is the "Warning" header. |
| 27 |
// Reference: https://www.rfc-editor.org/rfc/rfc7234#section-5.5 |
| 28 |
headerWarning = "Warning" |
| 29 |
|
| 30 |
// warnCode299 is the 299 warn-code. |
| 31 |
// Reference: https://www.rfc-editor.org/rfc/rfc7234#section-5.5 |
| 32 |
warnCode299 = 299 |
| 33 |
|
| 34 |
// warnAgentUnknown represents an unknown warn-agent. |
| 35 |
// Reference: https://www.rfc-editor.org/rfc/rfc7234#section-5.5 |
| 36 |
warnAgentUnknown = "-" |
| 37 |
) |
| 38 |
|
| 39 |
// errUnexpectedWarningFormat is returned by parseWarningHeader when |
| 40 |
// an unexpected warning format is encountered. |
| 41 |
var errUnexpectedWarningFormat = errors.New("unexpected warning format") |
| 42 |
|
| 43 |
// WarningValue represents the value of the Warning header. |
| 44 |
// |
| 45 |
// References: |
| 46 |
// - https://github.com/opencontainers/distribution-spec/blob/v1.1.1/spec.md#warnings |
| 47 |
// - https://www.rfc-editor.org/rfc/rfc7234#section-5.5 |
| 48 |
type WarningValue struct { |
| 49 |
// Code is the warn-code. |
| 50 |
Code int |
| 51 |
// Agent is the warn-agent. |
| 52 |
Agent string |
| 53 |
// Text is the warn-text. |
| 54 |
Text string |
| 55 |
} |
| 56 |
|
| 57 |
// Warning contains the value of the warning header and may contain |
| 58 |
// other information related to the warning. |
| 59 |
// |
| 60 |
// References: |
| 61 |
// - https://github.com/opencontainers/distribution-spec/blob/v1.1.1/spec.md#warnings |
| 62 |
// - https://www.rfc-editor.org/rfc/rfc7234#section-5.5 |
| 63 |
type Warning struct { |
| 64 |
// WarningValue is the value of the warning header. |
| 65 |
WarningValue |
| 66 |
} |
| 67 |
|
| 68 |
// parseWarningHeader parses the warning header into WarningValue. |
| 69 |
func parseWarningHeader(header string) (WarningValue, error) { |
| 70 |
if len(header) < 9 || !strings.HasPrefix(header, `299 - "`) || !strings.HasSuffix(header, `"`) { |
| 71 |
// minimum header value: `299 - "x"` |
| 72 |
return WarningValue{}, fmt.Errorf("%s: %w", header, errUnexpectedWarningFormat) |
| 73 |
} |
| 74 |
|
| 75 |
// validate text only as code and agent are fixed |
| 76 |
quotedText := header[6:] // behind `299 - `, quoted by " |
| 77 |
text, err := strconv.Unquote(quotedText) |
| 78 |
if err != nil { |
| 79 |
return WarningValue{}, fmt.Errorf("%s: unexpected text: %w: %v", header, errUnexpectedWarningFormat, err) |
| 80 |
} |
| 81 |
|
| 82 |
return WarningValue{ |
| 83 |
Code: warnCode299, |
| 84 |
Agent: warnAgentUnknown, |
| 85 |
Text: text, |
| 86 |
}, nil |
| 87 |
} |
| 88 |
|
| 89 |
// handleWarningHeaders parses the warning headers and handles the parsed |
| 90 |
// warnings using handleWarning. |
| 91 |
func handleWarningHeaders(headers []string, handleWarning func(Warning)) { |
| 92 |
for _, h := range headers { |
| 93 |
if value, err := parseWarningHeader(h); err == nil { |
| 94 |
// ignore warnings in unexpected formats |
| 95 |
handleWarning(Warning{ |
| 96 |
WarningValue: value, |
| 97 |
}) |
| 98 |
} |
| 99 |
} |
| 100 |
} |