当前位置: 首页>>代码示例>>C#>>正文


C# DreamMessage.Close方法代码示例

本文整理汇总了C#中DreamMessage.Close方法的典型用法代码示例。如果您正苦于以下问题:C# DreamMessage.Close方法的具体用法?C# DreamMessage.Close怎么用?C# DreamMessage.Close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在DreamMessage的用法示例。


在下文中一共展示了DreamMessage.Close方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: RequestHandler

        private void RequestHandler(IAsyncResult ar)
        {
            HttpListenerContext httpContext = null;
            Action<string> activity = null;
            HttpListener listener = (HttpListener)ar.AsyncState;

            // try to finish getting the current context
            try {
                httpContext = listener.EndGetContext(ar);
            } catch(Exception e) {
                _log.WarnExceptionFormat(e, "unable to finish acquiring the request context, unable to handle request");
            }

            // start listening for next request
            if(!listener.IsListening) {
                _log.Debug("dropping out of request handler, since the listener is no longer listening");
                return;
            }
            try {
                listener.BeginGetContext(RequestHandler, listener);
            } catch(Exception e) {
                _log.WarnExceptionFormat(e, "unable to re-aquire context, dropping out of request handler");
                return;
            }

            // if we didn't succeed in ending the GetContext call, drop out
            if(httpContext == null) {
                return;
            }
            DreamMessage request = null;
            try {

                // finish listening for current context
                string[] prefixes = new string[listener.Prefixes.Count];
                listener.Prefixes.CopyTo(prefixes, 0);
                XUri requestUri = HttpUtil.FromHttpContext(httpContext);
                _log.DebugMethodCall("RequestHandler", httpContext.Request.HttpMethod, requestUri);

                // create request message
                request = new DreamMessage(DreamStatus.Ok, new DreamHeaders(httpContext.Request.Headers), MimeType.New(httpContext.Request.ContentType), httpContext.Request.ContentLength64, httpContext.Request.InputStream);
                DreamUtil.PrepareIncomingMessage(request, httpContext.Request.ContentEncoding, prefixes[0], httpContext.Request.RemoteEndPoint.ToString(), httpContext.Request.UserAgent);
                requestUri = requestUri.AuthorizeDreamInParams(request, _dreamInParamAuthtoken);

                // check if the request was forwarded through Apache mod_proxy
                string hostname = requestUri.GetParam(DreamInParam.HOST, null) ?? request.Headers.ForwardedHost ?? request.Headers.Host ?? requestUri.HostPort;
                activity = new ActivityState(_env, httpContext.Request.HttpMethod, httpContext.Request.Url.ToString(), hostname).Message;
                activity("RequestHandler");

                // process message
                _env.UpdateInfoMessage(_sourceExternal, null);
                string verb = httpContext.Request.HttpMethod;
                _env.SubmitRequestAsync(verb, requestUri, httpContext.User, request, new Result<DreamMessage>(TimeSpan.MaxValue))
                    .WhenDone(result => Coroutine.Invoke(ResponseHandler, request, result, httpContext, activity, new Result(TimeSpan.MaxValue)));
            } catch(Exception ex) {
                _log.ErrorExceptionMethodCall(ex, "RequestHandler");
                if(request != null) {
                    request.Close();
                }
                try {
                    DreamMessage response = DreamMessage.InternalError(ex);
                    httpContext.Response.StatusCode = (int)response.Status;
                    Stream stream = response.ToStream();
                    httpContext.Response.Headers.Clear();
                    foreach(KeyValuePair<string, string> pair in response.Headers) {
                        HttpUtil.AddHeader(httpContext.Response, pair.Key, pair.Value);
                    }
                    httpContext.Response.KeepAlive = false;
                    long size = response.ContentLength;
                    if(((size == -1) || (size > 0)) && (stream != Stream.Null)) {
                        CopyStream(delegate { }, stream, httpContext.Response.OutputStream, size, new Result<long>(DreamHostService.MAX_REQUEST_TIME)).Block();
                    }
                    httpContext.Response.OutputStream.Flush();
                } catch {
                    httpContext.Response.StatusCode = (int)DreamStatus.InternalError;
                }
                httpContext.Response.Close();
                if(activity != null) {
                    activity(null);
                }
            }
        }
开发者ID:heran,项目名称:DReAM,代码行数:81,代码来源:httptransport.cs

示例2: ResponseHandler

        private Yield ResponseHandler(DreamMessage request, Result<DreamMessage> response, HttpListenerContext httpContext, Action<string> activity, Result result)
        {
            DreamMessage item = null;
            request.Close();
            try {
                activity("begin ResponseHandler");
                item = response.HasException ? DreamMessage.InternalError(response.Exception) : response.Value;

                // set status
                _log.TraceMethodCall("ResponseHandler: Status", item.Status, httpContext.Request.HttpMethod, httpContext.Request.Url);
                httpContext.Response.StatusCode = (int)item.Status;

                // remove internal headers
                item.Headers.DreamTransport = null;
                item.Headers.DreamPublicUri = null;

                // add out-going headers
                if(item.Headers.Server == null) {
                    item.Headers.Server = ServerSignature;
                }

                // create stream for response (this will force the creation of the 'Content-Length' header as well)
                Stream stream = item.ToStream();

                // copy headers
                httpContext.Response.Headers.Clear();
                foreach(KeyValuePair<string, string> pair in item.Headers) {
                    _log.TraceMethodCall("SendHttpResponse: Header", pair.Key, pair.Value);
                    HttpUtil.AddHeader(httpContext.Response, pair.Key, pair.Value);
                }

                // add set-cookie headers to response
                if(item.HasCookies) {
                    foreach(DreamCookie cookie in item.Cookies) {
                        httpContext.Response.Headers.Add(DreamHeaders.SET_COOKIE, cookie.ToSetCookieHeader());
                    }
                }

                // disable keep alive behavior
                httpContext.Response.KeepAlive = false;

                // send message stream
                long size = item.ContentLength;
                if(((size == -1) || (size > 0)) && (stream != Stream.Null)) {
                    activity(string.Format("pre CopyStream ({0} bytes)", size));
                    yield return CopyStream(activity, stream, httpContext.Response.OutputStream, size, new Result<long>(DreamHostService.MAX_REQUEST_TIME)).CatchAndLog(_log);
                    activity("post CopyStream");
                }
                activity("pre Flush");
                httpContext.Response.OutputStream.Flush();
                activity("post Flush");
                result.Return();
                activity("end ResponseHandler");
            } finally {
                activity(null);
                if(item != null) {
                    item.Close();
                }
                httpContext.Response.Close();
            }
        }
开发者ID:heran,项目名称:DReAM,代码行数:61,代码来源:httptransport.cs

示例3: DreamMessage

        //--- Methods ---
        void IHttpHandler.ProcessRequest(HttpContext httpContext)
        {
            var activity = _env.CreateActivityDescription();
            DreamMessage request = null;
            try {
                string verb = httpContext.Request.HttpMethod;
                XUri requestUri = HttpUtil.FromHttpContext(httpContext);
                activity.Description = string.Format("Incoming: {0} {1}", verb, requestUri);
                _log.DebugMethodCall("ProcessRequest", verb, requestUri);

                // create request message
                request = new DreamMessage(DreamStatus.Ok, new DreamHeaders(httpContext.Request.Headers), MimeType.New(httpContext.Request.ContentType), httpContext.Request.ContentLength, httpContext.Request.InputStream);
                DreamUtil.PrepareIncomingMessage(request, httpContext.Request.ContentEncoding, string.Format("{0}://{1}{2}", httpContext.Request.Url.Scheme, httpContext.Request.Url.Authority, httpContext.Request.ApplicationPath), httpContext.Request.UserHostAddress, httpContext.Request.UserAgent);
                requestUri = requestUri.AuthorizeDreamInParams(request, _handler.AppConfig.DreamInParamAuthToken);

                // TODO (arnec): should this happen before PrepareIncomingMessage?
                request.Headers.DreamTransport = _handler.GetRequestBaseUri(httpContext.Request).ToString();

                // process message
                var response = _env.SubmitRequestAsync(verb, requestUri, httpContext.User, request, new Result<DreamMessage>(TimeSpan.MaxValue)).Block();
                request.Close();
                if(response.HasException) {
                    _log.ErrorExceptionFormat(response.Exception, "Request Failed [{0}:{1}]: {2}", verb, requestUri.Path, response.Exception.Message);
                }
                var item = response.HasException ? DreamMessage.InternalError(response.Exception) : response.Value;

                // set status
                if(_log.IsDebugEnabled) {
                    _log.DebugMethodCall("ProcessRequest[Status]", item.Status, String.Format("{0}{1}", httpContext.Request.Url.GetLeftPart(UriPartial.Authority), httpContext.Request.RawUrl).Replace("/index.aspx", "/"));
                }
                httpContext.Response.StatusCode = (int)item.Status;

                // remove internal headers
                item.Headers.DreamTransport = null;
                item.Headers.DreamPublicUri = null;

                // create stream for response (this will force the creation of the 'Content-Length' header as well)
                Stream stream = item.ToStream();

                // copy headers
                foreach(KeyValuePair<string, string> pair in item.Headers) {
                    _log.TraceMethodCall("ProcessRequest[Header]", pair.Key, pair.Value);
                    httpContext.Response.AppendHeader(pair.Key, pair.Value);
                }

                // add set-cookie headers to response
                if(item.HasCookies) {
                    foreach(DreamCookie cookie in item.Cookies) {
                        httpContext.Response.AppendHeader(DreamHeaders.SET_COOKIE, cookie.ToSetCookieHeader());
                    }
                }

                // send message stream
                long size = item.ContentLength;
                if(((size == -1) || (size > 0)) && (stream != Stream.Null)) {
                    stream.CopyToStream(httpContext.Response.OutputStream, size, new Result<long>(TimeSpan.MaxValue)).Wait();
                }
                item.Close();
            } catch(Exception ex) {
                _log.ErrorExceptionMethodCall(ex, "CommonRequestHandler");
                if(request != null) {
                    request.Close();
                }
                if(httpContext != null) {
                    httpContext.Response.Close();
                }
            } finally {
                activity.Dispose();
            }
        }
开发者ID:nataren,项目名称:DReAM,代码行数:71,代码来源:RoutedHttpHandler.cs


注:本文中的DreamMessage.Close方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。