REST API: Include auto-discovery Link header when serving API requests.

The Link header allows clients to verify if a site has made the REST API available, as well as indicating how to access it.

Props danielbachhuber.
Fixes #35580.




git-svn-id: https://develop.svn.wordpress.org/trunk@37903 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Rachel Baker 2016-06-29 01:56:32 +00:00
parent a6d5755af0
commit 9e9adb0bfe
2 changed files with 16 additions and 0 deletions

View File

@ -228,6 +228,11 @@ class WP_REST_Server {
$this->send_header( 'Content-Type', $content_type . '; charset=' . get_option( 'blog_charset' ) );
$this->send_header( 'X-Robots-Tag', 'noindex' );
$api_root = get_rest_url();
if ( ! empty( $api_root ) ) {
$this->send_header( 'Link', '<' . esc_url_raw( $api_root ) . '>; rel="https://api.w.org/"' );
}
/*
* Mitigate possible JSONP Flash attacks.
*

View File

@ -726,6 +726,17 @@ class Tests_REST_Server extends WP_Test_REST_TestCase {
$this->assertEquals( 'noindex', $headers['X-Robots-Tag'] );
}
public function test_link_header_on_requests() {
$api_root = get_rest_url();
$request = new WP_REST_Request( 'GET', '/', array() );
$result = $this->server->serve_request('/');
$headers = $this->server->sent_headers;
$this->assertEquals( '<' . esc_url_raw( $api_root ) . '>; rel="https://api.w.org/"', $headers['Link'] );
}
public function test_nocache_headers_on_authenticated_requests() {
$editor = self::factory()->user->create( array( 'role' => 'editor' ) );
$request = new WP_REST_Request( 'GET', '/', array() );