'''
frFactory.py

Copyright 2006 Andres Riancho

This file is part of w3af, w3af.sourceforge.net .

w3af 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 version 2 of the License.

w3af 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 w3af; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

'''
import core.data.parsers.dpCache as dpCache

import core.data.parsers.urlParser as urlParser
import core.data.parsers.wsdlParser as wsdlParser

import core.data.request.httpPostDataRequest as httpPostDataRequest
import core.data.request.httpQsRequest as httpQsRequest
import core.data.request.wsPostDataRequest as wsPostDataRequest

from core.controllers.w3afException import w3afException
import core.data.kb.config as cf

def createFuzzableRequests( httpResponse ):
	'''
	Generates the fuzzable requests based on an http response instance.
	
	@parameter httpResponse: An httpResponse instance.
	'''
	res = []
	
	# query string
	url = httpResponse.getURL()
	QSObject = urlParser.getQueryString( httpResponse.getURI() )
	
	# Headers
	headers = {}
	for header in cf.cf.getData('fuzzableHeaders' ):
		headers[ header ] = ''
	
	# forms
	dp = dpCache.dpc.getDocumentParserFor( httpResponse.getBody(), url )
	formList = dp.getForms()

	# create a httpQsRequest
	qsr = httpQsRequest.httpQsRequest()
	qsr.setURL( url )
	qsr.setDc( QSObject )
	qsr.setHeaders( headers )
	res.append( qsr )
		
	if len( formList ) == 0:
		
		# Check if its a wsdl file
		wsdlp = wsdlParser.wsdlParser()
		try:
			wsdlp.setWsdl( httpResponse.getBody() )
		except w3afException:
			pass
		else:
			webServiceList = wsdlp.getMethods()
			if len( webServiceList ) != 0:
				for remoteMethod in webServiceList:
					wspdr = wsPostDataRequest.wsPostDataRequest()
					wspdr.setURL( remoteMethod.getLocation() )
					wspdr.setAction( remoteMethod.getAction() )
					wspdr.setParameters( remoteMethod.getParameters() )
					wspdr.setNS( remoteMethod.getNamespace() )
					wspdr.setMethodName( remoteMethod.getMethodName() )
					wspdr.setHeaders( headers )
					res.append( wspdr )		
		
	else:
		# create one httpPostDataRequest for each form
		for form in formList:
			if form.getMethod().upper() == 'GET':
				qsr = httpQsRequest.httpQsRequest()
				qsr.setURL( url )
				qsr.setDc( QSObject )
				qsr.setHeaders( headers )
				res.append( qsr )
			elif form.getMethod().upper() == 'POST':
				pdr = httpPostDataRequest.httpPostDataRequest()
				pdr.setURL( form.getAction() )
				pdr.setMethod( form.getMethod() )
				pdr.setFileVariables( form.getFileVariables() )
				pdr.setDc( form )
				pdr.setHeaders( headers )
				res.append( pdr )

	return res
