A Python UPnP Media Server
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
4.0 KiB

  1. --- TwistedWeb-0.5.0/twisted/web/static.py Sun Jan 2 15:33:41 2005
  2. +++ /usr/local/lib/python2.4/site-packages/twisted/web/static.py Fri Feb 17 23:55:04 2006
  3. @@ -306,7 +306,7 @@
  4. #for content-length
  5. fsize = size = self.getFileSize()
  6. -# request.setHeader('accept-ranges','bytes')
  7. + request.setHeader('accept-ranges','bytes')
  8. if self.type:
  9. request.setHeader('content-type', self.type)
  10. @@ -325,39 +325,59 @@
  11. if request.setLastModified(self.getmtime()) is http.CACHED:
  12. return ''
  13. + trans = True
  14. # Commented out because it's totally broken. --jknight 11/29/04
  15. -# try:
  16. -# range = request.getHeader('range')
  17. -#
  18. -# if range is not None:
  19. -# # This is a request for partial data...
  20. -# bytesrange = string.split(range, '=')
  21. -# assert bytesrange[0] == 'bytes',\
  22. -# "Syntactically invalid http range header!"
  23. -# start, end = string.split(bytesrange[1],'-')
  24. -# if start:
  25. -# f.seek(int(start))
  26. -# if end:
  27. -# end = int(end)
  28. -# size = end
  29. -# else:
  30. -# end = size
  31. -# request.setResponseCode(http.PARTIAL_CONTENT)
  32. -# request.setHeader('content-range',"bytes %s-%s/%s " % (
  33. -# str(start), str(end), str(size)))
  34. -# #content-length should be the actual size of the stuff we're
  35. -# #sending, not the full size of the on-server entity.
  36. -# fsize = end - int(start)
  37. -#
  38. -# request.setHeader('content-length', str(fsize))
  39. -# except:
  40. -# traceback.print_exc(file=log.logfile)
  41. +# XXX - fixed? jmg 2/17/06
  42. + try:
  43. + range = request.getHeader('range')
  44. +
  45. + tsize = size
  46. + if range is not None:
  47. + # This is a request for partial data...
  48. + bytesrange = string.split(range, '=')
  49. + assert bytesrange[0] == 'bytes',\
  50. + "Syntactically invalid http range header!"
  51. + start, end = string.split(bytesrange[1],'-', 1)
  52. + if start:
  53. + f.seek(int(start))
  54. + if end:
  55. + end = int(end)
  56. + else:
  57. + end = size - 1
  58. + else:
  59. + lastbytes = int(end)
  60. + if size < lastbytes:
  61. + lastbytes = size
  62. + start = size - lastbytes
  63. + f.seek(start)
  64. + fsize = lastbytes
  65. + end = size - 1
  66. + size = end + 1
  67. + fsize = end - int(start) + 1
  68. + # start is the byte offset to begin, and end is the byte offset
  69. + # to end.. fsize is size to send, tsize is the real size of
  70. + # the file, and size is the byte position to stop sending.
  71. +
  72. + if fsize <= 0:
  73. + request.setResponseCode(http.REQUESTED_RANGE_NOT_SATISFIABLE)
  74. + fsize = tsize
  75. + trans = False
  76. + else:
  77. + request.setResponseCode(http.PARTIAL_CONTENT)
  78. + request.setHeader('content-range',"bytes %s-%s/%s " % (
  79. + str(start), str(end), str(tsize)))
  80. + except:
  81. + traceback.print_exc(file=log.logfile)
  82. request.setHeader('content-length', str(fsize))
  83. - if request.method == 'HEAD':
  84. + if request.method == 'HEAD' or trans == False:
  85. + # pretend we're a HEAD request, so content-length
  86. + # won't be overwritten.
  87. + request.method = 'HEAD'
  88. return ''
  89. # return data
  90. + # size is the byte position to stop sending, not how many bytes to send
  91. FileTransfer(f, size, request)
  92. # and make sure the connection doesn't get closed
  93. return server.NOT_DONE_YET