Alleged Literature >> Damian Cugley >> 2003 >> May

Damian Cugley’s Archive

Backslashes in URLs

Sat. 17 May 2003

Had a bug in the Picky Picky Game where uploaded pictures might have backslashes left in their names (the picture name being derived from the file name supplied by the client computer). Technically it is OK to have backslashes in a URL, and they should be treated like any other character. Some web browsers second-guess you, however, and replace backslashes with slashes (http://foo/bar\baz is treated as http://foo/bar/baz), with the result that these pictures failed to appear.

The solution is, of course, to (a) change the code for translating file names in to picture names so that it removes backslashes, and (b) fix the existing databases. ZODB makes the second part pretty easy; having acquired a Game instance from the databse, you just run a script like

for rn, r in game.rounds.items():
    for pic in r.pictures:
	s = r.sanitizedName(pic.name, pic)
	if s != pic.name:
	    pic.name = s
	    pic.dataUri = s + picky.mediaTypeSuffix(pic.mediaType)

The function sanitizedName is the one that has to be fixed for part (a).