Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions Tests/HttpUnitTests/HttpListenerRequestTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// Copyright (c) .NET Foundation and Contributors
// See LICENSE file in the project root for full license information.
//


using System.Net;
using nanoFramework.TestFramework;

namespace HttpUnitTests
{
internal class HttpListenerRequestTests
Comment thread
benyuz marked this conversation as resolved.
{
// Verifies that malformed Authorization header (no space) does not cause a crash
[TestMethod]
public void Add_Authorization_NoSpaceMultipleChars_ShouldNotThrow()
{
Comment thread
benyuz marked this conversation as resolved.
var headers = new WebHeaderCollection();
headers.Add("Authorization: a111111");
string value = headers["Authorization"];
Assert.AreEqual("a111111", value);
}

// Verifies that a properly formatted Authorization header (with space) is parsed and stored correctly
[TestMethod]
public void Add_Authorization_ValidBasicToken_ShouldSucceed()
{
var headers = new WebHeaderCollection();
headers.Add("Authorization: Basic dXNlcjpwYXNz");
string value = headers["Authorization"];
Assert.AreEqual("Basic dXNlcjpwYXNz", value);
}
Comment thread
benyuz marked this conversation as resolved.
Comment thread
benyuz marked this conversation as resolved.
}
}
1 change: 1 addition & 0 deletions Tests/HttpUnitTests/HttpUnitTests.nfproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
</PropertyGroup>
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.props')" />
<ItemGroup>
<Compile Include="HttpListenerRequestTests.cs" />
<Compile Include="HttpUtilityTest.cs" />
<Compile Include="StreamContentTest.cs" />
<Compile Include="ByteArrayContentTest.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//
//
// Copyright (c) .NET Foundation and Contributors
// Portions Copyright (c) Microsoft Corporation. All rights reserved.
// See LICENSE file in the project root for full license information.
Expand Down Expand Up @@ -206,21 +206,26 @@ internal void ParseHTTPRequest()
if (headerName == "authorization")
{
int sepSpace = headerValue.IndexOf(' ');
string authType = headerValue.Substring(0, sepSpace);
if (authType.ToLower() == "basic")
// Authorization header value must contain an auth scheme followed by a space and its parameter(s), e.g. "Basic xxx" or "Bearer xxx". If not, ignore.
if (sepSpace > 0)
{
string authInfo = headerValue.Substring(sepSpace + 1);
// authInfo is base64 encoded username and password.
byte[] authInfoDecoded = Convert.FromBase64String(authInfo);
char[] authInfoDecChar = System.Text.Encoding.UTF8.GetChars(authInfoDecoded);
string strAuthInfo = new string(authInfoDecChar);
// The strAuthInfo comes in format username:password. Parse it.
int sepColon = strAuthInfo.IndexOf(':');
if (sepColon != -1)
string authType = headerValue.Substring(0, sepSpace);
if (authType.ToLower() == "basic")
{
m_NetworkCredentials = new NetworkCredential(strAuthInfo.Substring(0, sepColon), strAuthInfo.Substring(sepColon + 1));
string authInfo = headerValue.Substring(sepSpace + 1);
// authInfo is base64 encoded username and password.
byte[] authInfoDecoded = Convert.FromBase64String(authInfo);
char[] authInfoDecChar = System.Text.Encoding.UTF8.GetChars(authInfoDecoded);
string strAuthInfo = new string(authInfoDecChar);
Comment thread
benyuz marked this conversation as resolved.
// The strAuthInfo comes in format username:password. Parse it.
int sepColon = strAuthInfo.IndexOf(':');
if (sepColon != -1)
{
m_NetworkCredentials = new NetworkCredential(strAuthInfo.Substring(0, sepColon), strAuthInfo.Substring(sepColon + 1));
}
Comment thread
benyuz marked this conversation as resolved.
}
}

}
}

Expand Down