/// <summary> /// Closes the socket reader after transmitting an error message /// </summary> private void Close(IWebResults webResults) { WebConnection.SendResults(webResults); Close(); }
/// <summary> /// Generates the header /// </summary> public override void SendResults(IWebResults webResults) { using (TimedLock.Lock(_Connected)) { if (!_Connected.Value) throw new ResultsAlreadySent("Can not send results twice for the same web connection"); _Connected = false; } StringBuilder header = new StringBuilder(); // Convert the status enum into something that a web browser can handle string statusString = webResults.Status.ToString().Substring(1); statusString = statusString.Replace('_', ' '); header.AppendFormat("HTTP/1.1 {0}\r\n", statusString); Dictionary<string, string> headers = new Dictionary<string, string>(webResults.Headers); headers["Server"] = WebServer.ServerType; foreach (string headerKey in headers.Keys) header.AppendFormat("{0}: {1}\r\n", headerKey, headers[headerKey]); // set cookies string formattedCookies = FormatCookiesToSend(); header.Append(formattedCookies); Stream webResultStream = webResults.ResultsAsStream; header.AppendFormat("Content-Length: {0}\r\n", webResultStream.Length); if (log.IsDebugEnabled) log.Debug("Results sent to browser:\n" + header.ToString()); header.Append("\r\n"); MultiStream stream = new MultiStream(); stream.AddStream(new MemoryStream(Encoding.UTF8.GetBytes(header.ToString()))); stream.AddStream(webResultStream); SendToBrowser(stream); }
public HasWebResultsException(string message, Exception inner, IWebResults webResults) : base(message, inner) { _WebResults = webResults; }
public override void SendResults(IWebResults webResults) { Response.KeepAlive = WebServer.KeepAlive; Response.StatusCode = (int)webResults.Status; foreach (KeyValuePair<string, string> header in webResults.Headers) Response.Headers[header.Key] = header.Value; if (null != _Session) { // Make sure that the session cookie is sent... // It is set last to make sure that all changes to the session are persisted CookieToSet sessionCookie = new CookieToSet("SESSION"); sessionCookie.Path = "/"; sessionCookie.Value = _Session.SessionId.ToString(); if (_Session.KeepAlive) { TimeSpan maxAge = _Session.MaxAge; sessionCookie.Expires = DateTime.UtcNow + maxAge; sessionCookie.Value = sessionCookie.Value + ", " + maxAge.TotalDays.ToString(CultureInfo.InvariantCulture); } CookiesToSet.Add(sessionCookie); } // Use the HttpListener version of this server is discouraged. It just doesn't set cookies in a reliable manner CookieCollection cookies = new CookieCollection(); foreach (CookieToSet cookie in CookiesToSet) { Cookie nCookie = new Cookie( HTTPStringFunctions.EncodeRequestParametersForBrowser(cookie.Name), HTTPStringFunctions.EncodeRequestParametersForBrowser(cookie.Value)); cookies.Add(nCookie); if (null != cookie.Expires) nCookie.Expires = cookie.Expires.Value; nCookie.Secure = cookie.Secure; nCookie.Path = cookie.Path; nCookie.Version = 2; } Response.Cookies = cookies; Response.Headers["Server"] = WebServer.ServerType; Response.ContentType = webResults.ContentType; Response.ContentLength64 = webResults.ResultsAsStream.Length; // TODO: Move these to some kind of a writer thread byte[] buffer = new byte[webResults.ResultsAsStream.Length]; webResults.ResultsAsStream.Read(buffer, 0, buffer.Length); Response.OutputStream.Write(buffer, 0, buffer.Length); Response.OutputStream.Flush(); Response.Close(); _Connected = false; }
public WebResultsOverrideException(IWebResults webResults, string message, Exception inner) : base(message, inner) { _WebResults = webResults; }
public HasWebResultsException(string message, IWebResults webResults) : base(message) { _WebResults = webResults; }
public WebResultsOverrideException(IWebResults webResults) : base(webResults.ResultsAsString) { _WebResults = webResults; }
public WebResultsOverrideException(IWebResults webResults, Exception inner) : base(webResults.ResultsAsString, inner) { _WebResults = webResults; }
/// <summary> /// Sends the results to the client. Be careful, calling this multiple times can have undesired results! /// </summary> public abstract void SendResults(IWebResults webResults);
/// <summary> /// Closes the socket reader after transmitting an error message /// </summary> internal void Close(IWebResults webResults) { if (null != WebConnection) WebConnection.SendResults(webResults); Close(); }
/// <summary> /// Converts an IWebResults to something that can be passed back to Javascript /// </summary> /// <param name="webResults"></param> /// <returns></returns> private static Dictionary<string, object> ConvertWebResultToJavascript(IWebResults webResults) { // Only return on a successful status, otherwise, throw an exception if ((webResults.Status < Status._200_OK) || (webResults.Status > Status._207_Multi_Status)) throw new WebResultsOverrideException(webResults); Dictionary<string, object> toReturn = new Dictionary<string, object>(); toReturn["Status"] = (int)webResults.Status; toReturn["Content"] = webResults.ResultsAsString; toReturn["Headers"] = webResults.Headers; return toReturn; }
public override IWebResults GenerateResultsForClient() { AsyncWebResults = base.GenerateResultsForClient(); if (null == AsyncWebResultsPulser) using (TimedLock.Lock(AsyncWebResultsPulser)) Monitor.Wait(AsyncWebResultsPulser); return AsyncWebResults; }
public override void SendResults(IWebResults webResults) { AsyncWebResults = webResults; using (TimedLock.Lock(AsyncWebResultsPulser)) Monitor.Pulse(AsyncWebResultsPulser); }