Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add possibility to return a file. Use sendfile API if it's available
- Loading branch information
Showing
7 changed files
with
184 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import os | ||
|
||
WELCOME_FILE = os.path.join(os.path.dirname(__file__), "welcome.txt") | ||
|
||
def proxy(data): | ||
fno = os.open(WELCOME_FILE, os.O_RDONLY) | ||
return { | ||
"file": fno, | ||
"reply": "HTTP/1.1 200 OK\r\n\r\n" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
hello! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# -*- coding: utf-8 - | ||
# | ||
# This file is part of tproxy released under the MIT license. | ||
# See the NOTICE for more information. | ||
|
||
import errno | ||
import os | ||
import sys | ||
|
||
try: | ||
import ctypes | ||
import ctypes.util | ||
except MemoryError: | ||
# selinux execmem denial | ||
# https://bugzilla.redhat.com/show_bug.cgi?id=488396 | ||
raise ImportError | ||
|
||
SUPPORTED_PLATFORMS = ( | ||
'darwin', | ||
'freebsd', | ||
'dragonfly' | ||
'linux2') | ||
|
||
if sys.version_info < (2, 6) or \ | ||
sys.platform not in SUPPORTED_PLATFORMS: | ||
raise ImportError("sendfile isn't supported on this platform") | ||
|
||
_libc = ctypes.CDLL(ctypes.util.find_library("c"), use_errno=True) | ||
_sendfile = _libc.sendfile | ||
|
||
def sendfile(fdout, fdin, offset, nbytes): | ||
if sys.platform == 'darwin': | ||
_sendfile.argtypes = [ctypes.c_int, ctypes.c_int, ctypes.c_uint64, | ||
ctypes.POINTER(ctypes.c_uint64), ctypes.c_voidp, | ||
ctypes.c_int] | ||
_nbytes = ctypes.c_uint64(nbytes) | ||
result = _sendfile(fdin, fdout, offset, _nbytes, None, 0) | ||
if result == -1: | ||
e = ctypes.get_errno() | ||
if e == errno.EAGAIN and _nbytes.value: | ||
return nbytes.value | ||
raise OSError(e, os.strerror(e)) | ||
return _nbytes.value | ||
elif sys.platform in ('freebsd', 'dragonfly',): | ||
_sendfile.argtypes = [ctypes.c_int, ctypes.c_int, ctypes.c_uint64, | ||
ctypes.c_uint64, ctypes.c_voidp, | ||
ctypes.POINTER(ctypes.c_uint64), ctypes.c_int] | ||
_sbytes = ctypes.c_uint64(nbytes) | ||
result = _sendfile(fdin, fdout, offset, nbytes, None, _sbytes, 0) | ||
if result == -1: | ||
e = ctypes.get_errno() | ||
if e == errno.EAGAIN and _sbytes.value: | ||
return _sbytes.value | ||
raise OSError(e, os.strerror(e)) | ||
return _sbytes.value | ||
|
||
else: | ||
_sendfile.argtypes = [ctypes.c_int, ctypes.c_int, | ||
ctypes.POINTER(ctypes.c_uint64), ctypes.c_size_t] | ||
|
||
_offset = ctypes.c_uint64(offset) | ||
sent = _sendfile(fdout, fdin, _offset, nbytes) | ||
if sent == -1: | ||
e = ctypess.get_errno() | ||
This comment has been minimized.
Sorry, something went wrong. |
||
raise OSError(e, os.strerror(e)) | ||
return sent |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# -*- coding: utf-8 - | ||
# | ||
# This file is part of tproxy released under the MIT license. | ||
# See the NOTICE for more information. | ||
|
||
import errno | ||
import io | ||
import os | ||
try: | ||
from os import sendfile | ||
except ImportError: | ||
try: | ||
from _sendfile import sendfile | ||
except ImportError: | ||
def sendfile(fdout, fdin, offset, nbytes): | ||
fsize = os.fstat(fdin).st_size | ||
|
||
# max to send | ||
length = min(fsize-offset, nbytes) | ||
|
||
with os.fdopen(fdin) as fin: | ||
fin.seek(offset) | ||
|
||
while length > 0: | ||
l = min(length, io.DEFAULT_BUFFER_SIZE) | ||
os.write(fdout, fin.read(l)) | ||
length = length - l | ||
|
||
return length | ||
|
||
from gevent.socket import wait_write | ||
|
||
|
||
def async_sendfile(fdout, fdin, offset, nbytes): | ||
total_sent = 0 | ||
while total_sent < nbytes: | ||
try: | ||
sent = sendfile(fdout, fdin, offset + total_sent, | ||
nbytes - total_sent) | ||
total_sent += sent | ||
except OSError, e: | ||
if e.args[0] == errno.EAGAIN: | ||
wait_write(fdout) | ||
else: | ||
raise | ||
return total_sent |
typo in here? ctypes?