public byte[] Store(string commandName, string key, ulong flags, DateTime exptime, byte[] data) { if ((ulong)data.Length > _cache.Capacity) { return(Encoding.ASCII.GetBytes("ERROR Over capacity\r\n")); } bool stored = false; switch (commandName) { case "set": stored = _cache.Store(key, flags, data, exptime); break; case "replace": stored = _cache.Replace(key, flags, exptime, data); break; case "add": stored = _cache.Add(key, flags, exptime, data); break; case "append": case "prepend": stored = _cache.Append(key, flags, exptime, data, commandName == "append"); break; } return(stored ? Encoding.ASCII.GetBytes("STORED\r\n") : Encoding.ASCII.GetBytes("NOT_STORED\r\n")); }
public override async Task Put(HttpListenerContext context, params string[] matches) { // TODO: content type... var key = matches[0]; var streamReader = new StreamReader(context.Request.InputStream); var body = await streamReader.ReadToEndAsync(); _cache.Store(key, 0, Encoding.UTF8.GetBytes(body), DateTime.MaxValue); byte[] response = Encoding.UTF8.GetBytes("STORED\r\n"); context.Response.StatusCode = 200; context.Response.KeepAlive = false; context.Response.ContentLength64 = response.Length; var output = context.Response.OutputStream; await output.WriteAsync(response, 0, response.Length); context.Response.Close(); }