[go: up one dir, main page]

Menu

[12172e]: / src / yurl.cc  Maximize  Restore  History

Download this file

106 lines (83 with data), 2.5 kB

  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
/*
* IceWM - URL decoder
* Copyright (C) 2001 The Authors of IceWM
*
* Release under terms of the GNU Library General Public License
*
* 2001/02/25: Mathias Hasselmann <mathias.hasselmann@gmx.net>
* - initial version
*/
#include "config.h"
#include "yurl.h"
#include "base.h"
#include "intl.h"
#include <cstring>
/*******************************************************************************
* An URL decoder
******************************************************************************/
YURL::YURL():
fScheme(NULL), fUser(NULL), fPassword(NULL),
fHost(NULL), fPort(NULL), fPath(NULL) {
}
YURL::YURL(char const * url, bool expectInetScheme):
fScheme(NULL), fUser(NULL), fPassword(NULL),
fHost(NULL), fPort(NULL), fPath(NULL) {
assign(url, expectInetScheme);
}
YURL::~YURL() {
delete[] fScheme;
delete[] fPath;
}
void YURL::assign(char const * url, bool expectInetScheme) {
delete[] fScheme;
fScheme = newstr(url);
char * str;
if ((str = strchr(fScheme, ':'))) { // ======================= parse URL ===
*str++ = '\0';
if ('/' == *str++ && '/' == *str++) { // ---- common internet scheme ---
fHost = str;
if ((str = strchr(fHost, '@'))) { // ------- account information ---
*str++ = '\0';
fUser = fHost;
fHost = str;
if ((str = strchr(fUser, ':'))) { // -------------- password ---
*str++ = '\0';
fPassword = unescape(str);
}
fUser = unescape(fUser);
}
if ((str = strchr(fHost, '/'))) { // ------------------ url-path ---
if (*str) fPath = unescape(newstr(str));
*str++ = '\0';
}
if ((str = strchr(fHost, ':'))) { // --------------- custom port ---
*str++ = '\0';
if (*str) fPort = unescape(str);
}
fHost = unescape(fHost);
} else if (expectInetScheme)
warn(_("\"%s\" doesn't describe a common internet scheme"), url);
} else {
warn(_("\"%s\" contains no scheme description"), url);
delete[] fScheme;
fScheme = NULL;
}
}
char * YURL::unescape(char * str) {
if (str)
for (char * s = str; *s; ++s)
if (*s == '%') {
int a, b;
if ((a = unhex(s[1])) != -1 &&
(b = unhex(s[2])) != -1) {
*s = ((a << 4) + b);
memmove(s + 1, s + 3, strlen(s + 3) + 1);
} else
warn(_("Not a hexadecimal number: %c%c (in \"%s\")"),
s[1], s[2], str);
}
return str;
}
char * YURL::unescape(char const * str) {
return unescape(newstr(str));
}