Some cleanups and fixes. Really use the Url class. Fix building under Haiku.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@20181 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
François Revol 2007-02-20 14:27:49 +00:00
parent 53596257db
commit b5a3387b08

View File

@ -8,14 +8,17 @@
#include <String.h> #include <String.h>
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
#include <Debug.h>
#define HANDLE_FILE #define HANDLE_FILE
//#define HANDLE_MID_CID // http://www.rfc-editor.org/rfc/rfc2392.txt query MAIL:cid
#define HANDLE_SH #define HANDLE_SH
#define HANDLE_BESHARE #define HANDLE_BESHARE
//#define HANDLE_IM //#define HANDLE_IM
#define HANDLE_VLC #define HANDLE_VLC
const char *kAppSig = "application/x-vnd.haiku.urlwrapper"; const char *kAppSig = "application/x-vnd.haiku.urlwrapper";
const char *kTrackerSig = "application/x-vnd.Be-TRAK";
#ifdef __HAIKU__ #ifdef __HAIKU__
const char *kTerminalSig = "application/x-vnd.Haiku-Terminal"; const char *kTerminalSig = "application/x-vnd.Haiku-Terminal";
@ -23,10 +26,6 @@ const char *kTerminalSig = "application/x-vnd.Haiku-Terminal";
const char *kTerminalSig = "application/x-vnd.Be-SHEL"; const char *kTerminalSig = "application/x-vnd.Be-SHEL";
#endif #endif
#ifdef HANDLE_FILE
const char *kTrackerSig = "application/x-vnd.Be-TRAK";
#endif
#ifdef HANDLE_BESHARE #ifdef HANDLE_BESHARE
const char *kBeShareSig = "application/x-vnd.Sugoi-BeShare"; const char *kBeShareSig = "application/x-vnd.Sugoi-BeShare";
#endif #endif
@ -53,12 +52,14 @@ bool HasUser() const { return user.Length(); };
bool HasPass() const { return pass.Length(); }; bool HasPass() const { return pass.Length(); };
bool HasPath() const { return path.Length(); }; bool HasPath() const { return path.Length(); };
BString Proto() const { return BString(proto); }; BString Proto() const { return BString(proto); };
BString Full() const { return BString(full); }; // RFC1738's "sheme-part"
BString Host() const { return BString(host); }; BString Host() const { return BString(host); };
BString Port() const { return BString(port); }; BString Port() const { return BString(port); };
BString User() const { return BString(user); }; BString User() const { return BString(user); };
BString Pass() const { return BString(pass); }; BString Pass() const { return BString(pass); };
BString proto; BString proto;
BString full;
BString host; BString host;
BString port; BString port;
BString user; BString user;
@ -82,8 +83,6 @@ private:
}; };
// TODO: handle ":port" as well
// TODO: handle "/path" as well
// proto:[//]user:pass@host:port/path // proto:[//]user:pass@host:port/path
status_t Url::ParseAndSplit() status_t Url::ParseAndSplit()
{ {
@ -98,6 +97,7 @@ status_t Url::ParseAndSplit()
CopyInto(left, v + 1, Length() - v); CopyInto(left, v + 1, Length() - v);
if (left.FindFirst("//") == 0) if (left.FindFirst("//") == 0)
left.RemoveFirst("//"); left.RemoveFirst("//");
full = left;
// path part // path part
// actually some apps handle file://[host]/path // actually some apps handle file://[host]/path
@ -223,56 +223,40 @@ void UrlWrapperApp::ArgvReceived(int32 argc, char **argv)
const char *pausec = "; read -p 'Press any key'"; const char *pausec = "; read -p 'Press any key'";
char *args[] = { "/bin/sh", "-c", NULL, NULL}; char *args[] = { "/bin/sh", "-c", NULL, NULL};
BString proto;
BString host;
BString port;
BString user;
BString pass;
BString path;
Url u(argv[1]); Url u(argv[1]);
BString rawurl(argv[1]); BString url = u.Full();
BString url = rawurl; if (u.InitCheck() < 0) {
if (url.FindFirst(":") < 0) { fprintf(stderr, "malformed url: '%s'\n", u.String());
fprintf(stderr, "malformed url: '%s'\n", url.String());
return; return;
} }
url.MoveInto(proto, 0, url.FindFirst(":"));
url.Remove(0, 1);
if (url.FindFirst("//") == 0)
url.RemoveFirst("//");
// pre-slice the url, but you're not forced to use the result.
// original still in rawurl.
SplitUrl(u.String(), host, port, user, pass, path);
// XXX: debug // XXX: debug
printf("PROTO='%s'\n", proto.String()); PRINT(("PROTO='%s'\n", u.proto.String()));
printf("HOST='%s'\n", host.String()); PRINT(("HOST='%s'\n", u.host.String()));
printf("PORT='%s'\n", port.String()); PRINT(("PORT='%s'\n", u.port.String()));
printf("USER='%s'\n", user.String()); PRINT(("USER='%s'\n", u.user.String()));
printf("PASS='%s'\n", pass.String()); PRINT(("PASS='%s'\n", u.pass.String()));
printf("PATH='%s'\n", path.String()); PRINT(("PATH='%s'\n", u.path.String()));
if (proto == "telnet") { if (u.proto == "telnet") {
BString cmd("telnet "); BString cmd("telnet ");
if (user.Length()) if (u.HasUser())
cmd << "-l " << user << " "; cmd << "-l " << u.user << " ";
cmd << host; cmd << u.host;
printf("CMD='%s'\n", cmd.String()); PRINT(("CMD='%s'\n", cmd.String()));
cmd << failc; cmd << failc;
args[2] = (char *)cmd.String(); args[2] = (char *)cmd.String();
be_roster->Launch(kTerminalSig, 3, args); be_roster->Launch(kTerminalSig, 3, args);
return; return;
} }
if (proto == "ssh") { if (u.proto == "ssh") {
BString cmd("ssh "); BString cmd("ssh ");
if (user.Length()) if (u.HasUser())
cmd << "-l " << user << " "; cmd << "-l " << u.user << " ";
cmd << host; cmd << u.host;
printf("CMD='%s'\n", cmd.String()); PRINT(("CMD='%s'\n", cmd.String()));
cmd << failc; cmd << failc;
args[2] = (char *)cmd.String(); args[2] = (char *)cmd.String();
be_roster->Launch(kTerminalSig, 3, args); be_roster->Launch(kTerminalSig, 3, args);
@ -280,7 +264,7 @@ void UrlWrapperApp::ArgvReceived(int32 argc, char **argv)
return; return;
} }
if (proto == "ftp") { if (u.proto == "ftp") {
BString cmd("ftp "); BString cmd("ftp ");
/* /*
@ -288,8 +272,8 @@ void UrlWrapperApp::ArgvReceived(int32 argc, char **argv)
cmd << "-l " << user << " "; cmd << "-l " << user << " ";
cmd << host; cmd << host;
*/ */
cmd << url; cmd << u.full;
printf("CMD='%s'\n", cmd.String()); PRINT(("CMD='%s'\n", cmd.String()));
cmd << failc; cmd << failc;
args[2] = (char *)cmd.String(); args[2] = (char *)cmd.String();
be_roster->Launch(kTerminalSig, 3, args); be_roster->Launch(kTerminalSig, 3, args);
@ -297,16 +281,16 @@ void UrlWrapperApp::ArgvReceived(int32 argc, char **argv)
return; return;
} }
if (proto == "sftp") { if (u.proto == "sftp") {
BString cmd("sftp "); BString cmd("sftp ");
/* //cmd << url;
if (user.Length()) if (u.HasUser())
cmd << "-l " << user << " "; cmd << u.user << "@";
cmd << host; cmd << u.host;
*/ if (u.HasPath())
cmd << url; cmd << ":" << u.path;
printf("CMD='%s'\n", cmd.String()); PRINT(("CMD='%s'\n", cmd.String()));
cmd << failc; cmd << failc;
args[2] = (char *)cmd.String(); args[2] = (char *)cmd.String();
be_roster->Launch(kTerminalSig, 3, args); be_roster->Launch(kTerminalSig, 3, args);
@ -314,16 +298,15 @@ void UrlWrapperApp::ArgvReceived(int32 argc, char **argv)
return; return;
} }
if (proto == "finger") { if (u.proto == "finger") {
BString cmd("finger "); BString cmd("finger ");
// TODO: SplitUrl thinks the user is host when it's not present... FIXME. if (u.HasUser())
if (user.Length()) cmd << u.user;
cmd << user; if (u.HasHost() == 0)
if (host.Length() == 0) u.host = "127.0.0.1";
host = "127.0.0.1"; cmd << "@" << u.host;
cmd << "@" << host; PRINT(("CMD='%s'\n", cmd.String()));
printf("CMD='%s'\n", cmd.String());
cmd << pausec; cmd << pausec;
args[2] = (char *)cmd.String(); args[2] = (char *)cmd.String();
be_roster->Launch(kTerminalSig, 3, args); be_roster->Launch(kTerminalSig, 3, args);
@ -332,11 +315,11 @@ void UrlWrapperApp::ArgvReceived(int32 argc, char **argv)
} }
#ifdef HANDLE_FILE #ifdef HANDLE_FILE
if (proto == "file") { if (u.proto == "file") {
BMessage m(B_REFS_RECEIVED); BMessage m(B_REFS_RECEIVED);
entry_ref ref; entry_ref ref;
// UnurlString(path); // UnurlString(path);
if (get_ref_for_path(path.String(), &ref) < B_OK) if (get_ref_for_path(u.path.String(), &ref) < B_OK)
return; return;
m.AddRef("refs", &ref); m.AddRef("refs", &ref);
be_roster->Launch(kTrackerSig, &m); be_roster->Launch(kTrackerSig, &m);
@ -345,11 +328,11 @@ void UrlWrapperApp::ArgvReceived(int32 argc, char **argv)
#endif #endif
#ifdef HANDLE_SH #ifdef HANDLE_SH
if (proto == "sh") { if (u.proto == "sh") {
BString cmd(url); BString cmd(u.Full());
if (Warn(rawurl.String()) != B_OK) if (Warn(u.String()) != B_OK)
return; return;
printf("CMD='%s'\n", cmd.String()); PRINT(("CMD='%s'\n", cmd.String()));
cmd << pausec; cmd << pausec;
args[2] = (char *)cmd.String(); args[2] = (char *)cmd.String();
be_roster->Launch(kTerminalSig, 3, args); be_roster->Launch(kTerminalSig, 3, args);
@ -359,26 +342,24 @@ void UrlWrapperApp::ArgvReceived(int32 argc, char **argv)
#endif #endif
#ifdef HANDLE_BESHARE #ifdef HANDLE_BESHARE
if (proto == "beshare") { if (u.proto == "beshare") {
team_id team; team_id team;
BMessenger msgr(kBeShareSig); BMessenger msgr(kBeShareSig);
// if no instance is running, or we want a specific server, start it. // if no instance is running, or we want a specific server, start it.
if (!msgr.IsValid() || host.Length()) { if (!msgr.IsValid() || u.HasHost()) {
be_roster->Launch(kBeShareSig, (BMessage *)NULL, &team); be_roster->Launch(kBeShareSig, (BMessage *)NULL, &team);
msgr = BMessenger(NULL, team); msgr = BMessenger(NULL, team);
} }
if (host.Length()) { if (u.HasHost()) {
BMessage mserver('serv'); BMessage mserver('serv');
mserver.AddString("server", host); mserver.AddString("server", u.host);
//msgs.AddItem(&mserver); msgr.SendMessage(&mserver);
msgr.SendMessage(mserver);
} }
if (path.Length()) { if (u.HasPath()) {
BMessage mquery('quer'); BMessage mquery('quer');
mquery.AddString("query", path); mquery.AddString("query", u.path);
//msgs.AddItem(&mquery); msgr.SendMessage(&mquery);
msgr.SendMessage(mquery);
} }
// TODO: handle errors // TODO: handle errors
return; return;
@ -386,16 +367,15 @@ void UrlWrapperApp::ArgvReceived(int32 argc, char **argv)
#endif #endif
#ifdef HANDLE_IM #ifdef HANDLE_IM
if (proto == "icq" || proto == "msn") { if (u.proto == "icq" || u.proto == "msn") {
// TODO // TODO
team_id team; team_id team;
be_roster->Launch(kIMSig, (BMessage *)NULL, &team); be_roster->Launch(kIMSig, (BMessage *)NULL, &team);
BMessenger msgr(NULL, team); BMessenger msgr(NULL, team);
if (host.Length()) { if (u.HasHost()) {
BMessage mserver(B_REFS_RECEIVED); BMessage mserver(B_REFS_RECEIVED);
mserver.AddString("server", host); mserver.AddString("server", u.host);
//msgs.AddItem(&mserver); msgr.SendMessage(&httpmserver);
msgr.SendMessage(mserver);
} }
// TODO: handle errors // TODO: handle errors
@ -404,9 +384,9 @@ void UrlWrapperApp::ArgvReceived(int32 argc, char **argv)
#endif #endif
#ifdef HANDLE_VLC #ifdef HANDLE_VLC
if (proto == "mms" || proto == "rtp" || proto == "rtsp") { if (u.proto == "mms" || u.proto == "rtp" || u.proto == "rtsp") {
args[0] = "vlc"; args[0] = "vlc";
args[1] = (char *)rawurl.String(); args[1] = (char *)u.String();
be_roster->Launch(kVLCSig, 2, args); be_roster->Launch(kVLCSig, 2, args);
return; return;
} }
@ -417,8 +397,8 @@ void UrlWrapperApp::ArgvReceived(int32 argc, char **argv)
// //
// svn: ? // svn: ?
// cvs: ? // cvs: ?
// smb: ? // smb: cifsmount ?
// nfs: ? // nfs: mount_nfs ?
} }