#183 Set empty paths for OPTIONS * requests

This commit is contained in:
Chris R 2016-08-26 16:01:20 -07:00
parent c2f52db3a5
commit 3d1dbbaae5
2 changed files with 28 additions and 5 deletions

View File

@ -64,6 +64,10 @@ namespace Microsoft.Net.Http.Server
SslStatus = memoryBlob.RequestBlob->pSslInfo == null ? SslStatus.Insecure : SslStatus = memoryBlob.RequestBlob->pSslInfo == null ? SslStatus.Insecure :
memoryBlob.RequestBlob->pSslInfo->SslClientCertNegotiated == 0 ? SslStatus.NoClientCert : memoryBlob.RequestBlob->pSslInfo->SslClientCertNegotiated == 0 ? SslStatus.NoClientCert :
SslStatus.ClientCert; SslStatus.ClientCert;
KnownMethod = memoryBlob.RequestBlob->Verb;
Method = HttpApi.GetVerb(memoryBlob.RequestBlob);
if (memoryBlob.RequestBlob->pRawUrl != null && memoryBlob.RequestBlob->RawUrlLength > 0) if (memoryBlob.RequestBlob->pRawUrl != null && memoryBlob.RequestBlob->RawUrlLength > 0)
{ {
RawUrl = Marshal.PtrToStringAnsi((IntPtr)memoryBlob.RequestBlob->pRawUrl, memoryBlob.RequestBlob->RawUrlLength); RawUrl = Marshal.PtrToStringAnsi((IntPtr)memoryBlob.RequestBlob->pRawUrl, memoryBlob.RequestBlob->RawUrlLength);
@ -89,8 +93,14 @@ namespace Microsoft.Net.Http.Server
var prefix = requestContext.Server.Settings.UrlPrefixes.GetPrefix((int)memoryBlob.RequestBlob->UrlContext); var prefix = requestContext.Server.Settings.UrlPrefixes.GetPrefix((int)memoryBlob.RequestBlob->UrlContext);
var originalPath = RequestUriBuilder.GetRequestPath(RawUrl, cookedUrlPath, RequestContext.Logger); var originalPath = RequestUriBuilder.GetRequestPath(RawUrl, cookedUrlPath, RequestContext.Logger);
// 'OPTIONS * HTTP/1.1'
if (KnownMethod == HttpApi.HTTP_VERB.HttpVerbOPTIONS && string.Equals(RawUrl, "*", StringComparison.Ordinal))
{
PathBase = string.Empty;
Path = string.Empty;
}
// These paths are both unescaped already. // These paths are both unescaped already.
if (originalPath.Length == prefix.Path.Length - 1) else if (originalPath.Length == prefix.Path.Length - 1)
{ {
// They matched exactly except for the trailing slash. // They matched exactly except for the trailing slash.
PathBase = originalPath; PathBase = originalPath;
@ -119,8 +129,6 @@ namespace Microsoft.Net.Http.Server
ProtocolVersion = new Version(major, minor); ProtocolVersion = new Version(major, minor);
} }
KnownMethod = memoryBlob.RequestBlob->Verb;
Method = HttpApi.GetVerb(memoryBlob.RequestBlob);
Headers = new HeaderCollection(new RequestHeaders(_nativeRequestContext)); Headers = new HeaderCollection(new RequestHeaders(_nativeRequestContext));
var requestV2 = (HttpApi.HTTP_REQUEST_V2*)memoryBlob.RequestBlob; var requestV2 = (HttpApi.HTTP_REQUEST_V2*)memoryBlob.RequestBlob;

View File

@ -111,6 +111,21 @@ namespace Microsoft.Net.Http.Server
} }
} }
[Fact]
public async Task Request_OptionsStar_EmptyPath()
{
string root;
using (var server = Utilities.CreateHttpServerReturnRoot("/", out root))
{
var responseTask = SendSocketRequestAsync(root, "*", "OPTIONS");
var context = await server.AcceptAsync();
Assert.Equal("", context.Request.PathBase);
Assert.Equal("", context.Request.Path);
Assert.Equal("*", context.Request.RawUrl);
context.Dispose();
}
}
[Theory] [Theory]
// The test server defines these prefixes: "/", "/11", "/2/3", "/2", "/11/2" // The test server defines these prefixes: "/", "/11", "/2/3", "/2", "/11/2"
[InlineData("/", "", "/")] [InlineData("/", "", "/")]
@ -163,11 +178,11 @@ namespace Microsoft.Net.Http.Server
} }
} }
private async Task<string> SendSocketRequestAsync(string address, string path) private async Task<string> SendSocketRequestAsync(string address, string path, string method = "GET")
{ {
var uri = new Uri(address); var uri = new Uri(address);
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
builder.AppendLine("GET " + path + " HTTP/1.1"); builder.AppendLine($"{method} {path} HTTP/1.1");
builder.AppendLine("Connection: close"); builder.AppendLine("Connection: close");
builder.Append("HOST: "); builder.Append("HOST: ");
builder.AppendLine(uri.Authority); builder.AppendLine(uri.Authority);