Hello, DSAPI!

DSAPI(Domino WebServer API)は、DominoサーバのHTTPタスクにアドインするためのAPI。 今回は、これでHelloアプリを作る。

することは2つ。 1つは、インストールに成功したら、DominoのログにHello, DSAPI Init!の表示をする。 もう1つは、ブラウザから/helloとしたらHellp. DSAPI Filter!の表示をする。

#include <dsapi.h>
#include <string>

extern "C" __declspec(dllexport)
unsigned int FilterInit(FilterInitData *pInitData)
{
  pInitData->appFilterVersion = kInterfaceVersion;
  pInitData->eventFlags = kFilterParsedRequest;
  strcpy_s(pInitData->filterDesc, "Hello, DSAPI Init!");
  return kFilterHandledEvent;
}

extern "C" __declspec(dllexport)
unsigned int HttpFilterProc(FilterContext* ctx, unsigned int eventType, void*)
{
  try {
    unsigned int errId = 0;

    switch (eventType) {
    case kFilterParsedRequest:
      FilterRequest req;
      ctx->GetRequest(ctx, &req, &errId);
      if (errId != 0) throw errId;

      if (req.method == kRequestGET
          && std::strncmp(req.URL, "/hello", 6) == 0) {
        char content[] = "HTTP/1.1 200 OK\n"
                         "Content-Type: text/plain; charset=utf8\n"
                         "\n"
                         "Hello, DSAPI Filter!";
        ctx->WriteClient(ctx, content, strlen(content), 0, &errId);
        if (errId != 0) throw errId;
        return kFilterHandledRequest;
      }
      break;

    default:
      return kFilterNotHandled;
    }
  }
  catch (...) {
    return kFilterError;
  }
}
  1. コンパイルして、DLLを作成する。例えば、helloというアドインであれば、nhello.dllという名前でDLLを作る。頭文字のnWindows用のプリフィックス
  2. nhello.dllをDominoサーバのプログラムディレクトリにコピーする(他に必要なモジュールも同様にプログラムディレクトリに置く)。
  3. サーバ文書かWeb設定のDSAPI欄に、helloと記述する(n.dllは書かない)。
  4. Dominoサーバを再起動する。

すると、Dominoコンソールには以下のように表示される。

f:id:takahide-kondoh:20190708222937p:plain

続いて、ブラウザから次のようなリクエストを送る。

http://localhost/hello

すると、以下のようなレスポンスが返ってくる。

f:id:takahide-kondoh:20190708223147p:plain