vessel/web/srv.c
Arija A. e7f57f8526
impl: Tempfiles and ByteStreams
Signed-off-by: Arija A. <ari@ari.lt>
2025-06-14 20:57:28 +03:00

88 lines
3.6 KiB
C

#include "include/conf.h"
#include "include/srv.h"
#include "include/http.h"
size_t vw_srv_redirect(vw_HTTPRequest *req, uint16_t code, const char *location) {
if (!req || !location) {
return 0;
}
size_t total_written = 0;
size_t now_written = 0;
VS_IO_WITH_TEST(
vw_HTTPRequest_res_begin(req, code), now_written, VS_STREAM_ERROR, total_written);
VS_IO_WITH_TEST(vw_HTTPRequest_res_header(req, "content-type", "text/html"),
now_written,
VS_STREAM_ERROR,
total_written);
VS_IO_WITH_TEST(vw_HTTPRequest_res_header(req, "location", location),
now_written,
VS_STREAM_ERROR,
total_written);
/* clang-format off */
VS_IO_WITH_TEST(vw_HTTPRequest_writef(req,
"<!DOCTYPE html>" VW_HTTP_CRLF
"<html lang=\"en\">" VW_HTTP_CRLF
" <head>" VW_HTTP_CRLF
" <meta charset=\"UTF-8\" />" VW_HTTP_CRLF
" <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\" />" VW_HTTP_CRLF
" <meta http-equiv=\"refresh\" content=\"0; URL=%s\" />" VW_HTTP_CRLF
" <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" />" VW_HTTP_CRLF
" " VW_SRV_LICENSE VW_HTTP_CRLF
" <title>You are being redirected...</title>" VW_HTTP_CRLF
" </head>" VW_HTTP_CRLF
" <body aria-live=\"polite\">" VW_HTTP_CRLF
" <h1>You are being redirected...</h1>" VW_HTTP_CRLF
" <p>Click the following link if you not being <a href=\"%s\">redirected to %s</a> and are seeing this page.</p>" VW_HTTP_CRLF
" </body>" VW_HTTP_CRLF
"</html>" VW_HTTP_CRLF, location, location, location), now_written, VS_STREAM_ERROR, total_written);
/* clang-format on */
return total_written;
}
size_t vw_report_error(vw_HTTPRequest *req, uint16_t code, const char *back_link, const char *extra_info) {
if (!req || !back_link) {
return 0;
}
size_t total_written = 0;
size_t now_written = 0;
VS_IO_WITH_TEST(
vw_HTTPRequest_res_begin(req, code), now_written, VS_STREAM_ERROR, total_written);
VS_IO_WITH_TEST(vw_HTTPRequest_res_header(req, "content-type", "text/html"),
now_written,
VS_STREAM_ERROR,
total_written);
/* clang-format off */
VS_IO_WITH_TEST(vw_HTTPRequest_writef(req,
"<!DOCTYPE html>" VW_HTTP_CRLF
"<html lang=\"en\">" VW_HTTP_CRLF
" <head>" VW_HTTP_CRLF
" <meta charset=\"UTF-8\" />" VW_HTTP_CRLF
" <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\" />" VW_HTTP_CRLF
" <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" />" VW_HTTP_CRLF
" " VW_SRV_LICENSE VW_HTTP_CRLF
" <title>HTTP/%u - %s</title>" VW_HTTP_CRLF
" </head>" VW_HTTP_CRLF
" <body aria-live=\"polite\">" VW_HTTP_CRLF
" <h1>HTTP/%u - %s</h1>" VW_HTTP_CRLF
" <p>%s. Go back to <a href=\"%s\">URL: %s</a>.</p>" VW_HTTP_CRLF
" <p>Extra information: %s</p>" VW_HTTP_CRLF
" </body>" VW_HTTP_CRLF
"</html>" VW_HTTP_CRLF,
code, vw_HTTP_code_to_message(code),
code, vw_HTTP_code_to_message(code),
vw_HTTP_code_to_description(code),
back_link, back_link,
extra_info ? extra_info : "None"), now_written, VS_STREAM_ERROR, total_written);
/* clang-format on */
return total_written;
}