Setting the User-Agent header (#759)
This commit is contained in:
parent
ccc97c67ab
commit
b60e598277
|
|
@ -1,4 +1,5 @@
|
||||||
import { HttpError } from "./HttpError"
|
import { HttpError } from "./HttpError"
|
||||||
|
import { UserAgent } from "./UserAgent"
|
||||||
|
|
||||||
export interface IHttpClient {
|
export interface IHttpClient {
|
||||||
get(url: string, headers?: Map<string, string>): Promise<string>;
|
get(url: string, headers?: Map<string, string>): Promise<string>;
|
||||||
|
|
@ -25,7 +26,7 @@ export class HttpClient implements IHttpClient {
|
||||||
|
|
||||||
xhr.open(method, url, true);
|
xhr.open(method, url, true);
|
||||||
xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
|
xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
|
||||||
|
xhr.setRequestHeader("User-Agent", UserAgent);
|
||||||
if (headers) {
|
if (headers) {
|
||||||
headers.forEach((value, header) => xhr.setRequestHeader(header, value));
|
headers.forEach((value, header) => xhr.setRequestHeader(header, value));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ import { DataReceived, TransportClosed } from "./Common"
|
||||||
import { IHttpClient } from "./HttpClient"
|
import { IHttpClient } from "./HttpClient"
|
||||||
import { HttpError } from "./HttpError"
|
import { HttpError } from "./HttpError"
|
||||||
import { ILogger, LogLevel } from "./ILogger"
|
import { ILogger, LogLevel } from "./ILogger"
|
||||||
|
import { UserAgent } from "./UserAgent"
|
||||||
|
|
||||||
export enum TransportType {
|
export enum TransportType {
|
||||||
WebSockets,
|
WebSockets,
|
||||||
|
|
@ -195,6 +196,7 @@ export class LongPollingTransport implements ITransport {
|
||||||
}
|
}
|
||||||
|
|
||||||
let pollXhr = new XMLHttpRequest();
|
let pollXhr = new XMLHttpRequest();
|
||||||
|
pollXhr.setRequestHeader("User-Agent", UserAgent);
|
||||||
if (transferMode === TransferMode.Binary) {
|
if (transferMode === TransferMode.Binary) {
|
||||||
pollXhr.responseType = "arraybuffer";
|
pollXhr.responseType = "arraybuffer";
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
export const UserAgent = "Microsoft.AspNetCore.Sockets.Client.Http-js/1.0.0-alpha";
|
||||||
|
|
@ -0,0 +1,13 @@
|
||||||
|
// Copyright (c) .NET Foundation. All rights reserved.
|
||||||
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
|
using System.Net.Http.Headers;
|
||||||
|
|
||||||
|
namespace Microsoft.AspNetCore.Sockets.Client.Http
|
||||||
|
{
|
||||||
|
public class Constants
|
||||||
|
{
|
||||||
|
private static readonly string UserAgent = "Microsoft.AspNetCore.Sockets.Client.Http/1.0.0-alpha";
|
||||||
|
public static readonly ProductInfoHeaderValue UserAgentHeader = ProductInfoHeaderValue.Parse(UserAgent);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -9,8 +9,9 @@ using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Threading.Tasks.Channels;
|
using System.Threading.Tasks.Channels;
|
||||||
using Microsoft.AspNetCore.Http.Features;
|
using Microsoft.AspNetCore.Http.Features;
|
||||||
using Microsoft.AspNetCore.Sockets.Features;
|
using Microsoft.AspNetCore.Sockets.Client.Http;
|
||||||
using Microsoft.AspNetCore.Sockets.Client.Internal;
|
using Microsoft.AspNetCore.Sockets.Client.Internal;
|
||||||
|
using Microsoft.AspNetCore.Sockets.Features;
|
||||||
using Microsoft.AspNetCore.Sockets.Http.Internal;
|
using Microsoft.AspNetCore.Sockets.Http.Internal;
|
||||||
using Microsoft.AspNetCore.Sockets.Internal;
|
using Microsoft.AspNetCore.Sockets.Internal;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
@ -200,10 +201,13 @@ namespace Microsoft.AspNetCore.Sockets.Client
|
||||||
// Get a connection ID from the server
|
// Get a connection ID from the server
|
||||||
logger.EstablishingConnection(url);
|
logger.EstablishingConnection(url);
|
||||||
using (var request = new HttpRequestMessage(HttpMethod.Options, url))
|
using (var request = new HttpRequestMessage(HttpMethod.Options, url))
|
||||||
using (var response = await httpClient.SendAsync(request))
|
|
||||||
{
|
{
|
||||||
response.EnsureSuccessStatusCode();
|
request.Headers.UserAgent.Add(Constants.UserAgentHeader);
|
||||||
return await ParseNegotiateResponse(response, logger);
|
using (var response = await httpClient.SendAsync(request))
|
||||||
|
{
|
||||||
|
response.EnsureSuccessStatusCode();
|
||||||
|
return await ParseNegotiateResponse(response, logger);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ using System.Net.Http;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Threading.Tasks.Channels;
|
using System.Threading.Tasks.Channels;
|
||||||
|
using Microsoft.AspNetCore.Sockets.Client.Http;
|
||||||
using Microsoft.AspNetCore.Sockets.Client.Internal;
|
using Microsoft.AspNetCore.Sockets.Client.Internal;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Microsoft.Extensions.Logging.Abstractions;
|
using Microsoft.Extensions.Logging.Abstractions;
|
||||||
|
|
@ -89,7 +90,7 @@ namespace Microsoft.AspNetCore.Sockets.Client
|
||||||
while (!cancellationToken.IsCancellationRequested)
|
while (!cancellationToken.IsCancellationRequested)
|
||||||
{
|
{
|
||||||
var request = new HttpRequestMessage(HttpMethod.Get, pollUrl);
|
var request = new HttpRequestMessage(HttpMethod.Get, pollUrl);
|
||||||
request.Headers.UserAgent.Add(SendUtils.DefaultUserAgentHeader);
|
request.Headers.UserAgent.Add(Constants.UserAgentHeader);
|
||||||
|
|
||||||
var response = await _httpClient.SendAsync(request, cancellationToken);
|
var response = await _httpClient.SendAsync(request, cancellationToken);
|
||||||
response.EnsureSuccessStatusCode();
|
response.EnsureSuccessStatusCode();
|
||||||
|
|
|
||||||
|
|
@ -5,10 +5,10 @@ using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Net.Http.Headers;
|
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Threading.Tasks.Channels;
|
using System.Threading.Tasks.Channels;
|
||||||
|
using Microsoft.AspNetCore.Sockets.Client.Http;
|
||||||
using Microsoft.AspNetCore.Sockets.Client.Internal;
|
using Microsoft.AspNetCore.Sockets.Client.Internal;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
|
|
@ -16,9 +16,6 @@ namespace Microsoft.AspNetCore.Sockets.Client
|
||||||
{
|
{
|
||||||
internal static class SendUtils
|
internal static class SendUtils
|
||||||
{
|
{
|
||||||
private static readonly string DefaultUserAgent = "Microsoft.AspNetCore.SignalR.Client/0.0.0";
|
|
||||||
public static readonly ProductInfoHeaderValue DefaultUserAgentHeader = ProductInfoHeaderValue.Parse(DefaultUserAgent);
|
|
||||||
|
|
||||||
public static async Task SendMessages(Uri sendUrl, Channel<byte[], SendMessage> application, HttpClient httpClient,
|
public static async Task SendMessages(Uri sendUrl, Channel<byte[], SendMessage> application, HttpClient httpClient,
|
||||||
CancellationTokenSource transportCts, ILogger logger, string connectionId)
|
CancellationTokenSource transportCts, ILogger logger, string connectionId)
|
||||||
{
|
{
|
||||||
|
|
@ -41,7 +38,7 @@ namespace Microsoft.AspNetCore.Sockets.Client
|
||||||
|
|
||||||
// Send them in a single post
|
// Send them in a single post
|
||||||
var request = new HttpRequestMessage(HttpMethod.Post, sendUrl);
|
var request = new HttpRequestMessage(HttpMethod.Post, sendUrl);
|
||||||
request.Headers.UserAgent.Add(DefaultUserAgentHeader);
|
request.Headers.UserAgent.Add(Constants.UserAgentHeader);
|
||||||
|
|
||||||
// TODO: We can probably use a pipeline here or some kind of pooled memory.
|
// TODO: We can probably use a pipeline here or some kind of pooled memory.
|
||||||
// But where do we get the pool from? ArrayBufferPool.Instance?
|
// But where do we get the pool from? ArrayBufferPool.Instance?
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue