blob: 9260e7e479b0a1673bfde5c9d167ce59ec577846 [file] [log] [blame]
Guido van Rossume7e578f1995-08-04 04:00:20 +00001"""Simple HTTP Server.
2
3This module builds on BaseHTTPServer by implementing the standard GET
4and HEAD requests in a fairly straightforward manner.
5
6"""
7
8
Guido van Rossum3b7b8131995-09-18 21:50:43 +00009__version__ = "0.3"
Guido van Rossume7e578f1995-08-04 04:00:20 +000010
11
12import os
Guido van Rossume7e578f1995-08-04 04:00:20 +000013import sys
14import time
15import socket
16import string
17import posixpath
18import SocketServer
19import BaseHTTPServer
20
21
Guido van Rossume7e578f1995-08-04 04:00:20 +000022class SimpleHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
23
24 """Simple HTTP request handler with GET and HEAD commands.
25
26 This serves files from the current directory and any of its
27 subdirectories. It assumes that all files are plain text files
28 unless they have the extension ".html" in which case it assumes
29 they are HTML files.
30
31 The GET and HEAD requests are identical except that the HEAD
32 request omits the actual contents of the file.
33
34 """
35
36 server_version = "SimpleHTTP/" + __version__
37
38 def do_GET(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000039 """Serve a GET request."""
40 f = self.send_head()
41 if f:
42 self.copyfile(f, self.wfile)
43 f.close()
Guido van Rossume7e578f1995-08-04 04:00:20 +000044
45 def do_HEAD(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000046 """Serve a HEAD request."""
47 f = self.send_head()
48 if f:
49 f.close()
Guido van Rossume7e578f1995-08-04 04:00:20 +000050
51 def send_head(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000052 """Common code for GET and HEAD commands.
Guido van Rossume7e578f1995-08-04 04:00:20 +000053
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000054 This sends the response code and MIME headers.
Guido van Rossume7e578f1995-08-04 04:00:20 +000055
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000056 Return value is either a file object (which has to be copied
57 to the outputfile by the caller unless the command was HEAD,
58 and must be closed by the caller under all circumstances), or
59 None, in which case the caller has nothing further to do.
Guido van Rossume7e578f1995-08-04 04:00:20 +000060
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000061 """
62 path = self.translate_path(self.path)
63 if os.path.isdir(path):
64 self.send_error(403, "Directory listing not supported")
65 return None
66 try:
Guido van Rossum391c8b41998-12-07 03:53:18 +000067 f = open(path, 'rb')
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000068 except IOError:
69 self.send_error(404, "File not found")
70 return None
71 self.send_response(200)
72 self.send_header("Content-type", self.guess_type(path))
73 self.end_headers()
74 return f
Guido van Rossume7e578f1995-08-04 04:00:20 +000075
76 def translate_path(self, path):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000077 """Translate a /-separated PATH to the local filename syntax.
Guido van Rossume7e578f1995-08-04 04:00:20 +000078
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000079 Components that mean special things to the local file system
80 (e.g. drive or directory names) are ignored. (XXX They should
81 probably be diagnosed.)
Guido van Rossume7e578f1995-08-04 04:00:20 +000082
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000083 """
84 path = posixpath.normpath(path)
85 words = string.splitfields(path, '/')
86 words = filter(None, words)
87 path = os.getcwd()
88 for word in words:
89 drive, word = os.path.splitdrive(word)
90 head, word = os.path.split(word)
91 if word in (os.curdir, os.pardir): continue
92 path = os.path.join(path, word)
93 return path
Guido van Rossume7e578f1995-08-04 04:00:20 +000094
95 def copyfile(self, source, outputfile):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000096 """Copy all data between two file objects.
Guido van Rossume7e578f1995-08-04 04:00:20 +000097
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000098 The SOURCE argument is a file object open for reading
99 (or anything with a read() method) and the DESTINATION
100 argument is a file object open for writing (or
101 anything with a write() method).
Guido van Rossume7e578f1995-08-04 04:00:20 +0000102
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000103 The only reason for overriding this would be to change
104 the block size or perhaps to replace newlines by CRLF
105 -- note however that this the default server uses this
106 to copy binary data as well.
Guido van Rossume7e578f1995-08-04 04:00:20 +0000107
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000108 """
Guido van Rossume7e578f1995-08-04 04:00:20 +0000109
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000110 BLOCKSIZE = 8192
111 while 1:
112 data = source.read(BLOCKSIZE)
113 if not data: break
114 outputfile.write(data)
Guido van Rossume7e578f1995-08-04 04:00:20 +0000115
116 def guess_type(self, path):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000117 """Guess the type of a file.
Guido van Rossume7e578f1995-08-04 04:00:20 +0000118
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000119 Argument is a PATH (a filename).
Guido van Rossume7e578f1995-08-04 04:00:20 +0000120
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000121 Return value is a string of the form type/subtype,
122 usable for a MIME Content-type header.
Guido van Rossume7e578f1995-08-04 04:00:20 +0000123
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000124 The default implementation looks the file's extension
125 up in the table self.extensions_map, using text/plain
126 as a default; however it would be permissible (if
127 slow) to look inside the data to make a better guess.
Guido van Rossume7e578f1995-08-04 04:00:20 +0000128
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000129 """
Guido van Rossume7e578f1995-08-04 04:00:20 +0000130
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000131 base, ext = posixpath.splitext(path)
132 if self.extensions_map.has_key(ext):
133 return self.extensions_map[ext]
134 ext = string.lower(ext)
135 if self.extensions_map.has_key(ext):
136 return self.extensions_map[ext]
137 else:
138 return self.extensions_map['']
Guido van Rossume7e578f1995-08-04 04:00:20 +0000139
140 extensions_map = {
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000141 '': 'text/plain', # Default, *must* be present
142 '.html': 'text/html',
143 '.htm': 'text/html',
144 '.gif': 'image/gif',
145 '.jpg': 'image/jpeg',
146 '.jpeg': 'image/jpeg',
147 }
Guido van Rossume7e578f1995-08-04 04:00:20 +0000148
149
150def test(HandlerClass = SimpleHTTPRequestHandler,
Guido van Rossum5c3b3841998-12-07 04:08:30 +0000151 ServerClass = BaseHTTPServer.HTTPServer):
Guido van Rossume7e578f1995-08-04 04:00:20 +0000152 BaseHTTPServer.test(HandlerClass, ServerClass)
153
154
155if __name__ == '__main__':
156 test()