Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
31 changes: 31 additions & 0 deletions Tests/HttpUnitTests/HttpListenerRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
Comment thread
benyuz marked this conversation as resolved.
Outdated

using System.Net;
using nanoFramework.TestFramework;

namespace HttpUnitTests
{
internal class HttpListenerRequestTests
{
// Verifies that malformed Authorization header (no space) does not cause a crash
[TestMethod]
public void Add_Authorization_NoSpaceMultipleChars_ShouldNotThrow()
{
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);
}
}
}
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="HttpListenerRequest.cs" />
Comment thread
benyuz marked this conversation as resolved.
Outdated
<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 be in format "type credentials". If not, ignore.
Comment thread
benyuz marked this conversation as resolved.
Outdated
Comment thread
benyuz marked this conversation as resolved.
Outdated
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