33 #include <nsNetUtil.h>
40 static PRLogModuleInfo* gURICheckerLog = nsnull;
44 #define LOG(args) PR_LOG(gURICheckerLog, PR_LOG_WARN, args)
53 if (!gURICheckerLog) {
54 gURICheckerLog = PR_NewLogModule(
"sbURIChecker");
56 LOG((
"sbURIChecker::CheckURI(domain:%s path:%s)",
57 aDomain.BeginReading(), aPath.BeginReading()));
59 NS_ENSURE_ARG_POINTER(aURI);
61 nsCString domain(aDomain);
63 if ( NS_FAILED(rv) ) {
64 LOG((
"sbURIChecker::CheckURI() -- FAILED domain Check"));
68 nsCString
path(aPath);
70 if ( NS_FAILED(rv) ) {
71 LOG((
"sbURIChecker::CheckURI() -- FAILED path Check"));
76 if ( aDomain.IsEmpty() ) {
77 aDomain.Assign(domain);
79 if ( aPath.IsEmpty() ) {
89 NS_ENSURE_ARG_POINTER(aSiteURI);
90 LOG((
"sbURIChecker::CheckDomain(%s)", aDomain.BeginReading() ));
94 nsresult rv = aSiteURI->GetHost(host);
95 NS_ENSURE_SUCCESS( rv, rv );
99 NS_ENSURE_SUCCESS( rv, rv );
101 host.Assign(fixedHost);
103 if ( !aDomain.IsEmpty() ) {
104 LOG((
"sbURIChecker::CheckDomain() -- Have a domain from the user"));
106 nsCString fixedDomain;
108 NS_ENSURE_SUCCESS( rv, rv );
110 aDomain.Assign(fixedDomain);
114 if ( PR_StringToNetAddr( host.get(), &addr ) == PR_SUCCESS ) {
116 LOG((
"sbURIChecker::CheckDomain() -- Numerical Address "));
117 if ( !aDomain.Equals(host) ) {
118 LOG((
"sbURIChecker::CheckDomain() -- FAILED ip address check"));
119 return NS_ERROR_FAILURE;
123 LOG((
"sbURIChecker::CheckDomain() -- Domain based host "));
127 PRInt32 dot = aDomain.FindChar(
'.');
128 if ( dot < 0 && !host.Equals(
"localhost")) {
129 LOG((
"sbURIChecker::CheckDomain() -- FAILED dot test "));
130 return NS_ERROR_FAILURE;
134 aDomain.Insert( NS_LITERAL_CSTRING(
"."), 0 );
136 PRInt32 domainLength = aDomain.Length();
137 PRInt32 lengthDiff = host.Length() - domainLength;
138 if ( lengthDiff == -1 ) {
139 LOG((
"sbURIChecker::CheckDomain() -- long domain check"));
143 if ( !StringEndsWith(aDomain, host) ) {
144 LOG((
"sbURIChecker::CheckDomain() -- FAILED long domain check"));
145 return NS_ERROR_FAILURE;
147 }
else if ( lengthDiff == 0 ) {
148 LOG((
"sbURIChecker::CheckDomain() -- same length check"));
150 if ( !aDomain.Equals(host) ) {
151 LOG((
"sbURIChecker::CheckDomain() -- FAILED same length check"));
152 return NS_ERROR_FAILURE;
154 }
else if ( lengthDiff > 0 ) {
155 LOG((
"sbURIChecker::CheckDomain() -- parent domain check"));
158 if ( !StringEndsWith(host, aDomain) ) {
159 LOG((
"sbURIChecker::CheckDomain() -- FAILED parent domain check"));
160 return NS_ERROR_FAILURE;
165 LOG((
"sbURIChecker::CheckDomain() -- FAILED, user domain is superset"));
166 return NS_ERROR_FAILURE;
173 LOG((
"sbURIChecker::CheckDomain() -- NO domain from the user"));
176 if ( host.IsEmpty() ) {
178 rv = aSiteURI->SchemeIs(
"file", &isFileURI );
179 NS_ENSURE_SUCCESS( rv, rv );
183 LOG((
"sbURIChecker::CheckDomain() -- FAILED file scheme check"));
184 return NS_ERROR_FAILURE;
191 aDomain.Assign(host);
195 LOG((
"sbURIChecker::CheckDomain() -- PASSED match test"));
204 NS_ENSURE_ARG_POINTER(aSiteURI);
205 LOG((
"sbURIChecker::CheckPath(%s)", aPath.BeginReading() ));
209 nsCString fixedSitePath;
211 NS_ENSURE_SUCCESS( rv, rv );
213 if ( aPath.IsEmpty() ) {
215 aPath.Assign(fixedSitePath);
228 NS_ENSURE_SUCCESS( rv, rv );
231 if ( !StringBeginsWith( fixedSitePath, fixedPath ) ) {
232 LOG((
"sbURIChecker::CheckPath() -- FAILED Path Prefix Check"));
233 return NS_ERROR_FAILURE;
236 LOG((
"sbURIChecker::CheckPath() -- PASSED Path Prefix Check"));
238 aPath.Assign(fixedPath);
245 nsACString& _retval )
248 if ( aDomain.IsEmpty() ) {
253 nsCString domain(aDomain);
258 _retval.Assign(domain);
265 nsACString& _retval )
267 NS_ASSERTION( aURI,
"Don't you dare pass me a null pointer!" );
270 nsCOMPtr<nsIURL>
url( do_QueryInterface( aURI, &rv ) );
271 NS_ENSURE_SUCCESS( rv, rv );
286 rv = url->GetDirectory(path);
287 NS_ENSURE_SUCCESS( rv, rv );
290 rv = url->GetFileName(fileName);
291 NS_ENSURE_SUCCESS( rv, rv );
293 if ( !fileName.IsEmpty() ) {
295 rv = url->GetFileExtension(fileExt);
296 NS_ENSURE_SUCCESS( rv, rv );
298 if ( fileExt.IsEmpty() ) {
301 path.Append(fileName);
302 path.AppendLiteral(
"/");
306 _retval.Assign(path);
313 nsACString& _retval )
316 if ( aPath.IsEmpty() ) {
321 NS_NAMED_LITERAL_CSTRING( slashString,
"/" );
324 nsCString dummyURL(
"http://dummy.com");
328 if ( !StringBeginsWith( aPath, slashString ) ) {
329 dummyURL.Append(slashString);
332 dummyURL.Append(aPath);
334 nsCOMPtr<nsIURI>
uri;
335 nsresult rv = NS_NewURI( getter_AddRefs(uri), dummyURL );
336 NS_ENSURE_SUCCESS( rv, rv );
static nsresult CheckDomain(nsACString &aDomain, nsIURI *aSiteURI)
static nsresult CheckURI(nsACString &aDomain, nsACString &aPath, nsIURI *aURI)
static nsresult FixupDomain(const nsACString &aDomain, nsACString &_retval)
static nsresult CheckPath(nsACString &aPath, nsIURI *aSiteURI)
static nsresult FixupPath(nsIURI *aURI, nsACString &_retval)