Skip to content

Commit

Permalink
Remove the reference to System.Web
Browse files Browse the repository at this point in the history
System.Web is not available under all profiles. This makes it awkward
for people trying to use monotorrent under profiles without System.Web.
As this assembly is only used for Url encoding/decoding, just replace
those methods with a custom implementation which has been copied/pasted
from monos source tree.
  • Loading branch information
alanmcgovern committed Oct 3, 2010
1 parent 9b593ee commit 582f0af
Show file tree
Hide file tree
Showing 3 changed files with 184 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/MonoTorrent/MonoTorrent.Common/InfoHash.cs
Expand Up @@ -80,7 +80,7 @@ public override string ToString()

public string UrlEncode()
{
return HttpUtility.UrlEncode(Hash);
return UriHelper.UrlEncode(Hash);
}

public static bool operator ==(InfoHash left, InfoHash right)
Expand Down Expand Up @@ -165,7 +165,7 @@ public static InfoHash FromMagnetLink(string magnetLink)
public static InfoHash UrlDecode(string infoHash)
{
Check.InfoHash(infoHash);
return new InfoHash(HttpUtility.UrlDecodeToBytes(infoHash));
return new InfoHash(UriHelper.UrlDecode(infoHash));
}
}
}
181 changes: 181 additions & 0 deletions src/MonoTorrent/MonoTorrent.Common/UriHelper.cs
@@ -0,0 +1,181 @@
//
// System.Web.HttpUtility/HttpEncoder
//
// Authors:
// Patrik Torstensson (Patrik.Torstensson@labs2.com)
// Wictor Wilén (decode/encode functions) (wictor@ibizkit.se)
// Tim Coleman (tim@timcoleman.com)
// Gonzalo Paniagua Javier (gonzalo@ximian.com)
// Marek Habersack <mhabersack@novell.com>
//
// Copyright (C) 2005-2010 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

// THIS FILE IS COPIED/PASTED FROM THE MONO SOURCE TREE TO AVOID
// A DEPENDENCY ON SYSTEM.WEB WHICH IS NOT ALWAYS AVAILABLE.

using System;
using System.Text;
using System.IO;
using System.Collections.Generic;

namespace MonoTorrent
{
static class UriHelper
{
static readonly char [] hexChars = "0123456789abcdef".ToCharArray ();

public static string UrlEncode (byte[] bytes)
{
if (bytes == null)
throw new ArgumentNullException ("bytes");

var result = new MemoryStream (bytes.Length);
for (int i = 0; i < bytes.Length; i++)
UrlEncodeChar ((char)bytes [i], result, false);

return Encoding.ASCII.GetString (result.ToArray());
}

public static byte [] UrlDecode (string s)
{
if (null == s)
return null;

var e = Encoding.UTF8;
if (s.IndexOf ('%') == -1 && s.IndexOf ('+') == -1)
return e.GetBytes (s);

long len = s.Length;
var bytes = new List <byte> ();
int xchar;
char ch;

for (int i = 0; i < len; i++) {
ch = s [i];
if (ch == '%' && i + 2 < len && s [i + 1] != '%') {
if (s [i + 1] == 'u' && i + 5 < len) {
// unicode hex sequence
xchar = GetChar (s, i + 2, 4);
if (xchar != -1) {
WriteCharBytes (bytes, (char)xchar, e);
i += 5;
} else
WriteCharBytes (bytes, '%', e);
} else if ((xchar = GetChar (s, i + 1, 2)) != -1) {
WriteCharBytes (bytes, (char)xchar, e);
i += 2;
} else {
WriteCharBytes (bytes, '%', e);
}
continue;
}

if (ch == '+')
WriteCharBytes (bytes, ' ', e);
else
WriteCharBytes (bytes, ch, e);
}

return bytes.ToArray ();
}

static void UrlEncodeChar (char c, Stream result, bool isUnicode) {
if (c > ' ' && NotEncoded (c)) {
result.WriteByte ((byte)c);
return;
}
if (c==' ') {
result.WriteByte ((byte)'+');
return;
}
if ( (c < '0') ||
(c < 'A' && c > '9') ||
(c > 'Z' && c < 'a') ||
(c > 'z')) {
if (isUnicode && c > 127) {
result.WriteByte ((byte)'%');
result.WriteByte ((byte)'u');
result.WriteByte ((byte)'0');
result.WriteByte ((byte)'0');
}
else
result.WriteByte ((byte)'%');

int idx = ((int) c) >> 4;
result.WriteByte ((byte)hexChars [idx]);
idx = ((int) c) & 0x0F;
result.WriteByte ((byte)hexChars [idx]);
}
else {
result.WriteByte ((byte)c);
}
}

static int GetChar (string str, int offset, int length)
{
int val = 0;
int end = length + offset;
for (int i = offset; i < end; i++) {
char c = str [i];
if (c > 127)
return -1;

int current = GetInt ((byte) c);
if (current == -1)
return -1;
val = (val << 4) + current;
}

return val;
}

static int GetInt (byte b)
{
char c = (char) b;
if (c >= '0' && c <= '9')
return c - '0';

if (c >= 'a' && c <= 'f')
return c - 'a' + 10;

if (c >= 'A' && c <= 'F')
return c - 'A' + 10;

return -1;
}

static bool NotEncoded (char c)
{
return c == '!' || c == '(' || c == ')' || c == '*' || c == '-' || c == '.' || c == '_' || c == '\'';
}

static void WriteCharBytes (List<byte> buf, char ch, Encoding e)
{
if (ch > 255) {
foreach (byte b in e.GetBytes (new char[] { ch }))
buf.Add (b);
} else
buf.Add ((byte)ch);
}
}
}
2 changes: 1 addition & 1 deletion src/MonoTorrent/MonoTorrent.csproj
Expand Up @@ -90,7 +90,6 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Web" />
</ItemGroup>
<ItemGroup>
<Compile Include="AssemblyInfo.cs" />
Expand Down Expand Up @@ -327,6 +326,7 @@
<Compile Include="MonoTorrent.Client\NetworkIO\State.cs" />
<Compile Include="MonoTorrent.Client\NetworkIO\ReceiveMessageState.cs" />
<Compile Include="MonoTorrent.Common\MagnetLink.cs" />
<Compile Include="MonoTorrent.Common\UriHelper.cs" />
</ItemGroup>
<ItemGroup>
<BootstrapperPackage Include="Microsoft.Net.Framework.2.0">
Expand Down

0 comments on commit 582f0af

Please sign in to comment.