Skip to content

Commit

Permalink
APFirmware: use httpclient
Browse files Browse the repository at this point in the history
srtm: useragent check
  • Loading branch information
meee1 committed Feb 1, 2020
1 parent 1ee02bb commit bb0176c
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
9 changes: 8 additions & 1 deletion ExtLibs/ArduPilot/APFirmware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text.RegularExpressions;
using log4net;
using MissionPlanner.Utilities;

namespace MissionPlanner.ArduPilot
{
Expand Down Expand Up @@ -110,7 +112,12 @@ public static void GetList(string url = "https://firmware.ardupilot.org/manifest

log.Info(url);

var manifestgz = new WebClient().DownloadData(url);
var client = new HttpClient();

if (!String.IsNullOrEmpty(Settings.Instance.UserAgent))
client.DefaultRequestHeaders.Add("User-Agent", Settings.Instance.UserAgent);

var manifestgz = client.GetByteArrayAsync(url).GetAwaiter().GetResult();
var mssrc = new MemoryStream(manifestgz);
var msdest = new MemoryStream();
GZipStream gz = new GZipStream(mssrc, CompressionMode.Decompress);
Expand Down
12 changes: 7 additions & 5 deletions ExtLibs/Utilities/Download.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class DownloadStream : Stream
string _uri = "";
public int chunksize { get; set; } = 1000 * 250;

static HttpClient client = new HttpClient();
HttpClient client = new HttpClient();

private static object _lock = new object();
/// <summary>
Expand Down Expand Up @@ -73,13 +73,14 @@ static void expireCache()

private static Timer _timer;

static DownloadStream()
internal DownloadStream()
{
_timer = new Timer(a => { expireCache(); }, null, 1000 * 30, 1000 * 30);
client.DefaultRequestHeaders.Add("User-Agent", Settings.Instance.UserAgent);
if (!String.IsNullOrEmpty(Settings.Instance.UserAgent))
client.DefaultRequestHeaders.Add("User-Agent", Settings.Instance.UserAgent);
}

public DownloadStream(string uri)
public DownloadStream(string uri): this()
{
_uri = uri;
SetLength(Download.GetFileSize(uri));
Expand Down Expand Up @@ -374,7 +375,8 @@ public static async Task<bool> getFilefromNetAsync(string url, string saveto, Ac

static Download()
{
client.DefaultRequestHeaders.Add("User-Agent", Settings.Instance.UserAgent);
if (!String.IsNullOrEmpty(Settings.Instance.UserAgent))
client.DefaultRequestHeaders.Add("User-Agent", Settings.Instance.UserAgent);
}

static HttpClient client = new HttpClient();
Expand Down
3 changes: 2 additions & 1 deletion ExtLibs/Utilities/ParameterMetaDataParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ public static class ParameterMetaDataParser
public static void GetParameterInformation(string urls, string file = null)
{
httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add("User-Agent", Settings.Instance.UserAgent);
if (!String.IsNullOrEmpty(Settings.Instance.UserAgent))
httpClient.DefaultRequestHeaders.Add("User-Agent", Settings.Instance.UserAgent);
httpClient.Timeout = TimeSpan.FromSeconds(30);

var parameterLocationsString = urls;
Expand Down
3 changes: 2 additions & 1 deletion ExtLibs/Utilities/srtm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ static srtm()
}
}

client.DefaultRequestHeaders.Add("User-Agent", Settings.Instance.UserAgent);
if (!String.IsNullOrEmpty(Settings.Instance.UserAgent))
client.DefaultRequestHeaders.Add("User-Agent", Settings.Instance.UserAgent);

StartQueueProcess();
}
Expand Down

0 comments on commit bb0176c

Please sign in to comment.