/// <summary> /// On received action handler. /// </summary> /// <param name="context">The current client context.</param> private void OnReceivedActionHandler(Nequeo.Net.Provider.Context context) { try { // Create the http context and set the headers. Nequeo.Net.Http.HttpContext httpContext = CreateHttpContext(context); // Get the headers from the stream and assign the request data. bool headersExist = Nequeo.Net.Http.Utility.SetRequestHeaders(httpContext, base.HeaderTimeout, base.MaximumReadLength); // If the event has been assigned. if (OnHttpContext != null) { OnHttpContext(this, httpContext); } // Save the web context state objects. SaveWebContext(context, httpContext); } catch (Exception) { // Close the connection and release all resources used for communication. context.Close(); } }
/// <summary> /// Http member stream context. /// </summary> /// <param name="context">The http context.</param> public HttpMember(Nequeo.Net.Http.HttpContext context) { _context = context; _output = _context.HttpResponse.Output; // Set the initial timeout time. _initialTimeOutTime = DateTime.Now; }
/// <summary> /// /// </summary> /// <param name="context"></param> protected override void OnHttpContext(Nequeo.Net.Http.HttpContext context) { // Get the headers from the stream and assign the request data. bool headersExist = Nequeo.Net.Http.Utility.SetRequestHeaders(context, 30000, 10000); context.HttpResponse.ContentLength = 5; context.HttpResponse.ContentType = "text/plain"; context.HttpResponse.StatusCode = 200; context.HttpResponse.StatusDescription = "OK"; context.HttpResponse.WriteHttpHeaders(); context.HttpResponse.Write("Hello"); }
/// <summary> /// Create the http context. /// </summary> /// <param name="context">The current client context.</param> /// <returns>The http context.</returns> private Nequeo.Net.Http.HttpContext CreateHttpContext(Nequeo.Net.Provider.Context context) { // Get the underlying web context. Nequeo.Net.WebContext webContext = CreateWebContext(context); // Create the new http context from the web context. Nequeo.Net.Http.HttpContext httpContext = Nequeo.Net.Http.HttpContext.CreateFrom(webContext); // Assign response and request data. httpContext.HttpResponse = new HttpResponse(); httpContext.HttpRequest = new HttpRequest(); httpContext.HttpResponse.Output = webContext.WebResponse.Output; httpContext.HttpRequest.Input = webContext.WebRequest.Input; // Return the request context. return(httpContext); }
/// <summary> /// On received. /// </summary> /// <param name="context">The base context.</param> private void OnReceived(Context context) { // Create the context. WebContext webContext = base.CreateWebContext(context); // Create the new http context from the web context. Nequeo.Net.Http.HttpContext httpContext = Nequeo.Net.Http.HttpContext.CreateFrom(webContext); // Assign response and request data. httpContext.HttpResponse = new HttpResponse(); httpContext.HttpRequest = new HttpRequest(); httpContext.HttpResponse.Output = webContext.WebResponse.Output; httpContext.HttpRequest.Input = webContext.WebRequest.Input; // Pass the http context. OnHttpContext(httpContext); }
/// <summary> /// Create the http context from the web context. /// </summary> /// <param name="webContext">The web context to create from.</param> /// <returns>The http server context.</returns> public static HttpContext CreateFrom(Nequeo.Net.WebContext webContext) { Nequeo.Net.Http.HttpContext httpContext = new Nequeo.Net.Http.HttpContext(); httpContext.Context = webContext.Context; httpContext.IsStartOfConnection = webContext.IsStartOfConnection; httpContext.IsAuthenticated = webContext.IsAuthenticated; httpContext.IsSecureConnection = webContext.IsSecureConnection; httpContext.Name = webContext.Name; httpContext.NumberOfClients = webContext.NumberOfClients; httpContext.Port = webContext.Port; httpContext.RemoteEndPoint = webContext.RemoteEndPoint; httpContext.ServerEndPoint = webContext.ServerEndPoint; httpContext.ServiceName = webContext.ServiceName; httpContext.UniqueIdentifier = webContext.UniqueIdentifier; httpContext.ConnectionID = webContext.ConnectionID; httpContext.SessionID = webContext.SessionID; httpContext.User = webContext.User; httpContext.SocketState = webContext.SocketState; httpContext.IsAsyncMode = webContext.IsAsyncMode; return(httpContext); }
/// <summary> /// On received action handler. /// </summary> /// <param name="context">The current client context.</param> private void OnReceivedActionHandler(Nequeo.Net.Provider.Context context) { try { // Store data until all headers have been read. if (Nequeo.Net.Utility.IsParse2CRLF(context.Request.Input, context.RequestBufferStore, _maxHeaderBufferStore)) { // Create the http context and set the headers. Nequeo.Net.Http.HttpContext httpContext = CreateHttpContext(context); // Get the headers from the stream and assign the request data. bool headersExist = Nequeo.Net.Http.Utility.SetRequestHeaders(httpContext, base.HeaderTimeout, base.MaximumReadLength, context.RequestBufferStore); // If the event has been assigned. if (OnHttpContext != null) { OnHttpContext(this, httpContext); } // Save the web context state objects. SaveWebContext(context, httpContext); } // If the maximum request buffer store has been reached then close the connection. if (context.RequestBufferStore.Length > _maxHeaderBufferStore) { throw new Exception("Maximum request buffer store has been reached."); } } catch (Exception) { // Close the connection and release all resources used for communication. context.Close(); } }
/// <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; } }