forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebRequestUtils.cs
More file actions
98 lines (83 loc) · 3.48 KB
/
Copy pathWebRequestUtils.cs
File metadata and controls
98 lines (83 loc) · 3.48 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
using System;
using System.Net;
using System.Text;
using ServiceStack.Common.Web;
using ServiceStack.ServiceHost;
using ServiceStack.ServiceInterface.ServiceModel;
using ServiceStack.Text;
namespace ServiceStack.ServiceClient.Web
{
public class AuthenticationException : Exception
{
public AuthenticationException()
{
}
public AuthenticationException(string message) : base(message)
{
}
public AuthenticationException(string message, Exception innerException) : base(message, innerException)
{
}
}
public static class WebRequestUtils
{
internal static AuthenticationException CreateCustomException(string uri, AuthenticationException ex)
{
if (uri.StartsWith("https"))
{
return new AuthenticationException(
String.Format("Invalid remote SSL certificate, overide with: \nServicePointManager.ServerCertificateValidationCallback += ((sender, certificate, chain, sslPolicyErrors) => isValidPolicy);"),
ex);
}
return null;
}
internal static bool ShouldAuthenticate(Exception ex, string userName, string password)
{
var webEx = ex as WebException;
return (webEx != null
&& webEx.Response != null
&& ((HttpWebResponse) webEx.Response).StatusCode == HttpStatusCode.Unauthorized
&& !String.IsNullOrEmpty(userName)
&& !String.IsNullOrEmpty(password));
}
internal static void AddBasicAuth(this WebRequest client, string userName, string password)
{
client.Headers[HttpHeaders.Authorization]
= "Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes(userName + ":" + password));
}
/// <summary>
/// Naming convention for the request's Response DTO
/// </summary>
public const string ResponseDtoSuffix = "Response";
public static string GetResponseDtoName(object request)
{
var requestType = request.GetType();
return requestType != typeof(object)
? requestType.FullName + ResponseDtoSuffix
: request.GetType().FullName + ResponseDtoSuffix;
}
public static Type GetErrorResponseDtoType(object request)
{
if (request == null)
return typeof(ErrorResponse);
//If a conventionally-named Response type exists use that regardless if it has ResponseStatus or not
var responseDtoType = AssemblyUtils.FindType(GetResponseDtoName(request));
if (responseDtoType == null)
{
var genericDef = request.GetType().GetTypeWithGenericTypeDefinitionOf(typeof(IReturn<>));
if (genericDef != null)
{
var returnDtoType = genericDef.GetGenericArguments()[0];
var hasResponseStatus = returnDtoType is IHasResponseStatus
|| returnDtoType.GetProperty("ResponseStatus") != null;
//Only use the specified Return type if it has a ResponseStatus property
if (hasResponseStatus)
{
responseDtoType = returnDtoType;
}
}
}
return responseDtoType ?? typeof(ErrorResponse);
}
}
}