|
» Log In » Register » Suggest » Feeds » News » Podcasts » Tags » Pings » Documents » XML » Web Services » Categories » Statistics » Help » Site Map » About |
|
Previous Syndicated Feed |
Random Syndicated Feed |
Next Syndicated Feed |
|
Feed Tags
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Headlines | Poll Results | Statistics | XML | Action Log(1) | Notes(0) | Categories | Contacts | Locations | Subscribers | Changes |
| Title | Description |
| How to HTTP POST JSON using PHP | The PHP snippet encodes $input as JSON and makes a HTTP POST operation to a given URL. The response is also decoded from JSON back to a native PHP data type. /* POST JSON encoded version of $input to $url */
/* By Tim Hastings, http://www.nonhostile.com/howto-http-post-json-using-php.asp */
function JsonPost($url, $input) {
// encode and prepare headers
$json = json_encode($input);
$contentLength = strlen($json);
$headers = "Content-type: application/json\r\nContent-Length: {$contentLength}\r\n";
// populate the options,
$options = array(
'http' => array(
'method' => 'POST',
'header' => $headers,
'ignore_errors' => true,
'content' => $json
)
);
// create context
$context = stream_context_create($options);
$fp = @fopen($url, 'rb', false, $context);
if (!$fp) {
// failed here
$resp = null;
} else {
// get the response and decode
$resp = stream_get_contents($fp);
$resp = json_decode($resp, true);
// close the response
fclose($fp);
}
// decode JSON response
return $resp;
}
|
| Javascript ForEach Key Value Iteration Like PHP | Whilst getting started with node.js I found myself needing lots of simple Javascript snippets equivalent to common PHP snippets. Believe it or not, it took me too long for my liking to find the Javascript equivalent to PHP's foreach so I thought I'd put up another version. In PHP, key/value iteration looks like this: foreach ($myArray as $key => $value) {
// use $key and $value here
}
In Javascript, key/value iteration looks like this: for (key in myArray) {
var value = myArray[key];
// use key and value here
}
Happy Javascripting :-) |
| HTTP Posting some JSON using curl | Increasingly I find I am posting JSON all over the place. This linux snippet comes in handy for doing this. curl http://example.com/script \
-H "content-type: application/json" -d "{ \"woof\": \"bark\", \"de\": \"do doo\" }"
Hope this helps :-) |
| Sending HTTP DELETE with curl | The need arose today to issue a bulk set of HTTP DELETE operations against our applicaiton's REST-based API. It took me a little digging to find out how to use curl to do this, so here it is: curl -X DELETE http://localhost/example/ Loving curl! It is quickly becoming one of my favourite command line tools. |
| How to Install Node.js on Ubuntu | After reading a lot about node.js recently, I decided to give it a whirl. Having just built a clean desktop install of Ubuntu 10.4 I had the ideal environment. Here are the steps to get it up and running. First, start a super user console session sudo -i -- and enter password to elevate privileges Now, setup the pre-requisites for compilation and test execution: apt-get install g++ curl libssl-dev apache2-utils Next, download, unpack and build:
wget http://nodejs.org/dist/node-v0.4.5.tar.gz
gunzip node-v0.4.5.tar.gz
tar -xf node-v0.4.5.tar
cd node-v0.4.5/
./configure
make
make install
Finally, to run the tests: make test That's it, you're done. Happy noding! |
| VB.Net: How to Format unicodePwd for Active Directory LDIF Script for LDAP Import | If you are doing LDAP integration work with Active Directory and you want to update a user's password via an LDIF script you need to specify the password attribute in a very specific way. The sequence of steps is this:
So something like password becomes IgBzAGUAYwByAGUAdAAiAA== This all sounds well and good, here's a code sample:
Public Function FormatPassword(ByVal password As String) As String
If password Is Nothing Then Throw New ArgumentNullException("password", "password cannot be nothing")
' enclose in speech marks
password = String.Format("""{0}""", password)
' convert to bytes with unicode encoding
Dim bytes() As Byte = Encoding.Unicode.GetBytes(password)
' convert to base 64
Dim base64 As String = Convert.ToBase64String(bytes)
Return base64
End Function
Some sample passwords: admin IgBhAGQAbQBpAG4AIgA=
root IgByAG8AbwB0ACIA
password IgBwAGEAcwBzAHcAbwByAGQAIgA=
apple IgBhAHAAcABsAGUAIgA=
banana IgBiAGEAbgBhAG4AYQAiAA==
pencel IgBwAGUAbgBjAGkAbAAiAA==
LDIF Set Password ScriptTo use this encoded password in an LDIF script, you will need to create LDIF fragments like this: dn: cn=timhastings,ou=Users,dc=example,dc=com
changetype: modify
replace: unicodePwd
unicodePwd:: IgBwAGEAcwBzAHcAbwByAGQAIgA=
The double-colon is required on the unicodePwd attribute as it identifies that the data which follows is Base64 encoded. To import this into Active Directory, you will need to use the ldifde command line too. Password PolicyYou must make sure your passwords meet the strength and history requirements of the systems password policy, otherwise you will receive the following error message: The server side error is: 0x52d Unable to update the password.
The value provided for the new password does not meet the length, complexity, or history requirements of the domain.
The extended server error is: 0000052D: SvcErr: DSID-031A11E5, problem 5003 (WILL_NOT_PERFORM), data 0
ldifde Secure ConnectionWhen settings passwords, Active Directory insists you use a secure connection otherwise you will get an Unwilling To Perform error like this: Add error on entry starting on line 1: Unwilling To Perform
The server side error is: 0x1f A device attached to the system is not functioning.
The extended server error is:
0000001F: SvcErr: DSID-031A11E5, problem 5003 (WILL_NOT_PERFORM), data 0
To specify a secure connection to LDAP, use ldifde like this: ldifde -i -f -h -v myscript.ldf I hope this helps! |
| How to Find Which Process has a File Open on Linux | A number of times I have found myself unable to access a file because some other process is writing to it. Usually lock files which are used to prevent multiple instances of the same program from running at the same time, or to guard against multiple operations running at the same time that would interfere with each other. The Linux command which comes to the rescue is called lsof which I guess is short for "list of open files" It can be used in several different ways: List All Open Fileslsof This will list all the God damn files open on your system. There will be lots of other interesting things listed which are not files, things like pipes and network sockets. This is because on Linux, everything is abstracted to a file, even the keyboard. The output from lsof can be piped to grep to search for bits of file names. lsof | grep pony This will perform a very crude search for any file name, path or process with anything to do with 'pony' List All Open Files for a Given Process (by PID)For example: lsof -p 12345 Lists all file handles currently held by process 12345 lsof headingsBy default, the lsof output looks like this (without headings) mysqld 30353 mysql 219u REG 8,2 5976322292 6652117 /mnt/mysql/tagwalk/msg.MYD mysqld 30353 mysql 220u REG 8,2 672237472 6652170 /mnt/mysql/tagwalk/tag.MYD mysqld 30353 mysql 221u REG 8,2 672237472 6652170 /mnt/mysql/tagwalk/tag.MYD mysqld 30353 mysql 222u REG 8,2 5976322292 6652117 /mnt/mysql/tagwalk/msg.MYD (cmd) (pid) (user) (handle) (type) (device)(size/offset) (node) (path) From left-to-right, the columns are:
Hope this helps! |
| MonoAmi: Mono 2.2 Amazon EC2 Image - Hosting for ASP.Net In The Cloud |
This week, the Mono team released version 2.2 of the framework (release notes). I have created a new Amazon EC2 image with Mono 2.2 installed on Amazon's Fedora 8 image. This new image supersedes the previous versions I created for 1.9.1 and 2.0. Here's is a link to this image's page in the EC2 resource centre. The EC2 AMI and manifest path are: ami-dc8760b5 nonhostile-mono2-2-i386/image.manifest.xml Once you start this instance, you can browse to its public DNS name to see the XSP test sites. This instance is intended to serve web applications or run console based applications or services; it does not have any kind of graphical interface. Ingredients This instance was made from:
The following have been compiled and installed from the Mono 2.2 stable sources with the --prefix=/usr
The sample ASP.Net files for 1.0 and 2.0 are installed at this root: /usr/lib/xsp/test The Apache config (/etc/httpd/conf/httpd.conf) file has been configured to serve an ASP.Net application from this location. You can edit the Apache configuration file using vim. Use Ctrl-D in vim to page-scroll down. vim /etc/httpd/conf/httpd.conf # Set mono as the handler SetHandler mono # Configure a 'root' web application to run from root MonoApplications root "/:/usr/lib/xsp/test" MonoServerPath root /usr/bin/mod-mono-server2 <Location /> MonoSetServerAlias root </Location>Configuring your own ASP.Net applications To serve your own ASP.Net applications from this instance, you need to store the files somewhere, for example, /mnt/MyApp, then modify the MonoApplications directive at the bottom of the Apache configuration file. MonoApplications root "/:/mnt/MyApp" Good luck and happy Mono developing! Links
|
| ASP Fix: XML transformNodeToObject - Not implemented (80004001) | From time to time, my web host changes some aspect of the server that this weblog is hosted on. The code that runs this site is written in classic ASP which means it is hosted on IIS. Recently another configuration change took place, presumably one of Microsoft's many hot fixes or an upgrade to a new version of Windows or IIS. The following error started to appear tagged onto the bottom of every web page: msxml3.dll error '80004001'
Not implemented
Behind the scenes, this site use XML and XSL to separate the site's data from its layout, in the middle somewhere is a line of code which performs an XSLT transformation on the XML DOM into the HTML output which gets squirted out into the response.
' method to transform the response (with errors)
public sub TransformToResponse_Old(xml, xsl)
xml.transformNodeToObject xsl, Response
end sub
Somewhere in the depths of ASP or IIS, a change has occurred which has dropped the IStream support of the classic ASP response object (I presume looking at the symptoms). The fix introduces an intermediate stream to receive the XSLT transformation, and then send that to the response. This method also sets the response code page to be UTF8, which this site uses.
' method to transform the response (without errors)
public sub TransformToResponse_New(xml, xsl)
' By Tim Hastings, www.nonhostile.com
' prepare stream to receive transformation
dim outputStream
Set outputStream = Server.CreateObject("ADODB.Stream")
outputStream.Open
outputStream.Charset = "UTF-8"
outputStream.Type = 1 ' adTypeBinary
' transform and output to stream
xml.transformNodeToObject xsl, outputStream
' set character-set
Response.CharSet = "UTF-8"
Response.ContentType = "text/html"
Session.CodePage = 65001
' rewind the stream and send to response
outputStream.Position = 0
Response.BinaryWrite outputStream.Read()
' finished with the stream
outputStream.Close
set outputStream = Nothing
end sub
Hope this helps! |
| MonoAmi: Mono 2.0 Amazon EC2 Image - Cloud Hosting for ASP.Net |
On Monday, Mono 2.0 was released by the Mono Development team. This release has been in-progress for a couple of years and is a major step forward in the mission to run .Net on Linux and other platforms. If you check out the release notes you can see there's a bus load of new features supported; including Linq! To make it as easy to use Mono 2.0 in Amazon EC2 cloud I have built a new version of the image I created with Mono 1.9.1 a couple of months ago. Update: a Mono 2.2 AMI is available The EC2 AMI and manifest path are: ami-b627c3df nonhostile-mono2-i386/image.manifest.xml Once you start this instance, you can browse to its public DNS name to see the XSP test sites. This instance is intended to serve web applications or run console based applications or services; it does not have any kind of graphics interface. Ingredients This instance was made from:
The following have been compiled and installed from the Mono 2.0 stable sources with the --prefix=/usr
The sample ASP.Net files for 1.0 and 2.0 are installed at this root: /usr/lib/xsp/test The Apache config (/etc/httpd/conf/httpd.conf) file has been configured to serve an ASP.Net application from this location. You can edit the Apache configuration file using vim. Use Ctrl-D in vim to page-scroll down. vim /etc/httpd/conf/httpd.conf # Set mono as the handler SetHandler mono # Configure a 'root' web application to run from root MonoApplications root "/:/usr/lib/xsp/test" MonoServerPath root /usr/bin/mod-mono-server2 <Location /> MonoSetServerAlias root </Location>Configuring your own ASP.Net applications To serve your own ASP.Net applications from this instance, you need to store the files somewhere, for example, /mnt/MyApp, then modify the MonoApplications directive at the bottom of the Apache configuration file. MonoApplications root "/:/mnt/MyApp" Good luck and happy Mono developing! Links
|
| MySQL Replication between Amazon EC2 Instances - How To Setup Replication in The Cloud | The article discusses how to setup MySQL Replication between two Amazon EC2 instances. It walks you though setting up replication for an empty database server. Adding a slave to a server already full of data is a different article. It is assumed that you already know the basics of starting EC2 instances, connecting to them via SSH and editing files in Linux using vi/vim etc. For this tutorial, I am using the Amazon built machine image ami-2b5fba42 which is Fedora 8 base image. Overview In this tutorial, we will:
The steps for configuring the master are as follows:
First, launch an instance of the Fedora machine, and connect using SSH. This gives us a base Fedora instance. Next, install MySQL and configure it to start when the machine boots (in case we decide to restart it) Install MySQL Server and tools: yum install -y mysql mysql-server Rename the old config file (if you're interested in keeping it) and edit the /etc/my.cnf: mv /etc/my.cnf /etc/my.cnf.old vi /etc/my.cnf To look something like this: [mysqld] # replication settings for MASTER server-id = 1 # data folder datadir = /mnt/mysql # switch on binary logging (required for replication) log-bin = mysql-bin # system stuff user = mysql socket = /var/lib/mysql/mysql.sock [mysqld_safe] log-error = /var/log/mysqld.log pid-file = /var/run/mysqld/mysqld.pid Now we can configure MySQL to start at boot, start it now and connect. chkconfig --level 2345 mysqld on service mysqld start mysql Now we can create a replication user account: GRANT REPLICATION SLAVE ON *.* TO 'ReplicationUser' IDENTIFIED BY 'ReplicationPassword'; FLUSH PRIVILEGES; We're done. Next, the slave... Configuring the MySQL Slave (shown in green)
This is an almost identical sequence of steps, with a couple of minor differences.
Once the slave instance is running, connect using SSH and install MySQL and tools (just like the master): yum install -y mysql mysql-server Rename the old config file (if you're so inclined) and edit the /etc/my.cnf: mv /etc/my.cnf /etc/my.cnf.old vi /etc/my.cnf To look like this: [mysqld] # replication settings for SLAVE server-id = 2 # data folder datadir = /mnt/mysql # switch on binary logging (not essential but handy if you want to replicate from this slave in the future) log-bin = mysql-bin # system stuff user = mysql socket = /var/lib/mysql/mysql.sock [mysqld_safe] log-error = /var/log/mysqld.log pid-file = /var/run/mysqld/mysqld.pid Now we can configure MySQL to start at boot, start the service going and connect: chkconfig --level 2345 mysqld on service mysqld start mysql On the Master, we need to determine the binary log's starting position. This is the byte offset that our Slave should start reading from. On the master, type the following: mysql> SHOW MASTER STATUS; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000003 | 319 | | | +------------------+----------+--------------+------------------+ 1 row in set (0.00 sec) Next, we want to connect the SLAVE to the MASTER. For this, we need the replication user details we created earlier and the internal (or private) DNS name for the MASTER, this should be available from ec2-dim or the ElasticFox instances list. We also need the file and position we get from the master in the step above. This is done with the MySQL CHANGE MASTER command: CHANGE MASTER TO MASTER_HOST='domU-11-22-33-44-55-66.compute-1.internal', MASTER_USER='ReplicationUser', MASTER_PASSWORD='ReplicationPassword', MASTER_LOG_FILE='mysql-bin.000003', MASTER_LOG_POS=319;We can now start the slave's replication process: START SLAVE; Testing To See If Replication Is Working Now replication is working, we can issue commands against the Master, and check to see if the Slave replicates the result. To do this, use two terminals side by side, one connected to the Master and one to the Slave. mysql> CREATE DATABASE HelloWorld;
Query OK, 1 row affected (0.00 sec)
mysql> use HelloWorld
Database changed
mysql> CREATE TABLE Message (Content VARCHAR(100) NOT NULL PRIMARY KEY);
Query OK, 0 rows affected (0.01 sec)
mysql> INSERT INTO Message (Content) VALUES ('Howdy slave');
Query OK, 1 row affected (0.00 sec)
On the Slave, we can now verify if the changes made to Master have been replicated: mysql> SELECT * FROM HelloWorld.Message; +-------------+ | Content | +-------------+ | Howdy slave | +-------------+ 1 row in set (0.00 sec) Congratulations! You have a working replication. Summary There are a number of things you can use your slave for:
|
| Quotes, Wisdom and Snippets | Here's another round-up of quotes, wisdom and snippets... On succeeding
On the creative process
On getting things done
On economics
On technology
On software development
On everything else
Here are some previous quote dumps. © Copyright 2008 Tim Hastings (all rights reserved) |
| Now Witness the Power of This Fully Armed and Operational Debian NSLU2 |
This post is trumpeting the successful installation of Debian onto a Linksys NSLU2 Network Storage Link. The unit is a Network Attached Storage (NAS) server which allows upto two USB hard drive or flash drive to be network accessible from anywhere. There are lots of different firmware replacements available developed by the open source community and these allow you to use the NSLU2 as a dedicated Linux box. The NSLU2's specs are:
My partitions were configured as: 3.7GB – Primary partition, used for ext3, bootable, mounted as / 380MB – Logical partition, used for swap (lots more than the recommended 256MB) Changing the hostname from the serial number: echo slug > /etc/hostnameModified /etc/network/interfaces to end with: # The primary network interface
allow-hotplug eth0
iface eth0 inet dhcp
hostname slug
Links
|
| MonoAmi: Hosting ASP.Net, C# and VB.Net with Mono on Linux in The Amazon EC2 Cloud |
Mono AMI for Amazon EC2
Getting StartedFor anyone interested in experimenting with Mono on Amazon EC2 I have created a publicly available image which you can instantiate and play with. It can be used for both ASP.Net applications and console applications. The image does not have a GUI so this is not an appropriate platform to test applications with a GUI. Update: a Mono 2.2 AMI is available The EC2 AMI is: ami-0abe5a63 nonhostile-mono-i386/image.manifest.xml It is beyond the scope of this post to cover the basics of using Amazon EC2, there are already great tutorials on how to do this. Once your instance is up and running, you can take your browser to the public DNS name to play with the ASP.Net XSP sample pages. Most of the samples are implemented in C#. There is some chatter in the forums saying that some of the ASP.Net samples are a bit broken, most work, please feel free to fix any bugs and contribute back :-) If you want to host you own web pages, you will need to modify the Apache config file and either modify the served directory (at the bottom of /etc/httpd/conf/httpd.conf) or alternatively, add new virtual directories or virtual hosts as explained in mod_mono's documentation I would also strongly advise any .Net developers interested in Linux to knock up a quick "Hello World" console application in Visual Studio and the copy it across to Mono. You do not have to recompile for Mono, it just works™, the Mono boffins have created a binary compatible version of the CLR and you app doesn't know the different (unless it starts to get get nosey). The only difference is that .Net executables have to be launched with the Mono command line tool, like this: mono hello-world.exeIngredients This instance was made from:
As part of my What’s on my iPod? Facebook application I have been using Amazon EC2 to host the PHP front-end application and Mono to run the backend data processor which is a console application implemented in VB.Net (written in Visual Studio 2005). On occasion, I have tried to get ASP.Net working, but never quite got it right, and never really had to. But, this evening I have had a break-through, so I thought I would share my progress with anyone interested. Color Blue is not a valid color (I disagree) I had made many failed attempts to get ASP.Net working on Apache with mod_mono and mod-mono-server but usually came unstuck with obscure errors when executing some pages. Depending on the test page, it would report: "Color Green is not a valid color" or "Input string was not in the correct format" errors thrown from System.Drawing.WebColorConverter After much searching and experimenting, the problem was pinpointed to System.Drawing's dependency on libgdiplus.so, when this library is installed (via yum) it would install the library as "libgdiplus.so.0.0.0", and a symbolic link to it called "libgdiplus.so.0", but not create the "libgdiplus.so" symbolic link required by System.Drawing - which causes this fault. If you are experiencing this problem, make sure that libgdiplus.so exists in /usr/lib, if it does not, you can create a symbolic link to it with: ln -s /usr/lib/libgdiplus.so.0 /usr/lib/libgdiplus.soExtra credits go to wangli, Sebastien Pouliot-2, Max Karavaev and Abe Gillespie for figuring this out Links
p.s. please do not draw attention to the irony that this blog is implemented in Classic ASP – move along, there’s nothing to see here. |
| There's Been a Dirty Murder | In honour of Jon's birthday, here's some footage of him being killed by an Alien Axe Murderer. Which is nice. |