Passa al contenuto principale

Class WebServer

Namespace: TecnoPack.WebServer
Assembly: CO0002_Templ_FlowpackHMI_V00_26_M.dll

Lightweight embedded web server for serving static files, API endpoints, and dashboard content.

public class WebServer

Inheritance

object ← WebServer

Remarks

This class uses to handle HTTP requests and provides functionality to:

  • Serve static content from a project-relative folder.
  • Register dynamic routes from web route classes.
  • Enable SSL using a self-signed certificate.
  • Start and stop the server in a background long-running task.
  • Patch MIME types for proper browser handling of JavaScript files.

Properties

ContentFileAccess

The FileAccess value to use when accessing files within a content route via a FileStream. Default is FileAccess.Read.

public FileAccess ContentFileAccess { get; set; }

Property Value

FileAccess

ContentFileMode

The FileMode value to use when accessing files within a content route via a FileStream. Default is FileMode.Open.

public FileMode ContentFileMode { get; set; }

Property Value

FileMode

ContentFileShare

The FileShare value to use when accessing files within a content route via a FileStream. Default is FileShare.Read.

public FileShare ContentFileShare { get; set; }

Property Value

FileShare

Session

The current session object exposed to routes.

public static Session Session { get; }

Property Value

Session

Sessions

The current sessions object exposed to routes.

public static Sessions Sessions { get; }

Property Value

Sessions

Methods

FileHandler(HttpContextBase)

Handles HTTP requests for static files.

public Task FileHandler(HttpContextBase ctx)

Parameters

ctx HttpContextBase

The HTTP context containing the request and response objects. Must not be null.

Returns

Task

A representing the asynchronous operation.

Remarks

This method serves files from a predefined static directory (e.g., WebServerStaticFiles). It supports GET and HEAD HTTP methods. All other methods result in a 500 Internal Server Error.

The file path requested by the client is first normalized and decoded, including handling URL-encoded characters and converting any '+' characters to spaces for compatibility. Path traversal outside the base directory is blocked.

File resolution:

  • If the requested path matches a file directly, that file is returned.
  • If the path is a directory or does not include an extension, a set of default file names (e.g., index.html, default.htm, home.html) is probed sequentially within the directory.

Response behavior:

  • If the file is found and the method is GET, the file content is streamed asynchronously with appropriate content type and content length headers.
  • If the method is HEAD, only the headers are returned without content.
  • If the file is not found, a 404 Not Found status is returned.
  • If the HTTP method is unsupported, a 500 Internal Server Error status is returned.

Performance considerations:

  • File streams are opened with and for optimized asynchronous reading.
  • Default buffer size is set to 64 KB to balance memory usage and I/O throughput.

Security considerations:

  • Paths are resolved safely to prevent directory traversal outside the static directory.
  • Only files under the configured staticDirPath are served.

Exceptions

ArgumentNullException

Thrown if ctx or its Request or Response properties are null.

PatchMimeTypes()

Corrects default MIME type mappings.

public static void PatchMimeTypes()

Remarks

Specifically, updates ".js" files to "application/javascript" to ensure proper browser handling. Can be extended to patch other MIME types as needed.

SetSession(Session)

Sets the current session for the web server.

public static void SetSession(Session session)

Parameters

session Session

The object representing the active user session.

Start(IUANode, Session)

Starts the web server, registers routes, serves static content, and runs in a background task.

public void Start(IUANode logicNode, Session session)

Parameters

logicNode IUANode

The used for context and session management.

session Session

The active to associate with server requests.

Stop()

Stops the web server and disposes of resources.

public void Stop()
  • Remarks
  • Properties
    • ContentFileAccess
    • ContentFileMode
    • ContentFileShare
    • Session
    • Sessions
  • Methods
    • FileHandler(HttpContextBase)
    • PatchMimeTypes()
    • SetSession(Session)
    • Start(IUANode, Session)
    • Stop()