WordPress.org

Ready to get started?Download WordPress

Forums

CDN Linker lite
[Plugin: OSSDL CDN off-linker] Support multiple CDN URLs (9 posts)

  1. ericesev
    Member
    Posted 3 years ago #

    Here is a patch to support multiple off-site CDN URLs. This can speed up page loading by tricking the browser to make more requests in parallel. Separate multiple CDN URLs with a comma.

    --- ossdl-cdn.php.orig	2011-02-10 23:05:39.000000000 -0500
    +++ ossdl-cdn.php	2011-02-10 23:19:02.000000000 -0500
    @@ -17,6 +17,7 @@
     $ossdl_off_exclude = trim(get_option('ossdl_off_exclude'));
    
     $arr_of_excludes = array_map('trim', explode(',', $ossdl_off_exclude));
    +$ossdl_off_cdn_url_arr = preg_split("/[\s,]+/", $ossdl_off_cdn_url);
    
     /**
      * Determines whether to exclude a match.
    @@ -40,11 +41,16 @@
      * Called by #ossdl_off_filter.
      */
     function ossdl_off_rewriter($match) {
    -	global $ossdl_off_blog_url, $ossdl_off_cdn_url, $arr_of_excludes;
    +	global $ossdl_off_blog_url, $ossdl_off_cdn_url_arr, $arr_of_excludes;
     	if (ossdl_off_exclude_match($match[0], $arr_of_excludes)) {
     		return $match[0];
     	} else {
    -		return str_replace($ossdl_off_blog_url, $ossdl_off_cdn_url, $match[0]);
    +		list($key, $cdn_url) = each($ossdl_off_cdn_url_arr);
    +		if( $cdn_url == FALSE ) {
    +			reset($ossdl_off_cdn_url_arr);
    +			list($key, $cdn_url) = each($ossdl_off_cdn_url_arr);
    +		}
    +		return str_replace($ossdl_off_blog_url, $cdn_url, $match[0]);
     	}
     }
    
    @@ -109,14 +115,14 @@
     		<p>Many WordPress plugins misbehave when linking to their JS or CSS files, and yet there is no filter to let your old posts point to a statics' site or CDN for images.
     		Therefore this plugin replaces at any links into <code>wp-content</code> and <code>wp-includes</code> directories (except for PHP files) the <code>blog_url</code> by the URL you provide below.
     		That way you can either copy all the static content to a dedicated host or mirror the files at a CDN by <a href="http://knowledgelayer.softlayer.com/questions/365/How+does+Origin+Pull+work%3F" target="_blank">origin pull</a>.</p>
    -		<p><strong style="color: red">WARNING:</strong> Test some static urls e.g., <code><?php echo(get_option('ossdl_off_cdn_url') == get_option('siteurl') ? $example_cdn_uri : get_option('ossdl_off_cdn_url')); ?>/wp-includes/js/prototype.js</code> to ensure your CDN service is fully working before saving changes.</p>
    +		<p><strong style="color: red">WARNING:</strong> Test some static urls e.g., <code><?php echo(current(preg_split("/[\s,]+/", get_option('ossdl_off_cdn_url')))); ?>/wp-includes/js/prototype.js</code> to ensure your CDN service is fully working before saving changes.</p>
     		<p><form method="post" action="">
     		<table class="form-table"><tbod>
     			<tr valign="top">
     				<th scope="row"><label for="ossdl_off_cdn_url">off-site URL</label></th>
     				<td>
     					<input type="text" name="ossdl_off_cdn_url" value="<?php echo(get_option('ossdl_off_cdn_url')); ?>" size="64" class="regular-text code" />
    -					<span class="description">The new URL to be used in place of <?php echo(get_option('siteurl')); ?> for rewriting. No trailing <code>/</code> please. E.g. <code><?php echo($example_cdn_uri); ?></code>.</span>
    +					<span class="description">The new URL to be used in place of <?php echo(get_option('siteurl')); ?> for rewriting. No trailing <code>/</code> please. E.g. <code><?php echo($example_cdn_uri); ?></code>. Use a comma as a delimiter to round-robin through multiple CDN URLs.</span>
     				</td>
     			</tr>
     			<tr valign="top">

    http://wordpress.org/extend/plugins/ossdl-cdn-off-linker/

  2. digory
    Member
    Posted 3 years ago #

    I took your idea and did a different implementation.

    This patch allows you to use a CDN URL like "cdn%4%.foo.com", which gives you 4 hostnames to use (cdn1, cdn2, cdn3, cdn4). (You can use up to 16.)

    The main difference here is that we will use the same hostname every time for a specific resource. This prevents a user from having to re-download a cached resource upon their next visit.

    --- ossdl-cdn.php.orig  2011-02-22 12:18:40.000000000 -0500
    +++ ossdl-cdn.php       2011-02-22 12:48:07.000000000 -0500
    @@ -44,6 +44,16 @@
            if (ossdl_off_exclude_match($match[0], $arr_of_excludes)) {
                    return $match[0];
            } else {
    +               if (preg_match("/%([1-9][0-6]?)%/", $ossdl_off_cdn_url, $num_ary)) {
    +                       $num_hosts = $num_ary[1];
    +                       $md5 = md5($match[0]);
    +                       $md5 = substr($md5, 0, 1);
    +                       $md5 = hexdec($md5);
    +                       $md5 = $md5 % $num_hosts;
    +                       $md5++;
    +                       $multi_cdn_url = preg_replace("/%$num_hosts%/", $md5, $ossdl_off_cdn_url);
    +                       return str_replace($ossdl_off_blog_url, $multi_cdn_url, $match[0]);
    +               }
                    return str_replace($ossdl_off_blog_url, $ossdl_off_cdn_url, $match[0]);
            }
     }
    @@ -103,6 +113,7 @@
                    update_option('ossdl_off_exclude', $_POST['ossdl_off_exclude']);
            }
            $example_cdn_uri = str_replace('http://', 'http://cdn.', str_replace('www.', '', get_option('siteurl')));
    +       $example_cdns_uri = str_replace('http://', 'http://cdn%4%.', str_replace('www.', '', get_option('siteurl')));
    
            ?><div class="wrap">
                    <h2>OSSDL CDN off-linker</h2>
    @@ -116,7 +127,7 @@
                                    <th scope="row"><label for="ossdl_off_cdn_url">off-site URL</label></th>
                                    <td>
                                            <input type="text" name="ossdl_off_cdn_url" value="<?php echo(get_option('ossdl_off_cdn_url')); ?>" size="64" class="regular-text code" />
    -                                       <span class="description">The new URL to be used in place of <?php echo(get_option('siteurl')); ?> for rewriting. No trailing <code>/</code> please. E.g. <code><?php echo($example_cdn_uri); ?></code>.</span>
    +                                       <span class="description">The new URL to be used in place of <?php echo(get_option('siteurl')); ?> for rewriting. No trailing <code>/</code> please. E.g. <code><?php echo($example_cdn_uri); ?></code>. You can use <code><?php echo($example_cdns_uri); ?></code> (number between 1 and 16, surrounded by percent signs) to enable up to that many hostname variations.</span>
                                    </td>
                            </tr>
                            <tr valign="top">
  3. ericesev
    Member
    Posted 3 years ago #

    That's great! I ended up backing out my changes after I realized the issue you noted about different hostnames and caching. Your solution is perfect. Thank you!

  4. markb1439
    Member
    Posted 3 years ago #

    Hi,

    These are great contributions! I was just checking this forum for multiple CDN URLs!

    Thanks,

    Mark

  5. z4reg
    Member
    Posted 3 years ago #

    please share the complete file

  6. catnhat
    Member
    Posted 3 years ago #

    I am looking for this as well however cannot tell if you just added this code or replaced some of the original package.

  7. digory
    Member
    Posted 3 years ago #

    You should save the code in a file, then patch your original using the 'patch' command (http://www.cyberciti.biz/faq/appy-patch-file-using-patch-command/).

    If you don't have access to 'patch' (e.g. you're on Windows), then you should edit the original file, ossdl-cdn.php, taking out the lines that start with '-' in the patch, and adding the lines that start with '+'. Make sure you add them in the right location.

  8. catnhat
    Member
    Posted 3 years ago #

    Digory,
    Thanks for the direction. I updated manually and it seems to be working well.

  9. wmark
    Member
    Plugin Author

    Posted 3 years ago #

    The feature has become a part of version 1.3.0.
    Thanks a lot for your suggestions and the code!

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic