Fix inconsistent return value in send_head

This commit is contained in:
Daniel Goldberg 2017-10-01 19:25:53 +03:00
parent 637b704fa2
commit 39ab50f376
1 changed files with 23 additions and 19 deletions

View File

@ -1,10 +1,14 @@
import urllib, BaseHTTPServer, threading, os.path import BaseHTTPServer
import monkeyfs import os.path
from logging import getLogger
from base import TransportProxyBase, update_last_serve_time
from urlparse import urlsplit
import select import select
import socket import socket
import threading
import urllib
from logging import getLogger
from urlparse import urlsplit
import monkeyfs
from base import TransportProxyBase, update_last_serve_time
__author__ = 'hoffer' __author__ = 'hoffer'
@ -55,9 +59,9 @@ class FileServHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
f.close() f.close()
def send_head(self): def send_head(self):
if self.path != '/'+urllib.quote(os.path.basename(self.filename)): if self.path != '/' + urllib.quote(os.path.basename(self.filename)):
self.send_error (500, "") self.send_error(500, "")
return return None, 0, 0
f = None f = None
try: try:
f = monkeyfs.open(self.filename, 'rb') f = monkeyfs.open(self.filename, 'rb')
@ -101,8 +105,8 @@ class FileServHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
class HTTPConnectProxyHandler(BaseHTTPServer.BaseHTTPRequestHandler): class HTTPConnectProxyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
timeout = 30 # timeout with clients, set to None not to make persistent connection timeout = 30 # timeout with clients, set to None not to make persistent connection
proxy_via = None # pseudonym of the proxy in Via header, set to None not to modify original Via header proxy_via = None # pseudonym of the proxy in Via header, set to None not to modify original Via header
protocol_version = "HTTP/1.1" protocol_version = "HTTP/1.1"
def version_string(self): def version_string(self):
@ -120,7 +124,7 @@ class HTTPConnectProxyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
conn = socket.create_connection(address) conn = socket.create_connection(address)
except socket.error, e: except socket.error, e:
LOG.debug("HTTPConnectProxyHandler: Got exception while trying to connect to %s: %s" % (repr(address), e)) LOG.debug("HTTPConnectProxyHandler: Got exception while trying to connect to %s: %s" % (repr(address), e))
self.send_error(504) # 504 Gateway Timeout self.send_error(504) # 504 Gateway Timeout
return return
self.send_response(200, 'Connection Established') self.send_response(200, 'Connection Established')
self.send_header('Connection', 'close') self.send_header('Connection', 'close')
@ -163,11 +167,11 @@ class HTTPServer(threading.Thread):
@staticmethod @staticmethod
def report_download(dest=None): def report_download(dest=None):
LOG.info('File downloaded from (%s,%s)' % (dest[0],dest[1])) LOG.info('File downloaded from (%s,%s)' % (dest[0], dest[1]))
self.downloads += 1 self.downloads += 1
httpd = BaseHTTPServer.HTTPServer((self._local_ip, self._local_port), TempHandler) httpd = BaseHTTPServer.HTTPServer((self._local_ip, self._local_port), TempHandler)
httpd.timeout = 0.5 # this is irrelevant? httpd.timeout = 0.5 # this is irrelevant?
while not self._stopped and self.downloads < self.max_downloads: while not self._stopped and self.downloads < self.max_downloads:
httpd.handle_request() httpd.handle_request()