@@ -125,16 +125,16 @@ pub const URL = struct {
125
125
}
126
126
};
127
127
128
+ const normalized_src = if (src [0 ] == '/' ) src [1.. ] else src ;
129
+
128
130
if (std .mem .lastIndexOfScalar (u8 , base [protocol_end .. ], '/' )) | index | {
129
131
const last_slash_pos = index + protocol_end ;
130
132
if (last_slash_pos == base .len - 1 ) {
131
- return std .fmt .allocPrint (allocator , "{s}{s}" , .{ base , src });
132
- } else {
133
- return std .fmt .allocPrint (allocator , "{s}/{s}" , .{ base [0.. last_slash_pos ], src });
133
+ return std .fmt .allocPrint (allocator , "{s}{s}" , .{ base , normalized_src });
134
134
}
135
- } else {
136
- return std .fmt .allocPrint (allocator , "{s}/{s}" , .{ base , src });
135
+ return std .fmt .allocPrint (allocator , "{s}/{s}" , .{ base [0.. last_slash_pos ], normalized_src });
137
136
}
137
+ return std .fmt .allocPrint (allocator , "{s}/{s}" , .{ base , normalized_src });
138
138
}
139
139
140
140
pub fn concatQueryString (arena : Allocator , url : []const u8 , query_string : []const u8 ) ! []const u8 {
@@ -233,6 +233,16 @@ test "URL: Stitching Base & Src URLs (Just Ending Slash)" {
233
233
try testing .expectString ("https://www.google.com/something.js" , result );
234
234
}
235
235
236
+ test "URL: Stitching Base & Src URLs with leading slash" {
237
+ const allocator = testing .allocator ;
238
+
239
+ const base = "https://www.google.com/" ;
240
+ const src = "/something.js" ;
241
+ const result = try URL .stitch (allocator , src , base , .{});
242
+ defer allocator .free (result );
243
+ try testing .expectString ("https://www.google.com/something.js" , result );
244
+ }
245
+
236
246
test "URL: Stitching Base & Src URLs (No Ending Slash)" {
237
247
const allocator = testing .allocator ;
238
248
0 commit comments