Ejemplo n.º 1
0
 private static void OnRequests(SocketChannel channel, HttpRequestBase request)
 {
     var response = request.CreateResponse();
     response.StatusCode = 200;
     response.Body = GetStream();
     response.AddHeader("Keep-Alive", "timeout=15, max=100");
     response.Body.Write(Encoding.ASCII.GetBytes("HelloWorld"), 0, 10);
     channel.Send(response);
 }
        public void request_in_its_simplest_form()
        {
            var frame = new HttpRequestBase("POST", "/", "HTTP/1.1");
            var expected = "POST / HTTP/1.1\r\nContent-Length: 0\r\n\r\n";
            var buffer = new SocketBufferFake();

            var encoder = new HttpMessageEncoder();
            encoder.Prepare(frame);
            encoder.Send(buffer);
            var actual = Encoding.ASCII.GetString(buffer.Buffer, 0, buffer.Count);

            actual.Should().Be(expected);
        }
        public void request_with_body()
        {
            var frame = new HttpRequestBase("POST", "/?abc", "HTTP/1.1");
            frame.AddHeader("server", "Griffin.Networking");
            frame.AddHeader("X-Requested-With", "XHttpRequest");
            frame.ContentType = "text/plain";
            frame.Body = new MemoryStream(Encoding.ASCII.GetBytes("hello queue a"));
            var expected = "POST /?abc HTTP/1.1\r\nserver: Griffin.Networking\r\nX-Requested-With: XHttpRequest\r\nContent-Type: text/plain\r\nContent-Length: 13\r\n\r\nhello queue a";
            var buffer = new SocketBufferFake();

            var encoder = new HttpMessageEncoder();
            encoder.Prepare(frame);
            encoder.Send(buffer);
            var actual = Encoding.ASCII.GetString(buffer.Buffer, 0, buffer.Count);

            actual.Should().Be(expected);
        }
Ejemplo n.º 4
0
        private bool AuthenticateUser(ITcpChannel channel, HttpRequestBase request)
        {
            if (channel.Data["Principal"] != null)
            {
                Thread.CurrentPrincipal = (IPrincipal) channel.Data["Principal"];
                return true;
            }

            try
            {
                var user = Authenticator.Authenticate(request);
                if (user == null)
                    return true;

                if (PrincipalFactory != null)
                {
                    var ctx = new PrincipalFactoryContext(request, user);
                    Thread.CurrentPrincipal = PrincipalFactory.Create(ctx);
                    channel.Data["Principal"] = Thread.CurrentPrincipal;
                    return true;
                }

                var roles = user as IUserWithRoles;
                if (roles == null)
                    throw new InvalidOperationException(
                        "You must specify a PrincipalFactory if you do not return a IUserWithRoles from your IAccountService.");

                Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity(user.Username), roles.RoleNames);
                channel.Data["Principal"] = Thread.CurrentPrincipal;
            }
            catch (HttpException ex)
            {
                if (Logger != null)
                    Logger("Authentication failed.\r\nException:\r\n" + ex.ToString());
                var response = request.CreateResponse();
                response.StatusCode = ex.HttpCode;
                response.ReasonPhrase = FirstLine(ex.Message);
                channel.Send(response);
                return false;
            }

            return true;
        }
Ejemplo n.º 5
0
        private bool RespondWithRaw(Stream body, int statusCode, string mimeType, Dictionary<string, string> headers = null)
        {
            ITcpChannel _channel;
            lock (lockObject)
            {
                if (channel == null)
                    return false;
                _channel = channel;
                channel = null;
            }

            try
            {
                IHttpResponse response = request.CreateResponse();
                request = null;

                response.ContentLength = (int)body.Length;
                response.StatusCode = statusCode;
                response.ContentType = mimeType;

                if (currentUser != null)
                {
                    response.AddHeader("X-User-Name", currentUser.Value.name);
                    response.AddHeader("X-User-Readonly", currentUser.Value.readOnly ? "Yes" : "No");
                }

                response.AddHeader("Access-Control-Allow-Origin", "*");
                response.AddHeader("Access-Control-Allow-Headers", "Authorization");
                response.AddHeader("Access-Control-Allow-Methods", "POST, HEAD, PUT, DELETE, GET, OPTIONS");
                if (headers != null)
                    foreach (KeyValuePair<string, string> kvp in headers)
                        response.AddHeader(kvp.Key, kvp.Value);

                body.Position = 0;
                response.Body = body;

                _channel.Send(response);
            }
            catch
            {
                return false;
            }

            return true;
        }
Ejemplo n.º 6
0
 public RequestHandler(ITcpChannel channel, HttpRequestBase request)
 {
     this.channel = channel;
     this.request = request;
 }