#include <stdlib.h>
#include "apu.h"
#include "apr.h"
#include "apr_general.h"
#include "apr_strings.h"
#define APR_WANT_STRFUNC
#include "apr_want.h"
#include "apr_uri.h"
typedef struct schemes_t schemes_t;
struct schemes_t {
const char *name;
apr_port_t default_port;
};
static schemes_t schemes[] =
{
{"http", APR_URI_HTTP_DEFAULT_PORT},
{"ftp", APR_URI_FTP_DEFAULT_PORT},
{"https", APR_URI_HTTPS_DEFAULT_PORT},
{"gopher", APR_URI_GOPHER_DEFAULT_PORT},
{"ldap", APR_URI_LDAP_DEFAULT_PORT},
{"nntp", APR_URI_NNTP_DEFAULT_PORT},
{"snews", APR_URI_SNEWS_DEFAULT_PORT},
{"imap", APR_URI_IMAP_DEFAULT_PORT},
{"pop", APR_URI_POP_DEFAULT_PORT},
{"sip", APR_URI_SIP_DEFAULT_PORT},
{"rtsp", APR_URI_RTSP_DEFAULT_PORT},
{"wais", APR_URI_WAIS_DEFAULT_PORT},
{"z39.50r", APR_URI_WAIS_DEFAULT_PORT},
{"z39.50s", APR_URI_WAIS_DEFAULT_PORT},
{"prospero", APR_URI_PROSPERO_DEFAULT_PORT},
{"nfs", APR_URI_NFS_DEFAULT_PORT},
{"tip", APR_URI_TIP_DEFAULT_PORT},
{"acap", APR_URI_ACAP_DEFAULT_PORT},
{"telnet", APR_URI_TELNET_DEFAULT_PORT},
{"ssh", APR_URI_SSH_DEFAULT_PORT},
{ NULL, 0xFFFF }
};
APU_DECLARE(apr_port_t) apr_uri_port_of_scheme(const char *scheme_str)
{
schemes_t *scheme;
if (scheme_str) {
for (scheme = schemes; scheme->name != NULL; ++scheme) {
if (strcasecmp(scheme_str, scheme->name) == 0) {
return scheme->default_port;
}
}
}
return 0;
}
APU_DECLARE(char *) apr_uri_unparse(apr_pool_t *p,
const apr_uri_t *uptr,
unsigned flags)
{
char *ret = "";
if (!(flags & APR_URI_UNP_OMITSITEPART)) {
if (uptr->user || uptr->password) {
ret = apr_pstrcat(p,
(uptr->user && !(flags & APR_URI_UNP_OMITUSER))
? uptr->user : "",
(uptr->password && !(flags & APR_URI_UNP_OMITPASSWORD))
? ":" : "",
(uptr->password && !(flags & APR_URI_UNP_OMITPASSWORD))
? ((flags & APR_URI_UNP_REVEALPASSWORD)
? uptr->password : "XXXXXXXX")
: "",
((uptr->user && !(flags & APR_URI_UNP_OMITUSER)) ||
(uptr->password && !(flags & APR_URI_UNP_OMITPASSWORD)))
? "@" : "",
NULL);
}
if (uptr->hostname) {
int is_default_port;
const char *lbrk = "", *rbrk = "";
if (strchr(uptr->hostname, ':')) {
lbrk = "[";
rbrk = "]";
}
is_default_port =
(uptr->port_str == NULL ||
uptr->port == 0 ||
uptr->port == apr_uri_port_of_scheme(uptr->scheme));
ret = apr_pstrcat(p, "//", ret, lbrk, uptr->hostname, rbrk,
is_default_port ? "" : ":",
is_default_port ? "" : uptr->port_str,
NULL);
}
if (uptr->scheme) {
ret = apr_pstrcat(p, uptr->scheme, ":", ret, NULL);
}
}
if (!(flags & APR_URI_UNP_OMITPATHINFO)) {
ret = apr_pstrcat(p,
ret,
(uptr->path)
? uptr->path : "",
(uptr->query && !(flags & APR_URI_UNP_OMITQUERY))
? "?" : "",
(uptr->query && !(flags & APR_URI_UNP_OMITQUERY))
? uptr->query : "",
(uptr->fragment && !(flags & APR_URI_UNP_OMITQUERY))
? "#" : NULL,
(uptr->fragment && !(flags & APR_URI_UNP_OMITQUERY))
? uptr->fragment : NULL,
NULL);
}
return ret;
}
#define T_COLON 0x01
#define T_SLASH 0x02
#define T_QUESTION 0x04
#define T_HASH 0x08
#define T_NUL 0x80
#if APR_CHARSET_EBCDIC
static const unsigned char uri_delims[256] = {
T_NUL,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0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PATH (T_QUESTION | T_HASH | T_NUL)
APU_DECLARE(apr_status_t) apr_uri_parse(apr_pool_t *p, const char *uri,
apr_uri_t *uptr)
{
const char *s;
const char *s1;
const char *hostinfo;
char *endstr;
int port;
int v6_offset1 = 0, v6_offset2 = 0;
memset (uptr, '\0', sizeof(*uptr));
uptr->is_initialized = 1;
if (uri[0] == '/') {
if (uri[1] == '/' && uri[2] != '/') {
s = uri + 2 ;
goto deal_with_authority ;
}
deal_with_path:
s = uri;
while ((uri_delims[*(unsigned char *)s] & NOTEND_PATH) == 0) {
++s;
}
if (s != uri) {
uptr->path = apr_pstrmemdup(p, uri, s - uri);
}
if (*s == 0) {
return APR_SUCCESS;
}
if (*s == '?') {
++s;
s1 = strchr(s, '#');
if (s1) {
uptr->fragment = apr_pstrdup(p, s1 + 1);
uptr->query = apr_pstrmemdup(p, s, s1 - s);
}
else {
uptr->query = apr_pstrdup(p, s);
}
return APR_SUCCESS;
}
uptr->fragment = apr_pstrdup(p, s + 1);
return APR_SUCCESS;
}
s = uri;
while ((uri_delims[*(unsigned char *)s] & NOTEND_SCHEME) == 0) {
++s;
}
if (s == uri || s[0] != ':') {
goto deal_with_path;
}
uptr->scheme = apr_pstrmemdup(p, uri, s - uri);
if (s[1] != '/' || s[2] != '/') {
uri = s + 1;
goto deal_with_path;
}
s += 3;
deal_with_authority:
hostinfo = s;
while ((uri_delims[*(unsigned char *)s] & NOTEND_HOSTINFO) == 0) {
++s;
}
uri = s;
uptr->hostinfo = apr_pstrmemdup(p, hostinfo, uri - hostinfo);
do {
--s;
} while (s >= hostinfo && *s != '@');
if (s < hostinfo) {
deal_with_host:
if (*hostinfo == '[') {
v6_offset1 = 1;
v6_offset2 = 2;
s = memchr(hostinfo, ']', uri - hostinfo);
if (s == NULL) {
return APR_EGENERAL;
}
if (*++s != ':') {
s = NULL;
}
}
else {
s = memchr(hostinfo, ':', uri - hostinfo);
}
if (s == NULL) {
uptr->hostname = apr_pstrmemdup(p,
hostinfo + v6_offset1,
uri - hostinfo - v6_offset2);
goto deal_with_path;
}
uptr->hostname = apr_pstrmemdup(p,
hostinfo + v6_offset1,
s - hostinfo - v6_offset2);
++s;
uptr->port_str = apr_pstrmemdup(p, s, uri - s);
if (uri != s) {
port = strtol(uptr->port_str, &endstr, 10);
uptr->port = port;
if (*endstr == '\0') {
goto deal_with_path;
}
return APR_EGENERAL;
}
uptr->port = apr_uri_port_of_scheme(uptr->scheme);
goto deal_with_path;
}
s1 = memchr(hostinfo, ':', s - hostinfo);
if (s1) {
uptr->user = apr_pstrmemdup(p, hostinfo, s1 - hostinfo);
++s1;
uptr->password = apr_pstrmemdup(p, s1, s - s1);
}
else {
uptr->user = apr_pstrmemdup(p, hostinfo, s - hostinfo);
}
hostinfo = s + 1;
goto deal_with_host;
}
APU_DECLARE(apr_status_t) apr_uri_parse_hostinfo(apr_pool_t *p,
const char *hostinfo,
apr_uri_t *uptr)
{
const char *s;
char *endstr;
const char *rsb;
int v6_offset1 = 0;
memset(uptr, '\0', sizeof(*uptr));
uptr->is_initialized = 1;
uptr->hostinfo = apr_pstrdup(p, hostinfo);
if (*hostinfo == '[') {
if ((rsb = strchr(hostinfo, ']')) == NULL ||
*(rsb + 1) != ':') {
return APR_EGENERAL;
}
s = rsb + 1;
++hostinfo;
v6_offset1 = 1;
}
else {
s = strchr(hostinfo, ':');
}
if (s == NULL) {
return APR_EGENERAL;
}
uptr->hostname = apr_pstrndup(p, hostinfo, s - hostinfo - v6_offset1);
++s;
uptr->port_str = apr_pstrdup(p, s);
if (*s != '\0') {
uptr->port = (unsigned short) strtol(uptr->port_str, &endstr, 10);
if (*endstr == '\0') {
return APR_SUCCESS;
}
}
return APR_EGENERAL;
}