|
| 1 | +/* |
| 2 | + _____ _____ _____ ____ ______ _____ ------ |
| 3 | + | | | | | | | | | | | | | |
| 4 | + | | | | | | | | | | | | | |
| 5 | + | --- | | | | |-----| |---- | | |-----| |----- ------ |
| 6 | + | | | | | | | | | | | | | |
| 7 | + | ____| |_____ | ____| | ____| | |_____| _____| |_____ |_____ |
| 8 | +
|
| 9 | +
|
| 10 | + Licensed under the MIT License <http://opensource.org/licenses/MIT>. |
| 11 | +
|
| 12 | + Copyright © 2020-2025 Microsoft Corporation. All rights reserved. |
| 13 | + Author : <blobfusedev@microsoft.com> |
| 14 | +
|
| 15 | + Permission is hereby granted, free of charge, to any person obtaining a copy |
| 16 | + of this software and associated documentation files (the "Software"), to deal |
| 17 | + in the Software without restriction, including without limitation the rights |
| 18 | + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 19 | + copies of the Software, and to permit persons to whom the Software is |
| 20 | + furnished to do so, subject to the following conditions: |
| 21 | +
|
| 22 | + The above copyright notice and this permission notice shall be included in all |
| 23 | + copies or substantial portions of the Software. |
| 24 | +
|
| 25 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 26 | + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 27 | + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 28 | + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 29 | + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 30 | + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 31 | + SOFTWARE |
| 32 | +*/ |
| 33 | + |
| 34 | +package azstorage |
| 35 | + |
| 36 | +import ( |
| 37 | + "fmt" |
| 38 | + "net/http" |
| 39 | + |
| 40 | + "github.com/Azure/azure-sdk-for-go/sdk/azcore/policy" |
| 41 | + "github.com/Azure/azure-storage-fuse/v2/common" |
| 42 | +) |
| 43 | + |
| 44 | +// blobfuseTelemetryPolicy is a custom pipeline policy to prepend the blobfuse user agent string to the one coming from SDK. |
| 45 | +// This is added in the PerCallPolicies which executes after the SDK's default telemetry policy. |
| 46 | +type blobfuseTelemetryPolicy struct { |
| 47 | + telemetryValue string |
| 48 | +} |
| 49 | + |
| 50 | +// newBlobfuseTelemetryPolicy creates an object which prepends the blobfuse user agent string to the User-Agent request header |
| 51 | +func newBlobfuseTelemetryPolicy(telemetryValue string) policy.Policy { |
| 52 | + return &blobfuseTelemetryPolicy{telemetryValue: telemetryValue} |
| 53 | +} |
| 54 | + |
| 55 | +func (p blobfuseTelemetryPolicy) Do(req *policy.Request) (*http.Response, error) { |
| 56 | + userAgent := p.telemetryValue |
| 57 | + |
| 58 | + // prepend the blobfuse user agent string |
| 59 | + if ua := req.Raw().Header.Get(common.UserAgentHeader); ua != "" { |
| 60 | + userAgent = fmt.Sprintf("%s %s", userAgent, ua) |
| 61 | + } |
| 62 | + req.Raw().Header.Set(common.UserAgentHeader, userAgent) |
| 63 | + return req.Next() |
| 64 | +} |
| 65 | + |
| 66 | +// --------------------------------------------------------------------------------------------------------------------------------------------------- |
| 67 | +// Policy to override the service version if requested by user |
| 68 | +type serviceVersionPolicy struct { |
| 69 | + serviceApiVersion string |
| 70 | +} |
| 71 | + |
| 72 | +func newServiceVersionPolicy(version string) policy.Policy { |
| 73 | + return &serviceVersionPolicy{ |
| 74 | + serviceApiVersion: version, |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +func (r *serviceVersionPolicy) Do(req *policy.Request) (*http.Response, error) { |
| 79 | + req.Raw().Header["x-ms-version"] = []string{r.serviceApiVersion} |
| 80 | + return req.Next() |
| 81 | +} |
0 commit comments