forked from neo-project/neo-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOracleHttpsProtocol.cs
85 lines (78 loc) · 3.32 KB
/
OracleHttpsProtocol.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright (C) 2015-2021 The Neo Project.
//
// The Neo.Plugins.OracleService is free software distributed under the MIT software license,
// see the accompanying file LICENSE in the main directory of the
// project or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.
using Neo.Network.P2P.Payloads;
using System;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
namespace Neo.Plugins
{
class OracleHttpsProtocol : IOracleProtocol
{
private readonly HttpClient client = new(new HttpClientHandler() { AllowAutoRedirect = false });
public OracleHttpsProtocol()
{
CustomAttributeData attribute = Assembly.GetExecutingAssembly().CustomAttributes.First(p => p.AttributeType == typeof(AssemblyInformationalVersionAttribute));
string version = (string)attribute.ConstructorArguments[0].Value;
client.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("NeoOracleService", version));
}
public void Configure()
{
client.DefaultRequestHeaders.Accept.Clear();
foreach (string type in Settings.Default.AllowedContentTypes)
client.DefaultRequestHeaders.Accept.ParseAdd(type);
client.Timeout = Settings.Default.Https.Timeout;
}
public void Dispose()
{
client.Dispose();
}
public async Task<(OracleResponseCode, string)> ProcessAsync(Uri uri, CancellationToken cancellation)
{
Utility.Log(nameof(OracleHttpsProtocol), LogLevel.Debug, $"Request: {uri.AbsoluteUri}");
HttpResponseMessage message;
try
{
do
{
if (!Settings.Default.AllowPrivateHost)
{
IPHostEntry entry = await Dns.GetHostEntryAsync(uri.Host, cancellation);
if (entry.IsInternal())
return (OracleResponseCode.Forbidden, null);
}
message = await client.GetAsync(uri, HttpCompletionOption.ResponseContentRead, cancellation);
if (message.Headers.Location is not null)
{
uri = message.Headers.Location;
message = null;
}
} while (message == null);
}
catch
{
return (OracleResponseCode.Timeout, null);
}
if (message.StatusCode == HttpStatusCode.NotFound)
return (OracleResponseCode.NotFound, null);
if (message.StatusCode == HttpStatusCode.Forbidden)
return (OracleResponseCode.Forbidden, null);
if (!message.IsSuccessStatusCode)
return (OracleResponseCode.Error, null);
if (!Settings.Default.AllowedContentTypes.Contains(message.Content.Headers.ContentType.MediaType))
return (OracleResponseCode.ContentTypeNotSupported, null);
return (OracleResponseCode.Success, await message.Content.ReadAsStringAsync(cancellation));
}
}
}