1212import time
1313
1414from lib .core .bigarray import BigArray
15+ from lib .core .convert import getBytes
16+ from lib .core .convert import getText
1517from lib .core .settings import VERSION
1618from thirdparty .six .moves import BaseHTTPServer as _BaseHTTPServer
1719from thirdparty .six .moves import http_client as _http_client
@@ -48,8 +50,8 @@ def obtain(self):
4850
4951class RawPair :
5052 def __init__ (self , request , response , startTime = None , endTime = None , extendedArguments = None ):
51- self .request = request
52- self .response = response
53+ self .request = getBytes ( request )
54+ self .response = getBytes ( response )
5355 self .startTime = startTime
5456 self .endTime = endTime
5557 self .extendedArguments = extendedArguments or {}
@@ -119,20 +121,20 @@ def toDict(self):
119121 "queryString" : [],
120122 "headersSize" : - 1 ,
121123 "bodySize" : - 1 ,
122- "comment" : self .comment ,
124+ "comment" : getText ( self .comment ) ,
123125 }
124126
125127 if self .postBody :
126128 contentType = self .headers .get ("Content-Type" )
127129 out ["postData" ] = {
128130 "mimeType" : contentType ,
129- "text" : self .postBody .rstrip ("\r \n " ),
131+ "text" : getText ( self .postBody ) .rstrip ("\r \n " ),
130132 }
131133
132134 return out
133135
134136class Response :
135- extract_status = re .compile (r'\ ((\d{3}) (.*)\)' )
137+ extract_status = re .compile (b' \\ ((\\ d{3}) (.*)\ \ )' )
136138
137139 def __init__ (self , httpVersion , status , statusText , headers , content , raw = None , comment = None ):
138140 self .raw = raw
@@ -146,22 +148,22 @@ def __init__(self, httpVersion, status, statusText, headers, content, raw=None,
146148 @classmethod
147149 def parse (cls , raw ):
148150 altered = raw
149- comment = ""
151+ comment = b ""
150152
151- if altered .startswith ("HTTP response [" ) or altered .startswith ("HTTP redirect [" ):
152- stream = io .StringIO (raw )
153+ if altered .startswith (b "HTTP response [" ) or altered .startswith (b "HTTP redirect [" ):
154+ stream = io .BytesIO (raw )
153155 first_line = stream .readline ()
154156 parts = cls .extract_status .search (first_line )
155- status_line = "HTTP/1.0 %s %s" % (parts .group (1 ), parts .group (2 ))
157+ status_line = b "HTTP/1.0 %s %s" % (parts .group (1 ), parts .group (2 ))
156158 remain = stream .read ()
157- altered = status_line + "\r \n " + remain
159+ altered = status_line + b "\r \n " + remain
158160 comment = first_line
159161
160162 response = _http_client .HTTPResponse (FakeSocket (altered ))
161163 response .begin ()
162164
163165 try :
164- content = response .read (- 1 )
166+ content = response .read ()
165167 except _http_client .IncompleteRead :
166168 content = raw [raw .find ("\r \n \r \n " ) + 4 :].rstrip ("\r \n " )
167169
@@ -180,10 +182,12 @@ def toDict(self):
180182 "size" : len (self .content or "" )
181183 }
182184
183- binary = set (['\0 ' , '\1 ' ])
185+ binary = set ([b '\0 ' , b '\1 ' ])
184186 if any (c in binary for c in self .content ):
185187 content ["encoding" ] = "base64"
186- content ["text" ] = base64 .b64encode (self .content )
188+ content ["text" ] = getText (base64 .b64encode (self .content ))
189+ else :
190+ content ["text" ] = getText (content ["text" ])
187191
188192 return {
189193 "httpVersion" : self .httpVersion ,
@@ -195,15 +199,15 @@ def toDict(self):
195199 "headersSize" : - 1 ,
196200 "bodySize" : - 1 ,
197201 "redirectURL" : "" ,
198- "comment" : self .comment ,
202+ "comment" : getText ( self .comment ) ,
199203 }
200204
201205class FakeSocket :
202206 # Original source:
203207 # https://stackoverflow.com/questions/24728088/python-parse-http-response-string
204208
205209 def __init__ (self , response_text ):
206- self ._file = io .StringIO (response_text )
210+ self ._file = io .BytesIO (response_text )
207211
208212 def makefile (self , * args , ** kwargs ):
209213 return self ._file
@@ -214,10 +218,10 @@ class HTTPRequest(_BaseHTTPServer.BaseHTTPRequestHandler):
214218
215219 def __init__ (self , request_text ):
216220 self .comment = None
217- self .rfile = io .StringIO (request_text )
221+ self .rfile = io .BytesIO (request_text )
218222 self .raw_requestline = self .rfile .readline ()
219223
220- if self .raw_requestline .startswith ("HTTP request [" ):
224+ if self .raw_requestline .startswith (b "HTTP request [" ):
221225 self .comment = self .raw_requestline
222226 self .raw_requestline = self .rfile .readline ()
223227
0 commit comments