001 /** 002 * Implements the Cold Fusion Function gethttprequestdata 003 */ 004 package railo.runtime.functions.other; 005 006 import java.util.Enumeration; 007 008 import javax.servlet.ServletInputStream; 009 import javax.servlet.http.HttpServletRequest; 010 011 import railo.commons.io.IOUtil; 012 import railo.runtime.PageContext; 013 import railo.runtime.exp.PageException; 014 import railo.runtime.ext.function.Function; 015 import railo.runtime.net.http.ReqRspUtil; 016 import railo.runtime.tag.Http; 017 import railo.runtime.type.KeyImpl; 018 import railo.runtime.type.Struct; 019 import railo.runtime.type.StructImpl; 020 021 public final class GetHTTPRequestData implements Function { 022 public static Struct call(PageContext pc ) throws PageException { 023 024 Struct sct=new StructImpl(); 025 Struct headers=new StructImpl(); 026 HttpServletRequest req = pc.getHttpServletRequest(); 027 String charset = pc.getConfig().getWebCharset(); 028 // headers 029 Enumeration e = req.getHeaderNames(); 030 while(e.hasMoreElements()) { 031 String key=e.nextElement().toString(); 032 headers.set(KeyImpl.init(ReqRspUtil.decode(key, charset,false)),ReqRspUtil.decode(req.getHeader(key),charset,false)); 033 } 034 sct.set("headers", headers); 035 sct.set("protocol",req.getProtocol()); 036 sct.set("method",req.getMethod()); 037 sct.set("content",getContent(req)); 038 return sct; 039 } 040 041 private static Object getContent(HttpServletRequest req) { 042 String contentType = req.getContentType(); 043 String charEncoding = req.getCharacterEncoding(); 044 Object obj = ""; 045 046 boolean isBinary =!( 047 contentType == null || Http.isText(contentType) || 048 049 contentType.toLowerCase().startsWith("application/x-www-form-urlencoded")); 050 //print.err("err:"+contentType+":"+charEncoding); 051 052 if(req.getContentLength() > -1) { 053 ServletInputStream is=null; 054 try { 055 056 057 byte[] data = IOUtil.toBytes(is=req.getInputStream());//new byte[req.getContentLength()]; 058 059 if(isBinary) { 060 obj = data; 061 } 062 else if(charEncoding != null && charEncoding.length() > 0) 063 obj = new String(data, charEncoding); 064 else 065 obj = new String(data); 066 } 067 catch(Exception e) { 068 069 obj=""; 070 } 071 finally { 072 IOUtil.closeEL(is); 073 } 074 } 075 else { 076 obj=""; 077 } 078 return obj; 079 } 080 081 }