Problem with Varnish(2.1). Drupal’s pages aren’t cached by varnish.

I had a problem with varnish, trying to boost a drupal site by caching it. I noticed that while js, css and image files were getting cached, no other page could be cached by varnish and on each request the apache was asked to fetch the page.

That was not acceptable since the site isn’t updated really often. I found out that the reason why varnish didn’t cache is drupal’s (v7) file:
/var/www/drupal/includes/bootstrap.inc , line 1283

  'Expires' => 'Sun, 19 Nov 1978 05:00:00 GMT',

I didn’t want to just comment it out in the file, since a lot more problems could be faced regarding drupal and varnish and caching and updating drupal core later on etc.

A solution was to add the following lines in the varnish config file vcl_fetch :

if(beresp.http.Content-Type ~ "text/html") {
        set beresp.ttl = 1d;
    }

So now all pages are cached for 1 day and then the cache gets updated again when someone requests a page.

Cheers.

PS 1: I followed the instructions at https://drupal.org/node/1054886 and the documentation of varnish.
PS 2: A nice command to check if any page is requested from apache and is not in varnish cache is this:

varnishtop -b -i txurl

IE9 DOMParser is missing method selectSingleNode, selectNodes (and possibly others too …), a solution … (XMLSerializer missing methods also)

When you have browsers which have the DOMParser (like chrome, firefox etc.)
you can do the following in javascript:

var dom = (new DOMParser).parseFromString(str, "text/xml");
dom.selectSingleNode(...);
//etc.

In IE9 although DOMParser is available it seems that it lacks some methods, so you get errors like:
Object doesn’t support property or method ‘selectSingleNode’

The solution you can use in javascript level is something like that:

var dom = new ActiveXObject("Microsoft.XMLDOM");
dom.loadXML(str);
dom.selectSingleNode(...);
//etc.

To all of you who are using sarissa.js and you face the same problems then you need a little code tweaking for it to work. I will share what i did and worked for me. I don’t know if some other parts of the library require also fixing…

in sarissa.js at line 474 (file version 0.9.9.5) replace the line:

if(!window.DOMParser){

with:

if(!window.DOMParser || navigator.userAgent.indexOf("MSIE 9")>-1){

PS. In addition the case with XMLSerializer is the same like DOMParser. So in sarissa.js change the line 596 from:

if(!window.XMLSerializer && Sarissa.getDomDocument && Sarissa.getDomDocument("","foo", null).xml){

to:

if((!window.XMLSerializer && Sarissa.getDomDocument && Sarissa.getDomDocument("","foo", null).xml) || navigator.userAgent.indexOf("MSIE 9")>-1){

That’s all for now.
Cheers.

curl : How to set PHPSESSID in the request headers of a call, how to solve waiting and getting no response

I have been trying to use curl in PHP to post some data in some page of mine that requires login and thus a session to be present.
So i had to send the session along with the curl request. The problem was that the script seemed to be waiting for a response.

What happened actually was that my target script was waiting for the session to become available.
As is stated in session_write_close():

“session data is locked to prevent concurrent writes only one script may operate on a session at any time”

So my target script was waiting for the session to become writable while the current script (that made the post ) couldn’t complete because it was waiting the response of the target (cruel things the deadlocks). The solution is to call session_write_close() and free the session before you make the call curl_exec()

That’s it. Take a look at the following code. Take care.

Cheers.

<?php
    session_start();
     
    ...
    ...

    $ch = curl_init();
    if(!$ch){
        die('Failed to init curl ...');
    }

    curl_setopt($ch, CURLOPT_URL,            "https://example.org/subdir/something.php" );
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
    curl_setopt($ch, CURLOPT_POST,           1 );
    curl_setopt($ch, CURLOPT_POSTFIELDS,     $raw_data );
    curl_setopt($ch, CURLOPT_HTTPHEADER,     array('Content-Type: text/xml')); //remove this line if you want submit text/html
   
    $strCookie = 'PHPSESSID=' . session_id() . '; path=/';
    curl_setopt( $ch, CURLOPT_COOKIE, $strCookie ); //We set our session in the headers of the request!

    session_write_close(); //kmak - this is what makes the session transmission possible ... otherwise we will wait and wait ...
    $result=curl_exec ($ch);
    if(!$result)
    {
        die("Failed in curl_exec with error:".curl_error($ch));
    }
    curl_close($ch);

Drupal custom script and problem with file_copy, file_save, file_move and generally file functions.

I have recently been occupied with developing a custom script that uses drupal 7 api in order to programmatically create content nodes.
For all these i have found this very usefull post.

My nodes had to have many attachements so i was really happy that i had this code.

NOTE: i was executing my script by hand from a command line like this:

$php myscript.php

Despite doing everything correctly i couldn’t make file_copy() to succeed.

To make a long story short, the solution to this was to place my custom script in the root directory where my drupal site was running and then run it by hand again.
For security reasons of course i had to change the permissions to my script accordingly so that it couldn’t be executed from the web server but only by command line.

To make a short story long, the reason for the script not working originally had to do with drupal’s public:// stream handler producing relative paths
to the drupal installation directory, thus causing various checks like php’s is_dir(“public://”) functions to fail miserably when run from other place.

That was it.

Cheers!

Ubuntu 11.10 – how to remove unity / solve boot freeze / solve problem opening folders

I have been troubled for hours by some problems, so now i share something with you, so that you have an easier life.
In short if you want to remove unity in Ubuntu 11.10 remove all packages named *unity* from the ‘Ubuntu Software Center’
except: libunity6 and unity-greeter !!!!

If you use lightdm for login screen and you remove unity-greeter then at boot time your system will freeze!
If you use gdm then you can remove unity-greeter without problem.

In addition if you remove libunity6 then nautilus is also removed because it has it as a dependency.
Nautilus is the program that is responsible for opening your folders and showing you their contents when you click on them and generally it’s like the explorer in windows systems.

Cheers.

PS. Just in case you are stuck in command line and you are a novice user in ubuntu just make sure you install again the removed packages:

$sudo apt-get install unity-greeter
$sudo apt-get install nautilus

 

Avast fails to update, a solution

I have recently came accross an avast update problem. I have installed avast in windows wista and after the installation i couldn’t update the program.

I kept getting a failure to connect to server error, something like a timeout. After checking about internet connectivity and firewall settings i couldn’t find the root of the problem.

Solution:

Control Panel -> Add/Remove programs -> avast -> change/remove program -> and then select the repair option and click next

The avast installation will be repaired and then it updates succefully.

That’s all for now.

Flowplayer and crossdomain.xml error

I was playing around with flowplayer for a project. While playing with some demos i came across an error.

I was clicking a link and expecting for flowplayer to load some video stream and  i noticed in firebug Net tab that a 404 error appeared about crossdomain.xml file not being found!

Solution:

In a few words after some time i found out that this was caused because while playing with demo code from the flowplayer site i forgot to change the flowplayer’s url in my own domain:

From:

 $f("a.player", "http://releases.flowplayer.org/swf/flowplayer-3.2.5.swf", { ...

changed to:

 $f("a.player", "http://mydomain/flowplayer-3.2.5.swf", { ...

That fixed it!

JR qTip for WordPress

qTip is a really nice plugin to use in wordpress, but once you install it in a latest version of wordpress you might get the following error

f(this).data("qtip") is null

This has to do with qTip not being able to work right with latest jQuery versions.

The solution to the above was to manually edit the file jquery.qtip-1.0.0-rc3.min.js

Find the following line:

if(typeof f(this).data("qtip")=="object")

and change it to:

if(typeof f(this).data("qtip")==="object" && f(this).data("qtip"))

Thanks to simshaun that found the solution here: http://craigsworks.com/projects/forums/thread-solved-qtip-1-0-0rc3-does-not-work-with-latest-jquery-release

Acer laptop problem with keyboard and touchpad and solution

Recently i came across a really strange problem with my laptop.

It all started when i left it in hibernation mode and it run out of battery while in hibernation. When i plugged it into the power cord and opened it started behaving strangely. In particular its keyboard seemed unresponsive and its touchpad also. Thus i couldn’t log in. After a while i realised that if i kept a key pressed down long enough it would write it (really slow). At first i suspected a virus or something that could generate too much load to make it delay that much. It wasn’t so. In safe mode the problem seemed to disappear.

The solution to all above was to remove the laptop’s battery and only boot with the power cord!  Everything was again back to normal.  I can only give the explanation that the battery has been ‘damaged’ somehow due to the last hibernation and it caused the  system unresponsiveness. In safe mode the system due to fewer drivers (or other drivers) seems that it bypassed the problems it ‘detected’ regarding the battery with no impact on the system.