2020-01-01 10:07:14 -05:00
|
|
|
#!/usr/bin/env python3
|
2008-11-03 16:56:49 -05:00
|
|
|
|
|
|
|
import os
|
2020-01-01 10:07:14 -05:00
|
|
|
from http.server import *
|
2008-11-03 16:56:49 -05:00
|
|
|
import tempfile
|
|
|
|
import signal
|
|
|
|
|
|
|
|
r, w = os.pipe()
|
|
|
|
|
|
|
|
C_CR = 0
|
|
|
|
C_LF = 1
|
|
|
|
C_CRLF = 2
|
|
|
|
|
|
|
|
E_Raw = 0
|
|
|
|
E_Entity = 1
|
|
|
|
E_JavaScript = 2
|
|
|
|
|
|
|
|
F_Hidden = 0
|
|
|
|
F_TextArea = 1
|
|
|
|
|
|
|
|
def encode(ch, encoding):
|
2020-01-01 10:07:14 -05:00
|
|
|
if ch == C_CRLF:
|
|
|
|
return encode(C_CR, encoding) + encode(C_LF, encoding)
|
|
|
|
if ch == C_CR:
|
|
|
|
if encoding == E_Raw:
|
|
|
|
return "\r"
|
|
|
|
if encoding == E_JavaScript:
|
|
|
|
return "\\r"
|
|
|
|
if encoding == E_Entity:
|
|
|
|
return "
"
|
|
|
|
if ch == C_LF:
|
|
|
|
if encoding == E_Raw:
|
|
|
|
return "\n"
|
|
|
|
if encoding == E_JavaScript:
|
|
|
|
return "\\n"
|
|
|
|
if encoding == E_Entity:
|
|
|
|
return "
"
|
2008-11-03 16:56:49 -05:00
|
|
|
|
|
|
|
def get_form(ch, encoding, field):
|
2020-01-01 10:07:14 -05:00
|
|
|
text = "foo" + encode(ch, encoding) + "bar"
|
|
|
|
if encoding == E_JavaScript:
|
|
|
|
text_initial = ""
|
|
|
|
else:
|
|
|
|
text_initial = text
|
2008-11-03 16:56:49 -05:00
|
|
|
|
2020-01-01 10:07:14 -05:00
|
|
|
s = """<html>
|
2008-11-03 16:56:49 -05:00
|
|
|
<head>
|
|
|
|
<title>Form Test</title>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<form id="form1" name="form1" action="http://127.0.0.1:8090/">
|
|
|
|
"""
|
2020-01-01 10:07:14 -05:00
|
|
|
if field == F_Hidden:
|
|
|
|
s += '<input type="hidden" id="field1" name="field1" value="' + text_initial + '">'
|
|
|
|
elif field == F_TextArea:
|
|
|
|
s += '<textarea id="field1" name="field1">' + text_initial + '</textarea>'
|
|
|
|
s += "\n</form>"
|
|
|
|
if encoding == E_JavaScript:
|
|
|
|
s += """
|
2008-11-03 16:56:49 -05:00
|
|
|
<script>
|
|
|
|
document.form1.field1.value = '%s';
|
|
|
|
</script>""" % (text)
|
|
|
|
|
2020-01-01 10:07:14 -05:00
|
|
|
s += "</body></html>"
|
|
|
|
return s
|
2008-11-03 16:56:49 -05:00
|
|
|
|
|
|
|
class forwarder(BaseHTTPRequestHandler):
|
2020-01-01 10:07:14 -05:00
|
|
|
def do_GET(self):
|
|
|
|
w.write(self.path + "\n")
|
|
|
|
w.flush()
|
|
|
|
self.send_response(200)
|
|
|
|
self.send_header("Content-Type", "text/plain")
|
|
|
|
self.end_headers()
|
|
|
|
self.wfile.write(b"Dummy response")
|
2008-11-03 16:56:49 -05:00
|
|
|
|
|
|
|
def runtest(r, *args):
|
2020-01-01 10:07:14 -05:00
|
|
|
form = get_form(*args)
|
2008-11-03 16:56:49 -05:00
|
|
|
|
2020-01-01 10:07:14 -05:00
|
|
|
tmpfile, tmpname = tempfile.mkstemp(".html")
|
|
|
|
tmpfile = os.fdopen(tmpfile, 'w')
|
|
|
|
tmpfile.write(form)
|
|
|
|
tmpfile.close()
|
2008-11-03 16:56:49 -05:00
|
|
|
|
2020-01-01 10:07:14 -05:00
|
|
|
linkspid = os.spawnlp(os.P_NOWAIT, 'elinks', 'elinks',
|
|
|
|
'-config-dir', os.getcwd(),
|
|
|
|
'-config-file', 'crlf.conf',
|
|
|
|
'-no-connect', '1',
|
|
|
|
'-auto-submit', '1',
|
|
|
|
tmpname)
|
|
|
|
path = r.readline()
|
|
|
|
os.kill(linkspid, signal.SIGINT)
|
|
|
|
os.waitpid(linkspid, 0)
|
2008-11-03 16:56:49 -05:00
|
|
|
|
2020-01-01 10:07:14 -05:00
|
|
|
os.unlink(tmpname)
|
2008-11-03 16:56:49 -05:00
|
|
|
|
2020-01-01 10:07:14 -05:00
|
|
|
return path
|
2008-11-03 16:56:49 -05:00
|
|
|
|
|
|
|
pid = os.fork()
|
|
|
|
|
|
|
|
if pid:
|
2020-01-01 10:07:14 -05:00
|
|
|
os.close(w)
|
|
|
|
r = os.fdopen(r)
|
2008-11-03 16:56:49 -05:00
|
|
|
|
2020-01-01 10:07:14 -05:00
|
|
|
paths = []
|
2008-11-03 16:56:49 -05:00
|
|
|
|
2020-01-01 10:07:14 -05:00
|
|
|
for c in [C_CR, C_LF, C_CRLF]:
|
|
|
|
for e in [E_Raw, E_Entity, E_JavaScript]:
|
|
|
|
for f in [F_Hidden, F_TextArea]:
|
|
|
|
paths.append(("%d %d %d " % (c, e, f)) + runtest(r, c, e, f))
|
2008-11-03 16:56:49 -05:00
|
|
|
|
2020-01-01 10:07:14 -05:00
|
|
|
for path in paths:
|
|
|
|
print(path, end=' ')
|
2008-11-03 16:56:49 -05:00
|
|
|
|
2020-01-01 10:07:14 -05:00
|
|
|
os.kill(pid, signal.SIGTERM)
|
|
|
|
os.waitpid(pid, 0)
|
2008-11-03 16:56:49 -05:00
|
|
|
else:
|
2020-01-01 10:07:14 -05:00
|
|
|
os.close(r)
|
|
|
|
w = os.fdopen(w, 'w')
|
|
|
|
server_address = ('127.0.0.1', 8090)
|
|
|
|
httpd = HTTPServer(server_address, forwarder)
|
|
|
|
httpd.serve_forever()
|