001/** 002 * 003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved. 004 * 005 * This library is free software; you can redistribute it and/or 006 * modify it under the terms of the GNU Lesser General Public 007 * License as published by the Free Software Foundation; either 008 * version 2.1 of the License, or (at your option) any later version. 009 * 010 * This library is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 * Lesser General Public License for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public 016 * License along with this library. If not, see <http://www.gnu.org/licenses/>. 017 * 018 **/ 019package lucee.runtime.util; 020 021import lucee.commons.lang.StringUtil; 022import lucee.runtime.CFMLFactoryImpl; 023import lucee.runtime.MappingImpl; 024import lucee.runtime.PageContext; 025import lucee.runtime.PageSource; 026import lucee.runtime.exp.RequestTimeoutException; 027import lucee.runtime.listener.ApplicationListener; 028import lucee.runtime.op.Caster; 029import lucee.runtime.type.dt.TimeSpan; 030import lucee.runtime.type.dt.TimeSpanImpl; 031import lucee.runtime.type.util.KeyConstants; 032import lucee.runtime.type.util.ListUtil; 033 034public class PageContextUtil { 035 036 public static ApplicationListener getApplicationListener(PageContext pc) { 037 PageSource ps = pc.getBasePageSource(); 038 if(ps!=null) { 039 MappingImpl mapp=(MappingImpl) ps.getMapping(); 040 if(mapp!=null) return mapp.getApplicationListener(); 041 } 042 return pc.getConfig().getApplicationListener(); 043 } 044 045 046 public static String getCookieDomain(PageContext pc) { 047 if(!pc.getApplicationContext().isSetDomainCookies()) return null; 048 049 String result = Caster.toString(pc.cgiScope().get(KeyConstants._server_name, null),null); 050 051 if(!StringUtil.isEmpty(result)) { 052 053 String listLast = ListUtil.last(result, '.'); 054 if ( !lucee.runtime.op.Decision.isNumeric(listLast) ) { // if it's numeric then must be IP address 055 int numparts = 2; 056 int listLen = ListUtil.len( result, '.', true ); 057 058 if ( listLen > 2 ) { 059 if ( listLast.length() == 2 || !StringUtil.isAscii(listLast) ) { // country TLD 060 061 int tldMinus1 = ListUtil.getAt( result, '.', listLen - 1, true, "" ).length(); 062 063 if ( tldMinus1 == 2 || tldMinus1 == 3 ) // domain is in country like, example.co.uk or example.org.il 064 numparts++; 065 } 066 } 067 068 if ( listLen > numparts ) 069 result = result.substring( result.indexOf( '.' ) ); 070 else if ( listLen == numparts ) 071 result = "." + result; 072 } 073 } 074 075 return result; 076 } 077 078 079 public static TimeSpan remainingTime(PageContext pc, boolean throwWhenAlreadyTimeout) throws RequestTimeoutException { 080 long ms = pc.getRequestTimeout()-(System.currentTimeMillis()-pc.getStartTime()); 081 if(ms>0) { 082 if(ms<5); 083 else if(ms<10) ms=ms-1; 084 else if(ms<50) ms=ms-5; 085 else if(ms<200) ms=ms-10; 086 else if(ms<1000) ms=ms-50; 087 else ms=ms-100; 088 089 return TimeSpanImpl.fromMillis(ms); 090 } 091 092 if(throwWhenAlreadyTimeout) 093 throw CFMLFactoryImpl.createRequestTimeoutException(pc); 094 095 return TimeSpanImpl.fromMillis(0); 096 } 097}