Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Four updates:

1) Enabled support for wildcard etag. This allows the caller on PUT to use If-Match: * to match any exiting file.

2) Updated test to handle slash and turned on terminating slash for RemoteVfsManager

3) Added support for deleting empty directories.

4) Fixed bug in MediaTypeMap.GetMediaType not catching if we get a null registry key.
  • Loading branch information...
commit 964ac4210e4d078ceb83efe081fa5c59a94069be 1 parent 6282410
@HenrikFrystykNielsen HenrikFrystykNielsen authored
View
2  Kudu.Client/Editor/RemoteVfsManager.cs
@@ -9,7 +9,7 @@ namespace Kudu.Client.Editor
public class RemoteVfsManager : KuduRemoteClientBase
{
public RemoteVfsManager(string serviceUrl, ICredentials credentials = null, HttpMessageHandler handler = null)
- : base(serviceUrl, credentials, handler)
+ : base(UrlUtility.EnsureTrailingSlash(serviceUrl), credentials, handler)
{
}
View
34 Kudu.FunctionalTests/Vfs/VfsControllerBaseTest.cs
@@ -12,6 +12,7 @@ namespace Kudu.FunctionalTests
public class VfsControllerBaseTest
{
private static readonly char _segmentDelimiter = '/';
+ private static readonly char[] _segmentDelimiters = new char[] { _segmentDelimiter };
private static readonly byte[] _fileContent0 = Encoding.UTF8.GetBytes("aaa\r\nbbb\r\nccc\r\n");
private static readonly byte[] _fileContent1 = Encoding.UTF8.GetBytes("AAA\r\nbbb\r\nccc\r\n");
@@ -33,7 +34,7 @@ public VfsControllerBaseTest(RemoteVfsManager client, bool testConflictingUpdate
{
KuduClient = client;
Client = client.Client;
- BaseAddress = Client.BaseAddress.AbsoluteUri;
+ BaseAddress = RemoveTerminatingSlash(Client.BaseAddress);
_testConflictingUpdates = testConflictingUpdates;
}
@@ -184,7 +185,7 @@ public void RunIntegrationTest()
response = Client.PutAsync(dirAddress, CreateUploadContent(_fileContent0)).Result;
Assert.Equal(HttpStatusCode.Conflict, response.StatusCode);
- // Check that we can't delete a directory
+ // Check that we can't delete a non-empty directory
response = Client.DeleteAsync(dirAddress).Result;
Assert.Equal(HttpStatusCode.Conflict, response.StatusCode);
@@ -302,6 +303,10 @@ public void RunIntegrationTest()
response = Client.SendAsync(deleteRequest).Result;
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
}
+
+ // Check that we can delete an empty directory
+ response = Client.DeleteAsync(dirAddress).Result;
+ Assert.Equal(HttpStatusCode.OK, response.StatusCode);
}
else
{
@@ -343,6 +348,20 @@ public void RunIntegrationTest()
Assert.Equal(updatedEtag, response.Headers.ETag);
}
+ // Check that update with wildcard etag succeeds
+ using (HttpRequestMessage updateRequest = new HttpRequestMessage())
+ {
+ updateRequest.Method = HttpMethod.Put;
+ updateRequest.RequestUri = new Uri(fileAddress);
+ updateRequest.Headers.IfMatch.Add(EntityTagHeaderValue.Any);
+ updateRequest.Content = CreateUploadContent(_fileContent1);
+ response = Client.SendAsync(updateRequest).Result;
+ Assert.Equal(HttpStatusCode.NoContent, response.StatusCode);
+ Assert.NotNull(response.Headers.ETag);
+ Assert.NotEqual(originalEtag, response.Headers.ETag);
+ updatedEtag = response.Headers.ETag;
+ }
+
// Check that delete with invalid etag fails
using (HttpRequestMessage deleteRequest = new HttpRequestMessage())
{
@@ -373,6 +392,10 @@ public void RunIntegrationTest()
response = Client.SendAsync(deleteRequest).Result;
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
}
+
+ // Check that we can delete an empty directory
+ response = Client.DeleteAsync(dirAddress).Result;
+ Assert.Equal(HttpStatusCode.OK, response.StatusCode);
}
}
@@ -382,5 +405,12 @@ private static HttpContent CreateUploadContent(byte[] content)
uploadContent.Headers.ContentType = _fileMediaType;
return uploadContent;
}
+
+ private static string RemoveTerminatingSlash(Uri url)
+ {
+ UriBuilder address = new UriBuilder(url);
+ address.Path = address.Path.TrimEnd(_segmentDelimiters);
+ return address.Uri.AbsoluteUri;
+ }
}
}
View
41 Kudu.Services.Test/Infrastructure/MediaTypeMapTest.cs
@@ -0,0 +1,41 @@
+using System;
+using System.Net.Http.Headers;
+using Xunit;
+using Xunit.Extensions;
+
+namespace Kudu.Services.Infrastructure.Test
+{
+ public class MediaTypeMapTest
+ {
+ [Fact]
+ public void GetMediaType_Guards()
+ {
+ MediaTypeMap map = new MediaTypeMap();
+ ArgumentNullException ex = Assert.Throws<ArgumentNullException>(() => map.GetMediaType(null));
+ Assert.Equal("fileExtension", ex.ParamName);
+ }
+
+ [Theory]
+ [InlineData(".1234")]
+ [InlineData(".//")]
+ [InlineData(".1 4")]
+ [InlineData(".foo")]
+ public void GetMediaType_HandlesUnknownMediaTypes(string extension)
+ {
+ MediaTypeMap map = new MediaTypeMap();
+ MediaTypeHeaderValue expectedMediaType = new MediaTypeHeaderValue("application/octet-stream");
+
+ Assert.Equal(expectedMediaType, map.GetMediaType(extension));
+ }
+
+ [Theory]
+ [InlineData(".txt", "text/plain")]
+ [InlineData(".html", "text/html")]
+ public void GetMediaType_HandlesKnownMediaTypes(string extension, string expectedMediaType)
+ {
+ MediaTypeMap map = new MediaTypeMap();
+ MediaTypeHeaderValue expected = new MediaTypeHeaderValue(expectedMediaType);
+ Assert.Equal(expected, map.GetMediaType(extension));
+ }
+ }
+}
View
78 Kudu.Services.Test/Kudu.Services.Test.csproj
@@ -0,0 +1,78 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+ <ProjectGuid>{5DEE4A10-5CA1-484D-B7A3-64C972EA2546}</ProjectGuid>
+ <OutputType>Library</OutputType>
+ <AppDesignerFolder>Properties</AppDesignerFolder>
+ <RootNamespace>Kudu.Services.Test</RootNamespace>
+ <AssemblyName>Kudu.Services.Test</AssemblyName>
+ <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
+ <FileAlignment>512</FileAlignment>
+ <SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
+ <RestorePackages>true</RestorePackages>
+ <TargetFrameworkProfile />
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+ <DebugSymbols>true</DebugSymbols>
+ <DebugType>full</DebugType>
+ <Optimize>false</Optimize>
+ <OutputPath>bin\Debug\</OutputPath>
+ <DefineConstants>DEBUG;TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+ <DebugType>pdbonly</DebugType>
+ <Optimize>true</Optimize>
+ <OutputPath>bin\Release\</OutputPath>
+ <DefineConstants>TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <ItemGroup>
+ <Reference Include="System" />
+ <Reference Include="System.Core" />
+ <Reference Include="System.Net.Http, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
+ <HintPath>..\packages\Microsoft.Net.Http.2.0.20710.0\lib\net40\System.Net.Http.dll</HintPath>
+ </Reference>
+ <Reference Include="System.Net.Http.WebRequest, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
+ <HintPath>..\packages\Microsoft.Net.Http.2.0.20710.0\lib\net40\System.Net.Http.WebRequest.dll</HintPath>
+ </Reference>
+ <Reference Include="System.Xml.Linq" />
+ <Reference Include="System.Data.DataSetExtensions" />
+ <Reference Include="Microsoft.CSharp" />
+ <Reference Include="System.Data" />
+ <Reference Include="System.Xml" />
+ <Reference Include="xunit">
+ <HintPath>..\packages\xunit.1.9.1\lib\net20\xunit.dll</HintPath>
+ </Reference>
+ <Reference Include="xunit.extensions">
+ <HintPath>..\packages\xunit.extensions.1.9.1\lib\net20\xunit.extensions.dll</HintPath>
+ </Reference>
+ </ItemGroup>
+ <ItemGroup>
+ <Compile Include="Infrastructure\MediaTypeMapTest.cs" />
+ <Compile Include="Properties\AssemblyInfo.cs" />
+ </ItemGroup>
+ <ItemGroup>
+ <None Include="packages.config" />
+ </ItemGroup>
+ <ItemGroup>
+ <ProjectReference Include="..\Kudu.Services\Kudu.Services.csproj">
+ <Project>{d163e227-9eb6-4619-ad37-d8eef831aef0}</Project>
+ <Name>Kudu.Services</Name>
+ </ProjectReference>
+ </ItemGroup>
+ <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+ <Import Project="$(SolutionDir)\.nuget\nuget.targets" />
+ <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
+ Other similar extension points exist, see Microsoft.Common.targets.
+ <Target Name="BeforeBuild">
+ </Target>
+ <Target Name="AfterBuild">
+ </Target>
+ -->
+</Project>
View
35 Kudu.Services.Test/Properties/AssemblyInfo.cs
@@ -0,0 +1,35 @@
+using System.Reflection;
+using System.Runtime.InteropServices;
+
+// General Information about an assembly is controlled through the following
+// set of attributes. Change these attribute values to modify the information
+// associated with an assembly.
+[assembly: AssemblyTitle("Kudu.Services.Test")]
+[assembly: AssemblyDescription("")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyCompany("")]
+[assembly: AssemblyProduct("Kudu.Services.Test")]
+[assembly: AssemblyCopyright("")]
+[assembly: AssemblyTrademark("")]
+[assembly: AssemblyCulture("")]
+
+// Setting ComVisible to false makes the types in this assembly not visible
+// to COM components. If you need to access a type in this assembly from
+// COM, set the ComVisible attribute to true on that type.
+[assembly: ComVisible(false)]
+
+// The following GUID is for the ID of the typelib if this project is exposed to COM
+[assembly: Guid("50093a0c-4d7a-40ed-a839-f2d454484734")]
+
+// Version information for an assembly consists of the following four values:
+//
+// Major Version
+// Minor Version
+// Build Number
+// Revision
+//
+// You can specify all the values or you can default the Build and Revision Numbers
+// by using the '*' as shown below:
+// [assembly: AssemblyVersion("1.0.*")]
+[assembly: AssemblyVersion("1.0.0.0")]
+[assembly: AssemblyFileVersion("1.0.0.0")]
View
6 Kudu.Services.Test/packages.config
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="utf-8"?>
+<packages>
+ <package id="Microsoft.Net.Http" version="2.0.20710.0" targetFramework="net40" />
+ <package id="xunit" version="1.9.1" targetFramework="net40" />
+ <package id="xunit.extensions" version="1.9.1" targetFramework="net40" />
+</packages>
View
8 Kudu.Services/Editor/VfsController.cs
@@ -98,15 +98,15 @@ protected override Task<HttpResponseMessage> CreateItemPutResponse(FileSystemInf
// Existing resources require an etag to be updated.
if (Request.Headers.IfMatch == null)
{
- HttpResponseMessage conflictDirectoryResponse = Request.CreateErrorResponse(
+ HttpResponseMessage missingIfMatchResponse = Request.CreateErrorResponse(
HttpStatusCode.PreconditionFailed, Resources.VfsController_MissingIfMatch);
- return TaskHelpers.FromResult(conflictDirectoryResponse);
+ return TaskHelpers.FromResult(missingIfMatchResponse);
}
bool isMatch = false;
foreach (EntityTagHeaderValue etag in Request.Headers.IfMatch)
{
- if (currentEtag.Equals(etag))
+ if (currentEtag.Equals(etag) || etag == EntityTagHeaderValue.Any)
{
isMatch = true;
break;
@@ -189,7 +189,7 @@ protected override HttpResponseMessage CreateItemDeleteResponse(FileSystemInfo i
bool isMatch = false;
foreach (EntityTagHeaderValue etag in Request.Headers.IfMatch)
{
- if (currentEtag.Equals(etag))
+ if (currentEtag.Equals(etag) || etag == EntityTagHeaderValue.Any)
{
isMatch = true;
break;
View
11 Kudu.Services/Infrastructure/MediaTypeMap.cs
@@ -25,11 +25,14 @@ public MediaTypeHeaderValue GetMediaType(string fileExtension)
{
using (RegistryKey key = Registry.ClassesRoot.OpenSubKey(fileExtension))
{
- string keyValue = key.GetValue("Content Type") as string;
- MediaTypeHeaderValue mediaType;
- if (keyValue != null && MediaTypeHeaderValue.TryParse(keyValue, out mediaType))
+ if (key != null)
{
- return mediaType;
+ string keyValue = key.GetValue("Content Type") as string;
+ MediaTypeHeaderValue mediaType;
+ if (keyValue != null && MediaTypeHeaderValue.TryParse(keyValue, out mediaType))
+ {
+ return mediaType;
+ }
}
return _defaultMediaType;
}
View
19 Kudu.Services/Infrastructure/VfsControllerBase.cs
@@ -96,7 +96,7 @@ public virtual Task<HttpResponseMessage> PutItem()
if (itemExists && (info.Attributes & FileAttributes.Directory) != 0)
{
HttpResponseMessage conflictDirectoryResponse = Request.CreateErrorResponse(
- HttpStatusCode.Conflict, Resources.VfsController_CannotUpdateOrDeleteDirectory);
+ HttpStatusCode.Conflict, Resources.VfsController_CannotUpdateDirectory);
return TaskHelpers.FromResult(conflictDirectoryResponse);
}
else
@@ -128,9 +128,20 @@ public virtual HttpResponseMessage DeleteItem()
}
else if ((info.Attributes & FileAttributes.Directory) != 0)
{
- HttpResponseMessage conflictDirectoryResponse = Request.CreateErrorResponse(
- HttpStatusCode.Conflict, Resources.VfsController_CannotUpdateOrDeleteDirectory);
- return conflictDirectoryResponse;
+ try
+ {
+ info.Delete();
+ }
+ catch (Exception ex)
+ {
+ Tracer.TraceError(ex);
+ HttpResponseMessage conflictDirectoryResponse = Request.CreateErrorResponse(
+ HttpStatusCode.Conflict, Resources.VfsControllerBase_CannotDeleteDirectory);
+ return conflictDirectoryResponse;
+ }
+
+ // Delete directory succeeded.
+ return Request.CreateResponse(HttpStatusCode.OK);
}
else
{
View
17 Kudu.Services/Resources.Designer.cs
@@ -1,7 +1,7 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
-// Runtime Version:4.0.30319.586
+// Runtime Version:4.0.30319.18010
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
@@ -250,11 +250,11 @@ internal class Resources {
}
/// <summary>
- /// Looks up a localized string similar to The resource represents a directory which can not be updated or deleted..
+ /// Looks up a localized string similar to The resource represents a directory which can not be updated..
/// </summary>
- internal static string VfsController_CannotUpdateOrDeleteDirectory {
+ internal static string VfsController_CannotUpdateDirectory {
get {
- return ResourceManager.GetString("VfsController_CannotUpdateOrDeleteDirectory", resourceCulture);
+ return ResourceManager.GetString("VfsController_CannotUpdateDirectory", resourceCulture);
}
}
@@ -295,6 +295,15 @@ internal class Resources {
}
/// <summary>
+ /// Looks up a localized string similar to Cannot delete directory. It is either not empty or access is not allowed..
+ /// </summary>
+ internal static string VfsControllerBase_CannotDeleteDirectory {
+ get {
+ return ResourceManager.GetString("VfsControllerBase_CannotDeleteDirectory", resourceCulture);
+ }
+ }
+
+ /// <summary>
/// Looks up a localized string similar to ETag does not represent an existing commit ID: &apos;{0}&apos;. Updates must be based on an existing commit ID..
/// </summary>
internal static string VfsScmController_EtagMismatch {
View
7 Kudu.Services/Resources.resx
@@ -180,8 +180,8 @@
<data name="VfsController_Busy" xml:space="preserve">
<value>The repository is currently busy handling another request. Please try again later.</value>
</data>
- <data name="VfsController_CannotUpdateOrDeleteDirectory" xml:space="preserve">
- <value>The resource represents a directory which can not be updated or deleted.</value>
+ <data name="VfsController_CannotUpdateDirectory" xml:space="preserve">
+ <value>The resource represents a directory which can not be updated.</value>
</data>
<data name="VfsController_EtagMismatch" xml:space="preserve">
<value>ETag does not represent the latest state of the resource.</value>
@@ -204,4 +204,7 @@
<data name="VfsController_fileChanged" xml:space="preserve">
<value>The contents of the file has changed while the operation was in progress. Please retry the request to avoid conflict.</value>
</data>
+ <data name="VfsControllerBase_CannotDeleteDirectory" xml:space="preserve">
+ <value>Cannot delete directory. It is either not empty or access is not allowed.</value>
+ </data>
</root>
View
13 Kudu.sln
@@ -30,6 +30,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Kudu.TestHarness", "Kudu.Te
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Kudu.Console", "Kudu.Console\Kudu.Console.csproj", "{E2B0EE28-8C96-497A-AB08-605B9FD841E9}"
EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Kudu.Services.Test", "Kudu.Services.Test\Kudu.Services.Test.csproj", "{5DEE4A10-5CA1-484D-B7A3-64C972EA2546}"
+EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -160,6 +162,16 @@ Global
{E2B0EE28-8C96-497A-AB08-605B9FD841E9}.Release|Mixed Platforms.Build.0 = Release|x86
{E2B0EE28-8C96-497A-AB08-605B9FD841E9}.Release|x86.ActiveCfg = Release|x86
{E2B0EE28-8C96-497A-AB08-605B9FD841E9}.Release|x86.Build.0 = Release|x86
+ {5DEE4A10-5CA1-484D-B7A3-64C972EA2546}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {5DEE4A10-5CA1-484D-B7A3-64C972EA2546}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {5DEE4A10-5CA1-484D-B7A3-64C972EA2546}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
+ {5DEE4A10-5CA1-484D-B7A3-64C972EA2546}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
+ {5DEE4A10-5CA1-484D-B7A3-64C972EA2546}.Debug|x86.ActiveCfg = Debug|Any CPU
+ {5DEE4A10-5CA1-484D-B7A3-64C972EA2546}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {5DEE4A10-5CA1-484D-B7A3-64C972EA2546}.Release|Any CPU.Build.0 = Release|Any CPU
+ {5DEE4A10-5CA1-484D-B7A3-64C972EA2546}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
+ {5DEE4A10-5CA1-484D-B7A3-64C972EA2546}.Release|Mixed Platforms.Build.0 = Release|Any CPU
+ {5DEE4A10-5CA1-484D-B7A3-64C972EA2546}.Release|x86.ActiveCfg = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@@ -169,5 +181,6 @@ Global
{AB93801E-467D-4450-8951-60AE5A51CF4F} = {FA1EA7CE-E6A4-4E55-AD76-6B43572A9092}
{E08D0497-2553-4DF0-9FE4-4D6F75CB80C4} = {FA1EA7CE-E6A4-4E55-AD76-6B43572A9092}
{ACF3450A-8062-48D5-9C9D-8486261F290F} = {FA1EA7CE-E6A4-4E55-AD76-6B43572A9092}
+ {5DEE4A10-5CA1-484D-B7A3-64C972EA2546} = {FA1EA7CE-E6A4-4E55-AD76-6B43572A9092}
EndGlobalSection
EndGlobal
Please sign in to comment.
Something went wrong with that request. Please try again.