forked from benoitc/gunicorn
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor sendfile and add an example.
- Loading branch information
Showing
5 changed files
with
98 additions
and
65 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Hello world! |
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,23 @@ | ||
# -*- coding: utf-8 - | ||
# | ||
# This file is part of gunicorn released under the MIT license. | ||
# See the NOTICE for more information. | ||
# | ||
# Example code from Eventlet sources | ||
|
||
import os | ||
from wsgiref.validate import validator | ||
|
||
#@validator | ||
def app(environ, start_response): | ||
"""Simplest possible application object""" | ||
status = '200 OK' | ||
fname = os.path.join(os.path.dirname(__file__), "hello.txt") | ||
f = open(fname, 'rb') | ||
|
||
response_headers = [ | ||
('Content-type','text/plain'), | ||
] | ||
start_response(status, response_headers) | ||
|
||
return environ['wsgi.file_wrapper'](f) |
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 gunicorn 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() | ||
raise OSError(e, os.strerror(e)) | ||
return sent |
This file was deleted.
Oops, something went wrong.
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