Skip to content

Commit 0db69bb

Browse files
committed
**API.Utility** static class fixed:
* `API.Utility.IpAddress` suppressed. Use the public `API.Utility.GetIP()` instead. * `API.Utility.UserAgent` suppressed. Use the public `API.Utility.GetUserAgent()` instead. * `API.Utility.HttpGET` suppressed. Use the `JSONRPC_API` and `RESTful_API` public member `httpGET`. * `API.Utility.HttpPOST` suppressed. Use the `JSONRPC_API` and `RESTful_API` public member `httpPOST`.
1 parent e0c0730 commit 0db69bb

File tree

16 files changed

+103
-87
lines changed

16 files changed

+103
-87
lines changed

rls/API.Library.dll

512 Bytes
Binary file not shown.

rls/API.Library.pdb

2 KB
Binary file not shown.

src/API.Library/Entities/API.Common.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using Newtonsoft.Json;
22
using System;
3+
using System.Collections.Specialized;
34
using System.Configuration;
45
using System.DirectoryServices.AccountManagement;
6+
using System.IO;
57
using System.Linq;
68
using System.Web;
79

@@ -84,6 +86,16 @@ public class Common
8486
/// </summary>
8587
internal string NetworkUsername = null;
8688

89+
/// <summary>
90+
/// HTTP GET Request
91+
/// </summary>
92+
internal NameValueCollection httpGET = GetHttpGET();
93+
94+
/// <summary>
95+
/// HTTP POST Request
96+
/// </summary>
97+
internal string httpPOST = GetHttpPOST();
98+
8799
/// <summary>
88100
/// Authenticate the user in the context
89101
/// </summary>
@@ -316,6 +328,42 @@ public class Common
316328

317329
return true;
318330
}
331+
332+
/// <summary>
333+
/// Get the HTTP request for the GET method
334+
/// </summary>
335+
/// <returns></returns>
336+
internal static NameValueCollection GetHttpGET()
337+
{
338+
try
339+
{
340+
// Read the request from GET
341+
return HttpContext.Current.Request.QueryString;
342+
}
343+
catch (Exception e)
344+
{
345+
Log.Instance.Info(e);
346+
return null;
347+
}
348+
}
349+
350+
/// <summary>
351+
/// Get the HTTP request for the POST method
352+
/// </summary>
353+
/// <returns></returns>
354+
internal static string GetHttpPOST()
355+
{
356+
try
357+
{
358+
// Read the request from POST
359+
return new StreamReader(HttpContext.Current.Request.InputStream).ReadToEnd();
360+
}
361+
catch (Exception e)
362+
{
363+
Log.Instance.Info(e);
364+
return null;
365+
}
366+
}
319367
}
320368

321369
/// <summary>

src/API.Library/Entities/API.JSONRPC.cs

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Newtonsoft.Json.Linq;
22
using System;
33
using System.Collections.Generic;
4+
using System.Collections.Specialized;
45
using System.Configuration;
56
using System.Linq;
67
using System.Reflection;
@@ -24,7 +25,7 @@ public class JSONRPC : Common, IHttpHandler, IRequiresSessionState
2425
/// <summary>
2526
/// GET request parameter
2627
/// </summary>
27-
internal const string JSONRPC_GETparam = "data";
28+
internal const string JSONRPC_GetParam = "data";
2829

2930
/// <summary>
3031
/// JSON-stat mimetype
@@ -159,7 +160,7 @@ private void ParseError(ref HttpContext context, string id, JSONRPC_Error error)
159160
break;
160161
}
161162

162-
Log.Instance.Info("IP: " + Utility.IpAddress + ", Error Code: " + error.code.ToString() + ", Error Message: " + error.message.ToString() + ", Error Data: " + (error.data == null ? "" : error.data.ToString()));
163+
Log.Instance.Info("IP: " + Utility.GetIP() + ", Error Code: " + error.code.ToString() + ", Error Message: " + error.message.ToString() + ", Error Data: " + (error.data == null ? "" : error.data.ToString()));
163164
object output = new JSONRPC_ResponseError { jsonrpc = JSONRPC_Version, error = error, id = id };
164165
context.Response.Write(Utility.JsonSerialize_IgnoreLoopingReference(output));
165166
context.Response.End();
@@ -174,32 +175,24 @@ private JSONRPC_Request ParseRequest(ref HttpContext context)
174175
{
175176
// Initialise requests
176177
string request = null;
177-
string DATArequest = null;
178-
string POSTrequest = null;
179-
180-
// Read the request from GET
181-
DATArequest = Utility.HttpGET()[JSONRPC_GETparam];
182-
183-
// Read the request from POST
184-
POSTrequest = Utility.HttpPOST();
185178

186179
// Check the query input exists
187-
if (string.IsNullOrWhiteSpace(DATArequest)
188-
&& string.IsNullOrWhiteSpace(POSTrequest))
180+
if (string.IsNullOrWhiteSpace(httpGET[JSONRPC_GetParam])
181+
&& string.IsNullOrWhiteSpace(httpPOST))
189182
{
190183
JSONRPC_Error error = new JSONRPC_Error { code = -32700 };
191184
ParseError(ref context, null, error);
192185
}
193186

194187
// POST request overrides GET one
195-
if (!string.IsNullOrWhiteSpace(POSTrequest))
188+
if (!string.IsNullOrWhiteSpace(httpPOST))
196189
{
197-
request = POSTrequest;
190+
request = httpPOST;
198191
Log.Instance.Info("Request type: POST");
199192
}
200193
else
201194
{
202-
request = DATArequest;
195+
request = httpGET[JSONRPC_GetParam];
203196
Log.Instance.Info("Request type: GET");
204197
}
205198

@@ -320,8 +313,10 @@ private dynamic GetResult(ref HttpContext context, JSONRPC_Request JSONRPC_Reque
320313
apiRequest.method = JSONRPC_Request.method;
321314
apiRequest.parameters = JSONRPC_Request.@params;
322315
apiRequest.userPrincipal = UserPrincipal;
323-
apiRequest.ipAddress = Utility.IpAddress;
324-
apiRequest.userAgent = Utility.UserAgent;
316+
apiRequest.ipAddress = Utility.GetIP();
317+
apiRequest.userAgent = Utility.GetUserAgent();
318+
apiRequest.httpGET = httpGET;
319+
apiRequest.httpPOST = httpPOST;
325320

326321
// Hide password from logs
327322
Log.Instance.Info("API Request: " + MaskParameters(Utility.JsonSerialize_IgnoreLoopingReference(apiRequest)));
@@ -471,6 +466,16 @@ public class JSONRPC_API
471466
/// Client user agent
472467
/// </summary>
473468
public string userAgent { get; internal set; }
469+
470+
/// <summary>
471+
/// GET request
472+
/// </summary>
473+
public NameValueCollection httpGET { get; internal set; }
474+
475+
/// <summary>
476+
/// POST request
477+
/// </summary>
478+
public string httpPOST { get; internal set; }
474479
#endregion
475480
}
476481

src/API.Library/Entities/API.Map.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ public static JSONRPC_API RESTful2JSONRPC_API(RESTful_API RestfulApi)
2828
parameters = RestfulApi.parameters,
2929
userPrincipal = RestfulApi.userPrincipal,
3030
ipAddress = RestfulApi.ipAddress,
31-
userAgent = RestfulApi.userAgent
31+
userAgent = RestfulApi.userAgent,
32+
httpGET = RestfulApi.httpGET,
33+
httpPOST = RestfulApi.httpPOST
3234
};
3335
}
3436

@@ -48,7 +50,9 @@ public static RESTful_API JSONRPC2RESTful_API(JSONRPC_API JsonRpcApi)
4850
parameters = JsonRpcApi.parameters,
4951
userPrincipal = JsonRpcApi.userPrincipal,
5052
ipAddress = JsonRpcApi.ipAddress,
51-
userAgent = JsonRpcApi.userAgent
53+
userAgent = JsonRpcApi.userAgent,
54+
httpGET = JsonRpcApi.httpGET,
55+
httpPOST = JsonRpcApi.httpPOST
5256
};
5357
}
5458

src/API.Library/Entities/API.RESTful.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Collections.Specialized;
34
using System.Linq;
45
using System.Net;
56
using System.Net.Mime;
@@ -94,7 +95,7 @@ public void ProcessRequest(HttpContext context)
9495
/// <param name="statusDescription"></param>
9596
private void ParseError(ref HttpContext context, HttpStatusCode statusCode, string statusDescription = "")
9697
{
97-
Log.Instance.Info("IP: " + Utility.IpAddress + ", Status Code: " + statusCode.ToString() + ", Status Description: " + statusDescription);
98+
Log.Instance.Info("IP: " + Utility.GetIP() + ", Status Code: " + statusCode.ToString() + ", Status Description: " + statusDescription);
9899

99100
context.Response.StatusCode = (int)statusCode;
100101
if (!string.IsNullOrEmpty(statusDescription))
@@ -235,8 +236,10 @@ private dynamic GetResult(ref HttpContext context)
235236
apiRequest.method = RequestParams[0];
236237
apiRequest.parameters = RequestParams;
237238
apiRequest.userPrincipal = UserPrincipal;
238-
apiRequest.ipAddress = Utility.IpAddress;
239-
apiRequest.userAgent = Utility.UserAgent;
239+
apiRequest.ipAddress = Utility.GetIP();
240+
apiRequest.userAgent = Utility.GetUserAgent();
241+
apiRequest.httpGET = httpGET;
242+
apiRequest.httpPOST = httpPOST;
240243

241244
// Hide password from logs
242245
Log.Instance.Info("API Request: " + Utility.JsonSerialize_IgnoreLoopingReference(apiRequest));
@@ -319,6 +322,16 @@ public class RESTful_API
319322
/// </summary>
320323
public string userAgent { get; internal set; }
321324

325+
/// <summary>
326+
/// GET request
327+
/// </summary>
328+
public NameValueCollection httpGET { get; internal set; }
329+
330+
/// <summary>
331+
/// POST request
332+
/// </summary>
333+
public string httpPOST { get; internal set; }
334+
322335
#endregion
323336
}
324337
}

src/API.Library/Entities/Utility.cs

Lines changed: 3 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using log4net;
2-
using Newtonsoft.Json;
1+
using Newtonsoft.Json;
32
using System;
43
using System.Collections.Generic;
54
using System.Collections.Specialized;
@@ -10,7 +9,6 @@
109
using System.Linq;
1110
using System.Net;
1211
using System.Net.Sockets;
13-
using System.Reflection;
1412
using System.Security.Cryptography;
1513
using System.Text;
1614
using System.Web;
@@ -24,21 +22,6 @@ public static class Utility
2422
{
2523
#region Properties
2624

27-
/// <summary>
28-
/// Initiate Log4Net
29-
/// </summary>
30-
private static readonly ILog logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
31-
32-
/// <summary>
33-
/// Set the IP Address
34-
/// </summary>
35-
public static string IpAddress = GetIP();
36-
37-
/// <summary>
38-
/// Set the User Agent
39-
/// </summary>
40-
public static string UserAgent = GetUserAgent();
41-
4225
#endregion
4326

4427
#region Methods
@@ -101,7 +84,7 @@ public static string GetSHA256(string input)
10184
/// Get the IP Address of the current request
10285
/// </summary>
10386
/// <returns></returns>
104-
private static string GetIP()
87+
public static string GetIP()
10588
{
10689
// Initialise
10790
string ipAddress = "";
@@ -148,7 +131,7 @@ private static string GetIP()
148131
/// <summary>
149132
/// Get the User Agent from the Current Context
150133
/// </summary>
151-
private static string GetUserAgent()
134+
public static string GetUserAgent()
152135
{
153136
return HttpContext.Current == null ? "" : HttpContext.Current.Request.UserAgent.ToString();
154137
}
@@ -431,42 +414,6 @@ public static string GetUserAcceptLanguage()
431414
return null;
432415
}
433416
}
434-
435-
/// <summary>
436-
/// Get the HTTP request for the GET method
437-
/// </summary>
438-
/// <returns></returns>
439-
public static NameValueCollection HttpGET()
440-
{
441-
try
442-
{
443-
// Read the request from GET
444-
return HttpContext.Current.Request.QueryString;
445-
}
446-
catch (Exception e)
447-
{
448-
Log.Instance.Info(e);
449-
return null;
450-
}
451-
}
452-
453-
/// <summary>
454-
/// Get the HTTP request for the POST method
455-
/// </summary>
456-
/// <returns></returns>
457-
public static string HttpPOST()
458-
{
459-
try
460-
{
461-
// Read the request from POST
462-
return new StreamReader(HttpContext.Current.Request.InputStream).ReadToEnd();
463-
}
464-
catch (Exception e)
465-
{
466-
Log.Instance.Info(e);
467-
return null;
468-
}
469-
}
470417
#endregion
471418
}
472419
}

src/API.Library/Entities/eMail/eMail.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
2-
using System.Net.Mail;
32
using System.Collections.Generic;
43
using System.Configuration;
4+
using System.Net.Mail;
55

66
namespace API
77
{
@@ -136,7 +136,7 @@ public bool Send()
136136
public string ParseTemplate(string template, List<eMail_KeyValuePair> eMail_KeyValuePair)
137137
{
138138
eMail_KeyValuePair.Add(new eMail_KeyValuePair() { key = "{datetime}", value = DateTime.Now.ToString(API_EMAIL_DATETIME_MASK) });
139-
eMail_KeyValuePair.Add(new eMail_KeyValuePair() { key = "{ip}", value = Utility.IpAddress });
139+
eMail_KeyValuePair.Add(new eMail_KeyValuePair() { key = "{ip}", value = Utility.GetIP() });
140140

141141
Log.Instance.Info("eMail String-Template to parse: " + template);
142142
Log.Instance.Info("eMail List to parse: " + Utility.JsonSerialize_IgnoreLoopingReference(eMail_KeyValuePair));

src/API.Library/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
// You can specify all the values or you can default the Build and Revision Numbers
3232
// by using the '*' as shown below:
3333
// [assembly: AssemblyVersion("1.0.*")]
34-
[assembly: AssemblyVersion("4.1.3")]
35-
[assembly: AssemblyFileVersion("4.1.3")]
34+
[assembly: AssemblyVersion("4.2.0")]
35+
[assembly: AssemblyFileVersion("4.2.0")]
3636

3737
// Configure log4net using the Web.config file by default
3838
[assembly: log4net.Config.XmlConfigurator(Watch = true)]

test/API.Test/API.Test.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@
7272
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
7373
</PropertyGroup>
7474
<ItemGroup>
75-
<Reference Include="API.Library, Version=4.1.3, Culture=neutral, processorArchitecture=MSIL">
75+
<Reference Include="API.Library, Version=4.2.0, Culture=neutral, processorArchitecture=MSIL">
7676
<SpecificVersion>False</SpecificVersion>
77-
<HintPath>..\packages\API.Library.4.1.3\API.Library.dll</HintPath>
77+
<HintPath>..\packages\API.Library.4.2.0\API.Library.dll</HintPath>
7878
</Reference>
7979
<Reference Include="Enyim.Caching, Version=2.16.0.0, Culture=neutral, PublicKeyToken=cec98615db04012e, processorArchitecture=MSIL">
8080
<HintPath>..\packages\EnyimMemcached.2.16.0\lib\net35\Enyim.Caching.dll</HintPath>

0 commit comments

Comments
 (0)