Skip to content

Commit 91ee363

Browse files
committed
[Zig/Httpz] proper escape
1 parent 8f2e388 commit 91ee363

File tree

1 file changed

+7
-43
lines changed

1 file changed

+7
-43
lines changed

frameworks/Zig/httpz/src/endpoints.zig

Lines changed: 7 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ fn getFortunesHtml(allocator: std.mem.Allocator, pool: *pg.Pool) ![]const u8 {
9494
for (fortunes) |ft| {
9595
try writer.print("<tr><td>{d}</td><td>{s}</td></tr>", .{
9696
ft.id,
97-
try deescapeHtml(allocator, ft.message),
97+
try escape_html(allocator, ft.message),
9898
});
9999
}
100100

@@ -131,51 +131,15 @@ fn cmpFortuneByMessage(_: void, a: Fortune, b: Fortune) bool {
131131
return std.mem.order(u8, a.message, b.message).compare(std.math.CompareOperator.lt);
132132
}
133133

134-
fn deescapeHtml(allocator: std.mem.Allocator, input: []const u8) ![]const u8 {
134+
fn escape_html(allocator: std.mem.Allocator, input: []const u8) ![]const u8 {
135135
var output = try std.ArrayListUnmanaged(u8).initCapacity(allocator, 0);
136136
defer output.deinit(allocator);
137137

138-
var i: usize = 0;
139-
while (i < input.len) {
140-
if (std.mem.startsWith(u8, input[i..], "&#32;")) {
141-
try output.append(allocator, ' ');
142-
i += 5;
143-
} else if (std.mem.startsWith(u8, input[i..], "&#34;")) {
144-
try output.append(allocator, '"');
145-
i += 5;
146-
} else if (std.mem.startsWith(u8, input[i..], "&#38;")) {
147-
try output.append(allocator, '&');
148-
i += 5;
149-
} else if (std.mem.startsWith(u8, input[i..], "&#39;")) {
150-
try output.append(allocator, '\'');
151-
i += 5;
152-
} else if (std.mem.startsWith(u8, input[i..], "&#40;")) {
153-
try output.append(allocator, '(');
154-
i += 5;
155-
} else if (std.mem.startsWith(u8, input[i..], "&#41;")) {
156-
try output.append(allocator, ')');
157-
i += 5;
158-
} else if (std.mem.startsWith(u8, input[i..], "&#43;")) {
159-
try output.append(allocator, '+');
160-
i += 5;
161-
} else if (std.mem.startsWith(u8, input[i..], "&#44;")) {
162-
try output.append(allocator, ',');
163-
i += 5;
164-
} else if (std.mem.startsWith(u8, input[i..], "&#46;")) {
165-
try output.append(allocator, '.');
166-
i += 5;
167-
} else if (std.mem.startsWith(u8, input[i..], "&#47;")) {
168-
try output.append(allocator, '/');
169-
i += 5;
170-
} else if (std.mem.startsWith(u8, input[i..], "&#58;")) {
171-
try output.append(allocator, ':');
172-
i += 5;
173-
} else if (std.mem.startsWith(u8, input[i..], "&#59;")) {
174-
try output.append(allocator, ';');
175-
i += 5;
176-
} else {
177-
try output.append(allocator, input[i]);
178-
i += 1;
138+
for (input) |char| {
139+
switch (char) {
140+
'<' => try output.appendSlice(allocator, "&lt;"),
141+
'>' => try output.appendSlice(allocator, "&gt;"),
142+
else => try output.append(allocator, char),
179143
}
180144
}
181145

0 commit comments

Comments
 (0)