mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-12-16 08:18:59 +00:00
trim CR and LF characters from both ends
This commit is contained in:
28
src/url.zig
28
src/url.zig
@@ -85,25 +85,27 @@ pub const URL = struct {
|
|||||||
base: []const u8,
|
base: []const u8,
|
||||||
comptime opts: StitchOpts,
|
comptime opts: StitchOpts,
|
||||||
) !StitchReturn(opts) {
|
) !StitchReturn(opts) {
|
||||||
if (base.len == 0 or isCompleteHTTPUrl(path)) {
|
const trimmed_path = std.mem.trim(u8, path, &.{ '\n', '\r' });
|
||||||
return simpleStitch(allocator, path, opts);
|
|
||||||
|
if (base.len == 0 or isCompleteHTTPUrl(trimmed_path)) {
|
||||||
|
return simpleStitch(allocator, trimmed_path, opts);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (path.len == 0) {
|
if (trimmed_path.len == 0) {
|
||||||
return simpleStitch(allocator, base, opts);
|
return simpleStitch(allocator, base, opts);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (std.mem.startsWith(u8, path, "//")) {
|
if (std.mem.startsWith(u8, trimmed_path, "//")) {
|
||||||
// network-path reference
|
// network-path reference
|
||||||
const index = std.mem.indexOfScalar(u8, base, ':') orelse {
|
const index = std.mem.indexOfScalar(u8, base, ':') orelse {
|
||||||
return simpleStitch(allocator, path, opts);
|
return simpleStitch(allocator, trimmed_path, opts);
|
||||||
};
|
};
|
||||||
|
|
||||||
const protocol = base[0..index];
|
const protocol = base[0..index];
|
||||||
if (comptime opts.null_terminated) {
|
if (comptime opts.null_terminated) {
|
||||||
return std.fmt.allocPrintSentinel(allocator, "{s}:{s}", .{ protocol, path }, 0);
|
return std.fmt.allocPrintSentinel(allocator, "{s}:{s}", .{ protocol, trimmed_path }, 0);
|
||||||
}
|
}
|
||||||
return std.fmt.allocPrint(allocator, "{s}:{s}", .{ protocol, path });
|
return std.fmt.allocPrint(allocator, "{s}:{s}", .{ protocol, trimmed_path });
|
||||||
}
|
}
|
||||||
|
|
||||||
// Quick hack because domains have to be at least 3 characters.
|
// Quick hack because domains have to be at least 3 characters.
|
||||||
@@ -117,11 +119,11 @@ pub const URL = struct {
|
|||||||
root = base[0 .. pos + protocol_end];
|
root = base[0 .. pos + protocol_end];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (path[0] == '/') {
|
if (trimmed_path[0] == '/') {
|
||||||
if (comptime opts.null_terminated) {
|
if (comptime opts.null_terminated) {
|
||||||
return std.fmt.allocPrintSentinel(allocator, "{s}{s}", .{ root, path }, 0);
|
return std.fmt.allocPrintSentinel(allocator, "{s}{s}", .{ root, trimmed_path }, 0);
|
||||||
}
|
}
|
||||||
return std.fmt.allocPrint(allocator, "{s}{s}", .{ root, path });
|
return std.fmt.allocPrint(allocator, "{s}{s}", .{ root, trimmed_path });
|
||||||
}
|
}
|
||||||
|
|
||||||
var old_path = std.mem.trimStart(u8, base[root.len..], "/");
|
var old_path = std.mem.trimStart(u8, base[root.len..], "/");
|
||||||
@@ -133,7 +135,7 @@ pub const URL = struct {
|
|||||||
|
|
||||||
// We preallocate all of the space possibly needed.
|
// We preallocate all of the space possibly needed.
|
||||||
// This is the root, old_path, new path, 3 slashes and perhaps a null terminated slot.
|
// This is the root, old_path, new path, 3 slashes and perhaps a null terminated slot.
|
||||||
var out = try allocator.alloc(u8, root.len + old_path.len + path.len + 3 + if (comptime opts.null_terminated) 1 else 0);
|
var out = try allocator.alloc(u8, root.len + old_path.len + trimmed_path.len + 3 + if (comptime opts.null_terminated) 1 else 0);
|
||||||
var end: usize = 0;
|
var end: usize = 0;
|
||||||
@memmove(out[0..root.len], root);
|
@memmove(out[0..root.len], root);
|
||||||
end += root.len;
|
end += root.len;
|
||||||
@@ -146,8 +148,8 @@ pub const URL = struct {
|
|||||||
out[end] = '/';
|
out[end] = '/';
|
||||||
end += 1;
|
end += 1;
|
||||||
}
|
}
|
||||||
@memmove(out[end .. end + path.len], path);
|
@memmove(out[end .. end + trimmed_path.len], trimmed_path);
|
||||||
end += path.len;
|
end += trimmed_path.len;
|
||||||
|
|
||||||
var read: usize = root.len;
|
var read: usize = root.len;
|
||||||
var write: usize = root.len;
|
var write: usize = root.len;
|
||||||
|
|||||||
Reference in New Issue
Block a user