Patch for an Issue #976

This commit is contained in:
Miroslav Stampar 2014-11-26 13:38:21 +01:00
parent 8cd40f8917
commit 605b126758
4 changed files with 11 additions and 9 deletions

View File

@ -861,7 +861,7 @@ def dataToOutFile(filename, data):
retVal = os.path.join(conf.filePath, filePathToSafeString(filename)) retVal = os.path.join(conf.filePath, filePathToSafeString(filename))
try: try:
with codecs.open(retVal, "wb", UNICODE_ENCODING) as f: with openFile(retVal, "wb") as f:
f.write(data) f.write(data)
except IOError, ex: except IOError, ex:
errMsg = "something went wrong while trying to write " errMsg = "something went wrong while trying to write "
@ -1813,7 +1813,7 @@ def readCachedFileContent(filename, mode='rb'):
with kb.locks.cache: with kb.locks.cache:
if filename not in kb.cache.content: if filename not in kb.cache.content:
checkFile(filename) checkFile(filename)
with codecs.open(filename, mode, UNICODE_ENCODING) as f: with openFile(filename, mode) as f:
kb.cache.content[filename] = f.read() kb.cache.content[filename] = f.read()
return kb.cache.content[filename] return kb.cache.content[filename]
@ -1878,7 +1878,7 @@ def initCommonOutputs():
kb.commonOutputs = {} kb.commonOutputs = {}
key = None key = None
with codecs.open(paths.COMMON_OUTPUTS, 'r', UNICODE_ENCODING) as f: with openFile(paths.COMMON_OUTPUTS, 'r') as f:
for line in f.readlines(): # xreadlines doesn't return unicode strings when codec.open() is used for line in f.readlines(): # xreadlines doesn't return unicode strings when codec.open() is used
if line.find('#') != -1: if line.find('#') != -1:
line = line[:line.find('#')] line = line[:line.find('#')]
@ -1905,7 +1905,7 @@ def getFileItems(filename, commentPrefix='#', unicode_=True, lowercase=False, un
checkFile(filename) checkFile(filename)
try: try:
with codecs.open(filename, 'r', UNICODE_ENCODING, errors="ignore") if unicode_ else open(filename, 'r') as f: with openFile(filename, 'r', errors="ignore") if unicode_ else open(filename, 'r') as f:
for line in (f.readlines() if unicode_ else f.xreadlines()): # xreadlines doesn't return unicode strings when codec.open() is used for line in (f.readlines() if unicode_ else f.xreadlines()): # xreadlines doesn't return unicode strings when codec.open() is used
if commentPrefix: if commentPrefix:
if line.find(commentPrefix) != -1: if line.find(commentPrefix) != -1:
@ -2822,13 +2822,13 @@ def showHttpErrorCodes():
for code, count in kb.httpErrorCodes.items()) for code, count in kb.httpErrorCodes.items())
logger.warn(warnMsg) logger.warn(warnMsg)
def openFile(filename, mode='r'): def openFile(filename, mode='r', encoding=UNICODE_ENCODING, errors="replace"):
""" """
Returns file handle of a given filename Returns file handle of a given filename
""" """
try: try:
return codecs.open(filename, mode, UNICODE_ENCODING, "replace") return codecs.open(filename, mode, encoding, errors)
except IOError: except IOError:
errMsg = "there has been a file opening error for filename '%s'. " % filename errMsg = "there has been a file opening error for filename '%s'. " % filename
errMsg += "Please check %s permissions on a file " % ("write" if \ errMsg += "Please check %s permissions on a file " % ("write" if \

View File

@ -87,7 +87,7 @@ class Dump(object):
def setOutputFile(self): def setOutputFile(self):
self._outputFile = os.path.join(conf.outputPath, "log") self._outputFile = os.path.join(conf.outputPath, "log")
try: try:
self._outputFP = codecs.open(self._outputFile, "ab" if not conf.flushSession else "wb", UNICODE_ENCODING) self._outputFP = openFile(self._outputFile, "ab" if not conf.flushSession else "wb")
except IOError, ex: except IOError, ex:
errMsg = "error occurred while opening log file ('%s')" % ex errMsg = "error occurred while opening log file ('%s')" % ex
raise SqlmapGenericException(errMsg) raise SqlmapGenericException(errMsg)

View File

@ -11,6 +11,7 @@ from ConfigParser import MissingSectionHeaderError
from ConfigParser import ParsingError from ConfigParser import ParsingError
from lib.core.common import checkFile from lib.core.common import checkFile
from lib.core.common import openFile
from lib.core.common import unArrayizeValue from lib.core.common import unArrayizeValue
from lib.core.common import UnicodeRawConfigParser from lib.core.common import UnicodeRawConfigParser
from lib.core.data import conf from lib.core.data import conf
@ -65,7 +66,7 @@ def configFileParser(configFile):
logger.debug(debugMsg) logger.debug(debugMsg)
checkFile(configFile) checkFile(configFile)
configFP = codecs.open(configFile, "rb", UNICODE_ENCODING) configFP = openFile(configFile, "rb")
try: try:
config = UnicodeRawConfigParser() config = UnicodeRawConfigParser()

View File

@ -16,6 +16,7 @@ import time
from lib.core.common import clearConsoleLine from lib.core.common import clearConsoleLine
from lib.core.common import dataToStdout from lib.core.common import dataToStdout
from lib.core.common import findPageForms from lib.core.common import findPageForms
from lib.core.common import openFile
from lib.core.common import readInput from lib.core.common import readInput
from lib.core.common import safeCSValue from lib.core.common import safeCSValue
from lib.core.common import singleTimeWarnMessage from lib.core.common import singleTimeWarnMessage
@ -170,7 +171,7 @@ def storeResultsToFile(results):
infoMsg = "writing crawling results to a temporary file '%s' " % filename infoMsg = "writing crawling results to a temporary file '%s' " % filename
logger.info(infoMsg) logger.info(infoMsg)
with codecs.open(filename, "w+b", UNICODE_ENCODING) as f: with openFile(filename, "w+b") as f:
if conf.forms: if conf.forms:
f.write("URL,POST\n") f.write("URL,POST\n")