Fixing stuff and maybe other things.

Ok, if you’re reading this, I’m assuming you know what CORS means, so I won’t tell you that it stands for Cross Origin Resource Sharing. Or maybe I just told you.

Anyway, you want to enable it on your Apache server. Maybe, like me, you’re building an API-based web app. So you need some JavaScript to pull data from a remote server. (Or even, like in my case, a different subdomain on the same physical server.) It’s easy in Node.js, so it shouldn’t be hard in Apache.

So you google “apache enable cors”. The first result is from enable-cors.org. Wow, how relevant! Sounds so legit! And it says all you have to do is throw this somewhere:

Header set Access-Control-Allow-Origin "*"

So you put it in your httpd.conf file or .htaccess and boom done.

Except then you try it. And Firebug is all like: Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://buckle.up.because.thisll.suck.org. This can be fixed by moving the resource to the same domain or enabling CORS.

And Chrome says: XMLHttpRequest cannot load https://howdare.youthink.thiswouldbe.easy. No ‘Access-Control-Allow-Origin’ header is present on the requested resource. Origin ‘https://pain.and.suffering.org’ is therefore not allowed access.

But… you did it right. You did just what it said you should do. You even googled it a few more times and everyone says the same thing—Just that one line of code and you’re supposed to be done! You’re supposed to be kicking back with some nachos now! Chrome even says that the header is there, for crying out loud!

Yeah, no.

Turns out there’s a friggin metric crapton more to it than that. I won’t go into all the details here, but there’s a lot. What I will do is give you a list of quick and dirty things to try.

Enable mod_headers

There’s a module that allows Apache to add things to the request/response headers. You’ll need that. Near the top-ish of your httpd.conf file, look for…

#LoadModule headers_module modules/mod_headers.so

(Mine was on line 115 in my Apache 2.4 setup.)

If yours has that hash/number/octothorpe/# sign at the beginning, remove it. As with any change to httpd.conf, you’ll need to restart Apache for this to take effect.

“Always”

First, just putting that line in my .htaccess wasn’t working. I could see the header in Chrome (image above) but it was apparently being ignored for who-the-crap-knows-why. Adding “always” before “set” seemed to correct this. I also moved it from my .htaccess to httpd.conf, just above my virtual hosts section.

Header always set Access-Control-Allow-Origin "*"

You’ll also see different versions of this out there in the wild, some where it says to use header set, and others where it says header add. Both will work, but set is safer in this case because add can add multiple headers, which according to the CORS documentation is not allowed.

Depending on your situation, that might do it for you. But probably not. So…

The 2nd one determines what headers your requesting server (the one trying to make the remote call) is allowed to send. You likely don’t need all of those, but I left in a bunch for the sake of example.

The 3rd one is super important. It determines what kind of RESTful calls your app is allowed to make. Again, you probably don’t need all of them. In fact, if you’re only doing GET requests, that’s the only one you need. But if you want to POST, then you need OPTIONS, too. More on that below.

GET works, but POST doesn’t: welcome to “preflights”

I won’t go into details here, but I will say that POSTs are different than GETs beyond the obvious ways. One way being that with POST, browsers do what’s called a “preflight” check. Basically, it sends a request before your actual POST, checking to see if it’s allowed to do what it’s trying to.

This was very confusing at first, because my GET calls were working fine, but when I tried to do a POST call, Chrome: 1. showed it as an OPTIONS request; and 2. returned 404 (or 400 or 403 or 500). The heck?

What’s happening is likely that your server is trying to respond to that OPTIONS preflight as a normal page fetch. It’s literally trying to serve up a page. In my case, the URL in question was a PHP script that was expected POST data and wasn’t getting it, so it barfed up a 403 error.

Making PHP return 200 OK for preflights

I had to make a script called blank.php, which contained nothing but some ranty comments. Then, over in .htaccess, I added this:

What’s happening here is, whenever an OPTIONS request comes in (line 2), it redirects any-and-all of them to blank.php (line 3). Blank.php is, well, blank, so it returns an HTTP status of 200 OK. The browser then takes this as a successful attempt to discover what its OPTIONS are, learns from the other headers (Allow-Origin, Allow-Headers, Allow-Methods) that it’s allowed to do a POST, and then finally sends the real, actual, honest POST that you’ve been trying to get it to do this entire freakin’ time.

Hope that helped

Depending on your environment/needs, that might not be the end for you. If so, you have my sympathy. Either way, I hope this at least gets you a few steps closer.

So when a new version of any OS comes out, I like to do a clean install. On Macs, this has usually been fairly easy, since HFS+ partitions are pretty flexible — they’ll let you add/remove/resize without a lot of hassle.

Until Yosemite.

In the past, I’ve made a new partition on my drive, installed the new OS X (like Mavericks), copied all my files from the old partition to the new one. Once I’m sure that all is fine and dandy, I will go back and delete the older partition and give the space to the new one.

But when I tried this with 10.10 Yosemite, I ran into a new problem. Partitions were no longer flexible. I couldn’t delete or resize any of them. I found this out after spending a full day getting the new Yosemite partition just right.

Some research found that Yosemite will change its partition from HFS+ to “Core Storage”. (According to Ars Technica, there’s not a clear reason why, either.) I’m not sure of all the technical details, but this apparently killed flexibility. So my new Yosemite install was stuck on a partition with only 10GB of free space, when it could have had 400 from the old Mavericks partition. It wouldn’t even let me delete the Mavericks partition. Everything was locked.

Fortunately, I found a solution. You can convert Yosemite’s Core Storage partition back to HFS+. Once I did that, Disk Utility worked fine again. Some notes:

This is not destructive. You won’t lose anything.

This only works if you have not encrypted the partition with FileVault.

Here’s how:

First, you have to get something called a lvUUID for the partition. (Don’t worry about the annoying acronym. This is all scarier than it looks.)

Open Terminal, and run this (you can copy/paste):

diskutil cs list

This will give you a list of partitions similar to this:

Thanks to BrettApple at the MacRumors forum for this.

Okay. See how it says “Revertible: Yes”? That means we can convert it back. You’ll need that super-long string of letters/numbers. In the same Terminal window, type:

diskutil coreStorage revert [THAT LONG STRING OF STUFF]

So in this particular case, we would run:

diskutil coreStorage revert 47F9D6B1-F8F2-4E64-8AD4-9F2E2BD78E29

The results should be quick. Only seconds. Once that was finished, I was able to delete the Mavericks partition and resize my new Yosemite partition to fill all available space.

So you have a date from your database and it looks like this: 2014-01-31 01:02:03. It so happens that your server is on UTC time. You want to:

Format the date into something prettier, and

Offset the date so it’s adjusted for a user’s local time zone.

Ok, great. You only have to SUFFER THE PAIN OF A THOUSAND MIGRAINES ANDoh wait it’s actually sort of easy. But man, this took a lot of pain and searching to find. The key is moment.js, a glorious gift to developer-kind.

So the way I’m doing this is to create an HTML5 <time> element, but it can certainly be done other ways. First, after running a DB query, I have PHP produce this:

What we just did, is we read the date from our <time> element’s datetime attribute, then told moment what format it was in, so it could then spit it back out how we want it to, using .format(), as the human-readable part of the <time> element.

The result looks like this: 3 Jan 2014 at 12:57 PM. Yay. It’s nicer. But that’s still UTC time, and no timezone data was saved in our DB. So we have to add it.

Note that the stuff added here didn’t come from the DB, I added them manually. The “+0000”, or an offset of 0 hours for UTC time, is what we’re giving to moment.js. The “UTC” is just there for humans to read, just in case something goes wrong or Javascript is disabled. With those added, we inform moment.js that there’s a timezone offset in there:

var nice = moment(date,'YYYY-MM-DD HH:mm:ss ZZ').format(newformat);

See the “ZZ” we added in the middle? Now moment.js knows we have a timezone and what it is. And just like that, it’s been adjusted: 2 Jan 2014 at 5:57 PM(adjusted for America/Denver, -7:00).

It was so easy, that after all the suffering gone through before this point, I’m almost mad. But not really. But just a little bit.

Nice, huh? Super helpful. Well, as it turns out, the problem is that the example code I linked to above, and variations of it I’ve found all over the webs only work for live IPN responses. Meaning anything from www.sandbox.paypal.com (like PayPal itself tells you to use) will fail. EVEN IF YOU CHANGED IT to use Sandbox like so: (This is line 15 of the above-linked example code)

As near as I can tell, the problem is that the IPN responses are being sent from paypal.com, even though you want</> to — have been told by PayPal itself — to work with sandbox.paypal.com while developing.

Done. Problem should be solved. To sum up, it’s not enough simply to set the fsockopen() to use sandbox.paypal.com, you also need to set the response header correctly, or there will be a mismatch and sad, sad failure.

2. Open an elevated command prompt. (Search for “cmd.exe” from the Start ball, and then right-click on it, and choose Run as Administrator.)

3. Using the command prompt, navigate to wherever you put Sysinternals.

4. Elevate yourself once again to supreme power by using:

psexec -i -s cmd.exe

A new command prompt window will open that is running as the SYSTEM account.

5. Map the network drive:

net use z: \\[IP ADDRESS HERE]\[FOLDER NAME HERE] /persistent:yes

And you’re done!

Warnings:

You can only remove this mapping the same way you created it, from the SYSTEM account. If you need to remove it, follow steps 1 -4 but change the command on step 5 to: net use z: /delete.

The newly created mapped drive will now appear for ALL users of this system but they will see it displayed as “Disconnected Network Drive (W:)”. Don’t worry though! It displays as disconnected, but will work for any user.

Maybe you’re like me, and you’re migrating something from MySQL over to PostgreSQL. Maybe, like me, you’re swearing a great deal and experiencing high blood pressure, too.
(Or maybe not.)

I’ve seen numerous threads that just tell you how you can change all your code to use Postgres’s epoch_something_something_aint_nobody_got_time_fo_dat() function instead, but epic hero Janusz Slota has a better way. He shows you how to, rather easily, make it possible to run those functions in PG without having to change a thing.

In iTunes 10, you could skip to the next/previous track when Repeat One was turned on. In 11 & 12, they assume that by “skip ahead”, you somehow mean “rewind this track”.

This really bothers some people.Do not judge us, we have our reasons!

And here’s one way to fix it.

It took some work, but I finally came up with some AppleScript to handle this. Basically, we’re checking to see if Repeat One is on. If it is, we quickly disable it, skip to the next (or previous) track, and then turn it back on. Apple borked the old way of doing this (same with shuffle), so we’re using menu bar items instead.

-- This script lets you skip songs in iTunes 11/12 even if repeat one is on
tell application "System Events"
tell process "iTunes"
-- Find out if repeat one is on
-- This finds out if the menu item is checked
set isRepeatOneOn to (value of attribute "AXMenuItemMarkChar" of menu item 3 of menu 1 of menu item "Repeat" of menu 1 of menu bar item "Controls" of menu bar 1 as string) ≠ ""
if isRepeatOneOn is true then
-- Set repeat to ALL
click menu item 2 of menu 1 of menu item "Repeat" of menu 1 of menu bar item "Controls" of menu bar 1
-- Skip to previous track...
click menu item "Previous" of menu 1 of menu bar item "Controls" of menu bar 1
-- Need this, or the next step happens too fast
delay 0.1
-- Reactivate Repeat One
click menu item 3 of menu 1 of menu item "Repeat" of menu 1 of menu bar item "Controls" of menu bar 1
else
-- Just skip to previous track
click menu item "Previous" of menu 1 of menu bar item "Controls" of menu bar 1
end if
end tell
end tell

iTunes 11 is out, and most people seem to think it’s great. You and I are different, however. We hate it, and we have our reasons. (Mine happens to be the inability to skip tracks while Repeat One is on. Yup, deal-breaker for me. UPDATE: Fixed that!) So let’s make things right again.

First, you’ll need a backup of your iTunes Library.itl file, found under ~/Music/iTunes. Fortunately, I backed my library up right before installing iTunes 11. Note that any new songs, apps, etc. that you may have added since installing 11 will need to be replaced. In my case, I used the “Date Added” feature in iTunes to find which files I had added since November 28, and copied those out into a separate folder. When the downgrade was complete, I simply copied them back in.

3. Delete iTunes 11. There are different ways to do this. One is to use an app called AppZapper. The method that worked for me was this: Open Terminal.app and run these commands, one at a time:

killall iTunes

killall "iTunes Helper"

sudo rm -rf /Applications/iTunes.app/

That last one will need your password and will probably take a minute or so.

4. Now we have to reinstall iTunes 10.7 using an app called Pacifist (shareware, free). Download Pacifist and run it. (Mavericks: download Pacifist from the link at the bottom of the post instead.)

5. Choose the Open Package option. Browse to the iTunes 10.7 dmg file.

6. You’ll get a list of files. Select “Contents of Install iTunes.pkg”, and from the top left corner of the app, choose Install.

7.Be careful here! Every time Pacifist tells you a file already exists, make sure you check the “always” box and choose Replace (not update). This should happen around three or four times.

8. You’re almost done. Before running iTunes again, make sure you have recovered your “iTunes Library.itl” from a pre-iTunes-11 backup. After that, you should be good to go. But as ever, your mileage may vary.

Mavericks (10.9) Update

I was dreading Mavericks because it automatically updates iTunes to 11, and this is frankly unacceptable for some. Thank goodness this method worked like a charm this morning! I had 10.7 back in about 5 minutes.

Also, if you just installed Mavericks, don’t open iTunes! If you do, it’ll update your iTunes library files, and you’ll have to restore your old ones from a backup after you install iTunes 10.7. But if you never open iTunes 11, they won’t be changed, and you won’t have to restore a thing.

*Mavericks Update #2

So it appears that after downgrading to iTunes 10.7, the Mac App Store may become borked in the process — apps will neither update nor download from it. I’m not sure if this is just because Mavericks is fresh and will get updated by Apple or what. I use the Mac App Store just about never, so I don’t really care, but you might! More details as they become available.