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.net.http; 020 021import java.util.ArrayList; 022import java.util.Enumeration; 023import java.util.List; 024 025import javax.servlet.http.Cookie; 026import javax.servlet.http.HttpServletRequest; 027 028import lucee.commons.io.CharsetUtil; 029import lucee.commons.lang.Pair; 030import lucee.runtime.PageContext; 031import lucee.runtime.config.Config; 032import lucee.runtime.engine.ThreadLocalPageContext; 033import lucee.runtime.type.Struct; 034import lucee.runtime.type.StructImpl; 035 036public class HttpUtil { 037 038 /** 039 * read all headers from request and return it 040 * @param req 041 * @return 042 */ 043 public static Pair[] cloneHeaders(HttpServletRequest req) { 044 List headers=new ArrayList(); 045 Enumeration e = req.getHeaderNames(),ee; 046 String name; 047 while(e.hasMoreElements()){ 048 name=(String) e.nextElement(); 049 ee=req.getHeaders(name); 050 while(ee.hasMoreElements()){ 051 headers.add(new Pair(name,ee.nextElement().toString())); 052 } 053 } 054 return (Pair[]) headers.toArray(new Pair[headers.size()]); 055 } 056 057 public static Struct getAttributesAsStruct(HttpServletRequest req) { 058 Struct attributes=new StructImpl(); 059 Enumeration e = req.getAttributeNames(); 060 String name; 061 while(e.hasMoreElements()){ 062 name=(String) e.nextElement();// MUST (hhlhgiug) can throw ConcurrentModificationException 063 if(name!=null)attributes.setEL(name, req.getAttribute(name)); 064 } 065 return attributes; 066 } 067 068 public static Pair<String,Object>[] getAttributes(HttpServletRequest req) { 069 List<Pair<String,Object>> attributes=new ArrayList<Pair<String,Object>>(); 070 Enumeration e = req.getAttributeNames(); 071 String name; 072 while(e.hasMoreElements()){ 073 name=(String) e.nextElement(); 074 attributes.add(new Pair<String,Object>(name, req.getAttribute(name))); 075 } 076 return attributes.toArray(new Pair[attributes.size()]); 077 } 078 079 public static Pair<String,String>[] cloneParameters(HttpServletRequest req) { 080 List<Pair<String,String>> parameters=new ArrayList<Pair<String,String>>(); 081 Enumeration e = req.getParameterNames(); 082 String[] values; 083 String name; 084 085 while(e.hasMoreElements()){ 086 name=(String) e.nextElement(); 087 values=req.getParameterValues(name); 088 if(values==null && ReqRspUtil.needEncoding(name, false)) 089 values=req.getParameterValues(ReqRspUtil.encode(name, ReqRspUtil.getCharacterEncoding(null,req))); 090 if(values==null) { 091 PageContext pc = ThreadLocalPageContext.get(); 092 if(pc!=null && ReqRspUtil.identical(pc.getHttpServletRequest(),req) ) { 093 values=HTTPServletRequestWrap.getParameterValues(ThreadLocalPageContext.get(), name); 094 } 095 } 096 if(values!=null)for(int i=0;i<values.length;i++){ 097 parameters.add(new Pair<String,String>(name,values[i])); 098 } 099 } 100 return parameters.toArray(new Pair[parameters.size()]); 101 } 102 103 public static Cookie[] cloneCookies(Config config,HttpServletRequest req) { 104 Cookie[] src=ReqRspUtil.getCookies(req,CharsetUtil.getWebCharset()); 105 if(src==null)return new Cookie[0]; 106 107 Cookie[] dest=new Cookie[src.length]; 108 for(int i=0;i<src.length;i++) { 109 dest[i]=(Cookie) src[i].clone(); 110 } 111 return dest; 112 } 113 114}