Permalink
Browse files

Added ability to receive byte[] as HTTP request.

  • Loading branch information...
1 parent c7a7c7c commit 32165327709de01cb40087c4cb51c41b85df84da @anakryiko anakryiko committed Nov 13, 2012
@@ -1,4 +1,5 @@
using System;
+using System.Collections.Generic;
using System.Text;
using EventStore.Core.Data;
using EventStore.Core.Messages;
@@ -51,14 +52,14 @@ private static string XmlReadFormat
}
}
- public static string GetJsonWrite(string data, string metadata)
+ public static byte[] GetJsonWrite(string data, string metadata)
{
- return string.Format(JsonWriteFormat,
- "-1",
- String.Format("\"{0}\"", Guid.NewGuid()),
- "\"type\"",
- data,
- metadata);
+ return Encoding.UTF8.GetBytes(string.Format(JsonWriteFormat,
+ "-1",
+ String.Format("\"{0}\"", Guid.NewGuid()),
+ "\"type\"",
+ data,
+ metadata));
}
public static string GetJsonReadResult(ClientMessage.ReadEventCompleted completed, bool dataJson = true, bool metadataJson = true)
@@ -71,9 +72,14 @@ public static string GetJsonReadResult(ClientMessage.ReadEventCompleted complete
metadataJson ? JsonMetadata : WrapIntoQuotes(AsString(completed.Record.Metadata)));
}
- public static string GetXmlWrite(string data, string metadata)
+ public static byte[] GetXmlWrite(string data, string metadata, bool withBom = true)
{
- return string.Format(XmlWriteFormat, "-1", Guid.NewGuid(), "type", data, metadata);
+ IEnumerable<byte> result = Enumerable.Empty<byte>();
+ if (withBom)
+ result = result.Concat(Encoding.UTF8.GetPreamble());
+ result = result.Concat(Encoding.UTF8.GetBytes(
+ string.Format(XmlWriteFormat, "-1", Guid.NewGuid(), "type", data, metadata)));
+ return result.ToArray();
}
public static string GetXmlReadResult(ClientMessage.ReadEventCompleted completed, bool dataJson = true, bool metadataJson = true)
@@ -27,7 +27,9 @@
//
using System;
+using System.IO;
using System.Text;
+using System.Xml;
using System.Xml.Linq;
using EventStore.Common.Log;
using EventStore.Core.Data;
@@ -75,7 +77,7 @@ public static string SmartFormat(ClientMessage.ReadEventCompleted completed, ICo
}
}
- public static Tuple<int, Event[]> SmartParse(string request, ICodec sourceCodec)
+ public static Tuple<int, Event[]> SmartParse(byte[] request, ICodec sourceCodec)
{
var write = Load(request, sourceCodec);
if (write == null || write.Events == null || write.Events.Length == 0)
@@ -85,34 +87,39 @@ public static string SmartFormat(ClientMessage.ReadEventCompleted completed, ICo
return new Tuple<int, Event[]>(write.ExpectedVersion, events);
}
- private static HttpClientMessageDto.WriteEventsDynamic Load(string s, ICodec sourceCodec)
+ private static HttpClientMessageDto.WriteEventsDynamic Load(byte[] data, ICodec sourceCodec)
{
switch(sourceCodec.ContentType)
{
case ContentType.Json:
case ContentType.AtomJson:
- return LoadFromJson(s);
+ return LoadFromJson(data);
case ContentType.Xml:
case ContentType.ApplicationXml:
case ContentType.Atom:
- return LoadFromXml(s);
+ return LoadFromXml(data);
default:
return null;
}
}
- private static HttpClientMessageDto.WriteEventsDynamic LoadFromJson(string json)
+ private static HttpClientMessageDto.WriteEventsDynamic LoadFromJson(byte[] json)
{
- return Codec.Json.From<HttpClientMessageDto.WriteEventsDynamic>(json);
+ return Codec.Json.From<HttpClientMessageDto.WriteEventsDynamic>(Encoding.UTF8.GetString(json));
}
- private static HttpClientMessageDto.WriteEventsDynamic LoadFromXml(string xml)
+ private static HttpClientMessageDto.WriteEventsDynamic LoadFromXml(byte[] xml)
{
try
{
- var doc = XDocument.Parse(xml);
+ XDocument doc;
+ using (var memStream = new MemoryStream(xml))
+ using (var xmlTextReader = new XmlTextReader(memStream))
+ {
+ doc = XDocument.Load(xmlTextReader);
+ }
XNamespace jsonNsValue = "http://james.newtonking.com/projects/json";
XName jsonNsName = XNamespace.Xmlns + "json";
@@ -302,9 +302,10 @@ public string To<T>(T value)
try
{
using (var memory = new MemoryStream())
- using (var writer = new XmlTextWriter(memory, new UTF8Encoding(false)))
+// using (var writer = new XmlTextWriter(memory, new UTF8Encoding(false)))
{
- new XmlSerializer(typeof (T)).Serialize(writer, value);
+ //new XmlSerializer(typeof (T)).Serialize(writer, value);
+ new XmlSerializer(typeof (T)).Serialize(memory, value);
memory.Flush();
memory.Seek(0L, SeekOrigin.Begin);
return Encoding.UTF8.GetString(memory.GetBuffer(), 0, (int)memory.Length);
@@ -387,7 +387,7 @@ public void PostEntry(HttpEntity entity, string stream)
e => Log.ErrorException(e, "Error while reading request (POST entry)"));
}
- private void OnPostEntryRequestRead(HttpEntityManager manager, string body)
+ private void OnPostEntryRequestRead(HttpEntityManager manager, byte[] body)
{
var entity = manager.HttpEntity;
var stream = (string)manager.AsyncState;
@@ -213,10 +213,10 @@ private void WriteResponseAsync(byte[] response, Action<Exception> onError)
SetResponseLength(response.Length);
var state = new ManagerOperationState(HttpEntity, (sender, e) => {}, onError)
- {
- InputStream = new MemoryStream(response),
- OutputStream = HttpEntity.Response.OutputStream
- };
+ {
+ InputStream = new MemoryStream(response),
+ OutputStream = HttpEntity.Response.OutputStream
+ };
var copier = new AsyncStreamCopier<ManagerOperationState>(state.InputStream, state.OutputStream, state);
copier.Completed += ResponseWritten;
copier.Start();
@@ -242,14 +242,19 @@ private void ResponseWritten(object sender, EventArgs eventArgs)
public void ReadRequestAsync(Action<HttpEntityManager, string> onSuccess, Action<Exception> onError)
{
+ ReadRequestAsync((manager, bytes) => onSuccess(manager, Encoding.UTF8.GetString(bytes)), onError);
+ }
+
+ public void ReadRequestAsync(Action<HttpEntityManager, byte[]> onSuccess, Action<Exception> onError)
+ {
Ensure.NotNull(onSuccess, "onSuccess");
Ensure.NotNull(onError, "onError");
var state = new ManagerOperationState(HttpEntity, onSuccess, onError)
- {
- InputStream = HttpEntity.Request.InputStream,
- OutputStream = new MemoryStream()
- };
+ {
+ InputStream = HttpEntity.Request.InputStream,
+ OutputStream = new MemoryStream()
+ };
var copier = new AsyncStreamCopier<ManagerOperationState>(state.InputStream, state.OutputStream, state);
copier.Completed += RequestRead;
@@ -273,7 +278,12 @@ private void RequestRead(object sender, EventArgs e)
state.OutputStream.Seek(0, SeekOrigin.Begin);
var memory = (MemoryStream)state.OutputStream;
- var request = Encoding.UTF8.GetString(memory.GetBuffer(), 0, (int)memory.Length);
+ var request = memory.GetBuffer();
+ if (memory.Length != memory.GetBuffer().Length)
+ {
+ request = new byte[memory.Length];
+ Buffer.BlockCopy(memory.GetBuffer(), 0, request, 0, (int)memory.Length);
+ }
state.OnSuccess(this, request);
}
@@ -31,16 +31,16 @@
namespace EventStore.Transport.Http.EntityManagement
{
- public class ManagerOperationState
+ internal class ManagerOperationState
{
public readonly HttpEntity Entity;
- public readonly Action<HttpEntityManager, string> OnSuccess;
+ public readonly Action<HttpEntityManager, byte[]> OnSuccess;
public readonly Action<Exception> OnError;
public Stream InputStream { get; set; }
public Stream OutputStream { get; set; }
- public ManagerOperationState(HttpEntity entity, Action<HttpEntityManager, string> onSuccess, Action<Exception> onError)
+ public ManagerOperationState(HttpEntity entity, Action<HttpEntityManager, byte[]> onSuccess, Action<Exception> onError)
{
Ensure.NotNull(entity, "entity");
Ensure.NotNull(onSuccess, "onSuccess");

0 comments on commit 3216532

Please sign in to comment.