1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* ----- BEGIN LICENSE BLOCK -----
3
* Version: NPL 1.1/GPL 2.0/LGPL 2.1
5
* The contents of this file are subject to the Netscape Public License Version
6
* 1.1 (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
8
* http://www.mozilla.org/NPL/
10
* Software distributed under the License is distributed on an "AS IS" basis,
11
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12
* for the specific language governing rights and limitations under the
15
* The Original Code is the Mozilla Communicator client code.
17
* The Initial Developer of the Original Code is Netscape Communications
19
* Portions created by Netscape Communications Corporation are
20
* Copyright (C) 1998-2001 Netscape Communications Corporation.
21
* All Rights Reserved.
24
* Chris Waterson <waterson@netscape.com>
25
* Robert John Churchill <rjc@netscape.com>
26
* Pierre Phaneuf <pp@ludusdesign.com>
27
* Bradley Baetz <bbaetz@cs.mcgill.ca>
29
* Alternatively, the contents of this file may be used under the terms of
30
* either of the GNU General Public License Version 2 or later (the "GPL"),
31
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
32
* in which case the provisions of the GPL or the LGPL are applicable instead
33
* of those above. If you wish to allow use of your version of this file only
34
* under the terms of either the GPL or the LGPL, and not to allow others to
35
* use your version of this file under the terms of the MPL, indicate your
36
* decision by deleting the provisions above and replace them with the notice
37
* and other provisions required by the LGPL or the GPL. If you do not delete
38
* the provisions above, a recipient may use your version of this file under
39
* the terms of any one of the MPL, the GPL or the LGPL.
41
* ----- END LICENSE BLOCK ----- */
43
/* This parsing code originally lived in xpfe/components/directory/ - bbaetz */
47
#include "nsDirIndexParser.h"
48
#include "nsReadableUtils.h"
49
#include "nsDirIndex.h"
51
#include "nsIServiceManager.h"
52
#include "nsIInputStream.h"
53
#include "nsIChannel.h"
56
#include "nsIPrefService.h"
57
#include "nsIPrefBranch.h"
58
#include "nsIPrefLocalizedString.h"
60
NS_IMPL_THREADSAFE_ISUPPORTS3(nsDirIndexParser,
65
nsDirIndexParser::nsDirIndexParser() {
69
nsDirIndexParser::Init() {
71
mHasDescription = PR_FALSE;
74
// get default charset to be used for directory listings (fallback to
75
// ISO-8859-1 if pref is unavailable).
76
NS_NAMED_LITERAL_CSTRING(kFallbackEncoding, "ISO-8859-1");
77
nsXPIDLString defCharset;
78
nsCOMPtr<nsIPrefBranch> prefs(do_GetService(NS_PREFSERVICE_CONTRACTID));
80
nsCOMPtr<nsIPrefLocalizedString> prefVal;
81
prefs->GetComplexValue("intl.charset.default",
82
NS_GET_IID(nsIPrefLocalizedString),
83
getter_AddRefs(prefVal));
85
prefVal->ToString(getter_Copies(defCharset));
87
if (!defCharset.IsEmpty())
88
LossyCopyUTF16toASCII(defCharset, mEncoding); // charset labels are always ASCII
90
mEncoding.Assign(kFallbackEncoding);
94
if (gRefCntParser++ == 0)
95
rv = nsServiceManager::GetService(NS_ITEXTTOSUBURI_CONTRACTID,
96
NS_GET_IID(nsITextToSubURI),
97
NS_REINTERPRET_CAST(nsISupports**, &gTextToSubURI));
104
nsDirIndexParser::~nsDirIndexParser() {
106
// XXX not threadsafe
107
if (--gRefCntParser == 0) {
108
NS_IF_RELEASE(gTextToSubURI);
113
nsDirIndexParser::SetListener(nsIDirIndexListener* aListener) {
114
mListener = aListener;
119
nsDirIndexParser::GetListener(nsIDirIndexListener** aListener) {
120
NS_IF_ADDREF(*aListener = mListener.get());
125
nsDirIndexParser::GetComment(char** aComment) {
126
*aComment = ToNewCString(mComment);
129
return NS_ERROR_OUT_OF_MEMORY;
135
nsDirIndexParser::SetEncoding(const char* aEncoding) {
136
mEncoding.Assign(aEncoding);
141
nsDirIndexParser::GetEncoding(char** aEncoding) {
142
*aEncoding = ToNewCString(mEncoding);
145
return NS_ERROR_OUT_OF_MEMORY;
151
nsDirIndexParser::OnStartRequest(nsIRequest* aRequest, nsISupports* aCtxt) {
156
nsDirIndexParser::OnStopRequest(nsIRequest *aRequest, nsISupports *aCtxt,
157
nsresult aStatusCode) {
159
if (mBuf.Length() > (PRUint32) mLineStart) {
160
ProcessData(aRequest, aCtxt);
166
nsDirIndexParser::Field
167
nsDirIndexParser::gFieldTable[] = {
168
{ "Filename", FIELD_FILENAME },
169
{ "Description", FIELD_DESCRIPTION },
170
{ "Content-Length", FIELD_CONTENTLENGTH },
171
{ "Last-Modified", FIELD_LASTMODIFIED },
172
{ "Content-Type", FIELD_CONTENTTYPE },
173
{ "File-Type", FIELD_FILETYPE },
174
{ nsnull, FIELD_UNKNOWN }
177
nsrefcnt nsDirIndexParser::gRefCntParser = 0;
178
nsITextToSubURI *nsDirIndexParser::gTextToSubURI;
181
nsDirIndexParser::ParseFormat(const char* aFormatStr) {
182
// Parse a "200" format line, and remember the fields and their
183
// ordering in mFormat. Multiple 200 lines stomp on each other.
187
// Lets find out how many elements we have.
188
// easier to do this then realloc
189
const char* pos = aFormatStr;
192
while (*pos && nsCRT::IsAsciiSpace(PRUnichar(*pos)))
200
while (*pos && !nsCRT::IsAsciiSpace(PRUnichar(*pos)))
205
mFormat = new int[num+1];
210
while (*aFormatStr && nsCRT::IsAsciiSpace(PRUnichar(*aFormatStr)))
218
while (aFormatStr[len] && !nsCRT::IsAsciiSpace(PRUnichar(aFormatStr[len])))
220
name.SetCapacity(len + 1);
221
name.Append(aFormatStr, len);
224
// Okay, we're gonna monkey with the nsStr. Bold!
225
name.SetLength(nsUnescapeCount(name.BeginWriting()));
227
// All tokens are case-insensitive - http://www.area.com/~roeber/file_format.html
228
if (name.EqualsIgnoreCase("description"))
229
mHasDescription = PR_TRUE;
231
for (Field* i = gFieldTable; i->mName; ++i) {
232
if (name.EqualsIgnoreCase(i->mName)) {
233
mFormat[formatNum] = i->mType;
239
} while (*aFormatStr);
245
nsDirIndexParser::ParseData(nsIDirIndex *aIdx, char* aDataStr) {
246
// Parse a "201" data line, using the field ordering specified in
250
// Ignore if we haven't seen a format yet.
256
nsCAutoString filename;
258
for (PRInt32 i = 0; mFormat[i] != -1; ++i) {
259
// If we've exhausted the data before we run out of fields, just
264
while (*aDataStr && nsCRT::IsAsciiSpace(*aDataStr))
267
char *value = aDataStr;
269
if (*aDataStr == '"' || *aDataStr == '\'') {
270
// it's a quoted string. snarf everything up to the next quote character
271
const char quotechar = *(aDataStr++);
273
while (*aDataStr && *aDataStr != quotechar)
278
NS_WARNING("quoted value not terminated");
281
// it's unquoted. snarf until we see whitespace.
283
while (*aDataStr && (!nsCRT::IsAsciiSpace(*aDataStr)))
288
fieldType t = fieldType(mFormat[i]);
290
case FIELD_FILENAME: {
291
// don't unescape at this point, so that UnEscapeAndConvert() can
294
PRBool success = PR_FALSE;
296
nsAutoString entryuri;
299
PRUnichar *result = nsnull;
300
if (NS_SUCCEEDED(rv = gTextToSubURI->UnEscapeAndConvert(mEncoding.get(), filename.get(),
301
&result)) && (result)) {
303
aIdx->SetLocation(filename.get());
304
if (!mHasDescription)
305
aIdx->SetDescription(result);
310
NS_WARNING("UnEscapeAndConvert error");
314
if (success == PR_FALSE) {
315
// if unsuccessfully at charset conversion, then
316
// just fallback to unescape'ing in-place
317
// XXX - this shouldn't be using UTF8, should it?
318
// when can we fail to get the service, anyway? - bbaetz
319
aIdx->SetLocation(filename.get());
320
if (!mHasDescription) {
321
aIdx->SetDescription(NS_ConvertUTF8toUCS2(value).get());
326
case FIELD_DESCRIPTION:
328
aIdx->SetDescription(NS_ConvertUTF8toUCS2(value).get());
330
case FIELD_CONTENTLENGTH:
333
PRInt32 status = PR_sscanf(value, "%lld", &len);
337
aIdx->SetSize(LL_INIT(0, -1)); // -1 means unknown
340
case FIELD_LASTMODIFIED:
344
if (PR_ParseTimeString(value, PR_FALSE, &tm) == PR_SUCCESS) {
345
aIdx->SetLastModified(tm);
349
case FIELD_CONTENTTYPE:
350
aIdx->SetContentType(value);
355
if (!nsCRT::strcasecmp(value, "directory")) {
356
aIdx->SetType(nsIDirIndex::TYPE_DIRECTORY);
357
} else if (!nsCRT::strcasecmp(value, "file")) {
358
aIdx->SetType(nsIDirIndex::TYPE_FILE);
359
} else if (!nsCRT::strcasecmp(value, "symbolic-link")) {
360
aIdx->SetType(nsIDirIndex::TYPE_SYMLINK);
362
aIdx->SetType(nsIDirIndex::TYPE_UNKNOWN);
375
nsDirIndexParser::OnDataAvailable(nsIRequest *aRequest, nsISupports *aCtxt,
376
nsIInputStream *aStream,
377
PRUint32 aSourceOffset,
382
PRInt32 len = mBuf.Length();
384
// Ensure that our mBuf has capacity to hold the data we're about to
386
mBuf.SetCapacity(len + aCount + 1);
388
return NS_ERROR_OUT_OF_MEMORY;
390
// Now read the data into our buffer.
393
rv = aStream->Read(mBuf.BeginWriting() + len, aCount, &count);
394
if (NS_FAILED(rv)) return rv;
396
// Set the string's length according to the amount of data we've read.
397
// Note: we know this to work on nsCString. This isn't guaranteed to
398
// work on other strings.
399
mBuf.SetLength(len + count);
401
return ProcessData(aRequest, aCtxt);
405
nsDirIndexParser::ProcessData(nsIRequest *aRequest, nsISupports *aCtxt) {
407
return NS_ERROR_FAILURE;
409
PRInt32 numItems = 0;
414
PRInt32 eol = mBuf.FindCharInSet("\n\r", mLineStart);
416
mBuf.SetCharAt(PRUnichar('\0'), eol);
418
const char *line = mBuf.get() + mLineStart;
420
PRInt32 lineLen = eol - mLineStart;
421
mLineStart = eol + 1;
425
const char *buf = line;
429
if (buf[2] == '0' && buf[3] == ':') {
430
// 100. Human-readable comment line. Ignore
431
} else if (buf[2] == '1' && buf[3] == ':') {
432
// 101. Human-readable information line.
433
mComment.Append(buf + 4);
434
} else if (buf[2] == '2' && buf[3] == ':') {
435
// 102. Human-readable information line, HTML.
436
mComment.Append(buf + 4);
439
} else if (buf[0] == '2') {
441
if (buf[2] == '0' && buf[3] == ':') {
442
// 200. Define field names
443
rv = ParseFormat(buf + 4);
447
} else if (buf[2] == '1' && buf[3] == ':') {
449
nsCOMPtr<nsIDirIndex> idx = do_CreateInstance("@mozilla.org/dirIndex;1",&rv);
453
rv = ParseData(idx, ((char *)buf) + 4);
458
mListener->OnIndexAvailable(aRequest, aCtxt, idx);
461
} else if (buf[0] == '3') {
463
if (buf[2] == '0' && buf[3] == ':') {
464
// 300. Self-referring URL
465
} else if (buf[2] == '1' && buf[3] == ':') {
466
// 301. OUR EXTENSION - encoding
468
while (buf[i] && nsCRT::IsAsciiSpace(buf[i]))