Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Route overload, improvements to manostool and support for nullable types #101

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
3 changes: 2 additions & 1 deletion src/Manos.IO/Manos.IO.Managed/Socket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,8 @@ public override Manos.IO.Stream GetSocketStream ()

public override void Close ()
{
GetSocketStream ().Close ();
if (state == Socket.SocketState.Open)
GetSocketStream ().Close ();
}

public override void Connect (string host, int port, Action callback)
Expand Down
8 changes: 7 additions & 1 deletion src/Manos.IO/Manos.IO.Managed/TimerWatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,13 @@ public override void Stop ()
}

protected override void Dispose (bool disposing)
{
{
if (timer != null)
{
timer.Change(Timeout.Infinite, Timeout.Infinite);
timer.Dispose();
timer = null;
}
Context.Remove (this);
}

Expand Down
Binary file added src/Manos.suo
Binary file not shown.
2 changes: 1 addition & 1 deletion src/Manos/Manos.Http/HttpEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ internal virtual void HandleEnd ()

public void Complete (Action callback)
{
IAsyncWatcher completeWatcher;
IAsyncWatcher completeWatcher = null;
completeWatcher = Context.CreateAsyncWatcher (delegate {
completeWatcher.Dispose ();
callback ();
Expand Down
2 changes: 1 addition & 1 deletion src/Manos/Manos.Http/HttpTransaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public void OnResponseFinished ()

IEnumerable<ByteBuffer> ResponseFinishedCallback ()
{
IBaseWatcher handler;
IBaseWatcher handler = null;
handler = Server.Context.CreateIdleWatcher (delegate {
handler.Dispose ();
responseFinished = true;
Expand Down
95 changes: 92 additions & 3 deletions src/Manos/Manos.Http/HttpUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,98 @@ public static string UrlDecode (string s, Encoding e)
bytes = null;
return e.GetString (buf);

}

static int GetInt (byte b)
}

public static bool EncodeChar(char ch)
{
if ((((ch >= 'a') && (ch <= 'z')) || ((ch >= 'A') && (ch <= 'Z'))) || ((ch >= '0') && (ch <= '9')))
{
return false;
}
switch (ch)
{
case '(':
case ')':
case '*':
case '-':
case '.':
case '_':
case '!':
return false;
}
return true;
}

public static char ToHex(int n)
{
if (n <= 9)
{
return (char)('0' + n);
}
return (char)('A' + (n - 10));
}


public static string UrlEncode(string input)
{
return UrlEncode(input, Encoding.UTF8);
}

public static string UrlEncode(string input, Encoding e)
{
// Get byte array
var bytes = e.GetBytes(input);

// Count number of extra characters
int spaces = 0;
int non_safe = 0;
for (int i = 0; i < bytes.Length; i++)
{
byte ch = bytes[i];
if (ch == ' ')
{
spaces++;
}
else if (EncodeChar((char)ch))
{
non_safe++;
}
}

// Do we actually need to do anything?
if ((spaces == 0) && (non_safe == 0))
{
return Encoding.ASCII.GetString(bytes);
}

// Allocate a new buffer and do the encoding
byte[] buffer = new byte[bytes.Length + (non_safe * 2)];
int outpos = 0;
for (int i = 0; i < bytes.Length; i++)
{
byte ch = bytes[i];
if (ch == ' ')
{
buffer[outpos++] = (byte)'+';
}
else if (EncodeChar((char)ch))
{
buffer[outpos++] = (byte)'%';
buffer[outpos++] = (byte)ToHex((ch >> 4) & 0xF);
buffer[outpos++] = (byte)ToHex(ch & 15);
}
else
{
buffer[outpos++] = ch;
}
}

// Return as ascii encoded string
return Encoding.ASCII.GetString(buffer);
}


static int GetInt(byte b)
{
char c = (char) b;
if (c >= '0' && c <= '9')
Expand Down
8 changes: 6 additions & 2 deletions src/Manos/Manos.Routing/ParameterizedActionTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,12 @@ public static bool TryConvertUnsafeString (IManosContext ctx, Type type, Paramet
return true;
}

try {
data = Convert.ChangeType (str_value, type);
try {
var underlying_type = Nullable.GetUnderlyingType(type);
if (underlying_type != null)
data = Convert.ChangeType (str_value, underlying_type);
else
data = Convert.ChangeType (str_value, type);
} catch {
Console.Error.WriteLine ("Error while converting '{0}' to '{1}'.", str_value, type);

Expand Down