mercurial/hgweb/common.py
changeset 36873 98baf8dea553
parent 36869 7ad6a275316f
child 36877 02bea04b4c54
equal deleted inserted replaced
36872:89002d07a114 36873:98baf8dea553
   151             pycompat.osaltsep is not None and pycompat.osaltsep in part):
   151             pycompat.osaltsep is not None and pycompat.osaltsep in part):
   152             return False
   152             return False
   153 
   153 
   154     return True
   154     return True
   155 
   155 
   156 def staticfile(directory, fname, req):
   156 def staticfile(directory, fname, res):
   157     """return a file inside directory with guessed Content-Type header
   157     """return a file inside directory with guessed Content-Type header
   158 
   158 
   159     fname always uses '/' as directory separator and isn't allowed to
   159     fname always uses '/' as directory separator and isn't allowed to
   160     contain unusual path components.
   160     contain unusual path components.
   161     Content-Type is guessed using the mimetypes module.
   161     Content-Type is guessed using the mimetypes module.
   176         os.stat(path)
   176         os.stat(path)
   177         ct = mimetypes.guess_type(pycompat.fsdecode(path))[0] or "text/plain"
   177         ct = mimetypes.guess_type(pycompat.fsdecode(path))[0] or "text/plain"
   178         with open(path, 'rb') as fh:
   178         with open(path, 'rb') as fh:
   179             data = fh.read()
   179             data = fh.read()
   180 
   180 
   181         req.respond(HTTP_OK, ct, body=data)
   181         res.headers['Content-Type'] = ct
       
   182         res.setbodybytes(data)
       
   183         return res
   182     except TypeError:
   184     except TypeError:
   183         raise ErrorResponse(HTTP_SERVER_ERROR, 'illegal filename')
   185         raise ErrorResponse(HTTP_SERVER_ERROR, 'illegal filename')
   184     except OSError as err:
   186     except OSError as err:
   185         if err.errno == errno.ENOENT:
   187         if err.errno == errno.ENOENT:
   186             raise ErrorResponse(HTTP_NOT_FOUND)
   188             raise ErrorResponse(HTTP_NOT_FOUND)