             }
             
             file.marker = range.lowerBound
-            
-            return self.sendFile(remainingBytes: rangeCount, response: response, file: file) {
-                ok in
-                
-                file.close()
-                response.completed()
-            }
+			// send out headers
+			response.push { ok in
+				guard ok else {
+					file.close()
+					return response.completed()
+				}
+				return self.sendFile(remainingBytes: rangeCount, response: response, file: file) {
+					ok in
+					
+					file.close()
+					response.completed()
+				}
+			}
         } else if ranges.count > 0 {
             // !FIX! support multiple ranges
             response.status = .internalServerError

		}
        do {
            try file.open(.read)
            self.sendFile(request: req, response: response, file: file)
        } catch {
            return fnf(msg: "The file \(path) could not be opened \(error).")
        }
	}
	
	func sendFile(request req: HTTPRequest, response resp: HTTPResponse, file: File) {
		
		resp.addHeader(.acceptRanges, value: "bytes")

		if let rangeRequest = req.header(.range) {
            return self.performRangeRequest(rangeRequest: rangeRequest, request: req, response: resp, file: file)
        } else if let ifNoneMatch = req.header(.ifNoneMatch) {
            let eTag = self.getETag(file: file)
            if ifNoneMatch == eTag {
                resp.status = .notModified
                return resp.completed()
            }
        }
        
        let size = file.size
        let contentType = MimeType.forExtension(file.path.pathExtension)
        
		resp.status = .ok
		resp.addHeader(.contentType, value: contentType)
		resp.addHeader(.contentLength, value: "\(size)")
        
        self.addETag(response: resp, file: file)
        
		if case .head = req.method {
			return resp.completed()
		}
		
		self.sendFile(remainingBytes: size, response: resp, file: file) {
			ok in
			file.close()
			resp.completed()
		}
	}
	

//

import PerfectLib
import PerfectNet
import Foundation
	
extension String {
