Skip to content

Commit

Permalink
MDL-47545 web services: Allow cross origin requests for file downloads
Browse files Browse the repository at this point in the history
  • Loading branch information
jleyva committed Oct 24, 2014
1 parent 5e28991 commit 3ca3d25
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/filelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1953,6 +1953,13 @@ function file_mimetype_in_typegroup($mimetype, $groups) {
*/
function send_file_not_found() {
global $CFG, $COURSE;

// Allow cross-origin requests only for Web Services.
// This allow to receive requests done by Web Workers or webapps in different domains.
if (WS_SERVER) {
header('Access-Control-Allow-Origin: *');
}

send_header_404();
print_error('filenotfound', 'error', $CFG->wwwroot.'/course/view.php?id='.$COURSE->id); //this is not displayed on IIS??
}
Expand Down Expand Up @@ -2476,6 +2483,12 @@ function send_stored_file($stored_file, $lifetime=null, $filter=0, $forcedownloa
}
}

// Allow cross-origin requests only for Web Services.
// This allow to receive requests done by Web Workers or webapps in different domains.
if (WS_SERVER) {
header('Access-Control-Allow-Origin: *');
}

if (empty($filter)) {
// send the contents
readfile_accel($stored_file, $mimetype, !$dontdie);
Expand Down
2 changes: 2 additions & 0 deletions webservice/rest/locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ protected function send_headers() {
header('Expires: '. gmdate('D, d M Y H:i:s', 0) .' GMT');
header('Pragma: no-cache');
header('Accept-Ranges: none');
// Allow cross-origin requests only for Web Services.
// This allow to receive requests done by Web Workers or webapps in different domains.
header('Access-Control-Allow-Origin: *');
}

Expand Down

0 comments on commit 3ca3d25

Please sign in to comment.