TsgcWebSocketServer_HTTPAPIEvents › OnHTTPRequest

OnHTTPRequest Event

Fires when the server receives an HTTP request so the application can build the response.

Syntax

public event TsgcWSHTTPAPIRequestEventHandler OnHTTPRequest;
// delegate void TsgcWSHTTPAPIRequestEventHandler(TsgcWSConnection_HTTPAPI aConnection, THttpServerRequest aRequestInfo, out THttpServerResponse aResponseInfo)

Default Value

Remarks

OnHTTPRequest is the main entry point for every HTTP request that HTTP.SYS delivers to the server (GET, POST, PUT, DELETE, HEAD...). aRequestInfo exposes the decoded request (Document, Method, Headers, ContentType, Content, QueryParams, Cookies, ContentLength, AuthExists/AuthUsername/AuthPassword, Stream) and aResponseInfo collects the response the application wants to send back: set ResponseNo, ContentText (or ContentStream / FileName for binary/file responses), ContentType, CustomHeaders, Date/Expires/LastModified and CacheControl as needed. Leave aResponseInfo untouched to return the default HTTP 200, or assign ResponseNo to 404/500/... to signal error conditions. This event does not fire for WebSocket upgrade requests; those are routed through OnConnect / OnHandshake instead.

Example


void OnHTTPRequest(TsgcWSConnection_HTTPAPI aConnection,
  THttpServerRequest aRequestInfo,
  ref THttpServerResponse aResponseInfo)
{
  if (aRequestInfo.Method == "GET")
  {
    if (aRequestInfo.Document == "/test.html")
    {
      aResponseInfo.ResponseNo = 200;
      aResponseInfo.ContentText = "OK";
      aResponseInfo.ContentType = "text/html; charset=UTF-8";
    }
    else
      aResponseInfo.ResponseNo = 404;
  }
  else
    aResponseInfo.ResponseNo = 500;
}

Back to Events