Пример #1
0
        static IEnumerable<object> ProcessSocketInternal(this ISocket socket, IHttpSupport http, OwinApplication application)
        {
            var beginRequest = http.BeginRequest(socket);
            yield return beginRequest;

            var request = beginRequest.Result;

            var invoke = new ContinuationState<Tuple<string, IDictionary<string, IList<string>>, IEnumerable<object>>>
                ((r, e) =>
                    application(request,
                                (s,h,b) =>
                                    r(new Tuple<string,IDictionary<string,IList<string>>,IEnumerable<object>>(s, h, b)),
                                e));

            yield return invoke;

            var response = invoke.Result;

            yield return http.BeginResponse(socket, response.Item1, response.Item2);

            foreach (var obj in response.Item3)
            {
                var objectToWrite = obj;

                if (obj is Action<Action<object>, Action<Exception>>)
                {
                    var cs = new ContinuationState<object>(obj as Action<Action<object>, Action<Exception>>);

                    yield return cs;

                    objectToWrite = cs.Result;
                }

                if (objectToWrite is FileInfo)
                {
                    yield return new ContinuationState(socket.WriteFile((objectToWrite as FileInfo).Name));
                    continue;
                }

                var chunk = default(ArraySegment<byte>);

                if (obj is ArraySegment<byte>)
                    chunk = (ArraySegment<byte>)obj;
                else if (obj is byte[])
                    chunk = new ArraySegment<byte>(obj as byte[]);
                else
                    continue;
                    //throw new ArgumentException("Invalid object of type " + obj.GetType() + " '" + obj.ToString() + "'");

                var write = socket.WriteChunk(chunk);
                yield return write;

                // TODO enumerate to completion
                if (write.Exception != null)
                    throw write.Exception;
            }

            socket.Dispose();
        }
Пример #2
0
 public static void Host(this IKayakServer server, OwinApplication application, Action <Action> trampoline)
 {
     server.HostInternal(application, trampoline).AsContinuation <object>(trampoline)
         (_ => { }, e => {
         Console.WriteLine("Error while hosting application.");
         Console.Out.WriteException(e);
     });
 }
Пример #3
0
 public static void Host(this IKayakServer server, OwinApplication application, Action<Action> trampoline)
 {
     server.HostInternal(application, trampoline).AsContinuation<object>(trampoline)
         (_ => { }, e => {
             Console.WriteLine("Error while hosting application.");
             Console.Out.WriteException(e);
         });
 }
Пример #4
0
 public static void ProcessSocket(this ISocket socket, IHttpSupport http, OwinApplication application, Action<Action> trampoline)
 {
     socket.ProcessSocketInternal(http, application).AsContinuation<object>(trampoline)
         (_ => { }, e =>
         {
             Console.WriteLine("Error while processing request.");
             Console.Out.WriteException(e);
         });
 }
Пример #5
0
 public static void ProcessSocket(this ISocket socket, IHttpSupport http, OwinApplication application, Action <Action> trampoline)
 {
     socket.ProcessSocketInternal(http, application).AsContinuation <object>(trampoline)
         (_ => { }, e =>
     {
         Console.WriteLine("Error while processing request.");
         Console.Out.WriteException(e);
     });
 }
Пример #6
0
        static IEnumerable<object> HostInternal(this IKayakServer server, OwinApplication application, Action<Action> trampoline)
        {
            while (true)
            {
                var accept = new ContinuationState<ISocket>((r, e) => server.GetConnection()(r));
                yield return accept;

                if (accept.Result == null)
                    break;

                accept.Result.ProcessSocket(new HttpSupport(), application, trampoline);
            }
        }
Пример #7
0
        static IEnumerable <object> HostInternal(this IKayakServer server, OwinApplication application, Action <Action> trampoline)
        {
            while (true)
            {
                var accept = new ContinuationState <ISocket>((r, e) => server.GetConnection()(r));
                yield return(accept);

                if (accept.Result == null)
                {
                    break;
                }

                accept.Result.ProcessSocket(new HttpSupport(), application, trampoline);
            }
        }
        public async Task CanPostAndEnlistFiles()
        {
            const string InputFile = "Foo.pdf";
            const string InputFile2 = "Bar.pdf";

            using (var application = new OwinApplication(6161))
            {
                await application.Client.PostFileAsync(Identifier, InputFile);
                await application.Client.PostFileAsync(Identifier, InputFile2);
                
                var blobstoreFileList = await application.Client.EnlistFilesAsync(Identifier);
                
                blobstoreFileList.Files.Should()
                    .Contain(f => f.Identifier == Identifier && f.FileName == InputFile).And
                    .Contain(f => f.Identifier == Identifier && f.FileName == InputFile2);
            }
        }
Пример #9
0
        static IEnumerable<object> ProcessSocketInternal(this ISocket socket, IHttpSupport http, OwinApplication application)
        {
            var beginRequest = http.BeginRequest(socket);
            yield return beginRequest;

            var request = beginRequest.Result;

            var invoke = new ContinuationState<Tuple<string, IDictionary<string, IEnumerable<string>>, IEnumerable<object>>>
                ((r, e) => application(request, r, e));

            yield return invoke;

            var response = invoke.Result;

            yield return http.BeginResponse(socket, response.Item1, response.Item2);

            foreach (var obj in response.Item3)
            {
                var objectToWrite = obj;

                if (obj is Action<Action<object>, Action<Exception>>)
                {
                    var cs = new ContinuationState<object>(obj as Action<Action<object>, Action<Exception>>);

                    yield return cs;

                    objectToWrite = cs.Result;
                }

                if (objectToWrite is FileInfo)
                {
                    yield return new ContinuationState(socket.WriteFile((objectToWrite as FileInfo).Name));
                }
                var write = socket.WriteFileOrData(objectToWrite);
                yield return write;

                if (write.Exception != null)
                    throw write.Exception;
            }

            // HTTP/1.1 support might only close the connection if client wants to
            //Trace.Write("Closed connection.");
            socket.Dispose();
        }
        public async Task CanPostAndGetFile()
        {
            const string InputFile = "Foo.pdf";
            const string OutputFile = "Download.pdf";

            using (var application = new OwinApplication(6161))
            {
                await application.Client.PostFileAsync(Identifier, InputFile);

                using (var fileStream = new FileStream(OutputFile, FileMode.Create, FileAccess.Write, FileShare.ReadWrite))
                {
                    var httpContentStream = await application.Client.GetFileStreamAsync(Identifier, InputFile);
                    await httpContentStream.CopyToAsync(fileStream);
                }

                Compare(InputFile, OutputFile);
                Delete(OutputFile);
            }
        }
Пример #11
0
        public void GetAllCustomers(
            OwinApplication webService, 
            HttpResponseMessage message, 
            ResourceCollection<CustomerListModel> result)
        {
            "Given a web service"
                .f(() => webService = new OwinApplication(6161));

            "When requesting all customers"
                .f(async () => message = await webService.Client.GetAsync("api/customers"));

            "Then the status code of the HTTP message equals OK"
                .f(() => message.StatusCode.Should().Be(HttpStatusCode.OK));

            "Then the inner result can be parsed"
                .f(async () => result = await message.Content.ReadAsAsync<ResourceCollection<CustomerListModel>>());

            "Then there are some customers in the result"
                .f(() => result.Results.Count().Should().BeGreaterOrEqualTo(91));

            "Then there is a self link in the result"
                .f(() => result.Links.Should().Contain(l => l.Rel == "self"));
        }
Пример #12
0
        public void GetAllCustomers(
            OwinApplication webService,
            HttpResponseMessage message,
            ResourceCollection <CustomerListModel> result)
        {
            "Given a web service"
            .f(() => webService = new OwinApplication(6161));

            "When requesting all customers"
            .f(async() => message = await webService.Client.GetAsync("api/customers"));

            "Then the status code of the HTTP message equals OK"
            .f(() => message.StatusCode.Should().Be(HttpStatusCode.OK));

            "Then the inner result can be parsed"
            .f(async() => result = await message.Content.ReadAsAsync <ResourceCollection <CustomerListModel> >());

            "Then there are some customers in the result"
            .f(() => result.Results.Count().Should().BeGreaterOrEqualTo(91));

            "Then there is a self link in the result"
            .f(() => result.Links.Should().Contain(l => l.Rel == "self"));
        }
Пример #13
0
        static IEnumerable <object> ProcessSocketInternal(this ISocket socket, IHttpSupport http, OwinApplication application)
        {
            var beginRequest = http.BeginRequest(socket);

            yield return(beginRequest);

            var request = beginRequest.Result;

            var invoke = new ContinuationState <Tuple <string, IDictionary <string, IList <string> >, IEnumerable <object> > >
                             ((r, e) =>
                             application(request,
                                         (s, h, b) =>
                                         r(new Tuple <string, IDictionary <string, IList <string> >, IEnumerable <object> >(s, h, b)),
                                         e));

            yield return(invoke);

            var response = invoke.Result;

            yield return(http.BeginResponse(socket, response.Item1, response.Item2));

            foreach (var obj in response.Item3)
            {
                var objectToWrite = obj;

                if (obj is Action <Action <object>, Action <Exception> > )
                {
                    var cs = new ContinuationState <object>(obj as Action <Action <object>, Action <Exception> >);

                    yield return(cs);

                    objectToWrite = cs.Result;
                }

                if (objectToWrite is FileInfo)
                {
                    yield return(new ContinuationState(socket.WriteFile((objectToWrite as FileInfo).Name)));

                    continue;
                }

                var chunk = default(ArraySegment <byte>);

                if (objectToWrite is ArraySegment <byte> )
                {
                    chunk = (ArraySegment <byte>)objectToWrite;
                }
                else if (objectToWrite is byte[])
                {
                    chunk = new ArraySegment <byte>(objectToWrite as byte[]);
                }
                else
                {
                    continue;
                }
                //throw new ArgumentException("Invalid object of type " + obj.GetType() + " '" + obj.ToString() + "'");

                var write = socket.WriteChunk(chunk);
                yield return(write);

                // TODO enumerate to completion
                if (write.Exception != null)
                {
                    throw write.Exception;
                }
            }

            socket.Dispose();
        }
Пример #14
0
 public static void ProcessSocket(this ISocket socket, IHttpSupport http, OwinApplication application, Action <Action> trampoline)
 {
     socket.ProcessSocketInternal(http, application).AsContinuation <object>(trampoline)
         (_ => { }, e => logger.ErrorException("ProcessSocket", e));
 }
Пример #15
0
 public static void Host(this IKayakServer server, OwinApplication application, Action<Action> trampoline)
 {
     server.Host(new HttpSupport(), application, trampoline);
 }
Пример #16
0
 public static void Host(this IKayakServer server, OwinApplication application, Action <Action> trampoline)
 {
     server.HostInternal(application, trampoline).AsContinuation <object>(trampoline)
         (_ => { }, e => logger.ErrorException("KayakHost", e));
 }
Пример #17
0
 public static void Host(this IKayakServer server, OwinApplication application)
 {
     server.Host(application, null);
 }
Пример #18
0
 public static void Host(this IKayakServer server, OwinApplication application)
 {
     server.Host(application, null);
 }