February 7, 2000
By Karen Kenworthy
IN THIS ISSUE
Many thanks to those of you who sent warm messages last week, after my snow day! My mom enjoyed all the birthday wishes. And special thanks go to Norm, for the recipe for Teddybear Bread. I can't wait to try it, though I don't know if I'll be able to bear eating a loaf shaped like my favorite Teddy. :)
But if you're hungry, I have good news. Those of you who couldn't wait to get your hands on tasty Netscape morsels can finally have your fill. Yes, the newest Winmag.com Cookie Viewer now deletes Netscape Navigator web browser cookies!
All my Cookie Viewers since version 2.0 could delete Microsoft Internet Explorer (MSIE) cookies. Deleting those cookies was especially easy, since each MSIE cookie resides in its own file. Deleting the file deletes the cookie.
But Netscape's method of storing cookies made deleting them more difficult. Netscape stores all of a user's cookies in a single text file named cookies.txt. Within that file, each cookie occupies a single line. To delete one of these cookies, the whole file must be re- written, omitting the deleted cookie's line.
Now you may be thinking, "Karen, that doesn't sound so hard." And you're right, it doesn't *sound* difficult. But, as is often the case, the devil is in the details. Actually deleting Netscape cookies safely turns out to pose several interesting problems.
Now I'm not complaining. Solving these kinds of problems is one of three reasons computer programmers get the big money (the other two are our natural good looks, and winsome personalities). But if you've got a moment, I'd like to bend your ear ...
For starters, the new Cookie Viewer must backup Netscape's cookie file, before making any changes. That's because an errant change made to the single cookie file could possibly damage the remaining cookies. And nobody wants a corrupted cookie jar, err, file.
Reader Martina pointed the way. Why not, she wrote, "Backup the current cookie.txt file, then delete lines. One backup file per Cookie Viewer session, named cookie.001, cookie.002, etc."
That's almost what I did. The new Cookie Viewer creates a backup copy of the Netscape cookie file, each time you delete a batch of Netscape cookies. Each backup cookie file contains all the information found in the original Netscape cookie file, at the time the backup was made. To keep the number of backup files from growing out of hand, the new Cookie Viewer keeps a maximum of 10 copies of the Netscape cookie file, with names like cookies.bk0, cookies.bk1, cookies.bk2, etc.
After creating a backup file named cookies.bk9, the sequence of backup cookie filenames starts over with cookie.bk0. However, the Cookie Viewer will never overwrite a backup cookie file made "today". If ten backup cookie files already exist, and the oldest one was created earlier in the day, the Cookie Viewer will not create a new backup cookie file. Instead, it trusts that today's ten backup files already on your disk are sufficient.
Once we've chosen a name, creating a backup cookie file ought to be easy, right? After all, the Netscape cookie file looks, at first glance, to be an ordinary text file. Maybe it's the .TXT filename extension. Or perhaps the way it appears in Notepad, showing nothing but familiar, printable characters laid out in nice, orderly rows. But don't let this innocent appearance fool you. The Netscape Cookie file is weird.
Normally, each line of a text file ends with two special, and invisible, characters. For reasons that date back to the printing Teletype (when we carried bits five miles, in the snow, up hill, in wooden buckets), these characters are known as Carriage Return (CR) and Line Feed (LF). They signaled the end of a text line by ordering the Teletype's printing carriage to return to its starting position (usually, the far left side), and feed its roll of paper upwards one line.
But Netscape cookie files aren't normal. Instead of ending with one CR character, followed by one LF character, the lines of these files end two CR characters in a row, followed by a single LF character. In other words, the lines of text inside Netscape cookie files end with CR, CR, LF, instead of just CR, LF.
Apparently, this doesn't confuse Netscape's Navigator web browser. But it does confuse many other programs. Some programs think the cookie file is "double-spaced", with an extra blank line between each actual line. Others think the entire cookie file contains just one long line, with a few invisible characters scattered along the way.
Windows Notepad is one of the latter. At first, it appears to interpret a Netscape cookie file correctly. But after loading the file, ask Notepad to disable, then re-enable its "Word Wrap" feature (via Notepad's Edit menu). You'll then see the nicely spaced lines of cookie contents become one long jumble. Notepad has lost its way in the Netscape cookie jungle.
This peculiarity means the usual methods of reading, writing, and copying text files can't be used when accessing the Netscape cookie file. The cookie file must instead be treated as a "binary" file (one that can contain any characters, in any sequence). Extra care must be taken to properly interpret, and preserve, Netscape's special line- ending codes, especially when creating a modified cookie file.
Because Netscape cookie files are weird, the Cookie Viewer uses Visual Basic's built-in FileCopy function to create each backup cookie file. This function doesn't care what's stored in the files it's asked to copy. It dutifully copies the complete contents of each file, whether it contains text or binary data.
Unfortunately, FileCopy also copies each of a file's attributes, such as whether the file is read-only, hidden, or a system file. It also copies the original file's date and time of "last modification." As a result, even though backup cookie files are created at a later time, they all appear to be the same age as the original cookie file.
But the Cookie Viewer must know the exact date and time each backup cookie file was made, to ensure that the oldest backup files are overwritten first, and to prevent itself from overwriting a backup file created today. To do that, the Cookie Viewer must play a trick on Windows ...
Right after each backup cookie file is created by FileCopy, the Cookie Viewer pretends to alter the new file. First, it opens the file and reads its first four bytes. The Viewer then writes those same bytes back to the beginning of the file, and closes the file. Although the file's contents have not actually been changed by this ruse, Windows thinks they have, and updates its records accordingly!
Want more? OK. How about this headache: Although every Netscape cookie occupies a line in the cookie file, not every line holds a cookie. That's because some lines in file store important comments (such as a warning to never alter the cookie file). Other lines are simply blank. The Cookie Viewer must ignore these non-cookie lines when displaying or deleting cookies. But they must be carefully preserved each time the cookie file is re-written.
And sometimes it's impossible to rewrite the Netscape cookie file. If Netscape Navigator is running at the same time as the Cookie Viewer, the cookie file will be in use and cannot be changed by my program. Some people write-protect the Netscape cookie file, to prevent new cookies from being stored there. Unfortunately, this also prevents programs from making other changes to the file, such as deleting existing cookies.
Whatever the reason, if the Netscape cookie file cannot be rewritten, the new Cookie Viewer will display a warning message. It will also give you a chance to correct the problem. If you do so, the requested cookie deletion will finally take place once you click the Cookie Viewer's Retry button.
<taking a deep breath> ... OK ... I feel better now. Thanks for letting me get that off my chest. :)
Deleting Netscape cookies isn't the only thing we've been up to this week, here at the secluded Power Tool workshop. The new Cookie Viewer v2.3 can also display and delete cookies created by Microsoft Internet Explorer v5.5 (even though that browser is still in beta testing). I also fixed a bug, reported by reader Bill, that caused the Cookie Viewer to choke when trying to digest large (greater than 32KB) Netscape cookie files. All in all, it's been a busy week.
If you'd like to see the fruits of my labors, drop by my Cookie Viewer web page at https://www.karenware.com/powertools/ptcookie. There you'll be able to download the latest Cookie Viewer. And as always, the program is free, and so is its Visual Basic source code. And don't forget to visit my free Cookie Dispenser at /dispenser.asp.
In the meantime, keep those cards and letters coming. I love reading them (especially when they contain cookie recipes!), even though I can't reply to every one. And if you see me on the 'Net this week, be sure to wave and say "Hi!"
Power Tools Newsletter
- Directory Printer v5.4.4 Adds Unicode Characters Support
- Replicator v3.7.6 Eradicates Bug that caused Error 3
31515 Verified Subscribers
Subscribe to receive new issues of the newsletter about Karen and her free Power Tools.Click here to Subscribe