utils.py
def requestParser(requestStr):
		"""
		accept request string, return dictionary
		returning None indicates parsing error
		"""
		requestStr = requestStr.strip()
		#According to the RFC, the body starts after a \r\n\r\n sequence
		headerBodySplit = requestStr.split("\r\n\r\n".encode(), 1)
		reqlineAndHeaders = headerBodySplit[0].decode('utf-8')
		requestBody = ''
		#since the body maybe absent sometimes, this avoids an IndexError
		if(len(headerBodySplit)>1):
			requestBody = headerBodySplit[1]
		
		headerFields = reqlineAndHeaders.strip().split('\r\n')
		#RFC : Request-Line = Method SP Request-URI SP HTTP-Version CRLF
		requestFields = headerFields[0].split(" ")
		requestHeaders = headerFields[1:]
		requestLine = dict()
		requestLine['method'] = requestFields[0]
		#Request-URI = "*" | absoluteURI | abs_path | authority
		try:
			requestLine['requestUri'] = requestFields[1]
			requestLine['httpVersion'] = requestFields[2]

			headersDict = dict()
			for i in requestHeaders:
				keyValSplit = i.split(':', 1)
				key = keyValSplit[0]
				val = keyValSplit[1]
				#lower used since according to RFC, header keys are case insensitive
				#Some values maybe case sensitive or otherwise, depending on key, THAT CASE NOT HANDLED
				headersDict[key.strip().lower()] = val.strip()
			
			requestHeaders = headersDict
			#At this point requestLine(dictionary), requestHeaders(dictionary) and requestBody(string) constitute the entire message
			#uncomment line below if debugging to compare with original requestStr
			#print(requestStr)
			parsedRequest = {
				'requestLine': requestLine, 
				'requestHeaders': requestHeaders, 
				'requestBody': requestBody
			}
			return parsedRequest
		
		except IndexError:
			return None

		accept request string, return dictionary
		returning None indicates parsing error
		
accept request string, return dictionary
returning None indicates parsing error
accept request string return dictionary returning None indicates parsing error
def requestParser(requestStr):
		requestStr = requestStr.strip()
		headerBodySplit = requestStr.split("\r\n\r\n".encode(), 1)
		reqlineAndHeaders = headerBodySplit[0].decode('utf-8')
		requestBody = ''
		if(len(headerBodySplit)>1):
			requestBody = headerBodySplit[1]
		headerFields = reqlineAndHeaders.strip().split('\r\n')
		requestFields = headerFields[0].split(" ")
		requestHeaders = headerFields[1:]
		requestLine = dict()
		requestLine['method'] = requestFields[0]
		try:
			requestLine['requestUri'] = requestFields[1]
			requestLine['httpVersion'] = requestFields[2]
			headersDict = dict()
			for i in requestHeaders:
				keyValSplit = i.split(':', 1)
				key = keyValSplit[0]
				val = keyValSplit[1]
				headersDict[key.strip().lower()] = val.strip()
			requestHeaders = headersDict
			parsedRequest = {
				'requestLine': requestLine, 
				'requestHeaders': requestHeaders, 
				'requestBody': requestBody
			}
			return parsedRequest
		except IndexError:
			return None
def requestParser ( requestStr ) : requestStr = requestStr . strip ( ) headerBodySplit = requestStr . split ( "\r\n\r\n" . encode ( ) , 1 ) reqlineAndHeaders = headerBodySplit [ 0 ] . decode ( 'utf-8' ) requestBody = '' if ( len ( headerBodySplit ) > 1 ) : requestBody = headerBodySplit [ 1 ] headerFields = reqlineAndHeaders . strip ( ) . split ( '\r\n' ) requestFields = headerFields [ 0 ] . split ( " " ) requestHeaders = headerFields [ 1 : ] requestLine = dict ( ) requestLine [ 'method' ] = requestFields [ 0 ] try : requestLine [ 'requestUri' ] = requestFields [ 1 ] requestLine [ 'httpVersion' ] = requestFields [ 2 ] headersDict = dict ( ) for i in requestHeaders : keyValSplit = i . split ( ':' , 1 ) key = keyValSplit [ 0 ] val = keyValSplit [ 1 ] headersDict [ key . strip ( ) . lower ( ) ] = val . strip ( ) requestHeaders = headersDict parsedRequest = { 'requestLine' : requestLine , 'requestHeaders' : requestHeaders , 'requestBody' : requestBody } return parsedRequest except IndexError : return None
accept request string, return dictionary
returning None indicates parsing error
accept request string return dictionary returning None indicates parsing error
"""
		accept request string, return dictionary
		returning None indicates parsing error
		""" #According to the RFC, the body starts after a \r\n\r\n sequence #since the body maybe absent sometimes, this avoids an IndexError #RFC : Request-Line = Method SP Request-URI SP HTTP-Version CRLF #Request-URI = "*" | absoluteURI | abs_path | authority #lower used since according to RFC, header keys are case insensitive #Some values maybe case sensitive or otherwise, depending on key, THAT CASE NOT HANDLED #At this point requestLine(dictionary), requestHeaders(dictionary) and requestBody(string) constitute the entire message #uncomment line below if debugging to compare with original requestStr #print(requestStr)
requestStr