csp フォルダーにないサーブレットをリクエストすると、「404 Not Found」という応答が表示されます。
見つかりません
要求された URL はこのサーバーで見つかりませんでした。
カスタム 404 ページを作成するために、サーブレットが存在するかどうかを確認する方法はありますか?
csp フォルダーにないサーブレットをリクエストすると、「404 Not Found」という応答が表示されます。
見つかりません
要求された URL はこのサーバーで見つかりませんでした。
カスタム 404 ページを作成するために、サーブレットが存在するかどうかを確認する方法はありますか?
ギルの言うとおりだ。HDL_HTTP_ERRORS を使用して、HTTP エラーをインターセプトできます。より明確にするために、404 エラーをカスタム エラー メッセージに置き換える接続ハンドラーの例を次に示します。
#include "gwan.h"
int init(int argc, char *argv[])
{
u32 *states = (u32*)get_env(argv, US_HANDLER_STATES);
*states = (1 << HDL_HTTP_ERRORS);
return 0;
}
int main(int argc, char *argv[])
{
if((long)argv[0] != HDL_HTTP_ERRORS)
return 255; // Continue if not an error
// get the HTTP reply code
int *status = (int*)get_env(argv, HTTP_CODE);
if(!status || *status != 404)
return 255; // Continue if not a 404 error
static char custom_err[] =
"<!DOCTYPE HTML><html><head><title>404 Not Found</title><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"><link href=\"/imgs/errors.css\" rel=\"stylesheet\" type=\"text/css\"></head>"
"<body><h1>Not Found</h1>"
"<p>This is a custom 404 not found error. What makes you think that this link exist?!!</p></body></html>";
static char header[] =
"HTTP/1.1 %s\r\n"
"Server: G-WAN\r\n"
"Date: %s\r\n"
"Content-Type: text/html; charset=UTF-8\r\n"
"Content-Length: %u\r\n\r\n";
int len = sizeof(custom_err)-1;
char *date = (char*)get_env(argv, SERVER_DATE);
// Set our http reply headers
build_headers(argv, header,
http_status(*status),
date, // current server date
len); // Reply length
// Set our reply using our custom error
set_reply(argv, custom_err, len, *status);
return 2; // End request then send reply
}
void clean(int argc, char *argv[]) {}
サーブレットから 404 エラーを返す場合は注意してください。あなたがすることを確認してください
xbuf_empty(get_reply(argv));
応答バッファの内容を空にします。応答バッファにコンテンツがある場合、HDL_HTTP_ERRORS に到達しません。応答バッファにあるものは何でも応答します。
Content-Type ハンドラーと Connections ハンドラーはどちらも、リソースを提供する前にリソースが存在するかどうかを確認できます。
ただし、接続ハンドラーHDL_HTTP_ERRORS
の状態により、HTTP エラーをインターセプトして、G-WAN によって生成されたデフォルトの応答を変更できます。ドキュメント化された G-WAN API Handler Statesで定義されています。
それはおそらくあなたが探しているものです。