mirror of
https://github.com/Grasscutters/mitmproxy.git
synced 2024-11-23 00:01:36 +00:00
First pass at static file serving.
This commit is contained in:
parent
b4105be21e
commit
2c8f17eae7
@ -1,4 +1,4 @@
|
|||||||
import operator, string, random, sys, time
|
import operator, string, random, sys, time, mmap, os
|
||||||
import contrib.pyparsing as pp
|
import contrib.pyparsing as pp
|
||||||
import http, utils
|
import http, utils
|
||||||
import tornado.ioloop
|
import tornado.ioloop
|
||||||
@ -6,6 +6,7 @@ import tornado.ioloop
|
|||||||
TESTING = False
|
TESTING = False
|
||||||
|
|
||||||
class ParseException(Exception): pass
|
class ParseException(Exception): pass
|
||||||
|
class ServerError(Exception): pass
|
||||||
|
|
||||||
|
|
||||||
DATATYPES = dict(
|
DATATYPES = dict(
|
||||||
@ -81,6 +82,17 @@ class RandomGenerator:
|
|||||||
class FileGenerator:
|
class FileGenerator:
|
||||||
def __init__(self, path):
|
def __init__(self, path):
|
||||||
self.path = path
|
self.path = path
|
||||||
|
self.fp = file(path, "r")
|
||||||
|
self.map = mmap.mmap(self.fp.fileno(), 0, prot=mmap.PROT_READ)
|
||||||
|
|
||||||
|
def __len__(self):
|
||||||
|
return len(self.map)
|
||||||
|
|
||||||
|
def __getitem__(self, x):
|
||||||
|
return self.map.__getitem__(x)
|
||||||
|
|
||||||
|
def __getslice__(self, a, b):
|
||||||
|
return self.map.__getslice__(a, b)
|
||||||
|
|
||||||
|
|
||||||
class ValueLiteral:
|
class ValueLiteral:
|
||||||
@ -145,7 +157,13 @@ class ValueFile:
|
|||||||
return e.setParseAction(lambda x: klass(*x))
|
return e.setParseAction(lambda x: klass(*x))
|
||||||
|
|
||||||
def get_generator(self, settings):
|
def get_generator(self, settings):
|
||||||
raise NotImplementedError
|
sd = settings.get("staticdir")
|
||||||
|
if not sd:
|
||||||
|
raise ServerError("No static directory specified.")
|
||||||
|
path = os.path.join(sd, self.path)
|
||||||
|
if not os.path.exists(path):
|
||||||
|
raise ServerError("Static file does not exist: %s"%path)
|
||||||
|
return FileGenerator(path)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return "<%s"%(self.path)
|
return "<%s"%(self.path)
|
||||||
|
6
pathod
6
pathod
@ -1,6 +1,6 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
import argparse
|
import argparse
|
||||||
import libomnid
|
import libpathod
|
||||||
import tornado.ioloop
|
import tornado.ioloop
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
@ -12,8 +12,8 @@ if __name__ == "__main__":
|
|||||||
)
|
)
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
libomnid.application(staticdir=args.staticdir).listen(args.port)
|
libpathod.application(staticdir=args.staticdir).listen(args.port)
|
||||||
print "omnid listening on port %s"%args.port
|
print "pathod listening on port %s"%args.port
|
||||||
try:
|
try:
|
||||||
tornado.ioloop.IOLoop.instance().start()
|
tornado.ioloop.IOLoop.instance().start()
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import StringIO, sys
|
import StringIO, sys, os
|
||||||
import libpry
|
import libpry
|
||||||
from libpathod import rparse
|
from libpathod import rparse
|
||||||
|
|
||||||
@ -24,11 +24,43 @@ class uMisc(libpry.AutoTree):
|
|||||||
assert g[:] == "one"
|
assert g[:] == "one"
|
||||||
assert g[1] == "n"
|
assert g[1] == "n"
|
||||||
|
|
||||||
|
def test_filegenerator(self):
|
||||||
|
t = self.tmpdir()
|
||||||
|
path = os.path.join(t, "foo")
|
||||||
|
f = open(path, "w")
|
||||||
|
f.write("x"*10000)
|
||||||
|
f.close()
|
||||||
|
g = rparse.FileGenerator(path)
|
||||||
|
assert len(g) == 10000
|
||||||
|
assert g[0] == "x"
|
||||||
|
assert g[-1] == "x"
|
||||||
|
assert g[0:5] == "xxxxx"
|
||||||
|
|
||||||
def test_valueliteral(self):
|
def test_valueliteral(self):
|
||||||
v = rparse.ValueLiteral("foo")
|
v = rparse.ValueLiteral("foo")
|
||||||
assert v.expr()
|
assert v.expr()
|
||||||
assert str(v)
|
assert str(v)
|
||||||
|
|
||||||
|
def test_file_value(self):
|
||||||
|
v = rparse.Value.parseString("<'one two'")[0]
|
||||||
|
assert v.path == "one two"
|
||||||
|
|
||||||
|
v = rparse.Value.parseString("<path")[0]
|
||||||
|
assert v.path == "path"
|
||||||
|
|
||||||
|
t = self.tmpdir()
|
||||||
|
p = os.path.join(t, "path")
|
||||||
|
f = open(p, "w")
|
||||||
|
f.write("x"*10000)
|
||||||
|
f.close()
|
||||||
|
|
||||||
|
assert v.get_generator(dict(staticdir=t))
|
||||||
|
|
||||||
|
v = rparse.Value.parseString("<path2")[0]
|
||||||
|
libpry.raises(rparse.ServerError, v.get_generator, dict(staticdir=t))
|
||||||
|
libpry.raises("no static directory", v.get_generator, dict())
|
||||||
|
|
||||||
|
|
||||||
def test_generated_value(self):
|
def test_generated_value(self):
|
||||||
v = rparse.Value.parseString("!10b")[0]
|
v = rparse.Value.parseString("!10b")[0]
|
||||||
assert v.usize == 10
|
assert v.usize == 10
|
||||||
@ -56,11 +88,6 @@ class uMisc(libpry.AutoTree):
|
|||||||
assert rparse.Value.parseString('"val"')[0].val == "val"
|
assert rparse.Value.parseString('"val"')[0].val == "val"
|
||||||
assert rparse.Value.parseString('"\'val\'"')[0].val == "'val'"
|
assert rparse.Value.parseString('"\'val\'"')[0].val == "'val'"
|
||||||
|
|
||||||
v = rparse.Value.parseString("<path")[0]
|
|
||||||
assert v.path == "path"
|
|
||||||
v = rparse.Value.parseString("<'one two'")[0]
|
|
||||||
assert v.path == "one two"
|
|
||||||
|
|
||||||
def test_body(self):
|
def test_body(self):
|
||||||
e = rparse.Body.expr()
|
e = rparse.Body.expr()
|
||||||
v = e.parseString("b:foo")[0]
|
v = e.parseString("b:foo")[0]
|
||||||
|
Loading…
Reference in New Issue
Block a user