error response

This commit is contained in:
whaffman 2025-10-16 14:52:25 +02:00
parent 3370c53d3e
commit a8dd8a77a5
24 changed files with 1002 additions and 28 deletions

View File

@ -12,13 +12,13 @@ server {
error_page 400 ./htdocs/error_pages/400.html;
error_page 404 ./htdocs/error_pages/404.html;
error_page 403 /403.html;
error_page 404 /404.html;
error_page 405 /405.html;
error_page 413 /413.html;
error_page 500 /500.html;
error_page 502 /502.html;
error_page 504 /504.html;
error_page 404 ./htdocs/error_pages/404.html;
error_page 403 ./htdocs/error_pages/403.html;
error_page 405 ./htdocs/error_pages/405.html;
error_page 413 ./htdocs/error_pages/413.html;
error_page 500 ./htdocs/error_pages/500.html;
error_page 502 ./htdocs/error_pages/502.html;
error_page 504 ./htdocs/error_pages/504.html;
client_max_body_size 1M;
@ -42,6 +42,13 @@ server {
allowed_methods GET;
}
location /test {
root ./htdocs/site-1/tester;
autoindex off;
index index.html;
allowed_methods GET;
}
# cgi_enabled yes;
# cgi_ext .php /usr/bin/php-cgi;
}

View File

@ -0,0 +1,73 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>401 Unauthorized</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body {
background: linear-gradient(135deg, #ff6a6a 0%, #ffd86a 100%);
color: #222;
font-family: 'Segoe UI', 'Arial', sans-serif;
margin: 0;
padding: 0;
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
}
.container {
background: rgba(255,255,255,0.85);
border-radius: 16px;
box-shadow: 0 8px 32px rgba(0,0,0,0.18);
padding: 40px 32px;
text-align: center;
max-width: 400px;
}
.error-code {
font-size: 5rem;
font-weight: bold;
color: #ff6a6a;
letter-spacing: 2px;
margin-bottom: 10px;
text-shadow: 2px 2px 8px #ffd86a88;
}
.message {
font-size: 1.3rem;
margin-bottom: 18px;
}
.home-link {
display: inline-block;
margin-top: 12px;
padding: 10px 22px;
background: #ffd86a;
color: #222;
border-radius: 8px;
text-decoration: none;
font-weight: 500;
box-shadow: 0 2px 8px #ff6a6a44;
transition: background 0.2s;
}
.home-link:hover {
background: #ff6a6a;
color: #fff;
}
@media (max-width: 500px) {
.container {
padding: 24px 8px;
max-width: 95vw;
}
.error-code {
font-size: 3rem;
}
}
</style>
</head>
<body>
<div class="container">
<div class="error-code">401</div>
<div class="message">Access Denied!<br>Authentication is required to access this resource.</div>
<a class="home-link" href="/">Go Home</a>
</div>
</body>
</html>

View File

@ -0,0 +1,73 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>403 Forbidden</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body {
background: linear-gradient(135deg, #ff6a6a 0%, #ffd86a 100%);
color: #222;
font-family: 'Segoe UI', 'Arial', sans-serif;
margin: 0;
padding: 0;
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
}
.container {
background: rgba(255,255,255,0.85);
border-radius: 16px;
box-shadow: 0 8px 32px rgba(0,0,0,0.18);
padding: 40px 32px;
text-align: center;
max-width: 400px;
}
.error-code {
font-size: 5rem;
font-weight: bold;
color: #ff6a6a;
letter-spacing: 2px;
margin-bottom: 10px;
text-shadow: 2px 2px 8px #ffd86a88;
}
.message {
font-size: 1.3rem;
margin-bottom: 18px;
}
.home-link {
display: inline-block;
margin-top: 12px;
padding: 10px 22px;
background: #ffd86a;
color: #222;
border-radius: 8px;
text-decoration: none;
font-weight: 500;
box-shadow: 0 2px 8px #ff6a6a44;
transition: background 0.2s;
}
.home-link:hover {
background: #ff6a6a;
color: #fff;
}
@media (max-width: 500px) {
.container {
padding: 24px 8px;
max-width: 95vw;
}
.error-code {
font-size: 3rem;
}
}
</style>
</head>
<body>
<div class="container">
<div class="error-code">403</div>
<div class="message">Forbidden!<br>You don't have permission to access this resource.</div>
<a class="home-link" href="/">Go Home</a>
</div>
</body>
</html>

View File

@ -0,0 +1,73 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>405 Method Not Allowed</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body {
background: linear-gradient(135deg, #ff6a6a 0%, #ffd86a 100%);
color: #222;
font-family: 'Segoe UI', 'Arial', sans-serif;
margin: 0;
padding: 0;
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
}
.container {
background: rgba(255,255,255,0.85);
border-radius: 16px;
box-shadow: 0 8px 32px rgba(0,0,0,0.18);
padding: 40px 32px;
text-align: center;
max-width: 400px;
}
.error-code {
font-size: 5rem;
font-weight: bold;
color: #ff6a6a;
letter-spacing: 2px;
margin-bottom: 10px;
text-shadow: 2px 2px 8px #ffd86a88;
}
.message {
font-size: 1.3rem;
margin-bottom: 18px;
}
.home-link {
display: inline-block;
margin-top: 12px;
padding: 10px 22px;
background: #ffd86a;
color: #222;
border-radius: 8px;
text-decoration: none;
font-weight: 500;
box-shadow: 0 2px 8px #ff6a6a44;
transition: background 0.2s;
}
.home-link:hover {
background: #ff6a6a;
color: #fff;
}
@media (max-width: 500px) {
.container {
padding: 24px 8px;
max-width: 95vw;
}
.error-code {
font-size: 3rem;
}
}
</style>
</head>
<body>
<div class="container">
<div class="error-code">405</div>
<div class="message">Method Not Allowed!<br>The request method is not supported for this resource.</div>
<a class="home-link" href="/">Go Home</a>
</div>
</body>
</html>

View File

@ -0,0 +1,73 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>408 Request Timeout</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body {
background: linear-gradient(135deg, #ff6a6a 0%, #ffd86a 100%);
color: #222;
font-family: 'Segoe UI', 'Arial', sans-serif;
margin: 0;
padding: 0;
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
}
.container {
background: rgba(255,255,255,0.85);
border-radius: 16px;
box-shadow: 0 8px 32px rgba(0,0,0,0.18);
padding: 40px 32px;
text-align: center;
max-width: 400px;
}
.error-code {
font-size: 5rem;
font-weight: bold;
color: #ff6a6a;
letter-spacing: 2px;
margin-bottom: 10px;
text-shadow: 2px 2px 8px #ffd86a88;
}
.message {
font-size: 1.3rem;
margin-bottom: 18px;
}
.home-link {
display: inline-block;
margin-top: 12px;
padding: 10px 22px;
background: #ffd86a;
color: #222;
border-radius: 8px;
text-decoration: none;
font-weight: 500;
box-shadow: 0 2px 8px #ff6a6a44;
transition: background 0.2s;
}
.home-link:hover {
background: #ff6a6a;
color: #fff;
}
@media (max-width: 500px) {
.container {
padding: 24px 8px;
max-width: 95vw;
}
.error-code {
font-size: 3rem;
}
}
</style>
</head>
<body>
<div class="container">
<div class="error-code">408</div>
<div class="message">Request Timeout!<br>The server timed out waiting for the request.</div>
<a class="home-link" href="/">Go Home</a>
</div>
</body>
</html>

View File

@ -0,0 +1,73 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>411 Length Required</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body {
background: linear-gradient(135deg, #ff6a6a 0%, #ffd86a 100%);
color: #222;
font-family: 'Segoe UI', 'Arial', sans-serif;
margin: 0;
padding: 0;
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
}
.container {
background: rgba(255,255,255,0.85);
border-radius: 16px;
box-shadow: 0 8px 32px rgba(0,0,0,0.18);
padding: 40px 32px;
text-align: center;
max-width: 400px;
}
.error-code {
font-size: 5rem;
font-weight: bold;
color: #ff6a6a;
letter-spacing: 2px;
margin-bottom: 10px;
text-shadow: 2px 2px 8px #ffd86a88;
}
.message {
font-size: 1.3rem;
margin-bottom: 18px;
}
.home-link {
display: inline-block;
margin-top: 12px;
padding: 10px 22px;
background: #ffd86a;
color: #222;
border-radius: 8px;
text-decoration: none;
font-weight: 500;
box-shadow: 0 2px 8px #ff6a6a44;
transition: background 0.2s;
}
.home-link:hover {
background: #ff6a6a;
color: #fff;
}
@media (max-width: 500px) {
.container {
padding: 24px 8px;
max-width: 95vw;
}
.error-code {
font-size: 3rem;
}
}
</style>
</head>
<body>
<div class="container">
<div class="error-code">411</div>
<div class="message">Length Required!<br>The request must specify the content length.</div>
<a class="home-link" href="/">Go Home</a>
</div>
</body>
</html>

View File

@ -0,0 +1,73 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>413 Payload Too Large</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body {
background: linear-gradient(135deg, #ff6a6a 0%, #ffd86a 100%);
color: #222;
font-family: 'Segoe UI', 'Arial', sans-serif;
margin: 0;
padding: 0;
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
}
.container {
background: rgba(255,255,255,0.85);
border-radius: 16px;
box-shadow: 0 8px 32px rgba(0,0,0,0.18);
padding: 40px 32px;
text-align: center;
max-width: 400px;
}
.error-code {
font-size: 5rem;
font-weight: bold;
color: #ff6a6a;
letter-spacing: 2px;
margin-bottom: 10px;
text-shadow: 2px 2px 8px #ffd86a88;
}
.message {
font-size: 1.3rem;
margin-bottom: 18px;
}
.home-link {
display: inline-block;
margin-top: 12px;
padding: 10px 22px;
background: #ffd86a;
color: #222;
border-radius: 8px;
text-decoration: none;
font-weight: 500;
box-shadow: 0 2px 8px #ff6a6a44;
transition: background 0.2s;
}
.home-link:hover {
background: #ff6a6a;
color: #fff;
}
@media (max-width: 500px) {
.container {
padding: 24px 8px;
max-width: 95vw;
}
.error-code {
font-size: 3rem;
}
}
</style>
</head>
<body>
<div class="container">
<div class="error-code">413</div>
<div class="message">Payload Too Large!<br>The request entity is larger than the server is able to process.</div>
<a class="home-link" href="/">Go Home</a>
</div>
</body>
</html>

View File

@ -0,0 +1,73 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>414 URI Too Long</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body {
background: linear-gradient(135deg, #ff6a6a 0%, #ffd86a 100%);
color: #222;
font-family: 'Segoe UI', 'Arial', sans-serif;
margin: 0;
padding: 0;
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
}
.container {
background: rgba(255,255,255,0.85);
border-radius: 16px;
box-shadow: 0 8px 32px rgba(0,0,0,0.18);
padding: 40px 32px;
text-align: center;
max-width: 400px;
}
.error-code {
font-size: 5rem;
font-weight: bold;
color: #ff6a6a;
letter-spacing: 2px;
margin-bottom: 10px;
text-shadow: 2px 2px 8px #ffd86a88;
}
.message {
font-size: 1.3rem;
margin-bottom: 18px;
}
.home-link {
display: inline-block;
margin-top: 12px;
padding: 10px 22px;
background: #ffd86a;
color: #222;
border-radius: 8px;
text-decoration: none;
font-weight: 500;
box-shadow: 0 2px 8px #ff6a6a44;
transition: background 0.2s;
}
.home-link:hover {
background: #ff6a6a;
color: #fff;
}
@media (max-width: 500px) {
.container {
padding: 24px 8px;
max-width: 95vw;
}
.error-code {
font-size: 3rem;
}
}
</style>
</head>
<body>
<div class="container">
<div class="error-code">414</div>
<div class="message">URI Too Long!<br>The request URI is longer than the server is willing to interpret.</div>
<a class="home-link" href="/">Go Home</a>
</div>
</body>
</html>

View File

@ -0,0 +1,73 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>415 Unsupported Media Type</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body {
background: linear-gradient(135deg, #ff6a6a 0%, #ffd86a 100%);
color: #222;
font-family: 'Segoe UI', 'Arial', sans-serif;
margin: 0;
padding: 0;
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
}
.container {
background: rgba(255,255,255,0.85);
border-radius: 16px;
box-shadow: 0 8px 32px rgba(0,0,0,0.18);
padding: 40px 32px;
text-align: center;
max-width: 400px;
}
.error-code {
font-size: 5rem;
font-weight: bold;
color: #ff6a6a;
letter-spacing: 2px;
margin-bottom: 10px;
text-shadow: 2px 2px 8px #ffd86a88;
}
.message {
font-size: 1.3rem;
margin-bottom: 18px;
}
.home-link {
display: inline-block;
margin-top: 12px;
padding: 10px 22px;
background: #ffd86a;
color: #222;
border-radius: 8px;
text-decoration: none;
font-weight: 500;
box-shadow: 0 2px 8px #ff6a6a44;
transition: background 0.2s;
}
.home-link:hover {
background: #ff6a6a;
color: #fff;
}
@media (max-width: 500px) {
.container {
padding: 24px 8px;
max-width: 95vw;
}
.error-code {
font-size: 3rem;
}
}
</style>
</head>
<body>
<div class="container">
<div class="error-code">415</div>
<div class="message">Unsupported Media Type!<br>The media format of the requested data is not supported.</div>
<a class="home-link" href="/">Go Home</a>
</div>
</body>
</html>

View File

@ -0,0 +1,73 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>500 Internal Server Error</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body {
background: linear-gradient(135deg, #ff6a6a 0%, #ffd86a 100%);
color: #222;
font-family: 'Segoe UI', 'Arial', sans-serif;
margin: 0;
padding: 0;
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
}
.container {
background: rgba(255,255,255,0.85);
border-radius: 16px;
box-shadow: 0 8px 32px rgba(0,0,0,0.18);
padding: 40px 32px;
text-align: center;
max-width: 400px;
}
.error-code {
font-size: 5rem;
font-weight: bold;
color: #ff6a6a;
letter-spacing: 2px;
margin-bottom: 10px;
text-shadow: 2px 2px 8px #ffd86a88;
}
.message {
font-size: 1.3rem;
margin-bottom: 18px;
}
.home-link {
display: inline-block;
margin-top: 12px;
padding: 10px 22px;
background: #ffd86a;
color: #222;
border-radius: 8px;
text-decoration: none;
font-weight: 500;
box-shadow: 0 2px 8px #ff6a6a44;
transition: background 0.2s;
}
.home-link:hover {
background: #ff6a6a;
color: #fff;
}
@media (max-width: 500px) {
.container {
padding: 24px 8px;
max-width: 95vw;
}
.error-code {
font-size: 3rem;
}
}
</style>
</head>
<body>
<div class="container">
<div class="error-code">500</div>
<div class="message">Internal Server Error!<br>Something went wrong on our end. Please try again later.</div>
<a class="home-link" href="/">Go Home</a>
</div>
</body>
</html>

View File

@ -0,0 +1,73 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>501 Not Implemented</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body {
background: linear-gradient(135deg, #ff6a6a 0%, #ffd86a 100%);
color: #222;
font-family: 'Segoe UI', 'Arial', sans-serif;
margin: 0;
padding: 0;
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
}
.container {
background: rgba(255,255,255,0.85);
border-radius: 16px;
box-shadow: 0 8px 32px rgba(0,0,0,0.18);
padding: 40px 32px;
text-align: center;
max-width: 400px;
}
.error-code {
font-size: 5rem;
font-weight: bold;
color: #ff6a6a;
letter-spacing: 2px;
margin-bottom: 10px;
text-shadow: 2px 2px 8px #ffd86a88;
}
.message {
font-size: 1.3rem;
margin-bottom: 18px;
}
.home-link {
display: inline-block;
margin-top: 12px;
padding: 10px 22px;
background: #ffd86a;
color: #222;
border-radius: 8px;
text-decoration: none;
font-weight: 500;
box-shadow: 0 2px 8px #ff6a6a44;
transition: background 0.2s;
}
.home-link:hover {
background: #ff6a6a;
color: #fff;
}
@media (max-width: 500px) {
.container {
padding: 24px 8px;
max-width: 95vw;
}
.error-code {
font-size: 3rem;
}
}
</style>
</head>
<body>
<div class="container">
<div class="error-code">501</div>
<div class="message">Not Implemented!<br>The server does not support the functionality required to fulfill the request.</div>
<a class="home-link" href="/">Go Home</a>
</div>
</body>
</html>

View File

@ -0,0 +1,73 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>502 Bad Gateway</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body {
background: linear-gradient(135deg, #ff6a6a 0%, #ffd86a 100%);
color: #222;
font-family: 'Segoe UI', 'Arial', sans-serif;
margin: 0;
padding: 0;
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
}
.container {
background: rgba(255,255,255,0.85);
border-radius: 16px;
box-shadow: 0 8px 32px rgba(0,0,0,0.18);
padding: 40px 32px;
text-align: center;
max-width: 400px;
}
.error-code {
font-size: 5rem;
font-weight: bold;
color: #ff6a6a;
letter-spacing: 2px;
margin-bottom: 10px;
text-shadow: 2px 2px 8px #ffd86a88;
}
.message {
font-size: 1.3rem;
margin-bottom: 18px;
}
.home-link {
display: inline-block;
margin-top: 12px;
padding: 10px 22px;
background: #ffd86a;
color: #222;
border-radius: 8px;
text-decoration: none;
font-weight: 500;
box-shadow: 0 2px 8px #ff6a6a44;
transition: background 0.2s;
}
.home-link:hover {
background: #ff6a6a;
color: #fff;
}
@media (max-width: 500px) {
.container {
padding: 24px 8px;
max-width: 95vw;
}
.error-code {
font-size: 3rem;
}
}
</style>
</head>
<body>
<div class="container">
<div class="error-code">502</div>
<div class="message">Bad Gateway!<br>The server received an invalid response from an upstream server.</div>
<a class="home-link" href="/">Go Home</a>
</div>
</body>
</html>

View File

@ -0,0 +1,73 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>503 Service Unavailable</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body {
background: linear-gradient(135deg, #ff6a6a 0%, #ffd86a 100%);
color: #222;
font-family: 'Segoe UI', 'Arial', sans-serif;
margin: 0;
padding: 0;
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
}
.container {
background: rgba(255,255,255,0.85);
border-radius: 16px;
box-shadow: 0 8px 32px rgba(0,0,0,0.18);
padding: 40px 32px;
text-align: center;
max-width: 400px;
}
.error-code {
font-size: 5rem;
font-weight: bold;
color: #ff6a6a;
letter-spacing: 2px;
margin-bottom: 10px;
text-shadow: 2px 2px 8px #ffd86a88;
}
.message {
font-size: 1.3rem;
margin-bottom: 18px;
}
.home-link {
display: inline-block;
margin-top: 12px;
padding: 10px 22px;
background: #ffd86a;
color: #222;
border-radius: 8px;
text-decoration: none;
font-weight: 500;
box-shadow: 0 2px 8px #ff6a6a44;
transition: background 0.2s;
}
.home-link:hover {
background: #ff6a6a;
color: #fff;
}
@media (max-width: 500px) {
.container {
padding: 24px 8px;
max-width: 95vw;
}
.error-code {
font-size: 3rem;
}
}
</style>
</head>
<body>
<div class="container">
<div class="error-code">503</div>
<div class="message">Service Unavailable!<br>The server is temporarily overloaded or under maintenance.</div>
<a class="home-link" href="/">Go Home</a>
</div>
</body>
</html>

View File

@ -0,0 +1,73 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>504 Gateway Timeout</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body {
background: linear-gradient(135deg, #ff6a6a 0%, #ffd86a 100%);
color: #222;
font-family: 'Segoe UI', 'Arial', sans-serif;
margin: 0;
padding: 0;
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
}
.container {
background: rgba(255,255,255,0.85);
border-radius: 16px;
box-shadow: 0 8px 32px rgba(0,0,0,0.18);
padding: 40px 32px;
text-align: center;
max-width: 400px;
}
.error-code {
font-size: 5rem;
font-weight: bold;
color: #ff6a6a;
letter-spacing: 2px;
margin-bottom: 10px;
text-shadow: 2px 2px 8px #ffd86a88;
}
.message {
font-size: 1.3rem;
margin-bottom: 18px;
}
.home-link {
display: inline-block;
margin-top: 12px;
padding: 10px 22px;
background: #ffd86a;
color: #222;
border-radius: 8px;
text-decoration: none;
font-weight: 500;
box-shadow: 0 2px 8px #ff6a6a44;
transition: background 0.2s;
}
.home-link:hover {
background: #ff6a6a;
color: #fff;
}
@media (max-width: 500px) {
.container {
padding: 24px 8px;
max-width: 95vw;
}
.error-code {
font-size: 3rem;
}
}
</style>
</head>
<body>
<div class="container">
<div class="error-code">504</div>
<div class="message">Gateway Timeout!<br>The server did not receive a timely response from an upstream server.</div>
<a class="home-link" href="/">Go Home</a>
</div>
</body>
</html>

View File

@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Test Page</title>
</head>
<body>
<h1>This is a test HTML page</h1>
</body>
</html>

View File

@ -35,15 +35,6 @@ Client::~Client()
server_.remove(*clientSocket_);
};
int Client::getStatusCode() const noexcept
{
return statusCode_;
}
void Client::setStatusCode(int code)
{
statusCode_ = code;
}
ASocket &Client::getSocket(int fd) const
{
@ -191,7 +182,7 @@ void Client::poll() const
{
Log::info("Response is ready to be sent to client, fd: " + std::to_string(clientSocket_->getFd()));
clientSocket_->setCallback([this]() { respond(); });
server_.responseReady(clientSocket_->getFd());
server_.writable(clientSocket_->getFd());
}
}

View File

@ -34,24 +34,24 @@ class Client
void request();
void respond() const;
void poll() const;
void writeToCgi();
void readFromCgi();
void poll() const;
[[nodiscard]] int getStatusCode() const noexcept;
// [[nodiscard]] int getStatusCode() const noexcept;
[[nodiscard]] ASocket &getSocket(int fd = -1) const;
void setStatusCode(int code);
// void setStatusCode(int code);
void setCgiSockets(std::unique_ptr<CgiSocket> cgiStdIn, std::unique_ptr<CgiSocket> cgiStdOut);
[[nodiscard]] HttpRequest &getHttpRequest() const noexcept;
[[nodiscard]] HttpResponse &getHttpResponse() const noexcept;
private:
int statusCode_ = Http::StatusCode::OK;
// int statusCode_ = Http::StatusCode::OK;
constexpr static size_t bufferSize_ = 8192; // 8KB buffer for reading CGI output
std::unique_ptr<HttpRequest> httpRequest_;
std::unique_ptr<HttpResponse> httpResponse_;

View File

@ -20,6 +20,7 @@ void ErrorHandler::createErrorResponse(uint16_t statusCode, HttpResponse &respon
response.addHeader("Content-Type", "text/html");
response.addHeader("Connection", "close");
response.appendBody(generateErrorPage(statusCode, config));
response.setComplete();
}
std::string ErrorHandler::generateErrorPage(uint16_t statusCode, const AConfig *config)

View File

@ -23,6 +23,7 @@ FileHandler::FileHandler(const HttpRequest &request, HttpResponse &response)
void FileHandler::handleFile(const std::string &filepath) const
{
Log::trace(LOCATION);
Log::debug("Requested path is a file: " + filepath);
// auto response = std::make_unique<HttpResponse>();
std::string extension = FileUtils::getExtension(filepath);
@ -45,6 +46,7 @@ void FileHandler::handleFile(const std::string &filepath) const
void FileHandler::handleDirectory(const std::string &dirpath, ResourceType type) const
{
Log::trace(LOCATION);
Log::debug("Requested path is a directory: " + dirpath);
if (type == DIRECTORY_INDEX)
{
@ -72,7 +74,13 @@ void FileHandler::handleDirectory(const std::string &dirpath, ResourceType type)
void FileHandler::handle() const
{
Log::trace(LOCATION);
if (!uri_.isValid())
{
ErrorHandler::createErrorResponse(Http::StatusCode::NOT_FOUND, response_, config_);
return;
}
std::string filepath = uri_.getFullPath();
Log::debug("Handling file request for path: " + filepath);
ResourceType resourceType = getResourceType(filepath);
switch (resourceType)

View File

@ -17,6 +17,7 @@ URI::URI(const HttpRequest &request, const ServerConfig &serverConfig)
{
Log::trace(LOCATION);
parseUri(request.getTarget());
Log::debug("Parsed URI: " + uriTrimmed_, {{"ConfigType", config_->getType()}});
parseFullpath();
authority_ = request.getHeaders().getHost().value();
@ -84,17 +85,17 @@ void URI::parseFullpath()
for (const auto &segment : uriSegments)
{
std::string curDir = FileUtils::joinPath(dir_, segment);
std::string currentPath = FileUtils::joinPath(dir_, segment);
if (segment.empty())
{
continue;
}
if (FileUtils::isFile(curDir) && baseName_.empty())
if (FileUtils::isFile(currentPath) && baseName_.empty())
{
baseName_ = segment;
}
else if (FileUtils::isDirectory(curDir))
else if (FileUtils::isDirectory(currentPath))
{
dir_ = FileUtils::joinPath(dir_, segment);
}
@ -102,6 +103,12 @@ void URI::parseFullpath()
{
pathInfo_ = FileUtils::joinPath(pathInfo_, baseName_);
}
else // not file or dir, and no baseName yet
{
valid_ = false;
Log::warning("Invalid path segment encountered: " + currentPath);
return;
}
}
fullPath_ = FileUtils::joinPath(dir_, baseName_);
}
@ -146,7 +153,7 @@ bool URI::isDirectory() const noexcept
bool URI::isValid() const noexcept
{
return FileUtils::isValidPath(fullPath_);
return valid_ && FileUtils::isValidPath(fullPath_);
}
bool URI::isCgi() const noexcept

View File

@ -49,6 +49,7 @@ class URI
std::string query_;
std::string fragment_;
std::string authority_;
bool valid_ = true;
static const AConfig *matchConfig(const std::string &uri, const ServerConfig &serverConfig);
};

View File

@ -94,7 +94,6 @@ void HttpRequest::parseBuffer()
return; // Request is complete
case State::ParseError:
Log::warning("Parse error occurred, stopping further processing");
client_->setStatusCode(Http::StatusCode::BAD_REQUEST);
return;
}
}

View File

@ -168,7 +168,7 @@ void Server::handleRequest(struct epoll_event *event) const
client.getSocket(client_fd).callback();
}
void Server::responseReady(int client_fd) const
void Server::writable(int client_fd) const
{
Log::trace(LOCATION);
Log::debug("Response ready for client fd: " + std::to_string(client_fd));

View File

@ -36,7 +36,7 @@ class Server
void add(const ASocket &socket, uint32_t events, Client *client = nullptr);
void remove(const ASocket &socket);
void disconnect(const Client &client);
void responseReady(int client_fd) const;
void writable(int client_fd) const;
ServerSocket &getListener(int fd) const;
Client &getClient(int fd) const;