Skip to content

Commit

Permalink
Merge pull request #59 from cBrain-dk/master
Browse files Browse the repository at this point in the history
Fixes issue #58
  • Loading branch information
JornWildt authored Jan 18, 2023
2 parents 3f7c87c + fdf304b commit 0487f85
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 31 deletions.
4 changes: 4 additions & 0 deletions README.TXT
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# cBrain contribution guide

There are currently 2 branches *master* and *dev*. The branch *dev* represents our synchronization with the original fork. As such changes which we expect to push back should be made in *dev* and subsequently merged to *master*. Changes which are purely internal, such as our own versioning, should be made in *master*.

-----------------------------------------------------------------
Ramone - A C# library for working with REST services and Web APIs
-----------------------------------------------------------------
Expand Down
63 changes: 63 additions & 0 deletions Ramone.Tests/AuthenticationHeaderParsingTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System;
using System.Net.Http.Headers;
using NUnit.Framework;


namespace Ramone.Tests
{
[TestFixture]
public class AuthenticationHeaderValueTests
{
private void CanParseBase(string authenticationHeader, string expectedScheme, string expectedParameters)
{
// Act
AuthenticationHeaderValue authenticationHeaderValue = AuthenticationHeaderValue.Parse(authenticationHeader);

// Assert
Assert.That(authenticationHeaderValue.Scheme, Is.EqualTo(expectedScheme));
Assert.That(authenticationHeaderValue.Parameter, Is.EqualTo(expectedParameters));
}


[Test]
public void CanParseWithNoParameters() => CanParseBase(
authenticationHeader: "Basic",
expectedScheme: "Basic",
expectedParameters: null);


[Test]
public void CanParseSchemeWithTrailingWhitespace() => CanParseBase(
authenticationHeader: "Basic ",
expectedScheme: "Basic",
expectedParameters: null);


[Test]
public void CanParseSchemeWithTrailingWhitespaces() => CanParseBase(
authenticationHeader: "Basic ",
expectedScheme: "Basic",
expectedParameters: null);


[Test]
public void CanParseSchemeAndParameter() => CanParseBase(
authenticationHeader: "Basic dGVzdDoxMjPCow==",
expectedScheme: "Basic",
expectedParameters: "dGVzdDoxMjPCow==");


[Test]
public void CanParseSchemeAndParameters() => CanParseBase(
authenticationHeader: "Basic dGVzdDoxMjPCow==, charset=\"UTF-8\"",
expectedScheme: "Basic",
expectedParameters: "dGVzdDoxMjPCow==, charset=\"UTF-8\"");


[TestCase(null)]
[TestCase("")]
[TestCase(" ")]
public void CannotParse(string authenticationHeader) =>
Assert.That(() => AuthenticationHeaderValue.Parse(authenticationHeader), Throws.InstanceOf<FormatException>());
}
}
2 changes: 2 additions & 0 deletions Ramone.Tests/Ramone.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
<Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Net.Http" />
<Reference Include="System.ServiceModel" />
<Reference Include="System.ServiceModel.Web">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
Expand All @@ -84,6 +85,7 @@
<Compile Include="ApplicationErrorTests.cs" />
<Compile Include="AsyncEventTests.cs" />
<Compile Include="AsyncTests.cs" />
<Compile Include="AuthenticationHeaderParsingTests.cs" />
<Compile Include="BindingTests.cs" />
<Compile Include="Blog\BlogTestHelper.cs" />
<Compile Include="Blog\Codecs\Html\AuthorCodec_Html.cs" />
Expand Down
8 changes: 4 additions & 4 deletions Ramone.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29905.134
# Visual Studio Version 17
VisualStudioVersion = 17.3.32825.248
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Documents", "Documents", "{2615491B-5415-4FFF-A79F-1735FCF5E4E6}"
ProjectSection(SolutionItems) = preProject
Expand Down Expand Up @@ -72,8 +72,8 @@ Global
{DD3B040F-3D75-4C46-BB54-CD06F594F894}.Release|x86.ActiveCfg = Release|Any CPU
{B20077D3-7F5D-4EF5-AF35-22CA78C8790F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B20077D3-7F5D-4EF5-AF35-22CA78C8790F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B20077D3-7F5D-4EF5-AF35-22CA78C8790F}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{B20077D3-7F5D-4EF5-AF35-22CA78C8790F}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{B20077D3-7F5D-4EF5-AF35-22CA78C8790F}.Debug|Mixed Platforms.ActiveCfg = Release|Any CPU
{B20077D3-7F5D-4EF5-AF35-22CA78C8790F}.Debug|Mixed Platforms.Build.0 = Release|Any CPU
{B20077D3-7F5D-4EF5-AF35-22CA78C8790F}.Debug|x86.ActiveCfg = Debug|Any CPU
{B20077D3-7F5D-4EF5-AF35-22CA78C8790F}.Debug|x86.Build.0 = Debug|Any CPU
{B20077D3-7F5D-4EF5-AF35-22CA78C8790F}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down
25 changes: 12 additions & 13 deletions Ramone/BaseRequest.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using Ramone.Utility.Validation;
using Ramone.Utility.Validation;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Net;
using System.Net.Http.Headers;

namespace Ramone
{
Expand Down Expand Up @@ -387,7 +388,7 @@ protected HandleWebExceptionResult HandleWebException(WebException ex, Uri url,
{
if (response.StatusCode == HttpStatusCode.Unauthorized)
{
if (!HandleUnauthorized(response, ex))
if (!HandleUnauthorized(response))
return new HandleWebExceptionResult();

if (retryLevel == 0)
Expand All @@ -408,20 +409,18 @@ protected HandleWebExceptionResult HandleWebException(WebException ex, Uri url,
}


private bool HandleUnauthorized(HttpWebResponse response, WebException ex)
private bool HandleUnauthorized(HttpWebResponse response)
{
string authenticationHeader = response.Headers["WWW-Authenticate"];
if (!string.IsNullOrEmpty(authenticationHeader))
{
int pos = authenticationHeader.IndexOf(' ');
string scheme = authenticationHeader.Substring(0, pos);
string parameters = authenticationHeader.Substring(pos + 1);
IAuthorizationHandler handler = Session.AuthorizationDispatcher.Get(scheme);
if (handler != null && handler.HandleAuthorizationRequest(new AuthorizationContext(Session, response, scheme, parameters)))
return true;
}

return false;
if (!AuthenticationHeaderValue.TryParse(authenticationHeader, out AuthenticationHeaderValue authenticationHeaderValue))
return false;

IAuthorizationHandler handler = Session.AuthorizationDispatcher.Get(authenticationHeaderValue.Scheme);

return handler != null
&& handler.HandleAuthorizationRequest(
new AuthorizationContext(Session, response, authenticationHeaderValue.Scheme, authenticationHeaderValue.Parameter));
}
}
}
12 changes: 4 additions & 8 deletions Ramone/MediaTypes/Xml/XmlSerializerCodec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@
using System.Xml.Serialization;
using System;
using System.Collections.Generic;

using System.Collections.Concurrent;

namespace Ramone.MediaTypes.Xml
{
public class XmlSerializerCodec : XmlStreamCodecBase
{
// The XmlSerializer is thread safe according to the online docs, so it should be safe
// to share instances.
static Dictionary<Type, XmlSerializer> Serializers { get; set; }
static ConcurrentDictionary<Type, XmlSerializer> Serializers { get; set; }


static XmlSerializerCodec()
{
Serializers = new Dictionary<Type, XmlSerializer>();
Serializers = new ConcurrentDictionary<Type, XmlSerializer>();
}


Expand All @@ -38,11 +38,7 @@ protected override void WriteTo(object item, XmlWriter writer, WriterContext con

protected XmlSerializer GetSerializer(Type t)
{
if (!Serializers.ContainsKey(t))
{
Serializers[t] = CreateSerializer(t);
}
return Serializers[t];
return Serializers.GetOrAdd(t, CreateSerializer);
}


Expand Down
13 changes: 7 additions & 6 deletions Ramone/Ramone.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,26 @@

<ItemGroup Condition=" '$(TargetFramework)' == 'net48' ">
<Reference Include="System.Web" />
<Reference Include="System.Net.Http" />
</ItemGroup>

<PropertyGroup>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageProjectUrl>https://github.com/JornWildt/Ramone</PackageProjectUrl>
<RepositoryUrl>https://github.com/JornWildt/Ramone.git</RepositoryUrl>
<PackageProjectUrl>https://github.com/cBrain-dk/Ramone/</PackageProjectUrl>
<RepositoryUrl>https://github.com/cBrain-dk/Ramone/</RepositoryUrl>
<RepositoryType>GIT</RepositoryType>
<PackageTags>REST JSON XML HTTP WEB API</PackageTags>
<Version>4.1.5.0</Version>
<Version>4.1.5.2-cbrain</Version>
<Authors>Jørn Wildt and others</Authors>
<Company>Jørn Wildt</Company>
<Description>Ramone is a C# library that simplifies access to HTTP based Web APIs and REST services. It has a strong focus on REST and hypermedia and implements elements of the Uniform Interface as first class citizens of the API.

Ramone has built-in support for serialization of simple objects as JSON, XML, URL-encoding and multipart encoding.</Description>
<Copyright>Copyright ©2010, Ramone team</Copyright>
<Product />
<PackageReleaseNotes>Rebuild with the right code - was missing code from previous versions.</PackageReleaseNotes>
<AssemblyVersion>4.1.5.0</AssemblyVersion>
<FileVersion>4.1.5.0</FileVersion>
<PackageReleaseNotes>Use System.Net.Http.Headers.AuthenticationHeaderValue to parse authentication header.</PackageReleaseNotes>
<AssemblyVersion>4.1.5.2</AssemblyVersion>
<FileVersion>4.1.5.2</FileVersion>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit 0487f85

Please sign in to comment.