Smartly guess content type header

This commit is contained in:
Darshit Shah 2013-09-13 14:19:37 +05:30
parent d4d0e6378c
commit 43bb61b4e3
2 changed files with 32 additions and 2 deletions

View File

@ -1,3 +1,13 @@
2013-09-13 Darshit Shah <darnir@gmail.com>
* HTTPServer.py (_Handler.do_HEAD): If requested path is /, respond
with /index.html
(_Handler.do_HEAD): Smartly guess value of Content-Type Header from
file extension
(_Handler.guess_type): Use a preset list of extensions and
Content-Type strings. If the extension matches one in the list, use
that string, else default to "text/plain"
2013-09-13 Darshit Shah <darnir@gmail.com>
* WgetTest.py (CommonMethods._replace_substring): New method that will

View File

@ -1,4 +1,5 @@
from http.server import HTTPServer, BaseHTTPRequestHandler
from posixpath import basename, splitext
from base64 import b64encode
from random import random
from hashlib import md5
@ -328,7 +329,11 @@ class _Handler (WgetHTTPRequestHandler):
""" Common code for GET and HEAD Commands.
This method is overriden to use the fileSys dict.
"""
path = self.path[1:]
if self.path == "/":
path = "index.html"
else:
path = self.path[1:]
if path in self.server.fileSys:
self.rules = self.server.server_configs.get (path)
@ -368,7 +373,8 @@ class _Handler (WgetHTTPRequestHandler):
content_length - 1,
content_length))
content_length -= self.range_begin
self.send_header ("Content-type", "text/plain")
cont_type = self.guess_type (path)
self.send_header ("Content-type", cont_type)
self.send_header ("Content-Length", content_length)
self.finish_headers ()
return (content, self.range_begin)
@ -376,6 +382,20 @@ class _Handler (WgetHTTPRequestHandler):
self.send_error (404, "Not Found")
return (None, None)
def guess_type (self, path):
base_name = basename ("/" + path)
name, ext = splitext (base_name)
extension_map = {
".txt" : "text/plain",
".css" : "text/css",
".html" : "text/html"
}
if ext in extension_map:
return extension_map[ext]
else:
return "text/plain"
class HTTPd (threading.Thread):
server_class = StoppableHTTPServer
handler = _Handler