Skip to content

Commit

Permalink
Merge pull request #485 from brettryan/master
Browse files Browse the repository at this point in the history
Add compact framework support
  • Loading branch information
haacked committed Dec 19, 2013
2 parents 516c4a2 + 2493e15 commit 9d3405b
Show file tree
Hide file tree
Showing 35 changed files with 388 additions and 59 deletions.
2 changes: 1 addition & 1 deletion README.markdown
Expand Up @@ -6,7 +6,7 @@

### Features

* Supports .NET 3.5+, Silverlight 4, Windows Phone 7, Mono, MonoTouch, Mono for Android
* Supports .NET 3.5+, Silverlight 4, Windows Phone 7, Mono, MonoTouch, Mono for Android, Compact Framework 3.5
* Easy installation using [NuGet](http://nuget.org/packages/RestSharp) for most .NET flavors
* Automatic XML and JSON deserialization
* Supports custom serialization and deserialization via ISerializer and IDeserializer
Expand Down
20 changes: 20 additions & 0 deletions RestSharp.Compact.sln
@@ -0,0 +1,20 @@

Microsoft Visual Studio Solution File, Format Version 10.00
# Visual Studio 2008
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RestSharp.Compact", "RestSharp\RestSharp.Compact.csproj", "{A29E330B-F854-4287-BEB2-C4CBE9D9C637}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{A29E330B-F854-4287-BEB2-C4CBE9D9C637}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A29E330B-F854-4287-BEB2-C4CBE9D9C637}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A29E330B-F854-4287-BEB2-C4CBE9D9C637}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A29E330B-F854-4287-BEB2-C4CBE9D9C637}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
Expand Up @@ -49,7 +49,7 @@ public static void ForEach<T>(this IEnumerable<T> items, Action<T> action)
}
}

#if !WINDOWS_PHONE && !SILVERLIGHT
#if !WINDOWS_PHONE && !SILVERLIGHT && !PocketPC

public static void AddRange(this IDictionary<string, string> collection, NameValueCollection range)
{
Expand Down
Expand Up @@ -104,7 +104,7 @@ public static string PercentEncode(this string s)
}

private const RegexOptions Options =
#if !WINDOWS_PHONE && !SILVERLIGHT
#if !WINDOWS_PHONE && !SILVERLIGHT && !PocketPC
RegexOptions.Compiled | RegexOptions.IgnoreCase;
#else
RegexOptions.IgnoreCase;
Expand Down
2 changes: 1 addition & 1 deletion RestSharp/Authenticators/OAuth/HttpPostParameterType.cs
Expand Up @@ -2,7 +2,7 @@

namespace RestSharp.Authenticators.OAuth
{
#if !SILVERLIGHT && !WINDOWS_PHONE
#if !SILVERLIGHT && !WINDOWS_PHONE && !PocketPC
[Serializable]
#endif
internal enum HttpPostParameterType
Expand Down
2 changes: 1 addition & 1 deletion RestSharp/Authenticators/OAuth/OAuthParameterHandling.cs
Expand Up @@ -2,7 +2,7 @@

namespace RestSharp.Authenticators.OAuth
{
#if !SILVERLIGHT && !WINDOWS_PHONE
#if !SILVERLIGHT && !WINDOWS_PHONE && !PocketPC
[Serializable]
#endif
public enum OAuthParameterHandling
Expand Down
2 changes: 1 addition & 1 deletion RestSharp/Authenticators/OAuth/OAuthSignatureMethod.cs
Expand Up @@ -2,7 +2,7 @@

namespace RestSharp.Authenticators.OAuth
{
#if !SILVERLIGHT && !WINDOWS_PHONE
#if !SILVERLIGHT && !WINDOWS_PHONE && !PocketPC
[Serializable]
#endif
public enum OAuthSignatureMethod
Expand Down
2 changes: 1 addition & 1 deletion RestSharp/Authenticators/OAuth/OAuthSignatureTreatment.cs
Expand Up @@ -2,7 +2,7 @@

namespace RestSharp.Authenticators.OAuth
{
#if !SILVERLIGHT && !WINDOWS_PHONE
#if !SILVERLIGHT && !WINDOWS_PHONE && !PocketPC
[Serializable]
#endif
public enum OAuthSignatureTreatment
Expand Down
12 changes: 9 additions & 3 deletions RestSharp/Authenticators/OAuth/OAuthTools.cs
Expand Up @@ -6,7 +6,7 @@

namespace RestSharp.Authenticators.OAuth
{
#if !SILVERLIGHT && !WINDOWS_PHONE
#if !SILVERLIGHT && !WINDOWS_PHONE && !PocketPC
[Serializable]
#endif
internal static class OAuthTools
Expand All @@ -20,13 +20,13 @@ internal static class OAuthTools
private static readonly Random _random;
private static readonly object _randomLock = new object();

#if !SILVERLIGHT && !WINDOWS_PHONE
#if !SILVERLIGHT && !WINDOWS_PHONE && !PocketPC
private static readonly RandomNumberGenerator _rng = RandomNumberGenerator.Create();
#endif

static OAuthTools()
{
#if !SILVERLIGHT && !WINDOWS_PHONE
#if !SILVERLIGHT && !WINDOWS_PHONE && !PocketPC
var bytes = new byte[4];
_rng.GetNonZeroBytes(bytes);
_random = new Random(BitConverter.ToInt32(bytes, 0));
Expand Down Expand Up @@ -305,6 +305,7 @@ public static string GetSignature(OAuthSignatureMethod signatureMethod, string s
string signature;
switch (signatureMethod)
{
#if !PocketPC
case OAuthSignatureMethod.HmacSha1:
{
var crypto = new HMACSHA1();
Expand All @@ -315,14 +316,19 @@ public static string GetSignature(OAuthSignatureMethod signatureMethod, string s

break;
}
#endif
case OAuthSignatureMethod.PlainText:
{
signature = "{0}&{1}".FormatWith(consumerSecret, tokenSecret);

break;
}
default:
#if PocketPC
throw new NotImplementedException("Only PlainText is currently supported.");
#else
throw new NotImplementedException("Only HMAC-SHA1 is currently supported.");
#endif
}

var result = signatureTreatment == OAuthSignatureTreatment.Escaped
Expand Down
2 changes: 1 addition & 1 deletion RestSharp/Authenticators/OAuth/OAuthType.cs
Expand Up @@ -2,7 +2,7 @@

namespace RestSharp.Authenticators.OAuth
{
#if !SILVERLIGHT && !WINDOWS_PHONE
#if !SILVERLIGHT && !WINDOWS_PHONE && !PocketPC
[Serializable]
#endif
public enum OAuthType
Expand Down
2 changes: 1 addition & 1 deletion RestSharp/Authenticators/OAuth/OAuthWebQueryInfo.cs
Expand Up @@ -2,7 +2,7 @@

namespace RestSharp.Authenticators.OAuth
{
#if !SILVERLIGHT && !WINDOWS_PHONE
#if !SILVERLIGHT && !WINDOWS_PHONE && !PocketPC
[Serializable]
#endif
public class OAuthWebQueryInfo
Expand Down
10 changes: 7 additions & 3 deletions RestSharp/Authenticators/OAuth/OAuthWorkflow.cs
@@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;
using RestSharp.Authenticators.OAuth.Extensions;
#if !WINDOWS_PHONE && !SILVERLIGHT
#if !WINDOWS_PHONE && !SILVERLIGHT && !PocketPC
using RestSharp.Contrib;
#endif

Expand Down Expand Up @@ -213,19 +213,23 @@ public virtual OAuthWebQueryInfo BuildProtectedResourceInfo(string method, WebPa

// Include url parameters in query pool
var uri = new Uri(url);
#if !SILVERLIGHT && !WINDOWS_PHONE
#if !SILVERLIGHT && !WINDOWS_PHONE && !PocketPC
var urlParameters = HttpUtility.ParseQueryString(uri.Query);
#else
var urlParameters = uri.Query.ParseQueryString();
#endif

#if !SILVERLIGHT && !WINDOWS_PHONE
#if !SILVERLIGHT && !WINDOWS_PHONE && !PocketPC
foreach (var parameter in urlParameters.AllKeys)
#else
foreach (var parameter in urlParameters.Keys)
#endif
{
#if PocketPC
switch (method.ToUpper())
#else
switch (method.ToUpperInvariant())
#endif
{
case "POST":
parameters.Add(new HttpPostParameter(parameter, urlParameters[parameter]));
Expand Down
2 changes: 1 addition & 1 deletion RestSharp/Authenticators/OAuth/WebPairCollection.cs
Expand Up @@ -30,7 +30,7 @@ public WebPairCollection(IEnumerable<WebPair> parameters)
_parameters = new List<WebPair>(parameters);
}

#if !WINDOWS_PHONE && !SILVERLIGHT
#if !WINDOWS_PHONE && !SILVERLIGHT && !PocketPC
public WebPairCollection(NameValueCollection collection) : this()
{
AddCollection(collection);
Expand Down
4 changes: 2 additions & 2 deletions RestSharp/Authenticators/OAuth/WebParameter.cs
Expand Up @@ -6,10 +6,10 @@

namespace RestSharp.Authenticators.OAuth
{
#if !Smartphone
#if !Smartphone && !PocketPC
[DebuggerDisplay("{Name}:{Value}")]
#endif
#if !SILVERLIGHT && !WINDOWS_PHONE
#if !SILVERLIGHT && !WINDOWS_PHONE && !PocketPC
[Serializable]
#endif
internal class WebParameter : WebPair
Expand Down
2 changes: 1 addition & 1 deletion RestSharp/Authenticators/OAuth/WebParameterCollection.cs
Expand Up @@ -10,7 +10,7 @@ public WebParameterCollection(IEnumerable<WebPair> parameters)
{
}

#if !WINDOWS_PHONE && !SILVERLIGHT
#if !WINDOWS_PHONE && !SILVERLIGHT && !PocketPC
public WebParameterCollection(NameValueCollection collection) : base(collection)
{
}
Expand Down
4 changes: 4 additions & 0 deletions RestSharp/Authenticators/OAuth1Authenticator.cs
Expand Up @@ -156,7 +156,11 @@ private void AddOAuthData(IRestClient client, IRestRequest request, OAuthWorkflo
url = url.Substring(0, queryStringStart);

OAuthWebQueryInfo oauth;
#if PocketPC
var method = request.Method.ToString().ToUpper();
#else
var method = request.Method.ToString().ToUpperInvariant();
#endif

var parameters = new WebParameterCollection();

Expand Down
10 changes: 9 additions & 1 deletion RestSharp/Deserializers/JsonDeserializer.cs
Expand Up @@ -194,7 +194,11 @@ private object ConvertValue(Type type, object value)
{
return stringValue;
}
else if (type == typeof(DateTime) || type == typeof(DateTimeOffset))
else if (type == typeof(DateTime)
#if !PocketPC
|| type == typeof(DateTimeOffset)
#endif
)
{
DateTime dt;
if (DateFormat.HasValue())
Expand All @@ -207,6 +211,9 @@ private object ConvertValue(Type type, object value)
dt = stringValue.ParseJsonDate(Culture);
}

#if PocketPC
return dt;
#else
if (type == typeof(DateTime))
{
return dt;
Expand All @@ -215,6 +222,7 @@ private object ConvertValue(Type type, object value)
{
return (DateTimeOffset)dt;
}
#endif
}
else if (type == typeof(Decimal))
{
Expand Down
4 changes: 3 additions & 1 deletion RestSharp/Deserializers/XmlDeserializer.cs
Expand Up @@ -171,6 +171,7 @@ protected virtual void Map(object x, XElement root)

prop.SetValue(x, value, null);
}
#if !PocketPC
else if (type == typeof(DateTimeOffset))
{
var toConvert = value.ToString();
Expand Down Expand Up @@ -198,6 +199,7 @@ protected virtual void Map(object x, XElement root)
}
}
}
#endif
else if (type == typeof(Decimal))
{
value = Decimal.Parse(value.ToString(), Culture);
Expand Down Expand Up @@ -264,7 +266,7 @@ protected virtual void Map(object x, XElement root)

private static bool TryGetFromString(string inputString, out object result, Type type)
{
#if !SILVERLIGHT && !WINDOWS_PHONE
#if !SILVERLIGHT && !WINDOWS_PHONE && !PocketPC
var converter = TypeDescriptor.GetConverter(type);
if (converter.CanConvertFrom(typeof(string)))
{
Expand Down
4 changes: 2 additions & 2 deletions RestSharp/Extensions/MiscExtensions.cs
Expand Up @@ -25,7 +25,7 @@ namespace RestSharp.Extensions
/// </summary>
public static class MiscExtensions
{
#if !WINDOWS_PHONE
#if !WINDOWS_PHONE && !PocketPC
/// <summary>
/// Save a byte array to a file
/// </summary>
Expand Down Expand Up @@ -87,7 +87,7 @@ public static string AsString(this byte[] buffer)
Encoding encoding = Encoding.UTF8;

#if FRAMEWORK
return encoding.GetString(buffer);
return encoding.GetString(buffer, 0, buffer.Length);
#else
if (buffer == null || buffer.Length == 0)
return "";
Expand Down
12 changes: 10 additions & 2 deletions RestSharp/Extensions/MonoHttp/HtmlEncoder.cs
Expand Up @@ -48,7 +48,11 @@ class HttpEncoder
{
static char[] hexChars = "0123456789abcdef".ToCharArray();
static object entitiesLock = new object();
#if PocketPC
static Dictionary<string, char> entities;
#else
static SortedDictionary<string, char> entities;
#endif
#if NET_4_0
static Lazy <HttpEncoder> defaultEncoder;
static Lazy <HttpEncoder> currentEncoderLazy;
Expand Down Expand Up @@ -229,7 +233,8 @@ string UrlPathEncode(string value)
for (int i = 0; i < length; i++)
UrlPathEncodeChar(value[i], result);

return Encoding.ASCII.GetString(result.ToArray());
byte[] bytes = result.ToArray();
return Encoding.ASCII.GetString(bytes, 0, bytes.Length);
}

internal static byte[] UrlEncodeToBytes(byte[] bytes, int offset, int count)
Expand Down Expand Up @@ -659,8 +664,11 @@ static void InitEntities()
{
// Build the hash table of HTML entity references. This list comes
// from the HTML 4.01 W3C recommendation.
#if PocketPC
entities = new Dictionary<string, char>(StringComparer.Ordinal);
#else
entities = new SortedDictionary<string, char>(StringComparer.Ordinal);

#endif
entities.Add("nbsp", '\u00A0');
entities.Add("iexcl", '\u00A1');
entities.Add("cent", '\u00A2');
Expand Down
17 changes: 9 additions & 8 deletions RestSharp/Extensions/MonoHttp/HttpUtility.cs
Expand Up @@ -182,7 +182,7 @@ public static string UrlDecode(string s, Encoding e)

byte[] buf = bytes.ToArray();
bytes = null;
return e.GetString(buf);
return e.GetString(buf, 0, buf.Length);

}

Expand Down Expand Up @@ -412,7 +412,8 @@ public static string UrlEncode(string s, Encoding Enc)
// avoided GetByteCount call
byte[] bytes = new byte[Enc.GetMaxByteCount(s.Length)];
int realLen = Enc.GetBytes(s, 0, s.Length, bytes, 0);
return Encoding.ASCII.GetString(UrlEncodeToBytes(bytes, 0, realLen));
byte[] r = UrlEncodeToBytes(bytes, 0, realLen);
return Encoding.ASCII.GetString(r, 0, r.Length);
}

public static string UrlEncode(byte[] bytes)
Expand All @@ -422,8 +423,8 @@ public static string UrlEncode(byte[] bytes)

if (bytes.Length == 0)
return String.Empty;

return Encoding.ASCII.GetString(UrlEncodeToBytes(bytes, 0, bytes.Length));
byte[] r = UrlEncodeToBytes(bytes, 0, bytes.Length);
return Encoding.ASCII.GetString(r, 0, r.Length);
}

public static string UrlEncode(byte[] bytes, int offset, int count)
Expand All @@ -433,8 +434,8 @@ public static string UrlEncode(byte[] bytes, int offset, int count)

if (bytes.Length == 0)
return String.Empty;

return Encoding.ASCII.GetString(UrlEncodeToBytes(bytes, offset, count));
byte[] r = UrlEncodeToBytes(bytes, offset, count);
return Encoding.ASCII.GetString(r, 0, r.Length);
}

public static byte[] UrlEncodeToBytes(string str)
Expand Down Expand Up @@ -480,8 +481,8 @@ public static string UrlEncodeUnicode(string str)
{
if (str == null)
return null;

return Encoding.ASCII.GetString(UrlEncodeUnicodeToBytes(str));
byte[] r = UrlEncodeUnicodeToBytes(str);
return Encoding.ASCII.GetString(r, 0, r.Length);
}

public static byte[] UrlEncodeUnicodeToBytes(string str)
Expand Down

0 comments on commit 9d3405b

Please sign in to comment.