Ejemplo n.º 1
0
            protected static async Task <Tuple <string, HttpRequestMessage> > DuplicateAndComputeChecksum(
                HttpRequestMessage request)
            {
                Contract.Requires <ArgumentNullException>(request != null);
                var stream = new MemoryStream();

                try
                {
                    var result = await ComputeChecksum(request, stream);

                    string checksum = result.Item1;
                    long   offset   = result.Item2;

                    StreamContent content = null;

                    if (request.Content == null)
                    {
                        stream.Close();
                    }
                    else
                    {
                        content = new StreamContent(stream);

                        foreach (var header in request.Content.Headers)
                        {
                            content.Headers.Add(header.Key, header.Value);
                        }

                        content.Headers.ContentLength = request.Content.Headers.ContentLength;
                        stream.Seek(offset, SeekOrigin.Begin);
                    }

                    var duplicateRequest = new HttpRequestMessage(request.Method, request.RequestUri)
                    {
                        Content = content,
                        Version = request.Version
                    };

                    foreach (var header in request.Headers)
                    {
                        duplicateRequest.Headers.Add(header.Key, header.Value);
                    }

                    return(new Tuple <string, HttpRequestMessage>(checksum, duplicateRequest));
                }
                catch
                {
                    stream.Dispose();
                    throw;
                }
            }
Ejemplo n.º 2
0
            Recording(HttpResponseMessage response, byte[] content, string checksum)
            {
                Contract.Requires <ArgumentNullException>(response != null);
                Contract.Requires <ArgumentNullException>(content != null);
                Contract.Requires <ArgumentNullException>(checksum != null);

                this.checksum       = checksum;
                this.content        = Convert.ToBase64String(content);
                this.contentHeaders = response.Content.Headers.ToList();
                this.headers        = response.Headers.ToList();
                this.reasonPhrase   = response.ReasonPhrase;
                this.statusCode     = response.StatusCode;
                this.version        = response.Version;
            }
Ejemplo n.º 3
0
            protected static async Task <Tuple <string, long> > ComputeChecksum(HttpRequestMessage request, MemoryStream stream)
            {
                Contract.Requires <ArgumentNullException>(request != null);
                Contract.Requires <ArgumentNullException>(stream != null);

                string text;

                byte[] bytes;

                text  = string.Format("{0} {1} HTTP/{2}", request.Method, request.RequestUri, request.Version);
                bytes = Encoding.UTF8.GetBytes(text);
                stream.Write(bytes, 0, bytes.Length);
                stream.Write(crlf, 0, crlf.Length);

                foreach (var header in request.Headers)
                {
                    text  = string.Format("{0}: {1}", header.Key, string.Join(", ", header.Value));
                    bytes = Encoding.UTF8.GetBytes(text);
                    stream.Write(bytes, 0, bytes.Length);
                    stream.Write(crlf, 0, crlf.Length);
                }

                if (request.Content != null)
                {
                    foreach (var header in request.Content.Headers)
                    {
                        text  = string.Format("{0}: {1}", header.Key, string.Join(", ", header.Value));
                        bytes = Encoding.UTF8.GetBytes(text);
                        stream.Write(bytes, 0, bytes.Length);
                        stream.Write(crlf, 0, crlf.Length);
                    }
                }

                stream.Write(crlf, 0, crlf.Length);
                long offset = stream.Position;

                if (request.Content != null)
                {
                    await request.Content.CopyToAsync(stream);
                }

                var hashAlgorithm = SHA512.Create();

                stream.Seek(0, SeekOrigin.Begin);

                byte[] hash     = hashAlgorithm.ComputeHash(stream);
                var    checksum = Convert.ToBase64String(hash);

                return(new Tuple <string, long>(checksum, offset));
            }
Ejemplo n.º 4
0
        public static void Begin(string callerId)
        {
            Contract.Requires <ArgumentNullException>(callerId != null);

            RecordingFilename = Path.Combine(recordingDirectoryName, string.Join(".", callerId, "json", "gz"));

            switch (Mode)
            {
            case MockContextMode.Run:
                session   = new Session(callerId);
                getOrElse = Noop;
                break;

            case MockContextMode.Record:
                session   = new Session(callerId);
                getOrElse = Enqueue;
                break;

            case MockContextMode.Playback:

                var serializer = new DataContractJsonSerializer(
                    typeof(Session), null, int.MaxValue, false, new JsonDataContractSurrogate(), false);

                using (var stream = new FileStream(RecordingFilename, FileMode.Open))
                {
                    using (var compressedStream = new GZipStream(stream, CompressionMode.Decompress))
                    {
                        session = (Session)serializer.ReadObject(compressedStream);
                    }
                }

                getOrElse = Dequeue;
                break;

            default: throw new InvalidOperationException();
            }
        }
Ejemplo n.º 5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Application"/> class.
 /// </summary>
 /// <param name="service">
 /// An object representing a root Splunk service endpoint.
 /// </param>
 /// <param name="name">
 /// An object identifying a Splunk resource within
 /// <paramref name= "service"/>.<see cref="Namespace"/>.
 /// </param>
 /// <exception cref="ArgumentNullException">
 /// <paramref name="service"/> or <paramref name="name"/> are <c>null</c>.
 /// </exception>
 protected internal Job(Service service, string name)
     : this(service.Context, service.Namespace, name)
 {
     Contract.Requires<ArgumentNullException>(service != null);
 }