r76746 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r76745‎ | r76746 | r76747 >
Date:00:24, 16 November 2010
Author:neilk
Status:ok (Comments)
Tags:
Comment:
Followups mostly to r75906.

* Improved logic around error messages, and understandability of those messages.

* Removed FIXME on Content-Length header. When we serve the file
this way, it seems to be necessary to explicitly set the Content-Length. I tried
removing it and unsurprisingly it did not appear.

I am unsure what RoanKattouw is referring to when he says PHP can do this.
How can PHP know what the Content-Length is going to be, unless we set this explicitly?
Modified paths:
  • /trunk/phase3/includes/specials/SpecialUploadStash.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/specials/SpecialUploadStash.php
@@ -20,11 +20,16 @@
2121 // UploadStash
2222 private $stash;
2323
24 - // we should not be reading in really big files and serving them out
25 - private $maxServeFileSize = 262144; // 256K
 24+ // Since we are directly writing the file to STDOUT,
 25+ // we should not be reading in really big files and serving them out.
 26+ //
 27+ // We also don't want people using this as a file drop, even if they
 28+ // share credentials.
 29+ //
 30+ // This service is really for thumbnails and other such previews while
 31+ // uploading.
 32+ const MAX_SERVE_BYTES = 262144; // 256K
2633
27 - // $request is the request (usually wgRequest)
28 - // $subpage is everything in the URL after Special:UploadStash
2934 public function __construct( ) {
3035 parent::__construct( 'UploadStash', 'upload' );
3136 try {
@@ -52,23 +57,43 @@
5358 // prevent callers from doing standard HTML output -- we'll take it from here
5459 $wgOut->disable();
5560
56 - try {
57 - $file = $this->getStashFile( $subPage );
58 - if ( $file->getSize() > $this->maxServeFileSize ) {
59 - throw new MWException( 'file size too large' );
 61+ $code = 500;
 62+ $message = 'Unknown error';
 63+
 64+ if ( !isset( $subPage ) or $subPage === '' ) {
 65+ // the user probably visited the page just to see what would happen, so explain it a bit.
 66+ $code = '400';
 67+ $message = "Missing key\n\n"
 68+ . 'This page provides access to temporarily stashed files for the user that '
 69+ . 'uploaded those files. See the upload API documentation. To access a stashed file, '
 70+ . 'use the URL of this page, with a slash and the key of the stashed file appended.';
 71+ } else {
 72+ try {
 73+ $file = $this->getStashFile( $subPage );
 74+ $size = $file->getSize();
 75+ if ( $size === 0 ) {
 76+ $code = 500;
 77+ $message = 'File is zero length';
 78+ } else if ( $size > self::MAX_SERVE_BYTES ) {
 79+ $code = 500;
 80+ $message = 'Cannot serve a file larger than ' . self::MAX_SERVE_BYTES . ' bytes';
 81+ } else {
 82+ $this->outputFile( $file );
 83+ return true;
 84+ }
 85+ } catch( UploadStashFileNotFoundException $e ) {
 86+ $code = 404;
 87+ $message = $e->getMessage();
 88+ } catch( UploadStashBadPathException $e ) {
 89+ $code = 500;
 90+ $message = $e->getMessage();
 91+ } catch( Exception $e ) {
 92+ $code = 500;
 93+ $message = $e->getMessage();
6094 }
61 - $this->outputFile( $file );
62 - return true;
63 -
64 - } catch( UploadStashFileNotFoundException $e ) {
65 - $code = 404;
66 - } catch( UploadStashBadPathException $e ) {
67 - $code = 403;
68 - } catch( Exception $e ) {
69 - $code = 500;
7095 }
7196
72 - wfHttpError( $code, OutputPage::getStatusMessage( $code ), $e->getMessage() );
 97+ wfHttpError( $code, OutputPage::getStatusMessage( $code ), $message );
7398 return false;
7499 }
75100
@@ -130,8 +155,7 @@
131156 header( 'Content-Type: ' . $file->getMimeType(), true );
132157 header( 'Content-Transfer-Encoding: binary', true );
133158 header( 'Expires: Sun, 17-Jan-2038 19:14:07 GMT', true );
134 - header( 'Pragma: public', true );
135 - header( 'Content-Length: ' . $file->getSize(), true ); // FIXME: PHP can handle Content-Length for you just fine --RK
 159+ header( 'Content-Length: ' . $file->getSize(), true );
136160 readfile( $file->getPath() );
137161 }
138162 }

Follow-up revisions

RevisionCommit summaryAuthorDate
r76797uploadwizard-deployment: Merge recent revs from trunk: r75995, r76354, r76386...catrope14:47, 16 November 2010

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r75906core changes for UploadWizard (merged from r73549 to HEAD in branches/uploadw...neilk04:32, 3 November 2010

Comments

#Comment by Catrope (talk | contribs)   12:40, 16 November 2010

What I meant was that we also don't set an explicit Content-Length header when doing regular HTML output. Presumably either PHP or the web server adds the Content-Length header to the response in that case.

#Comment by Catrope (talk | contribs)   13:19, 16 November 2010

When I tried commenting out the line that sets Content-Length, the header is still set. It doesn't seem to make any difference either way though, so I guess it can just be left in if it doesn't cause any issues.

Status & tagging log