2010-10-22 14 views
9

में फ़ाइलों की सेवा नहीं करेगा मैं स्थिर फ़ाइलों को सेवा देने के लिए mod_xsendfile (v0.12) का उपयोग कर रहा हूं जहां Django उपयोगकर्ताओं और अनुमतियों के आधार पर फ़ाइलों तक पहुंच नियंत्रित कर रहा है।XSendFile Apache 2.2

मेरी conf फ़ाइल में, मैं है:

XSendFile On 
XSendFilePath e:/documents/ 

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

मेरी Django कोड में, मैं हेडर इसलिए की तरह सेट:

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 

और मेरी लॉग फ़ाइल में मैं:

[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 

mod_xsendfile के इस संस्करण में,

XSendFileAllowAbove On 

त्रुटि उत्पन्न:

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

मैं मान लिया है कि था, क्योंकि वे XSendFilePath सफेद सूची जोड़ लिया है। किसी और को यह काम करने के लिए मिला?

उत्तर

13

सामग्री-लंबाई स्वयं सेट न करें। यह केवल इस मामले में mod_wsgi जैसे हैंडलर को भ्रमित करेगा। mod_xsendfile स्वयं सही सामग्री-लंबाई निर्धारित करेगा।

विंडोज़ पर आपको न केवल ड्राइव अक्षर प्रदान करना होगा, ड्राइव अक्षर वास्तव में ऊपरी मामले (आईआईआरसी) में होना चाहिए!

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

ई में मेरे कार्य परीक्षण स्क्रिप्ट में से एक::

मैं एक काम कर परीक्षण विन्यास इतना की तरह है /Apache2.2/htdocs/ इस तरह दिखता है:

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

XSendFileAllowAbove हटा दिया गया था XSendFilePath

+1

यह सब कुछ मैंने पाया है कि पूरा पथ केस संवेदनशील है, भले ही आप खिड़कियों पर हों।अगर मैं लिनक्स पर था जो तुरंत मेरे पास होता, लेकिन खिड़कियों पर मैंने उन मुद्दों को त्याग दिया। ड्राइव पत्र अपरिपक्व होना चाहिए! – boatcoder

2

के पक्ष में कुछ समय पहले मुझे XSendfile पथ को कॉन्फ़िगर करने के लिए कई बार परेशानी हुई थी। विंडोज पर

<?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(); 


?> 

तो, मूल रूप से, एक्स Sendfile के लिए:

यहाँ मुझे विंडोज पर कई परिदृश्य परीक्षण कर रहा है (इस पोस्ट के अंत करने के लिए कूद निष्कर्ष विज्ञापन सुझाव देखने के लिए) को देखने के लिए कि क्या गलत था , सुनिश्चित करें कि:

  • आप XSendfilePath (अनिवार्य) के लिए अपने अपाचे कॉन्फ़िगरेशन में आगे की स्लैश का उपयोग करते हैं;
  • अपने पथों में मामले का सम्मान करें, भले ही हम विंडोज़ (अनिवार्य) पर हों;
  • उपयोग पूर्ण पथ हर जगह (अनुशंसित)
  • कोई अनुगामी XSendfilePath के लिए स्लैश (सिफारिश)

आशा है कि यह किसी को मदद करता है! फैबियन