# Copyright (C) 2003, 2004 Gregoire Lejeune <gregoire.lejeune@free.fr>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA

module Asp
  class Request
    require 'asp/params'
    require 'asp/common'

    public
    def form( key )
      if @oApacheRequest.request_method == "POST" 
        if @params[key].nil? == false and @params[key].size > 0
          if @params[key].size == 1
            return @params[key][0]
          else
            return @params[key]
          end
        else
          return nil
        end
      end
    end
  
    def queryString( key )
      if @oApacheRequest.request_method == "GET" or
         @oApacheRequest.request_method == "HEAD"
        if @params[key].nil? == false and @params[key].size > 0
          if @params[key].size == 1
            return @params[key][0]
          else
            return @params[key]
          end
        else
          return nil
        end
      end
    end

	def query
	  @param.getQuery( )
	end

    def cookies( name=nil, key=nil )
      if name.nil? == true
        return @cookies
      elsif key.nil? == true and @cookies[name].nil? == false
        return @cookies[name]
      elsif String( @cookies[name].class ) == "Hash" and @cookies[name][key].nil? == false
        return @cookies[name][key]
      end

      return nil
    end

    def method
      return @oApacheRequest.request_method
    end

    def totalBytes
      return @oApacheRequest.headers_in["Content-Length"]
    end

    def serverVariables( name=nil )
      if name.nil? == true
        return ENV.untaint
      else
        return ENV[name].untaint
      end
    end

    private 
## -- ##    def parseCookie( value )
## -- ##      values = Hash::new
## -- ##
## -- ##      parts = value.split( /[\&\;]/ )
## -- ##      parts.each { |v|
## -- ##        key, value = v.split( /\=/, 2 ).map { |x|
## -- ##          Asp::Common::url_decode( x )
## -- ##        }
## -- ##        values[key] = value
## -- ##      }
## -- ##
## -- ##      return values
## -- ##    end

    def getCookies
      cookiesStr = @oApacheRequest.headers_in['Cookie']
      cookies = Hash::new

      if cookiesStr.nil? == false
        parts = cookiesStr.split( /;\s*/ )
        parts.each { |v|
          name, value = v.split( /\=/, 2 )
          name = Asp::Common::url_decode( name )
          #next if name == "session-id"
          next if name == Asp::Common::sessionID
          next if cookies[name].nil? == false
          if /\=/ =~ value
## -- ##            cookies[name] = parseCookie( value )
            cookies[name] = Asp::Common::parseCookie( value )
          else
            cookies[name] = Asp::Common::url_decode( value )
          end
        }
      end

      return cookies
    end
    
    def initialize( r, oAspError, aspHandler )
      @oApacheRequest = r
      @oAspError = oAspError
      @oAspHandler = aspHandler
      
      @param = Asp::Params::new( @oApacheRequest )
      @params = @param.params
      @cookies = getCookies
    end
  
  end
end
