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

Skip to content

Commit 6bb4dce

Browse files
committed
minor refactoring
1 parent a2bb0d7 commit 6bb4dce

4 files changed

Lines changed: 23 additions & 23 deletions

File tree

extra/keepalive/keepalive.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
"""
7272
from httplib import _CS_REQ_STARTED, _CS_REQ_SENT, _CS_IDLE, CannotSendHeader
7373

74-
from lib.core.common import encodeUnicode
74+
from lib.core.convert import unicodeencode
7575
from lib.core.data import kb
7676

7777
import threading
@@ -336,7 +336,7 @@ def endheaders(self):
336336
self._send_output()
337337

338338
def send(self, str):
339-
httplib.HTTPConnection.send(self, encodeUnicode(str, kb.pageEncoding))
339+
httplib.HTTPConnection.send(self, unicodeencode(str, kb.pageEncoding))
340340

341341
#########################################################################
342342
##### TEST FUNCTIONS

lib/core/common.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1799,22 +1799,6 @@ def getUnicode(value, encoding=None, system=False):
17991799
except:
18001800
return getUnicode(value, UNICODE_ENCODING)
18011801

1802-
def encodeUnicode(value, encoding=None):
1803-
"""
1804-
Return 8-bit string representation of the supplied unicode value:
1805-
1806-
>>> encodeUnicode(u'test')
1807-
'test'
1808-
"""
1809-
1810-
retVal = value
1811-
if isinstance(value, unicode):
1812-
try:
1813-
retVal = value.encode(encoding or UNICODE_ENCODING)
1814-
except UnicodeEncodeError:
1815-
retVal = value.encode(UNICODE_ENCODING, errors="replace")
1816-
return retVal
1817-
18181802
# http://boredzo.org/blog/archives/2007-01-06/longest-common-prefix-in-python-2
18191803
def longestCommonPrefix(*sequences):
18201804
if len(sequences) == 1:

lib/core/convert.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,24 @@ def urlencode(value, safe="%&=", convall=False, limit=False):
124124

125125
return result
126126

127+
def unicodeencode(value, encoding=None):
128+
"""
129+
Return 8-bit string representation of the supplied unicode value:
130+
131+
>>> unicodeencode(u'test')
132+
'test'
133+
"""
134+
135+
retVal = value
136+
if isinstance(value, unicode):
137+
try:
138+
retVal = value.encode(encoding or UNICODE_ENCODING)
139+
except UnicodeEncodeError:
140+
retVal = value.encode(UNICODE_ENCODING, errors="replace")
141+
return retVal
142+
127143
def utf8encode(value):
128-
return value.encode("utf-8")
144+
return unicodeencode(value, "utf-8")
129145

130146
def utf8decode(value):
131147
return value.decode("utf-8")

lib/request/connect.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
from lib.core.common import calculateDeltaSeconds
2222
from lib.core.common import clearConsoleLine
2323
from lib.core.common import cpuThrottle
24-
from lib.core.common import encodeUnicode
2524
from lib.core.common import extractRegexResult
2625
from lib.core.common import getCurrentThreadData
2726
from lib.core.common import getFilteredPageContent
@@ -31,9 +30,10 @@
3130
from lib.core.common import readInput
3231
from lib.core.common import removeReflectiveValues
3332
from lib.core.common import stdev
33+
from lib.core.common import urlEncodeCookieValues
3434
from lib.core.common import wasLastRequestDelayed
35+
from lib.core.convert import unicodeencode
3536
from lib.core.convert import urlencode
36-
from lib.core.common import urlEncodeCookieValues
3737
from lib.core.data import conf
3838
from lib.core.data import kb
3939
from lib.core.data import logger
@@ -176,9 +176,9 @@ def getPage(**kwargs):
176176

177177
for key, item in headers.items():
178178
del headers[key]
179-
headers[encodeUnicode(key, kb.pageEncoding)] = encodeUnicode(item, kb.pageEncoding)
179+
headers[unicodeencode(key, kb.pageEncoding)] = unicodeencode(item, kb.pageEncoding)
180180

181-
post = encodeUnicode(post, kb.pageEncoding)
181+
post = unicodeencode(post, kb.pageEncoding)
182182

183183
if method:
184184
req = MethodRequest(url, post, headers)

0 commit comments

Comments
 (0)