]>
git.proxmox.com Git - mirror_edk2.git/blob - AppPkg/Applications/Python/Python-2.7.10/Lib/SimpleHTTPServer.py
3 This module builds on BaseHTTPServer by implementing the standard GET
4 and HEAD requests in a fairly straightforward manner.
11 __all__
= ["SimpleHTTPRequestHandler"]
23 from cStringIO
import StringIO
25 from StringIO
import StringIO
28 class SimpleHTTPRequestHandler(BaseHTTPServer
.BaseHTTPRequestHandler
):
30 """Simple HTTP request handler with GET and HEAD commands.
32 This serves files from the current directory and any of its
33 subdirectories. The MIME type for files is determined by
34 calling the .guess_type() method.
36 The GET and HEAD requests are identical except that the HEAD
37 request omits the actual contents of the file.
41 server_version
= "SimpleHTTP/" + __version__
44 """Serve a GET request."""
48 self
.copyfile(f
, self
.wfile
)
53 """Serve a HEAD request."""
59 """Common code for GET and HEAD commands.
61 This sends the response code and MIME headers.
63 Return value is either a file object (which has to be copied
64 to the outputfile by the caller unless the command was HEAD,
65 and must be closed by the caller under all circumstances), or
66 None, in which case the caller has nothing further to do.
69 path
= self
.translate_path(self
.path
)
71 if os
.path
.isdir(path
):
72 parts
= urlparse
.urlsplit(self
.path
)
73 if not parts
.path
.endswith('/'):
74 # redirect browser - doing basically what apache does
75 self
.send_response(301)
76 new_parts
= (parts
[0], parts
[1], parts
[2] + '/',
78 new_url
= urlparse
.urlunsplit(new_parts
)
79 self
.send_header("Location", new_url
)
82 for index
in "index.html", "index.htm":
83 index
= os
.path
.join(path
, index
)
84 if os
.path
.exists(index
):
88 return self
.list_directory(path
)
89 ctype
= self
.guess_type(path
)
91 # Always read in binary mode. Opening files in text mode may cause
92 # newline translations, making the actual size of the content
93 # transmitted *less* than the content-length!
96 self
.send_error(404, "File not found")
99 self
.send_response(200)
100 self
.send_header("Content-type", ctype
)
101 fs
= os
.fstat(f
.fileno())
102 self
.send_header("Content-Length", str(fs
[6]))
103 self
.send_header("Last-Modified", self
.date_time_string(fs
.st_mtime
))
110 def list_directory(self
, path
):
111 """Helper to produce a directory listing (absent index.html).
113 Return value is either a file object, or None (indicating an
114 error). In either case, the headers are sent, making the
115 interface the same as for send_head().
119 list = os
.listdir(path
)
121 self
.send_error(404, "No permission to list directory")
123 list.sort(key
=lambda a
: a
.lower())
125 displaypath
= cgi
.escape(urllib
.unquote(self
.path
))
126 f
.write('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">')
127 f
.write("<html>\n<title>Directory listing for %s</title>\n" % displaypath
)
128 f
.write("<body>\n<h2>Directory listing for %s</h2>\n" % displaypath
)
129 f
.write("<hr>\n<ul>\n")
131 fullname
= os
.path
.join(path
, name
)
132 displayname
= linkname
= name
133 # Append / for directories or @ for symbolic links
134 if os
.path
.isdir(fullname
):
135 displayname
= name
+ "/"
136 linkname
= name
+ "/"
137 if os
.path
.islink(fullname
):
138 displayname
= name
+ "@"
139 # Note: a link to a directory displays with @ and links with /
140 f
.write('<li><a href="%s">%s</a>\n'
141 % (urllib
.quote(linkname
), cgi
.escape(displayname
)))
142 f
.write("</ul>\n<hr>\n</body>\n</html>\n")
145 self
.send_response(200)
146 encoding
= sys
.getfilesystemencoding()
147 self
.send_header("Content-type", "text/html; charset=%s" % encoding
)
148 self
.send_header("Content-Length", str(length
))
152 def translate_path(self
, path
):
153 """Translate a /-separated PATH to the local filename syntax.
155 Components that mean special things to the local file system
156 (e.g. drive or directory names) are ignored. (XXX They should
157 probably be diagnosed.)
160 # abandon query parameters
161 path
= path
.split('?',1)[0]
162 path
= path
.split('#',1)[0]
163 # Don't forget explicit trailing slash when normalizing. Issue17324
164 trailing_slash
= path
.rstrip().endswith('/')
165 path
= posixpath
.normpath(urllib
.unquote(path
))
166 words
= path
.split('/')
167 words
= filter(None, words
)
170 drive
, word
= os
.path
.splitdrive(word
)
171 head
, word
= os
.path
.split(word
)
172 if word
in (os
.curdir
, os
.pardir
): continue
173 path
= os
.path
.join(path
, word
)
178 def copyfile(self
, source
, outputfile
):
179 """Copy all data between two file objects.
181 The SOURCE argument is a file object open for reading
182 (or anything with a read() method) and the DESTINATION
183 argument is a file object open for writing (or
184 anything with a write() method).
186 The only reason for overriding this would be to change
187 the block size or perhaps to replace newlines by CRLF
188 -- note however that this the default server uses this
189 to copy binary data as well.
192 shutil
.copyfileobj(source
, outputfile
)
194 def guess_type(self
, path
):
195 """Guess the type of a file.
197 Argument is a PATH (a filename).
199 Return value is a string of the form type/subtype,
200 usable for a MIME Content-type header.
202 The default implementation looks the file's extension
203 up in the table self.extensions_map, using application/octet-stream
204 as a default; however it would be permissible (if
205 slow) to look inside the data to make a better guess.
209 base
, ext
= posixpath
.splitext(path
)
210 if ext
in self
.extensions_map
:
211 return self
.extensions_map
[ext
]
213 if ext
in self
.extensions_map
:
214 return self
.extensions_map
[ext
]
216 return self
.extensions_map
['']
218 if not mimetypes
.inited
:
219 mimetypes
.init() # try to read system mime.types
220 extensions_map
= mimetypes
.types_map
.copy()
221 extensions_map
.update({
222 '': 'application/octet-stream', # Default
229 def test(HandlerClass
= SimpleHTTPRequestHandler
,
230 ServerClass
= BaseHTTPServer
.HTTPServer
):
231 BaseHTTPServer
.test(HandlerClass
, ServerClass
)
234 if __name__
== '__main__':