protected void Handle(HttpListenerContext httpListenerContext, HproseHttpListenerMethods methods)
        {
            HproseHttpListenerContext context = new HproseHttpListenerContext(httpListenerContext);

            SendHeader(context);
            string method = context.Request.HttpMethod;

            if (method == "GET")
            {
                if (getEnabled)
                {
                    MemoryStream            data         = DoFunctionList(methods, context);
                    NonBlockingWriteContext writeContext = new NonBlockingWriteContext();
                    writeContext.currentContext = context;
                    writeContext.ostream        = GetOutputStream(context);
                    writeContext.ostream.BeginWrite(data.GetBuffer(), 0, (int)data.Length,
                                                    new AsyncCallback(NonBlockingWriteCallback), writeContext);
                }
                else
                {
                    context.Response.StatusCode = 403;
                    context.Response.Close();
                }
            }
            else if (method == "POST")
            {
                NonBlockingHandle(context, methods);
            }
            else
            {
                context.Response.Close();
            }
        }
        private void NonBlockingHandle(HproseHttpListenerContext currentContext, HproseHttpListenerMethods methods)
        {
            NonBlockingReadContext context = new NonBlockingReadContext();

            context.currentContext = currentContext;
            context.methods        = methods;
            context.istream        = currentContext.Request.InputStream;
            int len = (int)currentContext.Request.ContentLength64;

            context.data         = (len > 0) ? new MemoryStream(len) : new MemoryStream();
            context.bufferlength = (len > 81920 || len < 0) ? 81920 : len;
            context.buffer       = new byte[context.bufferlength];
            context.istream.BeginRead(context.buffer, 0, context.bufferlength, new AsyncCallback(NonBlockingReadCallback), context);
        }
        protected void Handle(HttpListenerContext httpListenerContext, HproseHttpListenerMethods methods)
        {
            HproseHttpListenerContext context = new HproseHttpListenerContext(httpListenerContext);

            SendHeader(context);
            string method = context.Request.HttpMethod;

            if (method == "GET")
            {
                if (getEnabled)
                {
                    MemoryStream data = DoFunctionList(methods, context);
                    data.WriteTo(GetOutputStream(context));
                }
                else
                {
                    context.Response.StatusCode = 403;
                    context.Response.Close();
                }
            }
            else if (method == "POST")
            {
                int          len    = (int)context.Request.ContentLength64;
                Stream       stream = context.Request.InputStream;
                MemoryStream data   = new MemoryStream();
                int          length = (len > 4096 || len < 0) ? 4096 : len;
                byte[]       buffer = new byte[length];
                for (;;)
                {
                    int n = stream.Read(buffer, 0, length);
                    if (n > 0)
                    {
                        data.Write(buffer, 0, n);
                    }
                    else
                    {
                        break;
                    }
                }
                Handle(data, methods, context).WriteTo(GetOutputStream(context));
            }
            else
            {
                context.Response.Close();
            }
        }
        private Stream GetOutputStream(HproseHttpListenerContext currentContext)
        {
            Stream ostream = currentContext.Response.OutputStream;

            if (compressionEnabled)
            {
                string acceptEncoding = currentContext.Request.Headers["Accept-Encoding"];
                if (acceptEncoding != null)
                {
                    acceptEncoding = acceptEncoding.ToLower();
                    if (acceptEncoding.IndexOf("deflate") > -1)
                    {
                        ostream = new DeflateStream(ostream, CompressionMode.Compress);
                    }
                    else if (acceptEncoding.IndexOf("gzip") > -1)
                    {
                        ostream = new GZipStream(ostream, CompressionMode.Compress);
                    }
                }
            }
            return(ostream);
        }
        protected override object[] FixArguments(Type[] argumentTypes, object[] arguments, int count, HproseContext context)
        {
            HproseHttpListenerContext currentContext = (HproseHttpListenerContext)context;

            if (argumentTypes.Length != count)
            {
                object[] args = new object[argumentTypes.Length];
                System.Array.Copy(arguments, 0, args, 0, count);
                Type argType = argumentTypes[count];
                if (argType == typeof(HproseContext) ||
                    argType == typeof(HproseHttpListenerContext))
                {
                    args[count] = currentContext;
                }
                else if (argType == typeof(HttpListenerContext))
                {
                    args[count] = currentContext.Context;
                }
                else if (argType == typeof(HttpListenerRequest))
                {
                    args[count] = currentContext.Request;
                }
                else if (argType == typeof(HttpListenerResponse))
                {
                    args[count] = currentContext.Response;
                }
#if !dotNETMF
                else if (argType == typeof(IPrincipal))
                {
                    args[count] = currentContext.User;
                }
#endif
                return(args);
            }
            return(arguments);
        }
        private void SendHeader(HproseHttpListenerContext currentContext)
        {
            if (OnSendHeader != null)
            {
                OnSendHeader(currentContext);
            }
            HttpListenerRequest  request  = currentContext.Request;
            HttpListenerResponse response = currentContext.Response;

            response.ContentType = "text/plain";
            if (p3pEnabled)
            {
#if !dotNETMF
                response.AddHeader("P3P",
                                   "CP=\"CAO DSP COR CUR ADM DEV TAI PSA PSD " +
                                   "IVAi IVDi CONi TELo OTPi OUR DELi SAMi " +
                                   "OTRi UNRi PUBi IND PHY ONL UNI PUR FIN " +
                                   "COM NAV INT DEM CNT STA POL HEA PRE GOV\"");
#else
                response.Headers.Add("P3P",
                                     "CP=\"CAO DSP COR CUR ADM DEV TAI PSA PSD " +
                                     "IVAi IVDi CONi TELo OTPi OUR DELi SAMi " +
                                     "OTRi UNRi PUBi IND PHY ONL UNI PUR FIN " +
                                     "COM NAV INT DEM CNT STA POL HEA PRE GOV\"");
#endif
            }
            if (crossDomainEnabled)
            {
                string origin = request.Headers["Origin"];
                if (origin != null && origin != "" && origin != "null")
                {
#if !dotNETMF
                    if (origins.Count == 0 || origins.ContainsKey(origin))
                    {
                        response.AddHeader("Access-Control-Allow-Origin", origin);
                        response.AddHeader("Access-Control-Allow-Credentials", "true");
#else
                    if (origins.Count == 0 || origins.Contains(origin))
                    {
                        response.Headers.Add("Access-Control-Allow-Origin", origin);
                        response.Headers.Add("Access-Control-Allow-Credentials", "true");
#endif
                    }
                }
                else
                {
#if !dotNETMF
                    response.AddHeader("Access-Control-Allow-Origin", "*");
#else
                    response.Headers.Add("Access-Control-Allow-Origin", "*");
#endif
                }
            }
            if (compressionEnabled)
            {
                string acceptEncoding = request.Headers["Accept-Encoding"];
                if (acceptEncoding != null)
                {
                    acceptEncoding = acceptEncoding.ToLower();
                    if (acceptEncoding.IndexOf("deflate") > -1)
                    {
#if !dotNETMF
                        response.AddHeader("Content-Encoding", "deflate");
#else
                        response.Headers.Add("Content-Encoding", "deflate");
#endif
                    }
                    else if (acceptEncoding.IndexOf("gzip") > -1)
                    {
#if !dotNETMF
                        response.AddHeader("Content-Encoding", "gzip");
#else
                        response.Headers.Add("Content-Encoding", "gzip");
#endif
                    }
                }
            }
        }
 protected void Handle(HttpListenerContext httpListenerContext, HproseHttpListenerMethods methods) {
     HproseHttpListenerContext context = new HproseHttpListenerContext(httpListenerContext);
     SendHeader(context);
     string method = context.Request.HttpMethod;
     if (method == "GET") {
         if (getEnabled) {
             MemoryStream data = DoFunctionList(methods, context);
             NonBlockingWriteContext writeContext = new NonBlockingWriteContext();
             writeContext.currentContext = context;
             writeContext.ostream = GetOutputStream(context);
             writeContext.ostream.BeginWrite(data.GetBuffer(), 0, (int)data.Length,
                 new AsyncCallback(NonBlockingWriteCallback), writeContext);
         }
         else {
             context.Response.StatusCode = 403;
             context.Response.Close();
         }
     }
     else if (method == "POST") {
         NonBlockingHandle(context, methods);
     }
     else {
         context.Response.Close();
     }
 }
 private void SendHeader(HproseHttpListenerContext currentContext) {
     if (OnSendHeader != null) {
         OnSendHeader(currentContext);
     }
     HttpListenerRequest request = currentContext.Request;
     HttpListenerResponse response = currentContext.Response;
     response.ContentType = "text/plain";
     if (p3pEnabled) {
         response.AddHeader("P3P",
             "CP=\"CAO DSP COR CUR ADM DEV TAI PSA PSD " +
             "IVAi IVDi CONi TELo OTPi OUR DELi SAMi " +
             "OTRi UNRi PUBi IND PHY ONL UNI PUR FIN " +
             "COM NAV INT DEM CNT STA POL HEA PRE GOV\"");
     }
     if (crossDomainEnabled) {
         string origin = request.Headers["Origin"];
         if (origin != null && origin != "" && origin != "null") {
             if (origins.Count == 0 || origins.ContainsKey(origin)) {
                 response.AddHeader("Access-Control-Allow-Origin", origin);
                 response.AddHeader("Access-Control-Allow-Credentials", "true");
             }
         }
         else {
             response.AddHeader("Access-Control-Allow-Origin", "*");
         }
     }
     if (compressionEnabled) {
         string acceptEncoding = request.Headers["Accept-Encoding"];
         if (acceptEncoding != null) {
             acceptEncoding = acceptEncoding.ToLower();
             if (acceptEncoding.IndexOf("deflate") > -1) {
                 response.AddHeader("Content-Encoding", "deflate");
             }
             else if (acceptEncoding.IndexOf("gzip") > -1) {
                 response.AddHeader("Content-Encoding", "gzip");
             }
         }
     }
 }
 private void NonBlockingHandle(HproseHttpListenerContext currentContext, HproseHttpListenerMethods methods) {
     NonBlockingReadContext context = new NonBlockingReadContext();
     context.currentContext = currentContext;
     context.methods = methods;
     context.istream = currentContext.Request.InputStream;
     int len = (int)currentContext.Request.ContentLength64;
     context.data = (len > 0) ? new MemoryStream(len) : new MemoryStream();
     context.bufferlength = (len > 81920 || len < 0) ? 81920 : len;
     context.buffer = new byte[context.bufferlength];
     context.istream.BeginRead(context.buffer, 0, context.bufferlength, new AsyncCallback(NonBlockingReadCallback), context);
 }
 private Stream GetOutputStream(HproseHttpListenerContext currentContext) {
     Stream ostream = currentContext.Response.OutputStream;
     if (compressionEnabled) {
         string acceptEncoding = currentContext.Request.Headers["Accept-Encoding"];
         if (acceptEncoding != null) {
             acceptEncoding = acceptEncoding.ToLower();
             if (acceptEncoding.IndexOf("deflate") > -1) {
                 ostream = new DeflateStream(ostream, CompressionMode.Compress);
             }
             else if (acceptEncoding.IndexOf("gzip") > -1) {
                 ostream = new GZipStream(ostream, CompressionMode.Compress);
             }
         }
     }
     return ostream;
 }