//--- Messages --- internal void Message(string message) { lock (this) { if (message != null) { if (_messages != null) { _messages.Enqueue(message); if (_messages.Count > 10) { _messages.Dequeue(); } _env.AddActivityDescription(_key, string.Format("Outgoing: {0} {1} [{2}]", _verb, _uri, string.Join(" -> ", _messages.ToArray()))); } } else { _messages = null; _env.RemoveActivityDescription(_key); } } }
//--- Methods --- void IHttpHandler.ProcessRequest(HttpContext httpContext) { var key = new object(); DreamMessage request = null; try { string verb = httpContext.Request.HttpMethod; XUri requestUri = HttpUtil.FromHttpContext(httpContext); _env.AddActivityDescription(key, 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); // 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(); 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.CopyTo(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 { _env.RemoveActivityDescription(key); } }
/// <summary> /// Add a host activity. /// </summary> /// <param name="key">Activity key.</param> /// <param name="description">Description of activity.</param> public void AddActivityDescription(object key, string description) { _env.AddActivityDescription(key, description); }