Saturday, July 26, 2014

Configuring Apache to return CORS headers for Drupal Services

Here's what I did to configure Apache to return the proper CORS headers for my webapp consumption which is written in AngularJS:

Header always set Access-Control-Allow-Origin "*"
Header always set Access-Control-Allow-Methods "GET, POST, OPTIONS"
Header always set Access-Control-Allow-Headers "accept, content-type"
Header always set Access-Control-Allow-Credentials "true"

That can be set in the VirtualHost tags for your server instance. Please note that Access-Control-Allow-Origin value shouldn't be set to * in a production environment. This should only be done for testing environments. The mod_headers module must be enabled in Apache for this configuration to work.

Having these options should be sufficient. But since I'm using Drupal 7 Services, it doesn't play well with pre-flight call which uses HTTP OPTIONS method. Drupal services will return a 404 even if the correct endpoint is specified when OPTIONS method is used.

Here's the additional config using mod_rewrite to return HTTP 200 for all OPTIONS requests:

RewriteEngine On
RewriteRule ^(.*)$ $1 [R=200,L,E=HTTP_ORIGIN:%{HTTP:ORIGIN}]]

Tuesday, February 11, 2014

Resizing images in batch using mogrify

Here's a quick way to resize images via shell using mogrify. It's part of ImageMagick package.

mogrify -resize 60% *.jpg

This will resize all files ending with .jpg extension to 60% of their original size.

We're only scratching the surface of what mogrify can do. Have fun exploring :)

Monday, February 3, 2014

Default Parameter Values in PHP Functions

Here's a neat way to set some default parameter values in a PHP function. This only works if a function param is passed in as associative array.

function create_profile($values) {
  if (!is_array($values)) {
    $values = array();
  // Default key/values
  $values += array(
      'uid' => NULL,
      'username' => '',
      'mobile' => '99999999',
      'location' => 'POINT(0 0)',
      'fb_id' => ''

The most important line is at line 7; where both arrays are merged. If the key/value pair is in the $values array, it will be ignored; else the default values will be used.

Tuesday, January 28, 2014

Setting "placeholder" attribute in Drupal 7 form elements

Here's a quick way on how to use HTML5 placeholder attribute in Drupal forms. Example below is altering the login form in a custom theme:

function mytheme_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'user_login') {
    $form['name']['#attributes'] = array('placeholder' => $form['name']['#title']);
    $form['pass']['#attributes'] = array('placeholder' => $form['pass']['#title']);

Wednesday, September 11, 2013

Using JMeter's Regular Expression Extractor

Here's how to use JMeter's Regular Expression Extractor post-processor to extract the response from a HTTP Request sampler and use it in the subsequent request.

In the screen above, I'm extracting the value of the hidden form element named "t".  If there's no match to the regular expression, the default value "NO TOKEN" will be returned.

To use the extracted value in subsequent HTTP requests, just use the usual JMeter parameter notation.  In this case, it's ${TOKEN}; as defined in the Reference Name value.