Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 605b126

Browse files
committed
Patch for an Issue #976
1 parent 8cd40f8 commit 605b126

4 files changed

Lines changed: 11 additions & 9 deletions

File tree

lib/core/common.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -861,7 +861,7 @@ def dataToOutFile(filename, data):
861861
retVal = os.path.join(conf.filePath, filePathToSafeString(filename))
862862

863863
try:
864-
with codecs.open(retVal, "wb", UNICODE_ENCODING) as f:
864+
with openFile(retVal, "wb") as f:
865865
f.write(data)
866866
except IOError, ex:
867867
errMsg = "something went wrong while trying to write "
@@ -1813,7 +1813,7 @@ def readCachedFileContent(filename, mode='rb'):
18131813
with kb.locks.cache:
18141814
if filename not in kb.cache.content:
18151815
checkFile(filename)
1816-
with codecs.open(filename, mode, UNICODE_ENCODING) as f:
1816+
with openFile(filename, mode) as f:
18171817
kb.cache.content[filename] = f.read()
18181818

18191819
return kb.cache.content[filename]
@@ -1878,7 +1878,7 @@ def initCommonOutputs():
18781878
kb.commonOutputs = {}
18791879
key = None
18801880

1881-
with codecs.open(paths.COMMON_OUTPUTS, 'r', UNICODE_ENCODING) as f:
1881+
with openFile(paths.COMMON_OUTPUTS, 'r') as f:
18821882
for line in f.readlines(): # xreadlines doesn't return unicode strings when codec.open() is used
18831883
if line.find('#') != -1:
18841884
line = line[:line.find('#')]
@@ -1905,7 +1905,7 @@ def getFileItems(filename, commentPrefix='#', unicode_=True, lowercase=False, un
19051905
checkFile(filename)
19061906

19071907
try:
1908-
with codecs.open(filename, 'r', UNICODE_ENCODING, errors="ignore") if unicode_ else open(filename, 'r') as f:
1908+
with openFile(filename, 'r', errors="ignore") if unicode_ else open(filename, 'r') as f:
19091909
for line in (f.readlines() if unicode_ else f.xreadlines()): # xreadlines doesn't return unicode strings when codec.open() is used
19101910
if commentPrefix:
19111911
if line.find(commentPrefix) != -1:
@@ -2822,13 +2822,13 @@ def showHttpErrorCodes():
28222822
for code, count in kb.httpErrorCodes.items())
28232823
logger.warn(warnMsg)
28242824

2825-
def openFile(filename, mode='r'):
2825+
def openFile(filename, mode='r', encoding=UNICODE_ENCODING, errors="replace"):
28262826
"""
28272827
Returns file handle of a given filename
28282828
"""
28292829

28302830
try:
2831-
return codecs.open(filename, mode, UNICODE_ENCODING, "replace")
2831+
return codecs.open(filename, mode, encoding, errors)
28322832
except IOError:
28332833
errMsg = "there has been a file opening error for filename '%s'. " % filename
28342834
errMsg += "Please check %s permissions on a file " % ("write" if \

lib/core/dump.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def flush(self):
8787
def setOutputFile(self):
8888
self._outputFile = os.path.join(conf.outputPath, "log")
8989
try:
90-
self._outputFP = codecs.open(self._outputFile, "ab" if not conf.flushSession else "wb", UNICODE_ENCODING)
90+
self._outputFP = openFile(self._outputFile, "ab" if not conf.flushSession else "wb")
9191
except IOError, ex:
9292
errMsg = "error occurred while opening log file ('%s')" % ex
9393
raise SqlmapGenericException(errMsg)

lib/parse/configfile.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from ConfigParser import ParsingError
1212

1313
from lib.core.common import checkFile
14+
from lib.core.common import openFile
1415
from lib.core.common import unArrayizeValue
1516
from lib.core.common import UnicodeRawConfigParser
1617
from lib.core.data import conf
@@ -65,7 +66,7 @@ def configFileParser(configFile):
6566
logger.debug(debugMsg)
6667

6768
checkFile(configFile)
68-
configFP = codecs.open(configFile, "rb", UNICODE_ENCODING)
69+
configFP = openFile(configFile, "rb")
6970

7071
try:
7172
config = UnicodeRawConfigParser()

lib/utils/crawler.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from lib.core.common import clearConsoleLine
1717
from lib.core.common import dataToStdout
1818
from lib.core.common import findPageForms
19+
from lib.core.common import openFile
1920
from lib.core.common import readInput
2021
from lib.core.common import safeCSValue
2122
from lib.core.common import singleTimeWarnMessage
@@ -170,7 +171,7 @@ def storeResultsToFile(results):
170171
infoMsg = "writing crawling results to a temporary file '%s' " % filename
171172
logger.info(infoMsg)
172173

173-
with codecs.open(filename, "w+b", UNICODE_ENCODING) as f:
174+
with openFile(filename, "w+b") as f:
174175
if conf.forms:
175176
f.write("URL,POST\n")
176177

0 commit comments

Comments
 (0)