Reader small image

You're reading from  CORS Essentials

Product typeBook
Published inMay 2017
Reading LevelIntermediate
Publisher
ISBN-139781784393779
Edition1st Edition
Languages
Right arrow
Author (1)
Rajesh Gunasundaram
Rajesh Gunasundaram
author image
Rajesh Gunasundaram

Rajesh Gunasundaram is a software architect, technical writer and blogger. He has over 15 years of experience in the IT industry, with more than 12 years using Microsoft .NET, 2 years of BizTalk Server and a year of iOS application development. Rajesh is a founder and editor of technical blogs programmerguide and ioscorner and you can find many of his technical writings on .Net and iOS. He is also the founder and developer of VideoLens, a platform that analyses videos uploaded in Facebook pages and YouTube channels. Rajesh has also written four other books for Packt publishing. Rajesh worked on client premises located at various countries such as UK, Belarus and Norway. He also has experience in developing mobile applications for iPhone and iPad. His technical strengths include Azure, Xamarin, ASP.NET MVC, Web API, WCF, .Net Framework / .Net Core, C#, Objective-C, Angular, Bot Framework, BizTalk, SQL Server, REST, SOA, Design Patterns and Software Architecture. Rajesh is an early adopter of Angular since AngularJS. He has developed Rich interfaces using Angular, Bootstrap, HTML5 and CSS3. He has good experience in translation of designer mock-ups and wireframes into an AngularJS front-end. Good at unit testing Angular applications with Karma. Expertise in handling RESTful services in Angular. Supporting various web products developed using AngularJS and Angular.
Read more about Rajesh Gunasundaram

Right arrow

Incoming CORS requests


Web content management frameworks are often integrated with external platforms, APIs, and services. When the external source is hosted on a different domain, CORS is used to allow requests from the external local domain to the target domain of the web content management framework, without needing a proxy. We will look at techniques for allowing these requests.

When making a CORS request from the web content management framework as the local domain to a different target domain, it is either handled with custom code using the methods we covered in the earlier chapters, or a plugin may implement the headers and methods needed for the requests.

SAAS or self-hosted?


All the major content management frameworks offer a Software As A Service (SAAS) hosted/managed option. Since you do not actually control the server, and scripting is limited, implementing CORS may be difficult, or sometimes impossible.

In the SAAS platform WordPress.com, we shall see that CORS is very limited, and authenticated requests require explicit permission by the current user.

The Drupal SAAS platforms Drupal Gardens and Acquia Cloud Site Factory do not provide a way to implement CORS. There have been feature requests for adding CORS capability, so it may become possible.

It's unclear whether CORS is possible in the SAAS versions of Joomla! and Adobe Experience Manager.

The demand for the capability of CORS will likely be satisfied in these SAAS platforms eventually, so we recommend contacting support and asking them explicitly whether CORS can be implemented.

CORS in WordPress


WordPress can be self hosted, or you could use WordPress.com, which is the hosted/managed SaaS platform for creating WordPress sites. Although incoming and outgoing CORS requests are possible in the self-hosted WordPress, the WordPress.com platform only allows incoming requests.

Limited support for CORS in SAAS WordPress.com

WordPress.com provides a REST API, which can allow incoming CORS requests via JavaScript/jQuery only; other languages are not supported.

You need to whitelist the domains you want to allow to make requests to your application in the dashboard for the site. Authenticated and unauthenticated requests are supported.

Note

The REST API provides a way for other domains to interact with the WordPress.com application. Requests are made to the URI , which is the API endpoint for WordPress.com. There is no provision for making a CORS request from WordPress.com to another domain. That is a limitation of using the WordPress.com SAAS compared with the self-hosted WordPress...

CORS in Drupal


Custom code is the best way to implement CORS in Drupal, using techniques we have covered. There are also some contributed Drupal modules for CORS. Be mindful of Drupal core API differences among versions: Drupal 6, 7, 8. When Drupal 8 is released, support for Drupal 6 will end.

Enabling CORS in Drupal with custom code

CORS headers may be added to endpoints created in a custom module or in the template.php file in your theme.

The CORS headers can be added:

  • With the Drupal API function drupal_add_http_header

  • By configuring .htaccess to send headers, for example, using Apache's mod.headers

  • By explicitly setting the header with code in a custom module or theme template

Using the drupal_add_http_header function

The following custom function adds CORS headers in Drupal 7 for a JSON application, with the wildcard to allow requests from all domains, and allows GET, PUT, POST, and DELETE methods:

function set_custom_headers()
 {
  drupal_add_http_header('Content-Type', 'application/json...

CORS in Joomla!


To enable CORS in Joomla!, you need to add the standard CORS header(s). There are a few ways to add the header(s), which are covered in the following sections.

setHeader in JApplication web

Since Joomla! version 11.4, the core method to set headers is setHeader in the base class JapplicationWeb, which can be used to set the Access-Control-Allow-Origin header and can be used to set other headers.

The following example allows access from all domains with the wildcard:

setHeader('Access-Control-Allow-Origin', '*', true) : \JApplicationWeb

You may find discussions regarding the use of JResponse::setHeader for CORS in Joomla!; however, it has been deprecated since Joomla! Version 11.4. Use setHeader in the base class JApplicationWeb instead.

matware-libraries on GitHub

Although we could not find any Joomla! extensions available to configure allowed origins from the web admin, there is a project on GitHub named matware-libraries, which adds the Access-Control-Allow-Origin header. Matware...

CORS in Adobe Experience Manager


While the previous sections examined using CORS in frameworks built with PHP, Adobe Experience Manager is built with Java.

Tip

In 2013, Adobe systems renamed the Adobe CQ WCM to Adobe Experience Manager (also known as Adobe AEM or just AEM).

AEM has a Java package for CORS. Another option is to allow domains to post to Sling in the CRX console; when domains are trusted this way, CORS is unnecessary. In some cases, such as when using Scene 7, CORS headers may need to be added with rulesets.

The com.adobe.cq.social.commons.cors package

AEM includes a package named com.adobe.cq.social.commons.cors. The package contains four classes:

Summary


We have learned a lot about applying CORS in various content management systems. Let's do a recap.

You learned how to allow incoming CORS requests in WordPress, Drupal, Joomla!, and Adobe Expression Manager. You also learned that outgoing CORS requests in these frameworks should use custom code or existing plugins, apart from the fact that WordPress.com SAAS has limited CORS capability. We discussed how CORS support in other SAAS versions of these frameworks is uncertain, but it may become implemented due to demand.

We looked at two plugins for CORS in WordPress: one for adding headers, and the other enables CORS in the WordPress XML-RPC API. We also looked at adding CORS headers with custom code in the WordPress theme template.

We looked at a few Drupal modules for CORS. The CORS module adds headers mapped to paths. The CDN module supports CORS when using a Content Delivery Network. The Amazon S3 upload module implements CORS when using Amazon S3. We also looked at setting headers...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
CORS Essentials
Published in: May 2017Publisher: ISBN-13: 9781784393779
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Rajesh Gunasundaram

Rajesh Gunasundaram is a software architect, technical writer and blogger. He has over 15 years of experience in the IT industry, with more than 12 years using Microsoft .NET, 2 years of BizTalk Server and a year of iOS application development. Rajesh is a founder and editor of technical blogs programmerguide and ioscorner and you can find many of his technical writings on .Net and iOS. He is also the founder and developer of VideoLens, a platform that analyses videos uploaded in Facebook pages and YouTube channels. Rajesh has also written four other books for Packt publishing. Rajesh worked on client premises located at various countries such as UK, Belarus and Norway. He also has experience in developing mobile applications for iPhone and iPad. His technical strengths include Azure, Xamarin, ASP.NET MVC, Web API, WCF, .Net Framework / .Net Core, C#, Objective-C, Angular, Bot Framework, BizTalk, SQL Server, REST, SOA, Design Patterns and Software Architecture. Rajesh is an early adopter of Angular since AngularJS. He has developed Rich interfaces using Angular, Bootstrap, HTML5 and CSS3. He has good experience in translation of designer mock-ups and wireframes into an AngularJS front-end. Good at unit testing Angular applications with Karma. Expertise in handling RESTful services in Angular. Supporting various web products developed using AngularJS and Angular.
Read more about Rajesh Gunasundaram

Classes in com.adobe.cq.social.commons.cors

Description

CORSAuthenticationFilter

Adds CORS headers to HTTP responses

CORSAuthInfoPostProcessor

CORS authentication post processor

CORSConfig

Singleton configuration holder so that both the CORS filter and CORS post processor have access to the system settings

CORSConstants

CORS...