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 **/
019/**
020 * Implements the CFML Function gethttprequestdata
021 */
022package lucee.runtime.functions.other;
023
024import java.util.Enumeration;
025
026import javax.servlet.http.HttpServletRequest;
027
028import lucee.runtime.PageContext;
029import lucee.runtime.PageContextImpl;
030import lucee.runtime.exp.PageException;
031import lucee.runtime.ext.function.Function;
032import lucee.runtime.net.http.ReqRspUtil;
033import lucee.runtime.type.KeyImpl;
034import lucee.runtime.type.Struct;
035import lucee.runtime.type.StructImpl;
036import lucee.runtime.type.util.KeyConstants;
037
038public final class GetHTTPRequestData implements Function {
039
040        private static final long serialVersionUID = 1365182999286292317L;
041
042        public static Struct call(PageContext pc ) throws PageException {
043                
044                Struct sct=new StructImpl();
045                Struct headers=new StructImpl();
046                HttpServletRequest req = pc.getHttpServletRequest();
047                String charset = ((PageContextImpl)pc).getWebCharset().name();
048                // headers
049                Enumeration e = req.getHeaderNames();
050                while(e.hasMoreElements()) {
051                        String key=e.nextElement().toString();
052                        headers.set(KeyImpl.init(ReqRspUtil.decode(key, charset,false)),ReqRspUtil.decode(req.getHeader(key),charset,false));
053                }
054                sct.set(KeyConstants._headers, headers);
055                sct.set(KeyConstants._protocol,req.getProtocol());
056                sct.set(KeyConstants._method,req.getMethod());
057                sct.set(KeyConstants._content,ReqRspUtil.getRequestBody(pc,false,""));
058                return sct;
059        }
060}