2010-10-22 29 views
9

Tôi đang sử dụng mod_xsendfile (v0.12) để phân phát tệp tĩnh nơi Django đang kiểm soát quyền truy cập vào tệp dựa trên người dùng và quyền.XSendFile sẽ không phân phối các tệp trong Apache 2.2

Trong file conf của tôi, tôi có:

XSendFile On 
XSendFilePath e:/documents/ 

<Directory e:/Documents> 
    Order allow,deny 
    Allow from all 
</Directory> 

Trong mã django của tôi, tôi đặt tiêu đề như vậy:

assert(isinstance(filename, FieldFile)) 

xsendfile = filename.name 
if(platform.system() == 'Windows'): 
    xsendfile = xsendfile.replace('\\', '/') 

response = HttpResponse() 
response['X-Sendfile'] = xsendfile 
mimetype = mimetypes.guess_type(xsendfile)[0] 
response['Content-Type'] = mimetype 
response['Content-Length'] = filename.size 

Và trong tập tin đăng nhập của tôi, tôi nhận được:

[Fri Oct 22 08:54:22 2010] [error] [client 192.168.20.34] (20023)The given path 
was above the root path: xsendfile: unable to find file: 
e:/Documents/3/2010-10-20/TestDocument.pdf 

Trong phiên bản mod_xsendfile,

012 này

tạo ra các lỗi:

Invalid command 'XSendFileAllowAbove', perhaps misspelled or defined by a module 
not included in the server configuration 

tôi cho rằng đó là vì họ đã thêm XSendFilePath danh sách trắng. Bất cứ ai khác có điều này để làm việc?

Trả lời

13

Không tự đặt Độ dài nội dung. Điều này sẽ chỉ gây nhầm lẫn các trình xử lý như mod_wsgi trong trường hợp này. mod_xsendfile sẽ tự đặt Độ dài nội dung chính xác.

Trên Windows, bạn không được chỉ cung cấp ký tự ổ đĩa, ký tự ổ đĩa phải thực sự nằm trong chữ hoa (IIRC)!

Tôi có một cấu hình thử nghiệm làm việc giống như vậy:

<Directory "E:/"> 
    XSendFile on 
    XSendFilePath E:/localhosts 
</Directory> 

Một trong những kịch bản thử nghiệm làm việc của tôi trong E: /Apache2.2/htdocs/ trông như thế này:

<?php 
    header('X-SendFile: E:/localhosts/archive.tar.bz2'); 
    header('Content-type: application/octet-stream'); 
    header('Content-disposition: attachment; filename="blob"'); 
?> 

XSendFileAllowAbove đã được gỡ bỏ một lần nữa ủng hộ XSendFilePath

+1

Một điều khác mà tôi phát hiện ra trong tất cả điều này là toàn bộ đường dẫn phân biệt chữ hoa chữ thường, ngay cả khi bạn đang ở trên các cửa sổ.Tôi đã được trên Linux mà có thể đã xảy ra với tôi ngay lập tức, nhưng trên cửa sổ tôi loại bỏ các vấn đề đó. THẺ DRIVE PHẢI ĐƯỢC CẬP NHẬT! – boatcoder

2

Tôi đã gặp rất nhiều rắc rối trong hầu hết các lần tôi phải định cấu hình đường dẫn XSendfile.

Sau đây là tôi thử nghiệm một số kịch bản trên Windows để xem những gì đã sai (nhảy tới cuối bài này để xem các khuyến nghị quảng cáo kết luận):

<?php 

/* X-SENDFILE 
* This can be a b*tch to configure. So I'm writing various scenarios here so that I can rely on them in the future. 
* Example use: after re-installing XAMPP, after changing config file, in a new script, after some time without using it... 
* Tested on Windows 7 + XAMPP (Apache/2.4.3, PHP/5.4.7) + mod_xsendfile 1.0-P1 for Apache 2.4.x Win32 
*/ 

/** Environment Debug **/ 
//echo dirname(__FILE__); die(); 
//echo $_SERVER['DOCUMENT_ROOT']; die(); 

/** The damn fucking path, with comments **/ 

// Local file in execution directory. 
// Tested with: *no* XSendFilePath inside of the Apache config 
// Result: works fine. 
//header("X-Sendfile: " . 'localfile.zip'); 

// Local file in execution directory + relative path 
// Tested with: *no* XSendFilePath inside of the Apache config 
// Result: works fine. 
//header("X-Sendfile: " . '../xsendfile/localfile.zip'); 

// Local file in execution directory + absolute pathS 
// Tested with: *no* XSendFilePath inside of the Apache config 
// Result: works fine and a lot of flexibility on the slash and letter drive format combinations *BUT* case-sensitive 
//header("X-Sendfile: " . 'D:\Dropbox\XAMPP\web\tests\Languages\Apache\xsendfile\localfile.zip'); // works fine 
//header("X-Sendfile: " . '\Dropbox\XAMPP\web\tests\Languages\Apache\xsendfile\localfile.zip'); // works fine 
//header("X-Sendfile: " . 'D:/Dropbox/XAMPP/web/tests/Languages/Apache/xsendfile/localfile.zip'); // works fine 
//header("X-Sendfile: " . '/Dropbox/XAMPP/web/tests/Languages/Apache/xsendfile/localfile.zip'); // works fine 
//header("X-Sendfile: " . '/dropbox/XAMPP/web/tests/Languages/Apache/xsendfile/localfile.zip'); // FAILS (case-sensitive) 

// File in the XSendFilePath directory + Absolute path 
// Tested with: XSendFilePath D:\Dropbox\XAMPP\web -- Mind the backward slashes 
// Result: FAILS! error.log => [Wed Feb 20 19:08:02.617971 2013] [:error] [pid 15096:tid 1768] (20023)The given path was above the root path: [client ::1:56658] xsendfile: unable to find file: D:\\Dropbox\\XAMPP\\web\\xsfile.zip 
//header("X-Sendfile: " . 'D:\Dropbox\XAMPP\web\xsfile.zip'); 

// File in the XSendFilePath directory + Absolute path 
// Tested with: XSendFilePath D:/Dropbox/XAMPP/web <== mind the forward slashes this time 
// Result: WORKS! Conclusion: XSendFilePath needs use forward slashes on Windows AND we don't need any trailing slash in it. 
header("X-Sendfile: " . 'D:\Dropbox\XAMPP\web\xsfile.zip'); 

/** We might wanna test also: 
* - How does backward slashes in both XSendfilePath and the header combine? 
* - The use of subdirectories. 
*/

/** The rest of the headers (until otherwise stated, nothing special) **/ 
header("Content-Type: application/zip"); 
header("Content-Disposition: attachment; filename=\"" . 'blah.zip' . "\""); 
header("Content-Transfer-Encoding: binary"); 
header("Pragma: public"); 
header("Expires: 0"); 
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
header("Cache-Control: public"); 
header("Content-Description: File Transfer"); 

/** Tell the script to stop (so the file download may start) **/ 
die(); 


?> 

Vì vậy, về cơ bản, cho X-Sendfile trên Windows , hãy đảm bảo rằng:

  • Bạn sử dụng dấu gạch chéo chuyển tiếp trong cấu hình Apache cho XSendfilePath (bắt buộc);
  • Tôn trọng trường hợp trong đường dẫn của bạn, mặc dù chúng tôi đang ở trên Windows (bắt buộc);
  • Sử dụng đường dẫn tuyệt đối ở khắp mọi nơi (recommended)
  • Không dấu xuyệc cho XSendfilePath (đề nghị)

Hy vọng nó sẽ giúp người! Fabien

Các vấn đề liên quan