public EndpointAttributes GetEndpointAttributes(IHttpRequest request)
        {
            var portRestrictions = EndpointAttributes.None;

            portRestrictions |= HttpMethods.GetEndpointAttribute(request.HttpMethod);
            portRestrictions |= request.IsSecureConnection ? EndpointAttributes.Secure : EndpointAttributes.InSecure;

            if (request.UserHostAddress != null)
            {
                var isIpv4Address = request.UserHostAddress.IndexOf('.') != -1 &&
                                    request.UserHostAddress.IndexOf("::", StringComparison.InvariantCulture) == -1;
                var pieces          = request.UserHostAddress.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
                var ipAddressNumber = isIpv4Address
                                        ? pieces[0]
                                        : (request.UserHostAddress.Contains("]:")
                                                ? request.UserHostAddress.Substring(0, request.UserHostAddress.LastIndexOf(':'))
                                                : request.UserHostAddress);

                try
                {
                    ipAddressNumber = ipAddressNumber.SplitOnFirst(',')[0];
                    var ipAddress = ipAddressNumber.StartsWith("::1")
                        ? IPAddress.IPv6Loopback
                        : IPAddress.Parse(ipAddressNumber);
                    portRestrictions |= GetIpAddressEndpointAttributes(ipAddress);
                }
                catch (Exception ex)
                {
                    throw new ArgumentException("Could not parse Ipv{0} Address: {1} / {2}"
                                                .Fmt((isIpv4Address ? 4 : 6), request.UserHostAddress, ipAddressNumber), ex);
                }
            }

            return(portRestrictions);
        }
示例#2
0
        public EndpointAttributes GetEndpointAttributes(IHttpRequest request)
        {
            var portRestrictions = EndpointAttributes.None;

            portRestrictions |= HttpMethods.GetEndpointAttribute(request.HttpMethod);
            portRestrictions |= request.IsSecureConnection ? EndpointAttributes.Secure : EndpointAttributes.InSecure;

            if (request.UserHostAddress != null)
            {
                var isIpv4Address = request.UserHostAddress.IndexOf('.') != -1 &&
                                    request.UserHostAddress.IndexOf("::") == -1;
                var pieces          = request.UserHostAddress.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
                var ipAddressNumber = isIpv4Address
                                        ? pieces[0]
                                        : (pieces.Length == 9 || pieces.Length == 6 || pieces.Length == 4
                                                ? request.UserHostAddress.Substring(0, request.UserHostAddress.LastIndexOf(':'))
                                                : request.UserHostAddress);

                var ipAddress = IPAddress.Parse(ipAddressNumber);

                portRestrictions |= GetIpAddressEndpointAttributes(ipAddress);
            }

            return(portRestrictions);
        }
示例#3
0
        public static EndpointAttributes GetAttributes(this IHttpRequest request)
        {
            if (EndpointHost.DebugMode &&
                request.QueryString != null)    //Mock<IHttpRequest>
            {
                var simulate = request.QueryString["simulate"];
                if (simulate != null)
                {
                    return(ToEndpointAttributes(simulate.Split(',')));
                }
            }

            var portRestrictions = EndpointAttributes.None;

            portRestrictions |= HttpMethods.GetEndpointAttribute(request.HttpMethod);
            portRestrictions |= request.IsSecureConnection ? EndpointAttributes.Secure : EndpointAttributes.InSecure;

            if (request.UserHostAddress != null)
            {
                var isIpv4Address = request.UserHostAddress.IndexOf('.') != -1 &&
                                    request.UserHostAddress.IndexOf("::", StringComparison.InvariantCulture) == -1;

                string ipAddressNumber = null;
                if (isIpv4Address)
                {
                    ipAddressNumber = request.UserHostAddress.SplitOnFirst(":")[0];
                }
                else
                {
                    if (request.UserHostAddress.Contains("]:"))
                    {
                        ipAddressNumber = request.UserHostAddress.SplitOnLast(":")[0];
                    }
                    else
                    {
                        ipAddressNumber = request.UserHostAddress.LastIndexOf("%", StringComparison.InvariantCulture) > 0 ?
                                          request.UserHostAddress.SplitOnLast(":")[0] :
                                          request.UserHostAddress;
                    }
                }

                try
                {
                    ipAddressNumber = ipAddressNumber.SplitOnFirst(',')[0];
                    var ipAddress = ipAddressNumber.StartsWith("::1")
                        ? IPAddress.IPv6Loopback
                        : IPAddress.Parse(ipAddressNumber);
                    portRestrictions |= GetAttributes(ipAddress);
                }
                catch (Exception ex)
                {
                    throw new ArgumentException("Could not parse Ipv{0} Address: {1} / {2}"
                                                .Fmt((isIpv4Address ? 4 : 6), request.UserHostAddress, ipAddressNumber), ex);
                }
            }

            return(portRestrictions);
        }
        public EndpointAttributes GetEndpointAttributes(HttpRequest request)
        {
            var portRestrictions = EndpointAttributes.None;

            portRestrictions |= HttpMethods.GetEndpointAttribute(request.RequestType);
            portRestrictions |= request.IsSecureConnection ? EndpointAttributes.Secure : EndpointAttributes.InSecure;

            var ipAddress = IPAddress.Parse(request.UserHostAddress);

            portRestrictions |= GetIpAddressEndpointAttributes(ipAddress);

            return(portRestrictions);
        }
示例#5
0
        public EndpointAttributes GetEndpointAttributes(IHttpRequest request)
        {
            var portRestrictions = EndpointAttributes.None;

            portRestrictions |= HttpMethods.GetEndpointAttribute(request.HttpMethod);
            portRestrictions |= request.IsSecureConnection ? EndpointAttributes.Secure : EndpointAttributes.InSecure;

            if (request.UserHostAddress != null)
            {
                var isIpv4Address   = request.UserHostAddress.IndexOf('.') != -1;
                var ipAddressNumber = isIpv4Address
                    ? request.UserHostAddress.Split(':')[0]
                                        : request.UserHostAddress;

                var ipAddress = IPAddress.Parse(ipAddressNumber);

                portRestrictions |= GetIpAddressEndpointAttributes(ipAddress);
            }

            return(portRestrictions);
        }
        private void ProcessJsonRequest(string url, string operationName, string httpMethod, NameValueCollection queryString, Stream inputStream, HttpListenerResponseWrapper response)
        {
            try
            {
                var request = JsonHandlerBase.CreateRequest(operationName,
                                                            httpMethod, queryString, null, inputStream);

                if (url.Contains("/json/syncreply/"))
                {
                    var result = ExecuteService(request, EndpointAttributes.SyncReply | EndpointAttributes.Json | HttpMethods.GetEndpointAttribute(httpMethod));
                    response.WriteToResponse(result, x => JsonDataContractSerializer.Instance.Parse(result), ContentType.Json);
                }
                else if (url.Contains("/json/asynconeway/"))
                {
                    var result = ExecuteService(request, EndpointAttributes.AsyncOneWay | EndpointAttributes.Json | HttpMethods.GetEndpointAttribute(httpMethod));
                }
            }
            catch (Exception ex)
            {
                log.Error(ex);
                response.WriteJsonErrorToResponse(operationName, ex.Message, ex);
            }
        }
        private void ProcessJsvRequest(string url, string operationName, string httpMethod, NameValueCollection queryString, Stream inputStream, HttpListenerResponseWrapper response)
        {
            try
            {
                var request = JsvHandlerBase.CreateRequest(operationName, httpMethod, queryString, null, inputStream);

                var isDebugRequest = queryString["debug"] != null;

                var writeFn = isDebugRequest
                                        ? (Func <object, string>)JsvFormatter.SerializeAndFormat
                                        : TypeSerializer.SerializeToString;

                var contentType = isDebugRequest ? ContentType.PlainText : ContentType.JsvText;

                if (url.Contains("/jsv/syncreply/"))
                {
                    var result = ExecuteService(request, EndpointAttributes.SyncReply | EndpointAttributes.Jsv | HttpMethods.GetEndpointAttribute(httpMethod));
                    response.WriteToResponse(result, writeFn, contentType);
                }
                else if (url.Contains("/jsv/asynconeway/"))
                {
                    ExecuteService(request, EndpointAttributes.AsyncOneWay | EndpointAttributes.Jsv | HttpMethods.GetEndpointAttribute(httpMethod));
                }
            }
            catch (Exception ex)
            {
                log.Error(ex);
                response.WriteJsvErrorToResponse(operationName, ex.Message, ex);
            }
        }