001 package railo.runtime.net.http; 002 003 import java.util.ArrayList; 004 import java.util.Enumeration; 005 import java.util.List; 006 007 import javax.servlet.http.Cookie; 008 import javax.servlet.http.HttpServletRequest; 009 010 import railo.commons.lang.Pair; 011 import railo.runtime.config.Config; 012 import railo.runtime.type.Struct; 013 import railo.runtime.type.StructImpl; 014 015 public class HttpUtil { 016 017 /** 018 * read all headers from request and return it 019 * @param req 020 * @return 021 */ 022 public static Pair[] cloneHeaders(HttpServletRequest req) { 023 List headers=new ArrayList(); 024 Enumeration e = req.getHeaderNames(),ee; 025 String name; 026 while(e.hasMoreElements()){ 027 name=(String) e.nextElement(); 028 ee=req.getHeaders(name); 029 while(ee.hasMoreElements()){ 030 headers.add(new Pair(name,ee.nextElement().toString())); 031 } 032 } 033 return (Pair[]) headers.toArray(new Pair[headers.size()]); 034 } 035 036 public static Struct getAttributesAsStruct(HttpServletRequest req) { 037 Struct attributes=new StructImpl(); 038 Enumeration e = req.getAttributeNames(); 039 String name; 040 while(e.hasMoreElements()){ 041 name=(String) e.nextElement();// MUST (hhlhgiug) can throw ConcurrentModificationException 042 if(name!=null)attributes.setEL(name, req.getAttribute(name)); 043 } 044 return attributes; 045 } 046 047 public static Pair[] getAttributes(HttpServletRequest req) { 048 List attributes=new ArrayList(); 049 Enumeration e = req.getAttributeNames(); 050 String name; 051 while(e.hasMoreElements()){ 052 name=(String) e.nextElement(); 053 attributes.add(new Pair(name, req.getAttribute(name))); 054 } 055 return (Pair[]) attributes.toArray(new Pair[attributes.size()]); 056 } 057 058 public static Pair[] cloneParameters(HttpServletRequest req) { 059 List parameters=new ArrayList(); 060 Enumeration e = req.getParameterNames(); 061 String[] values; 062 String name; 063 while(e.hasMoreElements()){ 064 name=(String) e.nextElement(); 065 values=req.getParameterValues(name); 066 for(int i=0;i<values.length;i++){ 067 parameters.add(new Pair(name,values[i])); 068 } 069 } 070 return (Pair[]) parameters.toArray(new Pair[parameters.size()]); 071 } 072 073 public static Cookie[] cloneCookies(Config config,HttpServletRequest req) { 074 Cookie[] src=ReqRspUtil.getCookies(config, req); 075 if(src==null)return new Cookie[0]; 076 077 Cookie[] dest=new Cookie[src.length]; 078 for(int i=0;i<src.length;i++) { 079 dest[i]=(Cookie) src[i].clone(); 080 } 081 return dest; 082 } 083 084 }