2010-10-22 10 views
9

Sto usando mod_xsendfile (v0.12) per servire file statici in cui Django sta controllando l'accesso ai file in base agli utenti e alle autorizzazioni.XSendFile non servirà i file in Apache 2.2

Nel mio file di configurazione, ho:

XSendFile On 
XSendFilePath e:/documents/ 

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

Nel mio codice Django, ho impostato le intestazioni in questo modo:

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 

E nel mio file di log ottengo:

[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 

In questa versione di mod_xsendfile,

XSendFileAllowAbove On 

genera l'errore:

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

ho pensato che era perché hanno aggiunto alla lista bianca XSendFilePath. Qualcun altro ha funzionato?

risposta

13

Non impostare autonomamente la lunghezza del contenuto. In questo caso confonderà solo gestori come mod_wsgi. mod_xsendfile imposta automaticamente la lunghezza del contenuto corretta.

Su Windows non si deve solo fornire la lettera di unità, la lettera di unità deve essere effettivamente in maiuscolo (IIRC)!

Ho una configurazione di prova di lavoro in questo modo:

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

Uno dei miei script di test di funzionamento in E: /Apache2.2/htdocs/ assomiglia a questo:

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

XSendFileAllowAbove è stato rimosso un po 'di tempo fa a favore di XSendFilePath

+1

Un'altra cosa che ho scoperto in tutto questo è che l'intero percorso è case sensitive, anche se si è su Windows.Se fossi stato su Linux mi sarebbe venuto in mente immediatamente, ma su Windows ho scartato quei problemi. La LETTERA DI TRASMISSIONE DEVE ESSERE IN MAIUSCOLO! – boatcoder

2

Ho avuto un sacco di problemi la maggior parte delle volte ho dovuto configurare XSendfile path.

Qui sono io testando diversi scenari su Windows per vedere che cosa era sbagliato (salto alla fine di questo post per vedere le raccomandazioni conclusione di annunci):

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


?> 

Quindi, in sostanza, per X-Sendfile su Windows , assicurarsi che:

  • Si utilizzano le barre in avanti nella configurazione di Apache per XSendfilePath (obbligatorio);
  • Rispettare il caso nei percorsi, anche se siamo su Windows (obbligatorio);
  • Usa percorsi assoluti in tutto il mondo (consigliato)
  • Nessun trailing slash per XSendfilePath (consigliato)

Speranza che aiuta qualcuno! Fabien

Problemi correlati