-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtml_error.zig
More file actions
252 lines (224 loc) · 9.09 KB
/
html_error.zig
File metadata and controls
252 lines (224 loc) · 9.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
//! HTML Error Page Module for tinyproxy-zig
//!
//! Provides friendly HTML error pages with template variable substitution.
//!
//! Supported template variables:
//! {detail} - Error detail message
//! {cause} - Error cause/reason
//! {clientip} - Client IP address
//! {clienthost} - Client hostname (if resolved)
//! {method} - HTTP method
//! {protocol} - Request protocol
//! {host} - Request host
//! {url} - Full request URL
//! {version} - tinyproxy version
const std = @import("std");
pub const HttpError = enum(u16) {
bad_request = 400,
unauthorized = 401,
forbidden = 403,
not_found = 404,
method_not_allowed = 405,
proxy_auth_required = 407,
request_timeout = 408,
length_required = 411,
uri_too_long = 414,
internal_server_error = 500,
not_implemented = 501,
bad_gateway = 502,
service_unavailable = 503,
gateway_timeout = 504,
pub fn statusLine(self: HttpError) []const u8 {
return switch (self) {
.bad_request => "400 Bad Request",
.unauthorized => "401 Unauthorized",
.forbidden => "403 Forbidden",
.not_found => "404 Not Found",
.method_not_allowed => "405 Method Not Allowed",
.proxy_auth_required => "407 Proxy Authentication Required",
.request_timeout => "408 Request Timeout",
.length_required => "411 Length Required",
.uri_too_long => "414 URI Too Long",
.internal_server_error => "500 Internal Server Error",
.not_implemented => "501 Not Implemented",
.bad_gateway => "502 Bad Gateway",
.service_unavailable => "503 Service Unavailable",
.gateway_timeout => "504 Gateway Timeout",
};
}
pub fn title(self: HttpError) []const u8 {
return switch (self) {
.bad_request => "Bad Request",
.unauthorized => "Unauthorized",
.forbidden => "Forbidden",
.not_found => "Not Found",
.method_not_allowed => "Method Not Allowed",
.proxy_auth_required => "Authentication Required",
.request_timeout => "Request Timeout",
.length_required => "Length Required",
.uri_too_long => "URI Too Long",
.internal_server_error => "Internal Server Error",
.not_implemented => "Not Implemented",
.bad_gateway => "Bad Gateway",
.service_unavailable => "Service Unavailable",
.gateway_timeout => "Gateway Timeout",
};
}
pub fn defaultMessage(self: HttpError) []const u8 {
return switch (self) {
.bad_request => "The proxy could not understand your request.",
.unauthorized => "Authentication is required to access this resource.",
.forbidden => "Access to this resource has been denied.",
.not_found => "The requested resource could not be found.",
.method_not_allowed => "The request method is not supported.",
.proxy_auth_required => "Please authenticate to use this proxy.",
.request_timeout => "The request timed out while waiting.",
.length_required => "The request must include a Content-Length header.",
.uri_too_long => "The request URI is too long for the server to process.",
.internal_server_error => "An internal error occurred in the proxy.",
.not_implemented => "This feature is not implemented.",
.bad_gateway => "Could not connect to the upstream server.",
.service_unavailable => "The proxy is temporarily unavailable.",
.gateway_timeout => "The upstream server did not respond in time.",
};
}
};
/// Context for template variable substitution
pub const ErrorContext = struct {
detail: []const u8 = "",
cause: []const u8 = "",
client_ip: []const u8 = "",
client_host: []const u8 = "",
method: []const u8 = "",
protocol: []const u8 = "",
host: []const u8 = "",
url: []const u8 = "",
};
const VERSION = "tinyproxy-zig 0.1.0";
/// Default HTML error template
const default_template =
\\<!DOCTYPE html>
\\<html>
\\<head>
\\<title>{TITLE}</title>
\\<style>
\\body {{ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; margin: 0; padding: 40px; background: #f5f5f5; }}
\\h1 {{ color: #c0392b; margin-bottom: 10px; }}
\\.container {{ max-width: 600px; margin: 0 auto; background: white; padding: 30px; border-radius: 8px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); }}
\\.code {{ font-size: 4em; color: #e74c3c; font-weight: bold; margin-bottom: 0; }}
\\.message {{ color: #666; line-height: 1.6; }}
\\.detail {{ background: #fafafa; padding: 15px; border-radius: 4px; margin: 20px 0; font-family: monospace; font-size: 0.9em; color: #555; }}
\\hr {{ border: none; border-top: 1px solid #eee; margin: 20px 0; }}
\\.footer {{ color: #999; font-size: 0.8em; }}
\\</style>
\\</head>
\\<body>
\\<div class="container">
\\<p class="code">{CODE}</p>
\\<h1>{TITLE}</h1>
\\<p class="message">{MESSAGE}</p>
\\{DETAIL_SECTION}
\\<hr>
\\<p class="footer">{VERSION}</p>
\\</div>
\\</body>
\\</html>
;
const detail_section_template =
\\<div class="detail">{DETAIL}</div>
;
/// Render an error page to HTML
pub fn renderErrorPage(
allocator: std.mem.Allocator,
err: HttpError,
ctx: ErrorContext,
) ![]const u8 {
// Build detail section if detail is provided
var detail_section: []const u8 = "";
var detail_section_owned = false;
defer if (detail_section_owned) allocator.free(detail_section);
if (ctx.detail.len > 0) {
detail_section = try std.fmt.allocPrint(allocator, detail_section_template, .{ctx.detail});
detail_section_owned = true;
}
// Build message with cause if provided
var message: []const u8 = err.defaultMessage();
var message_owned = false;
defer if (message_owned) allocator.free(message);
if (ctx.cause.len > 0) {
message = try std.fmt.allocPrint(allocator, "{s} {s}", .{ err.defaultMessage(), ctx.cause });
message_owned = true;
}
return std.fmt.allocPrint(allocator, default_template, .{
err.title(),
@intFromEnum(err),
err.title(),
message,
detail_section,
VERSION,
});
}
/// Send an error response with HTML body
pub fn sendError(
allocator: std.mem.Allocator,
writer: anytype,
err: HttpError,
ctx: ErrorContext,
) !void {
const body = try renderErrorPage(allocator, err, ctx);
defer allocator.free(body);
try writer.print(
"HTTP/1.1 {s}\r\n" ++
"Content-Type: text/html; charset=utf-8\r\n" ++
"Content-Length: {d}\r\n" ++
"Connection: close\r\n" ++
"\r\n" ++
"{s}",
.{ err.statusLine(), body.len, body },
);
}
/// Build a simple error response (without HTML rendering, for inline use)
pub fn buildSimpleResponse(err: HttpError, buf: []u8) ![]const u8 {
const message = err.defaultMessage();
return std.fmt.bufPrint(buf, "HTTP/1.1 {s}\r\nContent-Type: text/plain\r\nContent-Length: {d}\r\nConnection: close\r\n\r\n{s}", .{
err.statusLine(),
message.len,
message,
});
}
// ============================================================================
// Tests
// ============================================================================
test "HttpError status lines" {
try std.testing.expectEqualStrings("400 Bad Request", HttpError.bad_request.statusLine());
try std.testing.expectEqualStrings("502 Bad Gateway", HttpError.bad_gateway.statusLine());
try std.testing.expectEqualStrings("403 Forbidden", HttpError.forbidden.statusLine());
}
test "HttpError titles" {
try std.testing.expectEqualStrings("Bad Request", HttpError.bad_request.title());
try std.testing.expectEqualStrings("Bad Gateway", HttpError.bad_gateway.title());
}
test "renderErrorPage basic" {
const allocator = std.testing.allocator;
const html = try renderErrorPage(allocator, .bad_gateway, .{});
defer allocator.free(html);
try std.testing.expect(std.mem.indexOf(u8, html, "502") != null);
try std.testing.expect(std.mem.indexOf(u8, html, "Bad Gateway") != null);
try std.testing.expect(std.mem.indexOf(u8, html, "tinyproxy-zig") != null);
}
test "renderErrorPage with detail" {
const allocator = std.testing.allocator;
const html = try renderErrorPage(allocator, .forbidden, .{
.detail = "Connection refused by ACL rule",
});
defer allocator.free(html);
try std.testing.expect(std.mem.indexOf(u8, html, "403") != null);
try std.testing.expect(std.mem.indexOf(u8, html, "Forbidden") != null);
try std.testing.expect(std.mem.indexOf(u8, html, "Connection refused by ACL rule") != null);
}
test "buildSimpleResponse" {
var buf: [512]u8 = undefined;
const response = try buildSimpleResponse(.not_found, &buf);
try std.testing.expect(std.mem.indexOf(u8, response, "404 Not Found") != null);
try std.testing.expect(std.mem.indexOf(u8, response, "Content-Type: text/plain") != null);
}