You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
346 lines
9.2 KiB
346 lines
9.2 KiB
6 years ago
|
/****************************************************************************
|
||
|
* Copyright (C) 2003-2008 by Albert Astals Cid *
|
||
|
* aacid@kde.org *
|
||
|
* *
|
||
|
* This program is free software; you can redistribute it and/or modify *
|
||
|
* it under the terms of the GNU General Public License as published by *
|
||
|
* the Free Software Foundation; either version 2 of the License, or *
|
||
|
* (at your option) any later version. *
|
||
|
****************************************************************************/
|
||
|
|
||
6 years ago
|
#include "tdeio_gopher.h"
|
||
6 years ago
|
|
||
6 years ago
|
#include <stdlib.h>
|
||
|
#include <kurl.h>
|
||
|
#include <kdebug.h>
|
||
|
#include <kmdcodec.h>
|
||
|
#include <kinstance.h>
|
||
6 years ago
|
#include <kdebug.h>
|
||
6 years ago
|
#include <tdelocale.h>
|
||
6 years ago
|
#include <kmimetype.h>
|
||
|
|
||
6 years ago
|
#include <ntqbuffer.h>
|
||
|
#include <ntqfile.h>
|
||
6 years ago
|
|
||
6 years ago
|
using namespace TDEIO;
|
||
6 years ago
|
|
||
|
extern "C"
|
||
|
{
|
||
|
int KDE_EXPORT kdemain( int argc, char **argv )
|
||
|
{
|
||
6 years ago
|
TDEInstance instance( "tdeio_gopher" );
|
||
6 years ago
|
|
||
|
if (argc != 4)
|
||
|
{
|
||
6 years ago
|
fprintf(stderr, "Usage: tdeio_gopher protocol domain-socket1 domain-socket2\n");
|
||
6 years ago
|
exit(-1);
|
||
|
}
|
||
|
|
||
6 years ago
|
GopherProtocol slave(argv[2], argv[3]);
|
||
6 years ago
|
slave.dispatchLoop();
|
||
|
return 0;
|
||
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
|
||
6 years ago
|
/* gopher */
|
||
|
|
||
6 years ago
|
GopherProtocol::GopherProtocol(const TQCString &pool_socket, const TQCString &app_socket)
|
||
|
: TCPSlaveBase(70, "gopher", pool_socket, app_socket)
|
||
6 years ago
|
{
|
||
|
}
|
||
|
|
||
6 years ago
|
void GopherProtocol::get(const KURL& url )
|
||
6 years ago
|
{
|
||
|
// gopher urls are
|
||
|
// gopher://<host>:<port>/<gopher-path>
|
||
|
//
|
||
|
// where <gopher-path> is one of
|
||
|
//
|
||
|
// <gophertype><selector>
|
||
|
// <gophertype><selector>%09<search>
|
||
|
// <gophertype><selector>%09<search>%09<gopher+_string>
|
||
|
int port;
|
||
6 years ago
|
TQChar type;
|
||
|
TQString path(url.path());
|
||
|
TQString query(url.query());
|
||
6 years ago
|
|
||
|
// determine the type
|
||
|
if (path != "/" && path != "") type = path[1];
|
||
|
else type = '1';
|
||
|
|
||
|
// determine the port
|
||
|
if (url.port() > 0) port = url.port();
|
||
|
else port = 70;
|
||
|
|
||
6 years ago
|
setBlockConnection(true);
|
||
|
|
||
6 years ago
|
// connect to the host
|
||
6 years ago
|
if (!connectToHost(url.host(), port)) return;
|
||
6 years ago
|
|
||
|
if (type == '7' && query.isNull())
|
||
|
{
|
||
6 years ago
|
closeDescriptor();
|
||
6 years ago
|
handleSearch(url.host(), path, port);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
int i, bytes;
|
||
|
char aux[10240];
|
||
6 years ago
|
TQBuffer received;
|
||
6 years ago
|
received.open(IO_WriteOnly);
|
||
6 years ago
|
|
||
6 years ago
|
infoMessage(i18n("Connecting to %1...").arg(url.host()));
|
||
|
infoMessage(i18n("%1 contacted. Retrieving data...").arg(url.host()));
|
||
6 years ago
|
bytes = 0;
|
||
6 years ago
|
|
||
6 years ago
|
// send the selector
|
||
|
path.remove(0, 2);
|
||
6 years ago
|
write(path.latin1(), path.length());
|
||
|
write(query.latin1(), query.length());
|
||
6 years ago
|
write("\r\n", 2);
|
||
|
|
||
|
// read the data
|
||
|
while((i = read(aux, 10240)) > 0)
|
||
|
{
|
||
|
bytes += i;
|
||
6 years ago
|
received.writeBlock(aux, i);
|
||
6 years ago
|
processedSize(bytes);
|
||
6 years ago
|
infoMessage(i18n("Retrieved %1 bytes from %2...").arg(bytes).arg(url.host()));
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
if (type == '1' || type =='7')
|
||
|
{
|
||
|
processDirectory(received.buffer().data(), url.host(), url.path());
|
||
|
}
|
||
6 years ago
|
else
|
||
|
{
|
||
|
KMimeType::Ptr result = KMimeType::findByContent(received.buffer());
|
||
|
mimeType(result->name());
|
||
|
data(received.buffer());
|
||
|
}
|
||
6 years ago
|
closeDescriptor();
|
||
6 years ago
|
}
|
||
|
finished();
|
||
|
}
|
||
|
|
||
6 years ago
|
void GopherProtocol::processDirectory(const TQString &received_str, const TQString &host, const TQString &path)
|
||
6 years ago
|
{
|
||
6 years ago
|
TQString received(received_str);
|
||
6 years ago
|
TQString pathToShow;
|
||
6 years ago
|
if (path == "/" || path == "/1")
|
||
|
{
|
||
|
pathToShow = "";
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
pathToShow = path;
|
||
|
}
|
||
6 years ago
|
mimeType("text/html");
|
||
6 years ago
|
TQString info;
|
||
|
TQString show("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">\n"
|
||
|
"<html xmlns=\"http://www.w3.org/1999/xhtml\">\n\t<head>\n\t\t<title>");
|
||
|
show += host.utf8();
|
||
|
show += pathToShow.utf8();
|
||
|
show += TQString("</title>\n\t\t<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n"
|
||
|
"\t\t<style type=\"text/css\">\n\t\t\t.info{ font-size : small; display : block; font-family : monospace; "
|
||
|
"white-space : pre; margin-left : 18px; }\n\t\t</style>\n\t</head>\n\t<body>\n\t\t<h1>");
|
||
|
show += host.utf8();
|
||
|
show += pathToShow.utf8();
|
||
|
show += "</h1>\n";
|
||
|
int i, remove;
|
||
6 years ago
|
findLine(received, &i, &remove);
|
||
|
while(i != -1)
|
||
|
{
|
||
6 years ago
|
processDirectoryLine(received.left(i), show, info);
|
||
|
received.remove(0, i + remove);
|
||
6 years ago
|
findLine(received, &i, &remove);
|
||
|
}
|
||
6 years ago
|
show += "\t</body>\n</html>\n";
|
||
|
TQByteArray showdata;
|
||
|
showdata.duplicate(show.utf8(), show.length());
|
||
|
data(showdata);
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
void GopherProtocol::processDirectoryLine(const TQString &d, TQString &show, TQString &info)
|
||
6 years ago
|
{
|
||
|
// gopher <type><display><tab><selector><tab><server><tab><port><\r><\n>
|
||
|
// gopher+ <type><display><tab><selector><tab><server><tab><port><tab><things><\r><\n>
|
||
|
int i;
|
||
6 years ago
|
TQString data(d);
|
||
6 years ago
|
|
||
6 years ago
|
TQString type = data.left(1);
|
||
6 years ago
|
data.remove(0, 1);
|
||
|
|
||
6 years ago
|
i = data.find("\t");
|
||
|
TQString name = data.left(i);
|
||
6 years ago
|
data.remove(0, i + 1);
|
||
|
|
||
6 years ago
|
i = data.find("\t");
|
||
|
TQString url = data.left(i);
|
||
6 years ago
|
data.remove(0, i + 1);
|
||
|
|
||
6 years ago
|
i = data.find("\t");
|
||
|
TQString server = data.left(i);
|
||
6 years ago
|
data.remove(0, i + 1);
|
||
|
|
||
6 years ago
|
TQString port = parsePort(data);
|
||
6 years ago
|
|
||
|
if (type == "i")
|
||
|
{
|
||
|
if (!info.isEmpty())
|
||
|
info.append("\n");
|
||
|
info.append(name);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
if (!info.isEmpty())
|
||
|
{
|
||
|
show.append("\t\t<div class=\"info\">");
|
||
|
show.append(info);
|
||
|
show.append("</div>\n");
|
||
|
info = "";
|
||
|
}
|
||
|
// it's the final line, ignore it
|
||
|
if (type == ".") return;
|
||
|
// those are the standard gopher types defined in the rfc
|
||
|
// 0 Item is a file
|
||
|
// 1 Item is a directory
|
||
|
// 2 Item is a CSO phone-book server
|
||
|
// 3 Error
|
||
|
// 4 Item is a BinHexed Macintosh file.
|
||
|
// 5 Item is DOS binary archive of some sort. Client must read until the TCP connection closes. Beware.
|
||
|
// 6 Item is a UNIX uuencoded file.
|
||
|
// 7 Item is an Index-Search server.
|
||
|
// 8 Item points to a text-based telnet session.
|
||
|
// 9 Item is a binary file! Client must read until the TCP connection closes. Beware.
|
||
|
// + Item is a redundant server
|
||
|
// T Item points to a text-based tn3270 session.
|
||
|
// g Item is a GIF format graphics file.
|
||
|
// I Item is some kind of image file. Client decides how to display.
|
||
|
show.append("\t\t\t<div>");
|
||
|
// support the non-standard extension for URL to external sites
|
||
|
// in this case, url begins with 'URL:'
|
||
6 years ago
|
TQString finalUrl;
|
||
|
TQString iconUrl;
|
||
6 years ago
|
if (url.startsWith("URL:"))
|
||
|
{
|
||
|
finalUrl = url.mid(4);
|
||
|
iconUrl = finalUrl;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
finalUrl = "gopher://" + server;
|
||
|
if (port != "70")
|
||
|
{
|
||
|
finalUrl.append(":");
|
||
|
finalUrl.append(port);
|
||
|
}
|
||
|
finalUrl.append('/' + type + url);
|
||
|
iconUrl = url;
|
||
|
}
|
||
|
show.append("\t\t\t\t<a href=\"");
|
||
|
show.append(finalUrl);
|
||
|
show.append("\">");
|
||
|
addIcon(type, iconUrl, show);
|
||
|
show.append(name);
|
||
|
show.append("</a><br />\n");
|
||
|
show.append("\t\t\t</div>");
|
||
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
TQString GopherProtocol::parsePort(TQString &received)
|
||
6 years ago
|
{
|
||
6 years ago
|
uint i = 0;
|
||
|
TQString port;
|
||
6 years ago
|
bool found = false;
|
||
6 years ago
|
TQChar c;
|
||
6 years ago
|
while (!found && i < received.length())
|
||
6 years ago
|
{
|
||
6 years ago
|
c = received[i];
|
||
|
if (c.isDigit())
|
||
|
i++;
|
||
|
else
|
||
|
found = true;
|
||
6 years ago
|
}
|
||
6 years ago
|
port = received.left(i);
|
||
|
received.remove(0, i);
|
||
6 years ago
|
return port;
|
||
|
}
|
||
|
|
||
6 years ago
|
void GopherProtocol::findLine(const TQString &received, int *i, int *remove)
|
||
6 years ago
|
{
|
||
|
// it's not in the rfc but most servers don't follow the spec
|
||
|
// find lines ending only in \n and in \r\n
|
||
|
int aux, aux2;
|
||
6 years ago
|
aux = received.find("\r\n");
|
||
|
aux2 = received.find("\n");
|
||
6 years ago
|
|
||
|
if (aux == -1)
|
||
|
{
|
||
|
*i = aux2;
|
||
|
*remove = 1;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
if (aux2 < aux)
|
||
|
{
|
||
|
*remove = 1;
|
||
|
*i = aux2;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
*remove = 2;
|
||
|
*i = aux;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
void GopherProtocol::handleSearch(const TQString &host, const TQString &path, int port)
|
||
6 years ago
|
{
|
||
6 years ago
|
TQString sPort;
|
||
|
if (port != 70) sPort = ':' + TQString::number(port);
|
||
6 years ago
|
mimeType("text/html");
|
||
6 years ago
|
TQString show("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">\n"
|
||
|
"<html xmlns=\"http://www.w3.org/1999/xhtml\">\n\t<head>\n\t\t<title>");
|
||
|
show += host.utf8();
|
||
|
show += path.utf8();
|
||
|
show += "</title>\n\t\t<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n"
|
||
|
"\t\t<script type=\"text/javascript\">\n\t\t\tfunction search()\n\t\t\t{\n\t\t\t\tdocument.location = 'gopher://";
|
||
|
show += host.utf8();
|
||
|
show += sPort.utf8();
|
||
|
show += path.utf8();
|
||
|
show += "?' + document.getElementById('what').value;\n\t\t\t}\n\t\t</script>\n\t</head>\n\t<body>\n\t\t<h1>";
|
||
|
show.append(host.utf8());
|
||
|
show.append(path.utf8());
|
||
|
show += "</h1>\n\t\t";
|
||
|
show += i18n("Enter a search term:").utf8();
|
||
|
show += "<br />\n\t\t<input id=\"what\" type=\"text\">\n\t\t<input type=\"button\" value=\"";
|
||
|
show += i18n("Text on a search button, like at a search engine", "Search").utf8();
|
||
|
show += "\" onClick=\"search()\">\n\t</body>\n</html>\n";
|
||
|
TQByteArray showdata;
|
||
|
showdata.duplicate(show.utf8(), show.length());
|
||
|
data(showdata);
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
void GopherProtocol::addIcon(const TQString &type, const TQString &url, TQString &show)
|
||
6 years ago
|
{
|
||
6 years ago
|
TQString icon;
|
||
6 years ago
|
if (type == "1") icon = "inode-directory.png";
|
||
|
else if (type == "3") icon = "dialog-error.png";
|
||
|
else if (type == "7") icon = "system-search.png";
|
||
|
else if (type == "g") icon = "image-gif.png";
|
||
|
else if (type == "I") icon = "image-x-generic.png";
|
||
|
else
|
||
|
{
|
||
6 years ago
|
KMimeType::Ptr mime = KMimeType::findByURL(KURL(url), 0, false, true);
|
||
|
icon = mime->icon(TQString::null, false);
|
||
6 years ago
|
}
|
||
6 years ago
|
TQFile file(m_iconLoader.iconPath(icon, -16));
|
||
6 years ago
|
file.open(IO_ReadOnly);
|
||
6 years ago
|
TQByteArray ba = file.readAll();
|
||
6 years ago
|
show.append("<img width=\"16\" height=\"16\" src=\"data:image/png;base64,");
|
||
|
show.append(KCodecs::base64Encode(ba));
|
||
|
show.append("\" /> ");
|
||
|
}
|