/// <summary> /// Configures the response to redirect the client to the specified URL. /// </summary> /// <param name="url">The URL that the client should use to locate the requested resource.</param> public void Redirect(string url) { // Get the redirection code. Nequeo.Net.Http.Common.HttpStatusCode statusCode = Nequeo.Net.Http.Utility.GetStatusCode(302); if (statusCode != null) { // Assign the status code. StatusCode = statusCode.Code; StatusDescription = statusCode.Description; } // Add the location header used for redirection. AddHeader("Location", url); }
/// <summary> /// Write the response status to the stream. /// </summary> public void WriteWebSocketHeaders() { byte[] buffer = null; string data = ""; // Set the upgrade value. Upgrade = "websocket"; ProtocolVersion = "HTTP/1.1"; // Get the status code. Nequeo.Net.Http.Common.HttpStatusCode statusCode = Nequeo.Net.Http.Utility.GetStatusCode(101); StatusCode = statusCode.Code; StatusDescription = statusCode.Description; // If the SecWebSocketProtocol has been specified. if (!String.IsNullOrEmpty(SecWebSocketProtocol)) { AddHeader("Sec-WebSocket-Protocol", SecWebSocketProtocol); } // If the SecWebSocketAccept has been specified. if (!String.IsNullOrEmpty(SecWebSocketAccept)) { AddHeader("Sec-WebSocket-Accept", SecWebSocketAccept); } // If the SecWebSocketExtensions has been specified. if (!String.IsNullOrEmpty(SecWebSocketExtensions)) { AddHeader("Sec-WebSocket-Extensions", SecWebSocketExtensions); } // Write the response status to the stream. WriteWebResponseHeaders(false); // Send the header end space. data = _deli; buffer = Encoding.Default.GetBytes(data); Write(buffer, 0, buffer.Length); }
/// <summary> /// Check to see if the request is a web socket. /// </summary> /// <param name="webSocketContext">The current web socket context.</param> private void CheckIfWebSocketRequest(Nequeo.Net.WebSockets.WebSocketContext webSocketContext) { // Make sure all handshaking has complete before continuing. if (!webSocketContext.HandshakeComplete) { // Get the request and response. Nequeo.Net.WebSockets.WebSocketRequest request = webSocketContext.WebSocketRequest; Nequeo.Net.WebSockets.WebSocketResponse response = webSocketContext.WebSocketResponse; Nequeo.Net.Http.Common.HttpStatusCode statusCode = null; // If not a web socket request. if (!webSocketContext.WebSocketRequest.IsWebSocketRequest) { // Get the bad request. statusCode = Nequeo.Net.Http.Utility.GetStatusCode(400); // Send bad request. response.Server = base.Name; response.ContentLength = 0; response.StatusCode = statusCode.Code; response.StatusDescription = statusCode.Description; response.ProtocolVersion = request.ProtocolVersion; } else { // Get the internal error. statusCode = Nequeo.Net.Http.Utility.GetStatusCode(500); // Send internal error. response.Server = base.Name; response.ContentLength = 0; response.StatusCode = statusCode.Code; response.StatusDescription = statusCode.Description; response.ProtocolVersion = request.ProtocolVersion; } // Not valid. throw new Exception("Not a valid WebSocket request."); } }
/// <summary> /// Http context. /// </summary> /// <param name="sender"></param> /// <param name="context"></param> private void HttpContext(object sender, Nequeo.Net.Http.HttpContext context) { System.IO.MemoryStream unzip = null; System.IO.MemoryStream zip = null; Nequeo.Net.Http.HttpRequest request = null; Nequeo.Net.Http.HttpResponse response = null; byte[] responseData = null; byte[] requestBuffer = null; try { // Get the context data. request = context.HttpRequest; response = context.HttpResponse; // if request context exists. if (request.ContentLength > 0) { // Read the data. requestBuffer = new byte[(int)request.ContentLength]; request.Read(requestBuffer, 0, (int)request.ContentLength); } // Set the response. response.Server = _httpServer.Name; response.StatusCode = 200; response.StatusDescription = "OK"; response.KeepAlive = false; response.ProtocolVersion = request.ProtocolVersion; // Get the local path. string pathBase = _basePath.TrimEnd(new char[] { '\\' }) + "\\"; string pathLocal = request.Url.LocalPath.Replace("/", "\\").TrimStart(new char[] { '\\' }); string path = pathBase + pathLocal; // If the path not exists. if (!System.IO.File.Exists(path)) { // default.htm. path = pathBase + pathLocal.TrimEnd(new char[] { '\\' }) + "\\default.htm"; if (!System.IO.File.Exists(path)) { // default.html. path = pathBase + pathLocal.TrimEnd(new char[] { '\\' }) + "\\default.html"; if (!System.IO.File.Exists(path)) { // index.htm. path = pathBase + pathLocal.TrimEnd(new char[] { '\\' }) + "\\index.htm"; if (!System.IO.File.Exists(path)) { // index.html. path = pathBase + pathLocal.TrimEnd(new char[] { '\\' }) + "\\index.html"; if (!System.IO.File.Exists(path)) { throw new Exception("Resource can not be found."); } } } } } // Set the content type. response.ContentType = Data.MimeType.GetMimeType(System.IO.Path.GetExtension(path)); // If in cache then get the file data. if (Data.Helper.FileCache.ContainsKey(path)) { // If in the cache then check to see if the file has changed. FileInfo fileInfo = new System.IO.FileInfo(path); // If the file has changed, then reload the file. if ((fileInfo.Length != Data.Helper.FileCache.GetFileSize(path)) || Data.Helper.FileCache.HasBeenModified(path, fileInfo.LastWriteTime)) { // Load the static file from base. responseData = System.IO.File.ReadAllBytes(path); // Set the new file data. Data.Helper.FileCache.Set(path, responseData); Data.Helper.FileCache.SetModifiedTime(path, fileInfo.LastWriteTime); } else { // Load from the cache. responseData = Data.Helper.FileCache.Get(path); } } else { // Load the static file from base. responseData = System.IO.File.ReadAllBytes(path); // If cache size has not been reached. if (Data.Helper.FileCache.GetCacheSize() <= Nequeo.Net.Properties.Settings.Default.HttpStaticMaxCacheSize) { // Add the file data to the cache. Data.Helper.FileCache.Add(path, responseData); Data.Helper.FileCache.SetModifiedTime(path, DateTime.Now); } } } catch { try { if (response != null) { response.ContentType = "text/html"; response.ContentLength = 0; response.StatusCode = 400; response.StatusDescription = "Bad Request"; Nequeo.Net.Http.Common.HttpStatusCode statusCode = Nequeo.Net.Http.Utility.GetStatusCode(400); responseData = Encoding.Default.GetBytes("<html>" + statusCode.HtmlHead + statusCode.HtmlBody + "</html>"); } } catch { } } try { // If requesting gzip data. if (request.AcceptEncoding != null && request.AcceptEncoding.Contains("gzip")) { // Create the compression. unzip = new System.IO.MemoryStream(responseData); zip = new System.IO.MemoryStream(); Nequeo.IO.Compression.GZipStream.Compress(unzip, zip); // Send the data. response.ContentLength = zip.Length; response.ContentEncoding = "gzip"; response.WriteHttpHeaders(); response.Write(zip.ToArray(), 0, (int)zip.Length); } else { response.ContentLength = responseData.Length; response.WriteHttpHeaders(); response.Write(responseData, 0, (int)responseData.Length); } response.Flush(); } catch { } finally { if (unzip != null) { unzip.Dispose(); } if (zip != null) { zip.Dispose(); } responseData = null; requestBuffer = null; unzip = null; zip = null; } }