PageRenderTime 40ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/lib-python/2.7/CGIHTTPServer.py

https://bitbucket.org/pypy/pypy/
Python | 377 lines | 351 code | 5 blank | 21 comment | 18 complexity | ae5a65176d5d046aa79bcfb659dade77 MD5 | raw file
Possible License(s): AGPL-3.0, BSD-3-Clause, Apache-2.0
  1. """CGI-savvy HTTP Server.
  2. This module builds on SimpleHTTPServer by implementing GET and POST
  3. requests to cgi-bin scripts.
  4. If the os.fork() function is not present (e.g. on Windows),
  5. os.popen2() is used as a fallback, with slightly altered semantics; if
  6. that function is not present either (e.g. on Macintosh), only Python
  7. scripts are supported, and they are executed by the current process.
  8. In all cases, the implementation is intentionally naive -- all
  9. requests are executed sychronously.
  10. SECURITY WARNING: DON'T USE THIS CODE UNLESS YOU ARE INSIDE A FIREWALL
  11. -- it may execute arbitrary Python code or external programs.
  12. Note that status code 200 is sent prior to execution of a CGI script, so
  13. scripts cannot send other status codes such as 302 (redirect).
  14. """
  15. __version__ = "0.4"
  16. __all__ = ["CGIHTTPRequestHandler"]
  17. import os
  18. import sys
  19. import urllib
  20. import BaseHTTPServer
  21. import SimpleHTTPServer
  22. import select
  23. import copy
  24. class CGIHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
  25. """Complete HTTP server with GET, HEAD and POST commands.
  26. GET and HEAD also support running CGI scripts.
  27. The POST command is *only* implemented for CGI scripts.
  28. """
  29. # Determine platform specifics
  30. have_fork = hasattr(os, 'fork')
  31. have_popen2 = hasattr(os, 'popen2')
  32. have_popen3 = hasattr(os, 'popen3')
  33. # Make rfile unbuffered -- we need to read one line and then pass
  34. # the rest to a subprocess, so we can't use buffered input.
  35. rbufsize = 0
  36. def do_POST(self):
  37. """Serve a POST request.
  38. This is only implemented for CGI scripts.
  39. """
  40. if self.is_cgi():
  41. self.run_cgi()
  42. else:
  43. self.send_error(501, "Can only POST to CGI scripts")
  44. def send_head(self):
  45. """Version of send_head that support CGI scripts"""
  46. if self.is_cgi():
  47. return self.run_cgi()
  48. else:
  49. return SimpleHTTPServer.SimpleHTTPRequestHandler.send_head(self)
  50. def is_cgi(self):
  51. """Test whether self.path corresponds to a CGI script.
  52. Returns True and updates the cgi_info attribute to the tuple
  53. (dir, rest) if self.path requires running a CGI script.
  54. Returns False otherwise.
  55. If any exception is raised, the caller should assume that
  56. self.path was rejected as invalid and act accordingly.
  57. The default implementation tests whether the normalized url
  58. path begins with one of the strings in self.cgi_directories
  59. (and the next character is a '/' or the end of the string).
  60. """
  61. collapsed_path = _url_collapse_path(urllib.unquote(self.path))
  62. dir_sep = collapsed_path.find('/', 1)
  63. head, tail = collapsed_path[:dir_sep], collapsed_path[dir_sep+1:]
  64. if head in self.cgi_directories:
  65. self.cgi_info = head, tail
  66. return True
  67. return False
  68. cgi_directories = ['/cgi-bin', '/htbin']
  69. def is_executable(self, path):
  70. """Test whether argument path is an executable file."""
  71. return executable(path)
  72. def is_python(self, path):
  73. """Test whether argument path is a Python script."""
  74. head, tail = os.path.splitext(path)
  75. return tail.lower() in (".py", ".pyw")
  76. def run_cgi(self):
  77. """Execute a CGI script."""
  78. dir, rest = self.cgi_info
  79. path = dir + '/' + rest
  80. i = path.find('/', len(dir)+1)
  81. while i >= 0:
  82. nextdir = path[:i]
  83. nextrest = path[i+1:]
  84. scriptdir = self.translate_path(nextdir)
  85. if os.path.isdir(scriptdir):
  86. dir, rest = nextdir, nextrest
  87. i = path.find('/', len(dir)+1)
  88. else:
  89. break
  90. # find an explicit query string, if present.
  91. i = rest.rfind('?')
  92. if i >= 0:
  93. rest, query = rest[:i], rest[i+1:]
  94. else:
  95. query = ''
  96. # dissect the part after the directory name into a script name &
  97. # a possible additional path, to be stored in PATH_INFO.
  98. i = rest.find('/')
  99. if i >= 0:
  100. script, rest = rest[:i], rest[i:]
  101. else:
  102. script, rest = rest, ''
  103. scriptname = dir + '/' + script
  104. scriptfile = self.translate_path(scriptname)
  105. if not os.path.exists(scriptfile):
  106. self.send_error(404, "No such CGI script (%r)" % scriptname)
  107. return
  108. if not os.path.isfile(scriptfile):
  109. self.send_error(403, "CGI script is not a plain file (%r)" %
  110. scriptname)
  111. return
  112. ispy = self.is_python(scriptname)
  113. if not ispy:
  114. if not (self.have_fork or self.have_popen2 or self.have_popen3):
  115. self.send_error(403, "CGI script is not a Python script (%r)" %
  116. scriptname)
  117. return
  118. if not self.is_executable(scriptfile):
  119. self.send_error(403, "CGI script is not executable (%r)" %
  120. scriptname)
  121. return
  122. # Reference: http://hoohoo.ncsa.uiuc.edu/cgi/env.html
  123. # XXX Much of the following could be prepared ahead of time!
  124. env = copy.deepcopy(os.environ)
  125. env['SERVER_SOFTWARE'] = self.version_string()
  126. env['SERVER_NAME'] = self.server.server_name
  127. env['GATEWAY_INTERFACE'] = 'CGI/1.1'
  128. env['SERVER_PROTOCOL'] = self.protocol_version
  129. env['SERVER_PORT'] = str(self.server.server_port)
  130. env['REQUEST_METHOD'] = self.command
  131. uqrest = urllib.unquote(rest)
  132. env['PATH_INFO'] = uqrest
  133. env['PATH_TRANSLATED'] = self.translate_path(uqrest)
  134. env['SCRIPT_NAME'] = scriptname
  135. if query:
  136. env['QUERY_STRING'] = query
  137. host = self.address_string()
  138. if host != self.client_address[0]:
  139. env['REMOTE_HOST'] = host
  140. env['REMOTE_ADDR'] = self.client_address[0]
  141. authorization = self.headers.getheader("authorization")
  142. if authorization:
  143. authorization = authorization.split()
  144. if len(authorization) == 2:
  145. import base64, binascii
  146. env['AUTH_TYPE'] = authorization[0]
  147. if authorization[0].lower() == "basic":
  148. try:
  149. authorization = base64.decodestring(authorization[1])
  150. except binascii.Error:
  151. pass
  152. else:
  153. authorization = authorization.split(':')
  154. if len(authorization) == 2:
  155. env['REMOTE_USER'] = authorization[0]
  156. # XXX REMOTE_IDENT
  157. if self.headers.typeheader is None:
  158. env['CONTENT_TYPE'] = self.headers.type
  159. else:
  160. env['CONTENT_TYPE'] = self.headers.typeheader
  161. length = self.headers.getheader('content-length')
  162. if length:
  163. env['CONTENT_LENGTH'] = length
  164. referer = self.headers.getheader('referer')
  165. if referer:
  166. env['HTTP_REFERER'] = referer
  167. accept = []
  168. for line in self.headers.getallmatchingheaders('accept'):
  169. if line[:1] in "\t\n\r ":
  170. accept.append(line.strip())
  171. else:
  172. accept = accept + line[7:].split(',')
  173. env['HTTP_ACCEPT'] = ','.join(accept)
  174. ua = self.headers.getheader('user-agent')
  175. if ua:
  176. env['HTTP_USER_AGENT'] = ua
  177. co = filter(None, self.headers.getheaders('cookie'))
  178. if co:
  179. env['HTTP_COOKIE'] = ', '.join(co)
  180. # XXX Other HTTP_* headers
  181. # Since we're setting the env in the parent, provide empty
  182. # values to override previously set values
  183. for k in ('QUERY_STRING', 'REMOTE_HOST', 'CONTENT_LENGTH',
  184. 'HTTP_USER_AGENT', 'HTTP_COOKIE', 'HTTP_REFERER'):
  185. env.setdefault(k, "")
  186. self.send_response(200, "Script output follows")
  187. decoded_query = query.replace('+', ' ')
  188. if self.have_fork:
  189. # Unix -- fork as we should
  190. args = [script]
  191. if '=' not in decoded_query:
  192. args.append(decoded_query)
  193. nobody = nobody_uid()
  194. self.wfile.flush() # Always flush before forking
  195. pid = os.fork()
  196. if pid != 0:
  197. # Parent
  198. pid, sts = os.waitpid(pid, 0)
  199. # throw away additional data [see bug #427345]
  200. while select.select([self.rfile], [], [], 0)[0]:
  201. if not self.rfile.read(1):
  202. break
  203. if sts:
  204. self.log_error("CGI script exit status %#x", sts)
  205. return
  206. # Child
  207. try:
  208. try:
  209. os.setuid(nobody)
  210. except os.error:
  211. pass
  212. os.dup2(self.rfile.fileno(), 0)
  213. os.dup2(self.wfile.fileno(), 1)
  214. os.execve(scriptfile, args, env)
  215. except:
  216. self.server.handle_error(self.request, self.client_address)
  217. os._exit(127)
  218. else:
  219. # Non Unix - use subprocess
  220. import subprocess
  221. cmdline = [scriptfile]
  222. if self.is_python(scriptfile):
  223. interp = sys.executable
  224. if interp.lower().endswith("w.exe"):
  225. # On Windows, use python.exe, not pythonw.exe
  226. interp = interp[:-5] + interp[-4:]
  227. cmdline = [interp, '-u'] + cmdline
  228. if '=' not in query:
  229. cmdline.append(query)
  230. self.log_message("command: %s", subprocess.list2cmdline(cmdline))
  231. try:
  232. nbytes = int(length)
  233. except (TypeError, ValueError):
  234. nbytes = 0
  235. p = subprocess.Popen(cmdline,
  236. stdin = subprocess.PIPE,
  237. stdout = subprocess.PIPE,
  238. stderr = subprocess.PIPE,
  239. env = env
  240. )
  241. if self.command.lower() == "post" and nbytes > 0:
  242. data = self.rfile.read(nbytes)
  243. else:
  244. data = None
  245. # throw away additional data [see bug #427345]
  246. while select.select([self.rfile._sock], [], [], 0)[0]:
  247. if not self.rfile._sock.recv(1):
  248. break
  249. stdout, stderr = p.communicate(data)
  250. self.wfile.write(stdout)
  251. if stderr:
  252. self.log_error('%s', stderr)
  253. p.stderr.close()
  254. p.stdout.close()
  255. status = p.returncode
  256. if status:
  257. self.log_error("CGI script exit status %#x", status)
  258. else:
  259. self.log_message("CGI script exited OK")
  260. def _url_collapse_path(path):
  261. """
  262. Given a URL path, remove extra '/'s and '.' path elements and collapse
  263. any '..' references and returns a colllapsed path.
  264. Implements something akin to RFC-2396 5.2 step 6 to parse relative paths.
  265. The utility of this function is limited to is_cgi method and helps
  266. preventing some security attacks.
  267. Returns: A tuple of (head, tail) where tail is everything after the final /
  268. and head is everything before it. Head will always start with a '/' and,
  269. if it contains anything else, never have a trailing '/'.
  270. Raises: IndexError if too many '..' occur within the path.
  271. """
  272. # Similar to os.path.split(os.path.normpath(path)) but specific to URL
  273. # path semantics rather than local operating system semantics.
  274. path_parts = path.split('/')
  275. head_parts = []
  276. for part in path_parts[:-1]:
  277. if part == '..':
  278. head_parts.pop() # IndexError if more '..' than prior parts
  279. elif part and part != '.':
  280. head_parts.append( part )
  281. if path_parts:
  282. tail_part = path_parts.pop()
  283. if tail_part:
  284. if tail_part == '..':
  285. head_parts.pop()
  286. tail_part = ''
  287. elif tail_part == '.':
  288. tail_part = ''
  289. else:
  290. tail_part = ''
  291. splitpath = ('/' + '/'.join(head_parts), tail_part)
  292. collapsed_path = "/".join(splitpath)
  293. return collapsed_path
  294. nobody = None
  295. def nobody_uid():
  296. """Internal routine to get nobody's uid"""
  297. global nobody
  298. if nobody:
  299. return nobody
  300. try:
  301. import pwd
  302. except ImportError:
  303. return -1
  304. try:
  305. nobody = pwd.getpwnam('nobody')[2]
  306. except KeyError:
  307. nobody = 1 + max(map(lambda x: x[2], pwd.getpwall()))
  308. return nobody
  309. def executable(path):
  310. """Test for executable file."""
  311. try:
  312. st = os.stat(path)
  313. except os.error:
  314. return False
  315. return st.st_mode & 0111 != 0
  316. def test(HandlerClass = CGIHTTPRequestHandler,
  317. ServerClass = BaseHTTPServer.HTTPServer):
  318. SimpleHTTPServer.test(HandlerClass, ServerClass)
  319. if __name__ == '__main__':
  320. test()