Skip to content

Commit aa88480

Browse files
committed
trim CR and LF characters from both ends
1 parent 53c73c5 commit aa88480

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

src/url.zig

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -85,25 +85,27 @@ pub const URL = struct {
8585
base: []const u8,
8686
comptime opts: StitchOpts,
8787
) !StitchReturn(opts) {
88-
if (base.len == 0 or isCompleteHTTPUrl(path)) {
89-
return simpleStitch(allocator, path, opts);
88+
const trimmed_path = std.mem.trim(u8, path, &.{ '\n', '\r' });
89+
90+
if (base.len == 0 or isCompleteHTTPUrl(trimmed_path)) {
91+
return simpleStitch(allocator, trimmed_path, opts);
9092
}
9193

92-
if (path.len == 0) {
94+
if (trimmed_path.len == 0) {
9395
return simpleStitch(allocator, base, opts);
9496
}
9597

96-
if (std.mem.startsWith(u8, path, "//")) {
98+
if (std.mem.startsWith(u8, trimmed_path, "//")) {
9799
// network-path reference
98100
const index = std.mem.indexOfScalar(u8, base, ':') orelse {
99-
return simpleStitch(allocator, path, opts);
101+
return simpleStitch(allocator, trimmed_path, opts);
100102
};
101103

102104
const protocol = base[0..index];
103105
if (comptime opts.null_terminated) {
104-
return std.fmt.allocPrintSentinel(allocator, "{s}:{s}", .{ protocol, path }, 0);
106+
return std.fmt.allocPrintSentinel(allocator, "{s}:{s}", .{ protocol, trimmed_path }, 0);
105107
}
106-
return std.fmt.allocPrint(allocator, "{s}:{s}", .{ protocol, path });
108+
return std.fmt.allocPrint(allocator, "{s}:{s}", .{ protocol, trimmed_path });
107109
}
108110

109111
// Quick hack because domains have to be at least 3 characters.
@@ -117,11 +119,11 @@ pub const URL = struct {
117119
root = base[0 .. pos + protocol_end];
118120
}
119121

120-
if (path[0] == '/') {
122+
if (trimmed_path[0] == '/') {
121123
if (comptime opts.null_terminated) {
122-
return std.fmt.allocPrintSentinel(allocator, "{s}{s}", .{ root, path }, 0);
124+
return std.fmt.allocPrintSentinel(allocator, "{s}{s}", .{ root, trimmed_path }, 0);
123125
}
124-
return std.fmt.allocPrint(allocator, "{s}{s}", .{ root, path });
126+
return std.fmt.allocPrint(allocator, "{s}{s}", .{ root, trimmed_path });
125127
}
126128

127129
var old_path = std.mem.trimStart(u8, base[root.len..], "/");
@@ -133,7 +135,7 @@ pub const URL = struct {
133135

134136
// We preallocate all of the space possibly needed.
135137
// This is the root, old_path, new path, 3 slashes and perhaps a null terminated slot.
136-
var out = try allocator.alloc(u8, root.len + old_path.len + path.len + 3 + if (comptime opts.null_terminated) 1 else 0);
138+
var out = try allocator.alloc(u8, root.len + old_path.len + trimmed_path.len + 3 + if (comptime opts.null_terminated) 1 else 0);
137139
var end: usize = 0;
138140
@memmove(out[0..root.len], root);
139141
end += root.len;
@@ -146,8 +148,8 @@ pub const URL = struct {
146148
out[end] = '/';
147149
end += 1;
148150
}
149-
@memmove(out[end .. end + path.len], path);
150-
end += path.len;
151+
@memmove(out[end .. end + trimmed_path.len], trimmed_path);
152+
end += trimmed_path.len;
151153

152154
var read: usize = root.len;
153155
var write: usize = root.len;

0 commit comments

Comments
 (0)