Skip to content

Commit

Permalink
Add user-agent to all http clients using RawClientHttpRequestSettings.
Browse files Browse the repository at this point in the history
  • Loading branch information
TingluoHuang committed Jan 30, 2024
1 parent b7ab810 commit 6b4c1f7
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 3 deletions.
25 changes: 24 additions & 1 deletion src/Runner.Sdk/Util/VssUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ public static void InitializeVssClientSettings(List<ProductInfoHeaderValue> addi

if (VssClientHttpRequestSettings.Default.UserAgent != null && VssClientHttpRequestSettings.Default.UserAgent.Count > 0)
{
headerValues.AddRange(VssClientHttpRequestSettings.Default.UserAgent);
foreach (var headerVal in VssClientHttpRequestSettings.Default.UserAgent)
{
if (!headerValues.Contains(headerVal))
{
headerValues.Add(headerVal);
}
}
}

VssClientHttpRequestSettings.Default.UserAgent = headerValues;
Expand All @@ -33,6 +39,23 @@ public static void InitializeVssClientSettings(List<ProductInfoHeaderValue> addi
{
VssClientHttpRequestSettings.Default.ServerCertificateValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator;
}

var rawHeaderValues = new List<ProductInfoHeaderValue>();
rawHeaderValues.AddRange(additionalUserAgents);
rawHeaderValues.Add(new ProductInfoHeaderValue($"({StringUtil.SanitizeUserAgentHeader(RuntimeInformation.OSDescription)})"));

if (RawClientHttpRequestSettings.Default.UserAgent != null && RawClientHttpRequestSettings.Default.UserAgent.Count > 0)
{
foreach (var headerVal in RawClientHttpRequestSettings.Default.UserAgent)
{
if (!rawHeaderValues.Contains(headerVal))
{
rawHeaderValues.Add(headerVal);
}
}
}

RawClientHttpRequestSettings.Default.UserAgent = rawHeaderValues;
}

public static VssConnection CreateConnection(
Expand Down
3 changes: 3 additions & 0 deletions src/Runner.Worker/JobRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ public async Task<TaskResult> RunAsync(AgentJobRequestMessage message, Cancellat
!string.IsNullOrEmpty(orchestrationId.Value))
{
HostContext.UserAgents.Add(new ProductInfoHeaderValue("OrchestrationId", orchestrationId.Value));

// make sure orchestration id is in the user-agent header.
VssUtil.InitializeVssClientSettings(HostContext.UserAgents, HostContext.WebProxy);
}

var jobServerQueueTelemetry = false;
Expand Down
3 changes: 1 addition & 2 deletions src/Sdk/Common/Common/RawHttpHeaders.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using System;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;

namespace GitHub.Services.Common.Internal
{
[EditorBrowsable(EditorBrowsableState.Never)]
public static class RawHttpHeaders
{
public const String SessionHeader = "X-Runner-Session";
public const String SessionHeader = "X-Actions-Session";
}
}
2 changes: 2 additions & 0 deletions src/Sdk/Common/Common/RawHttpMessageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ protected override async Task<HttpResponseMessage> SendAsync(
response.Dispose();
}

this.Settings.ApplyTo(request);

// Let's start with sending a token
IssuedToken token = null;
if (m_tokenProvider != null)
Expand Down

0 comments on commit 6b4c1f7

Please sign in to comment.