TsgcWebSocketServer_HTTPAPI이벤트 › OnHTTPRequest

OnHTTPRequest 이벤트

서버가 HTTP 요청을 수신하여 애플리케이션이 응답을 빌드할 수 있을 때 발생합니다.

구문

__property TsgcWSHTTPAPIRequestEvent OnHTTPRequest;
// typedef void __fastcall (__closure *TsgcWSHTTPAPIRequestEvent)(TsgcWSConnection_HTTPAPI * aConnection, const THttpServerRequest aRequestInfo, THttpServerResponse &aResponseInfo);

기본값

설명

OnHTTPRequest는 HTTP.SYS가 서버로 전달하는 모든 HTTP 요청(GET, POST, PUT, DELETE, HEAD...)의 주요 진입점입니다. aRequestInfo는 디코딩된 요청(Document, Method, Headers, ContentType, Content, QueryParams, Cookies, ContentLength, AuthExists/AuthUsername/AuthPassword, Stream)을 노출하며, aResponseInfo는 애플리케이션이 다시 보내려는 응답을 수집합니다. 필요에 따라 ResponseNo, ContentText(또는 바이너리/파일 응답의 경우 ContentStream / FileName), ContentType, CustomHeaders, Date/Expires/LastModified 및 CacheControl을 설정하십시오. 기본 HTTP 200을 반환하려면 aResponseInfo를 그대로 두고, 오류 조건을 알리려면 ResponseNo를 404/500/...로 지정하십시오. 이 이벤트는 WebSocket 업그레이드 요청에 대해서는 발생하지 않으며, 그러한 요청은 대신 OnConnect / OnHandshake를 통해 라우팅됩니다.

예제


void __fastcall OnHTTPRequest(TsgcWSConnection_HTTPAPI *aConnection,
  const THttpServerRequest *aRequestInfo,
  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;
}

이벤트로 돌아가기