Skip to content
This repository has been archived by the owner on Jul 7, 2019. It is now read-only.

Validate that keys are no more than 250 characters in the text protocol #140

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
</PropertyGroup>
<Import Project="..\build\CommonProperties.targets" />
<ItemGroup>
<Reference Include="log4net">
<HintPath>..\binaries\log4net\log4net.dll</HintPath>
<Reference Include="log4net, Version=1.2.13.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\log4net.2.0.3\lib\net35-full\log4net.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
Expand All @@ -42,6 +43,7 @@
<SubType>Designer</SubType>
</None>
<None Include="Enyim.Caching.Log4NetAdapter.nuspec" />
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
Expand Down
4 changes: 4 additions & 0 deletions Enyim.Caching.Log4NetAdapter/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="log4net" version="2.0.3" targetFramework="net35" />
</packages>
4 changes: 2 additions & 2 deletions Enyim.Caching/Memcached/Locators/VBucketNodeLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ public VBucketNodeLocator(string hashAlgorithm, VBucket[] buckets)
throw new ArgumentException("Unknown hash algorithm: " + hashAlgorithm, "hashAlgorithm");
}

[ThreadStatic]
private static HashAlgorithm currentAlgo;
//[ThreadStatic]
//private static HashAlgorithm currentAlgo;

//private HashAlgorithm GetAlgo()
//{
Expand Down
16 changes: 16 additions & 0 deletions Enyim.Caching/Memcached/Protocol/Text/TextOperationFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,31 @@ namespace Enyim.Caching.Memcached.Protocol.Text
{
public class TextOperationFactory : IOperationFactory
{
private static void ValidateKey(string key, string parameterName)
{
// spec - https://github.com/memcached/memcached/blob/master/doc/protocol.txt#L47
if (key.Length > 250)
throw new ArgumentException("Keys must be no more than 250 characters in length.", parameterName);
}

IGetOperation IOperationFactory.Get(string key)
{
ValidateKey(key, "key");
return new GetOperation(key);
}

IMultiGetOperation IOperationFactory.MultiGet(IList<string> keys)
{
foreach (var key in keys)
{
ValidateKey(key, "keys");
}
return new MultiGetOperation(keys);
}

IStoreOperation IOperationFactory.Store(StoreMode mode, string key, CacheItem value, uint expires, ulong cas)
{
ValidateKey(key, "key");
if (cas == 0)
return new StoreOperation(mode, key, value, expires);

Expand All @@ -25,20 +38,23 @@ IStoreOperation IOperationFactory.Store(StoreMode mode, string key, CacheItem va

IDeleteOperation IOperationFactory.Delete(string key, ulong cas)
{
ValidateKey(key, "key");
if (cas > 0) throw new NotSupportedException("Text protocol does not support delete with cas.");

return new DeleteOperation(key);
}

IMutatorOperation IOperationFactory.Mutate(MutationMode mode, string key, ulong defaultValue, ulong delta, uint expires, ulong cas)
{
ValidateKey(key, "key");
if (cas > 0) throw new NotSupportedException("Text protocol does not support " + mode + " with cas.");

return new MutatorOperation(mode, key, delta);
}

IConcatOperation IOperationFactory.Concat(ConcatenationMode mode, string key, ulong cas, ArraySegment<byte> data)
{
ValidateKey(key, "key");
if (cas > 0) throw new NotSupportedException("Text protocol does not support " + mode + " with cas.");

return new ConcateOperation(mode, key, data);
Expand Down
6 changes: 1 addition & 5 deletions Enyim.Caching/Memcached/Results/Helpers/ResultHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,14 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Enyim.Caching.Memcached.Results.Factories;

namespace Enyim.Caching.Memcached.Results.Helpers
{

public static class ResultHelper
{

public static string ProcessResponseData(ArraySegment<byte> data, string message = "")
{

if (data != null && data.Count > 0)
if (data.Count > 0)
{
try
{
Expand Down
24 changes: 12 additions & 12 deletions Enyim.Caching/MemcachedClient.Results.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public IStoreOperationResult ExecuteStore(StoreMode mode, string key, object val
ulong tmp = 0;
int status;

return this.PerformStore(mode, key, value, MemcachedClient.GetExpiration(validFor, null), ref tmp, out status);
return this.PerformStore(mode, key, value, MemcachedClient.GetExpiration(validFor), ref tmp, out status);
}

/// <summary>
Expand All @@ -58,7 +58,7 @@ public IStoreOperationResult ExecuteStore(StoreMode mode, string key, object val
ulong tmp = 0;
int status;

return this.PerformStore(mode, key, value, MemcachedClient.GetExpiration(null, expiresAt), ref tmp, out status);
return this.PerformStore(mode, key, value, MemcachedClient.GetExpiration(expiresAt), ref tmp, out status);
}

#endregion
Expand Down Expand Up @@ -101,7 +101,7 @@ public IStoreOperationResult ExecuteCas(StoreMode mode, string key, object value
/// <returns>A CasResult object containing the version of the item and the result of the operation (true if the item was successfully stored in the cache; false otherwise).</returns>
public IStoreOperationResult ExecuteCas(StoreMode mode, string key, object value, TimeSpan validFor, ulong cas)
{
return this.PerformStore(mode, key, value, MemcachedClient.GetExpiration(validFor, null), cas);
return this.PerformStore(mode, key, value, MemcachedClient.GetExpiration(validFor), cas);
}

/// <summary>
Expand All @@ -115,7 +115,7 @@ public IStoreOperationResult ExecuteCas(StoreMode mode, string key, object value
/// <returns>A CasResult object containing the version of the item and the result of the operation (true if the item was successfully stored in the cache; false otherwise).</returns>
public IStoreOperationResult ExecuteCas(StoreMode mode, string key, object value, DateTime expiresAt, ulong cas)
{
return this.PerformStore(mode, key, value, MemcachedClient.GetExpiration(null, expiresAt), cas);
return this.PerformStore(mode, key, value, MemcachedClient.GetExpiration(expiresAt), cas);
}
#endregion

Expand Down Expand Up @@ -222,7 +222,7 @@ public IMutateOperationResult ExecuteIncrement(string key, ulong defaultValue, u
/// <remarks>If the client uses the Text protocol, the item must be inserted into the cache before it can be changed. It must be inserted as a <see cref="T:System.String"/>. Moreover the Text protocol only works with <see cref="System.UInt32"/> values, so return value -1 always indicates that the item was not found.</remarks>
public IMutateOperationResult ExecuteIncrement(string key, ulong defaultValue, ulong delta, TimeSpan validFor)
{
return this.PerformMutate(MutationMode.Increment, key, defaultValue, delta, MemcachedClient.GetExpiration(validFor, null));
return this.PerformMutate(MutationMode.Increment, key, defaultValue, delta, MemcachedClient.GetExpiration(validFor));
}

/// <summary>
Expand All @@ -236,7 +236,7 @@ public IMutateOperationResult ExecuteIncrement(string key, ulong defaultValue, u
/// <remarks>If the client uses the Text protocol, the item must be inserted into the cache before it can be changed. It must be inserted as a <see cref="T:System.String"/>. Moreover the Text protocol only works with <see cref="System.UInt32"/> values, so return value -1 always indicates that the item was not found.</remarks>
public IMutateOperationResult ExecuteIncrement(string key, ulong defaultValue, ulong delta, DateTime expiresAt)
{
return this.PerformMutate(MutationMode.Increment, key, defaultValue, delta, MemcachedClient.GetExpiration(null, expiresAt));
return this.PerformMutate(MutationMode.Increment, key, defaultValue, delta, MemcachedClient.GetExpiration(expiresAt));
}

/// <summary>
Expand Down Expand Up @@ -265,7 +265,7 @@ public IMutateOperationResult ExecuteIncrement(string key, ulong defaultValue, u
/// <remarks>If the client uses the Text protocol, the item must be inserted into the cache before it can be changed. It must be inserted as a <see cref="T:System.String"/>. Moreover the Text protocol only works with <see cref="System.UInt32"/> values, so return value -1 always indicates that the item was not found.</remarks>
public IMutateOperationResult ExecuteIncrement(string key, ulong defaultValue, ulong delta, TimeSpan validFor, ulong cas)
{
return this.CasMutate(MutationMode.Increment, key, defaultValue, delta, MemcachedClient.GetExpiration(validFor, null), cas);
return this.CasMutate(MutationMode.Increment, key, defaultValue, delta, MemcachedClient.GetExpiration(validFor), cas);
}

/// <summary>
Expand All @@ -280,7 +280,7 @@ public IMutateOperationResult ExecuteIncrement(string key, ulong defaultValue, u
/// <remarks>If the client uses the Text protocol, the item must be inserted into the cache before it can be changed. It must be inserted as a <see cref="T:System.String"/>. Moreover the Text protocol only works with <see cref="System.UInt32"/> values, so return value -1 always indicates that the item was not found.</remarks>
public IMutateOperationResult ExecuteIncrement(string key, ulong defaultValue, ulong delta, DateTime expiresAt, ulong cas)
{
return this.CasMutate(MutationMode.Increment, key, defaultValue, delta, MemcachedClient.GetExpiration(null, expiresAt), cas);
return this.CasMutate(MutationMode.Increment, key, defaultValue, delta, MemcachedClient.GetExpiration(expiresAt), cas);
}

/// <summary>
Expand All @@ -307,7 +307,7 @@ public IMutateOperationResult ExecuteDecrement(string key, ulong defaultValue, u
/// <remarks>If the client uses the Text protocol, the item must be inserted into the cache before it can be changed. It must be inserted as a <see cref="T:System.String"/>. Moreover the Text protocol only works with <see cref="System.UInt32"/> values, so return value -1 always indicates that the item was not found.</remarks>
public IMutateOperationResult ExecuteDecrement(string key, ulong defaultValue, ulong delta, TimeSpan validFor)
{
return this.PerformMutate(MutationMode.Decrement, key, defaultValue, delta, MemcachedClient.GetExpiration(validFor, null));
return this.PerformMutate(MutationMode.Decrement, key, defaultValue, delta, MemcachedClient.GetExpiration(validFor));
}

/// <summary>
Expand All @@ -321,7 +321,7 @@ public IMutateOperationResult ExecuteDecrement(string key, ulong defaultValue, u
/// <remarks>If the client uses the Text protocol, the item must be inserted into the cache before it can be changed. It must be inserted as a <see cref="T:System.String"/>. Moreover the Text protocol only works with <see cref="System.UInt32"/> values, so return value -1 always indicates that the item was not found.</remarks>
public IMutateOperationResult ExecuteDecrement(string key, ulong defaultValue, ulong delta, DateTime expiresAt)
{
return this.PerformMutate(MutationMode.Decrement, key, defaultValue, delta, MemcachedClient.GetExpiration(null, expiresAt));
return this.PerformMutate(MutationMode.Decrement, key, defaultValue, delta, MemcachedClient.GetExpiration(expiresAt));
}

/// <summary>
Expand Down Expand Up @@ -350,7 +350,7 @@ public IMutateOperationResult ExecuteDecrement(string key, ulong defaultValue, u
/// <remarks>If the client uses the Text protocol, the item must be inserted into the cache before it can be changed. It must be inserted as a <see cref="T:System.String"/>. Moreover the Text protocol only works with <see cref="System.UInt32"/> values, so return value -1 always indicates that the item was not found.</remarks>
public IMutateOperationResult ExecuteDecrement(string key, ulong defaultValue, ulong delta, TimeSpan validFor, ulong cas)
{
return this.CasMutate(MutationMode.Decrement, key, defaultValue, delta, MemcachedClient.GetExpiration(validFor, null), cas);
return this.CasMutate(MutationMode.Decrement, key, defaultValue, delta, MemcachedClient.GetExpiration(validFor), cas);
}

/// <summary>
Expand All @@ -365,7 +365,7 @@ public IMutateOperationResult ExecuteDecrement(string key, ulong defaultValue, u
/// <remarks>If the client uses the Text protocol, the item must be inserted into the cache before it can be changed. It must be inserted as a <see cref="T:System.String"/>. Moreover the Text protocol only works with <see cref="System.UInt32"/> values, so return value -1 always indicates that the item was not found.</remarks>
public IMutateOperationResult ExecuteDecrement(string key, ulong defaultValue, ulong delta, DateTime expiresAt, ulong cas)
{
return this.CasMutate(MutationMode.Decrement, key, defaultValue, delta, MemcachedClient.GetExpiration(null, expiresAt), cas);
return this.CasMutate(MutationMode.Decrement, key, defaultValue, delta, MemcachedClient.GetExpiration(expiresAt), cas);
}
#endregion

Expand Down
Loading