Beutelevision

by Thomas Beutel

Nginx with php-fpm generating blank page

I struggled a long time trying to figure out why my nginz + php-fpm setup was not working. I could tell that the gateway was fine and I was getting a HTTP 200 OK response, but the output was blank and there were no illuminating entries in either the nginx or php-fpm logs.

After a ton of research, I found the hint that I needed here:

https://bbs.archlinux.org/viewtopic.php?pid=903811#p903811

Essentially, I needed to set the SCRIPT_FILENAME param like so:

fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;

Php_fpm needs this so that it knows the path to the PHP file. You may need to set this param differently according to where you store your PHP files.

The default nginx setup I used includes a fastcgi_params file and a fastcgi.conf file. The fastcgi.conf file defines SCRIPT_FILENAME whereas the fastcgi_params does not. My guess is that the fastcgi.conf is not being used, which is why SCRIPT_FILENAME was not set.

Quick fix for “Expected token not present” in Apache2::Cookie

A client’s website of mine had been running fine for many years until a few weeks ago. Certain users of IE started getting this error: “Expected token not present” and were no longer able to login. My guess is that the design team switched some elements from Flash to HTML5/jQuery and introduced some bad cookie code in Javascript. Not wanting to dive into a bunch of JS or jQuery code, I looked into solving the problem on the server side.

I tracked it down to Apache2::Cookie rejecting cookies with invalid formatting and then exiting. So I took the advice on this page and made a change the fetch method of Cookie.pm:

#my $jar = $req->jar or return;
#...avoid cookie parse error "Expected token not present"
my $jar = eval {$req->jar()} || $@->jar; # if fail, parse the cookie anyway
return unless $jar;

I don’t like making direct changes to installed modules like this, but in this case I made an exception. The website in question will be put on a PHP platform soon and it wasn’t worth spending more time on this problem.

Making Aquamacs kill and yank work with the clipboard

My favorite editor on the Mac is Aquamacs (me and Emacs go waaay back) and something that has long been a productivity killer for me was the different actions of C-y and CMD-V.

This is due to there being two clipboards, the one belonging to the Mac and the one in Emacs (the kill ring).

Often I would use one or the other and get the wrong pasted contents. Then I’d have to stop, back up, and find the original contents and do it again. Worse, sometimes the contents would be cut or killed, meaning that I would have to undo the cut/kill, redo the cut/kill, then yank/paste.

Well, I finally had enough and looked for ways to harmonize the two clipboards via the .emacs file. Here is what I came up with:


(defun swap-and-kill ()
"Swap point and mark, then clipboard kill region"
(interactive)
(exchange-point-and-mark)
(clipboard-kill-region (region-beginning) (region-end))
(deactivate-mark)
)
(global-set-key "\C-w" 'swap-and-kill)
(global-set-key "\M-w" 'clipboard-kill-ring-save)
(global-set-key "\C-y" 'clipboard-yank)

Now everything goes to the Mac clipboard, even if I use my beloved mark and point with C-w.

I found most of the clues for this solution here:

http://www.emacswiki.org/emacs/CopyAndPaste

Get the first N words with Javascript

Here’s how I get the first 20 words using a regular expression and the Javascript replace function. I place an ellipsis at the end to indicate that the sentence was shortened:


var str1 = "Here is a sentence that just seems to go on and on. Here is a sentence that just seems to go on and on. Here is a sentence that just seems to go on and on.";
var str2 = str1.replace(/(([^\s]+\s\s*){20})(.*)/,"$1…");
document.write(str2);
// result:
// Here is a sentence that just seems to go on and on. Here is a sentence that just seems to …

Find a Javascript parent node that matches a certain string

Here’s a small bit of javascript that I use in my Sencha Touch projects to find a parent with an id that matches a string.


function findParentNodeRegex(regex, childObj) {
var testObj = childObj;
var count = 1;
while(!testObj.id.match(regex)) {
//console.log('My name is ' + testObj.id + '. Let\'s try moving up one level to see what we get.');
testObj = testObj.parentNode;
count++;
}
// now you have the object you are looking for - do something with it
//console.log('Finally found ' + testObj.id + ' after going up ' + count + ' level(s) through the DOM tree');
return testObj;
}

Suppose that you tapped an paragraph element that was nested like so and “target” points to the paragraph:

<div id="news1234"><div class="content"><p>Tap me!<p></div></div>

You can find the parent that matches the string “news” like so:

var newsParent = findParentNodeRegex(/news/,target);
console.log("Id is "+newsParent.id); // prints "Id is news1234"

I adapted the code from here

Click poster frame to start HTML5 video using jQuery

I needed to have an HTML5 video with a clickable poster frame. The first issue was that Safari would preload the movie, removing the poster frame after a few seconds. I fixed that by setting preload=”none”:

<div id="promovid">
<video id="video" controls="controls" poster="video-poster.jpg" preload="none" width="640" height="480">
...
</div>

Them to make the poster frame clickable, I used this jQuery snippet:

<script>
$(document).ready(function(){
$('#promovid').click( function(){
$('#video').get(0).play();       // get(0) gets the original DOM element
$('#promovid').unbind('click');  // remove click listener
});
</script>

Notice that I remove the click listener after the first click. This viewer can then use the native controls to pause or fullscreen the movie.

Also note the use of get(0) on the $(‘#video’) element. My first inclination was to say $(‘#video’).play(). However, jQuery elements don’t usually expose the methods of the selected DOM element. To access the actual DOM element, I had to use get(0).

References:

http://stackoverflow.com/questions/4646998/play-pause-html-5-video-using-jquery

http://stackoverflow.com/questions/209029/best-way-to-remove-an-event-handler-in-jquery

Arduino upload problem: avrdude: stk500_getsync(): not in sync: resp=0x00

There are a number of good posts out there discussing problems with uploading sketches to an arduino, but my problem was really basic:

I had some wiring connected to digital ports 0 and 1 (TX and RX) and that was causing communication errors. I unplugged the wires to those ports and the upload then worked just fine.

Here is the post that helped me:
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1248528012/19#19

Daily Solar Tweets

I’m playing with a perl Net::Twitter app to see if I can tweet my solar production at the end of every day.

RSS Has Been Replaced By Twitter

I’ve been playing with Flipboard the past few days and a thought occurred to me. RSS has been replaced by Twitter.

Flipboard is a compelling concept… it displays news from your Facebook account and any number of Twitter accounts in a magazine style format. That means large Helvetica headlines, easy to read Times content, and lot’s of photos.

But it doesn’t (yet) support RSS feeds as a source. Flipboard says they will at some point.

I wonder though if it is even necessary. I wanted to create sections for some of my favorite sources… Jay Rosen and Dave Winer for media commentary, Techmeme for tech stuff, and Against Dumb, for variety.

It turns out that they all have Twitter accounts. Those accounts typically link to all the same content that their RSS feeds do. Often more, because they will contain links to content that the authors find interesting, but that don’t appear in their RSS feeds.

And guess what. Twitter accounts are much easier to work with than RSS. Here’s why:

  • Twitter accounts have names that are easy to find, remember and refer to. People can even talk about them.
  • RSS feeds are links. They are technical. No one talks about them in normal discourse.
  • Posting to Twitter is easier than adding to RSS. Even with WordPress around.
  • Twitter accounts are easy to aggregate into lists.

I understand that Twitter has a different intent than RSS, and that RSS provides much of the foundation of information flow on the internet.

But I just wonder if Twitter is ending up to be a much easier way to say “here’s what’s new… and interesting”. And if it’s easier, then why use RSS?

Why tether? Just build iPad’s data plan into EVERY Apple product!

Now that the iPad 3G will come with the option of a month-by-month no-contract data plan, why not offer all Apple products with that option? I would buy a new MacBook in a heartbeat if it offered the same sort of data plan.