1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
|
/*
* Copyright (C) 2007, 2008 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#if ENABLE(FTPDIR)
#include "FTPDirectoryDocument.h"
#include "ExceptionCodePlaceholder.h"
#include "HTMLDocumentParser.h"
#include "HTMLNames.h"
#include "HTMLTableElement.h"
#include "LocalizedStrings.h"
#include "Logging.h"
#include "FTPDirectoryParser.h"
#include "SegmentedString.h"
#include "Settings.h"
#include "SharedBuffer.h"
#include "Text.h"
#include <wtf/CurrentTime.h>
#include <wtf/GregorianDateTime.h>
#include <wtf/StdLibExtras.h>
#include <wtf/text/CString.h>
#include <wtf/text/WTFString.h>
#include <wtf/unicode/CharacterNames.h>
using namespace std;
namespace WebCore {
using namespace HTMLNames;
class FTPDirectoryDocumentParser : public HTMLDocumentParser {
public:
static PassRefPtr<FTPDirectoryDocumentParser> create(HTMLDocument* document)
{
return adoptRef(new FTPDirectoryDocumentParser(document));
}
virtual void append(PassRefPtr<StringImpl>);
virtual void finish();
virtual bool isWaitingForScripts() const { return false; }
inline void checkBuffer(int len = 10)
{
if ((m_dest - m_buffer) > m_size - len) {
// Enlarge buffer
int newSize = max(m_size * 2, m_size + len);
int oldOffset = m_dest - m_buffer;
m_buffer = static_cast<UChar*>(fastRealloc(m_buffer, newSize * sizeof(UChar)));
m_dest = m_buffer + oldOffset;
m_size = newSize;
}
}
private:
FTPDirectoryDocumentParser(HTMLDocument*);
// The parser will attempt to load the document template specified via the preference
// Failing that, it will fall back and create the basic document which will have a minimal
// table for presenting the FTP directory in a useful manner
bool loadDocumentTemplate();
void createBasicDocument();
void parseAndAppendOneLine(const String&);
void appendEntry(const String& name, const String& size, const String& date, bool isDirectory);
PassRefPtr<Element> createTDForFilename(const String&);
RefPtr<HTMLTableElement> m_tableElement;
bool m_skipLF;
int m_size;
UChar* m_buffer;
UChar* m_dest;
String m_carryOver;
ListState m_listState;
};
FTPDirectoryDocumentParser::FTPDirectoryDocumentParser(HTMLDocument* document)
: HTMLDocumentParser(document, false)
, m_skipLF(false)
, m_size(254)
, m_buffer(static_cast<UChar*>(fastMalloc(sizeof(UChar) * m_size)))
, m_dest(m_buffer)
{
}
void FTPDirectoryDocumentParser::appendEntry(const String& filename, const String& size, const String& date, bool isDirectory)
{
RefPtr<Element> rowElement = m_tableElement->insertRow(-1, IGNORE_EXCEPTION);
rowElement->setAttribute("class", "ftpDirectoryEntryRow", IGNORE_EXCEPTION);
RefPtr<Element> element = document()->createElement(tdTag, false);
element->appendChild(Text::create(document(), String(&noBreakSpace, 1)), IGNORE_EXCEPTION);
if (isDirectory)
element->setAttribute("class", "ftpDirectoryIcon ftpDirectoryTypeDirectory", IGNORE_EXCEPTION);
else
element->setAttribute("class", "ftpDirectoryIcon ftpDirectoryTypeFile", IGNORE_EXCEPTION);
rowElement->appendChild(element, IGNORE_EXCEPTION);
element = createTDForFilename(filename);
element->setAttribute("class", "ftpDirectoryFileName", IGNORE_EXCEPTION);
rowElement->appendChild(element, IGNORE_EXCEPTION);
element = document()->createElement(tdTag, false);
element->appendChild(Text::create(document(), date), IGNORE_EXCEPTION);
element->setAttribute("class", "ftpDirectoryFileDate", IGNORE_EXCEPTION);
rowElement->appendChild(element, IGNORE_EXCEPTION);
element = document()->createElement(tdTag, false);
element->appendChild(Text::create(document(), size), IGNORE_EXCEPTION);
element->setAttribute("class", "ftpDirectoryFileSize", IGNORE_EXCEPTION);
rowElement->appendChild(element, IGNORE_EXCEPTION);
}
PassRefPtr<Element> FTPDirectoryDocumentParser::createTDForFilename(const String& filename)
{
String fullURL = document()->baseURL().string();
if (fullURL[fullURL.length() - 1] == '/')
fullURL.append(filename);
else
fullURL.append("/" + filename);
RefPtr<Element> anchorElement = document()->createElement(aTag, false);
anchorElement->setAttribute("href", fullURL, IGNORE_EXCEPTION);
anchorElement->appendChild(Text::create(document(), filename), IGNORE_EXCEPTION);
RefPtr<Element> tdElement = document()->createElement(tdTag, false);
tdElement->appendChild(anchorElement, IGNORE_EXCEPTION);
return tdElement.release();
}
static String processFilesizeString(const String& size, bool isDirectory)
{
if (isDirectory)
return "--";
bool valid;
int64_t bytes = size.toUInt64(&valid);
if (!valid)
return unknownFileSizeText();
if (bytes < 1000000)
return String::format("%.2f KB", static_cast<float>(bytes)/1000);
if (bytes < 1000000000)
return String::format("%.2f MB", static_cast<float>(bytes)/1000000);
return String::format("%.2f GB", static_cast<float>(bytes)/1000000000);
}
static bool wasLastDayOfMonth(int year, int month, int day)
{
static int lastDays[] = { 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (month < 0 || month > 11)
return false;
if (month == 2) {
if (year % 4 == 0 && (year % 100 || year % 400 == 0)) {
if (day == 29)
return true;
return false;
}
if (day == 28)
return true;
return false;
}
return lastDays[month] == day;
}
static String processFileDateString(const FTPTime& fileTime)
{
// FIXME: Need to localize this string?
String timeOfDay;
if (!(fileTime.tm_hour == 0 && fileTime.tm_min == 0 && fileTime.tm_sec == 0)) {
int hour = fileTime.tm_hour;
ASSERT(hour >= 0 && hour < 24);
if (hour < 12) {
if (hour == 0)
hour = 12;
timeOfDay = String::format(", %i:%02i AM", hour, fileTime.tm_min);
} else {
hour = hour - 12;
if (hour == 0)
hour = 12;
timeOfDay = String::format(", %i:%02i PM", hour, fileTime.tm_min);
}
}
// If it was today or yesterday, lets just do that - but we have to compare to the current time
GregorianDateTime now;
now.setToCurrentLocalTime();
if (fileTime.tm_year == now.year()) {
if (fileTime.tm_mon == now.month()) {
if (fileTime.tm_mday == now.monthDay())
return "Today" + timeOfDay;
if (fileTime.tm_mday == now.monthDay() - 1)
return "Yesterday" + timeOfDay;
}
if (now.monthDay() == 1 && (now.month() == fileTime.tm_mon + 1 || (now.month() == 0 && fileTime.tm_mon == 11)) &&
wasLastDayOfMonth(fileTime.tm_year, fileTime.tm_mon, fileTime.tm_mday))
return "Yesterday" + timeOfDay;
}
if (fileTime.tm_year == now.year() - 1 && fileTime.tm_mon == 12 && fileTime.tm_mday == 31 && now.month() == 1 && now.monthDay() == 1)
return "Yesterday" + timeOfDay;
static const char* months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "???" };
int month = fileTime.tm_mon;
if (month < 0 || month > 11)
month = 12;
String dateString;
if (fileTime.tm_year > -1)
dateString = String(months[month]) + " " + String::number(fileTime.tm_mday) + ", " + String::number(fileTime.tm_year);
else
dateString = String(months[month]) + " " + String::number(fileTime.tm_mday) + ", " + String::number(now.year());
return dateString + timeOfDay;
}
void FTPDirectoryDocumentParser::parseAndAppendOneLine(const String& inputLine)
{
ListResult result;
CString latin1Input = inputLine.latin1();
FTPEntryType typeResult = parseOneFTPLine(latin1Input.data(), m_listState, result);
// FTPMiscEntry is a comment or usage statistic which we don't care about, and junk is invalid data - bail in these 2 cases
if (typeResult == FTPMiscEntry || typeResult == FTPJunkEntry)
return;
String filename(result.filename, result.filenameLength);
if (result.type == FTPDirectoryEntry) {
filename.append("/");
// We have no interest in linking to "current directory"
if (filename == "./")
return;
}
LOG(FTP, "Appending entry - %s, %s", filename.ascii().data(), result.fileSize.ascii().data());
appendEntry(filename, processFilesizeString(result.fileSize, result.type == FTPDirectoryEntry), processFileDateString(result.modifiedTime), result.type == FTPDirectoryEntry);
}
static inline PassRefPtr<SharedBuffer> createTemplateDocumentData(Settings* settings)
{
RefPtr<SharedBuffer> buffer = 0;
if (settings)
buffer = SharedBuffer::createWithContentsOfFile(settings->ftpDirectoryTemplatePath());
if (buffer)
LOG(FTP, "Loaded FTPDirectoryTemplate of length %i\n", buffer->size());
return buffer.release();
}
bool FTPDirectoryDocumentParser::loadDocumentTemplate()
{
DEFINE_STATIC_LOCAL(RefPtr<SharedBuffer>, templateDocumentData, (createTemplateDocumentData(document()->settings())));
// FIXME: Instead of storing the data, we'd rather actually parse the template data into the template Document once,
// store that document, then "copy" it whenever we get an FTP directory listing. There are complexities with this
// approach that make it worth putting this off.
if (!templateDocumentData) {
LOG_ERROR("Could not load templateData");
return false;
}
HTMLDocumentParser::insert(String(templateDocumentData->data(), templateDocumentData->size()));
RefPtr<Element> tableElement = document()->getElementById("ftpDirectoryTable");
if (!tableElement)
LOG_ERROR("Unable to find element by id \"ftpDirectoryTable\" in the template document.");
else if (!isHTMLTableElement(tableElement.get()))
LOG_ERROR("Element of id \"ftpDirectoryTable\" is not a table element");
else
m_tableElement = toHTMLTableElement(tableElement.get());
// Bail if we found the table element
if (m_tableElement)
return true;
// Otherwise create one manually
tableElement = document()->createElement(tableTag, false);
m_tableElement = toHTMLTableElement(tableElement.get());
m_tableElement->setAttribute("id", "ftpDirectoryTable", IGNORE_EXCEPTION);
// If we didn't find the table element, lets try to append our own to the body
// If that fails for some reason, cram it on the end of the document as a last
// ditch effort
if (Element* body = document()->body())
body->appendChild(m_tableElement, IGNORE_EXCEPTION);
else
document()->appendChild(m_tableElement, IGNORE_EXCEPTION);
return true;
}
void FTPDirectoryDocumentParser::createBasicDocument()
{
LOG(FTP, "Creating a basic FTP document structure as no template was loaded");
// FIXME: Make this "basic document" more acceptable
RefPtr<Element> bodyElement = document()->createElement(bodyTag, false);
document()->appendChild(bodyElement, IGNORE_EXCEPTION);
RefPtr<Element> tableElement = document()->createElement(tableTag, false);
m_tableElement = toHTMLTableElement(tableElement.get());
m_tableElement->setAttribute("id", "ftpDirectoryTable", IGNORE_EXCEPTION);
bodyElement->appendChild(m_tableElement, IGNORE_EXCEPTION);
}
void FTPDirectoryDocumentParser::append(PassRefPtr<StringImpl> inputSource)
{
String source(inputSource);
// Make sure we have the table element to append to by loading the template set in the pref, or
// creating a very basic document with the appropriate table
if (!m_tableElement) {
if (!loadDocumentTemplate())
createBasicDocument();
ASSERT(m_tableElement);
}
bool foundNewLine = false;
m_dest = m_buffer;
SegmentedString str = source;
while (!str.isEmpty()) {
UChar c = str.currentChar();
if (c == '\r') {
*m_dest++ = '\n';
foundNewLine = true;
// possibly skip an LF in the case of an CRLF sequence
m_skipLF = true;
} else if (c == '\n') {
if (!m_skipLF)
*m_dest++ = c;
else
m_skipLF = false;
} else {
*m_dest++ = c;
m_skipLF = false;
}
str.advance();
// Maybe enlarge the buffer
checkBuffer();
}
if (!foundNewLine) {
m_dest = m_buffer;
return;
}
UChar* start = m_buffer;
UChar* cursor = start;
while (cursor < m_dest) {
if (*cursor == '\n') {
m_carryOver.append(String(start, cursor - start));
LOG(FTP, "%s", m_carryOver.ascii().data());
parseAndAppendOneLine(m_carryOver);
m_carryOver = String();
start = ++cursor;
} else
cursor++;
}
// Copy the partial line we have left to the carryover buffer
if (cursor - start > 1)
m_carryOver.append(String(start, cursor - start - 1));
}
void FTPDirectoryDocumentParser::finish()
{
// Possible the last line in the listing had no newline, so try to parse it now
if (!m_carryOver.isEmpty()) {
parseAndAppendOneLine(m_carryOver);
m_carryOver = String();
}
m_tableElement = 0;
fastFree(m_buffer);
HTMLDocumentParser::finish();
}
FTPDirectoryDocument::FTPDirectoryDocument(Frame* frame, const KURL& url)
: HTMLDocument(frame, url)
{
#if !LOG_DISABLED
LogFTP.state = WTFLogChannelOn;
#endif
}
PassRefPtr<DocumentParser> FTPDirectoryDocument::createParser()
{
return FTPDirectoryDocumentParser::create(this);
}
}
#endif // ENABLE(FTPDIR)
|