me.fier.tcpserver/module/http/header.py

52 lines
1.1 KiB
Python

import urllib.parse
http = mfp.require("http")
def receive(connection,limit = 0):
count = 0
nl = False
data = b""
while True:
if limit > 0:
count += 1
if count >= limit:
raise http.exception.length("Header exceeding given limit")
b = http.connection.recv(connection,1)
data = data + b
if b[0] == 0x0A:
if nl == False:
nl = True
continue
if nl == True:
return data
else:
if b[0] != 0x0D:
nl = False
def parse(text):
text = text.split("\n")
text[0] = text[0].rstrip("\r").split(" ",2)
i = 1
length = len(text)
while i < length:
text[i] = text[i].rstrip(" \t\r")
text[i] = text[i].lstrip(" \t")
if text[i] == "":
del text[i]
length -= 1
continue
text[i] = text[i].split(":",1)
if len(text[i]) != 2: raise http.exception.parse("No separator (:) in header.")
text[i][0] = text[i][0].rstrip(" \t").lower()
text[i][1] = text[i][1].lstrip(" \t")
i += 1
return text
def create(headers):
text = ""
for i in range(len(headers)):
text = text + headers[i][0] + ": " +headers[i][1]+ "\r\n"
return text