Configuring WebDAV and Digest Authentication for Ubuntu

I'm looking at using WPKG as a mechanism for distributing software updates to client workstations. WPKG appears to be a pretty nice system, but it has one big downside: one has to set up a WebDAV-enabled server if the updates are to be pulled from the Internet instead of from a local shared directory. So I've spent a few hours learning the intricacies of setting up WebDAV on my Ubuntu-based Internet server and accessing it from Windows machines. Here's what I learned.

Deserializing Objects from XML in C#

Here's another C# code snippet that takes me way too much time to recreate by just reading the documentation.

This is a simple example of a class that can be serialized to/from XML. In this case the "ServerConfig" XML string can contain a list of servers, looking like this:

<ServerConfig loggingEnabled="1">
  <Servers>
    <Server host="test1.example.com" port="9999" />
    <Server host="test2.example.com" port="8888" />
  </Servers>
</ServerConfig>

The client code can just do "var serverConfig = ServerConfig.FromXmlString(s);" to deserialize it into a ServerConfig object.

(The method that would serialize a ServerConfig to an XML string is left as an exercise for the reader. I rarely need to do that.)

Pretty-formatting XML in C#

I had a need to convert an XML string to a nice, indented format. It was a little more complicated than I expected, so I'm posting this snippet here where I can find it again when I need it.

using System;
using System.Text;
using System.Xml;
using System.Xml.Linq;

static string PrettyXml(string xml)
{
    var stringBuilder = new StringBuilder();

    var element = XElement.Parse(xml);

    var settings = new XmlWriterSettings();
    settings.OmitXmlDeclaration = true;
    settings.Indent = true;
    settings.NewLineOnAttributes = true;

    using (var xmlWriter = XmlWriter.Create(stringBuilder, settings))
    {
        element.Save(xmlWriter);
    }

    return stringBuilder.ToString();
}

Note that this method can throw exceptions for a variety of reasons.

Code Reuse Is Not Lazy

Stack Overflow is an amazing resource for computer programmers. I've asked a lot of questions there, and answered a lot of questions there, and I've found the answers to lots and lots of questions that someone else has already asked. Participating in Stack Overflow has been great.

However, there have been a few cases where I've asked a question in this form:

I need some code that will [...]. Can someone point me to some existing code that does this?

And then I have received responses and comments like these:

We aren't here to do your homework for you.

You can't expect help without doing a little bit of work on your own.

You mean "Plez sir send me the codez?"

I don't understand why my questions provoke these responses. It is possible that I am asking the question in a way that the readers interpret as demanding or rude. But I also wonder if some people think that looking for existing code to solve a problem is a dishonorable thing to do, and Real Programmers write everything they need from scratch.

What if I had asked this:

I am new to C. I need to format some printed output in my C program. What's the easiest way to do this?

I hope someone would respond with

There is a standard function called "printf()" that will do what you want.

and not

Why can't you just figure out how to write your own formatting function It's the best way to learn C. Or are you just too lazy to spend time learning things?

Appropriate reuse of already written and debugged code is the mark of a professional, and time spent in research of existing implementations of algorithms you need is time well invested. If you want to write the millionth from-scratch implementation of Quicksort in your favorite programming language, go right ahead, but those of us who have real work to do are going to look for a library or a code snippet.

Steve Jobs

It's OK to have heroes, right?

I didn't think I had any heroes, but when Steve Jobs passed away last week, it hit me harder than expected. I didn't cry, I didn't go into a funk, but I was sad and felt like I'd lost something.

We idealize our heroes. We ignore their flaws and the fact that they are just regular people, and instead create simple character sketches based on a few noble aspects of their personalities and deeds. For example, Albert Einstein was a very smart, yet very warm and kind person, Abraham Lincoln freed the slaves, and Thomas Jefferson wrote the Declaration of Independence. We reduce a hero to a simple sentence, knowing that it is an incomplete picture, but focusing on what it is that makes the hero important to us.

Steve Jobs inspired people to make awesome things.

To me he represents the drive to make things better. But "better" wasn't good enough; he insisted that they be great. Sure, he exaggerated the greatness of Apple's products, and didn't give credit to the sources of many of the ideas that went into those products, but he did everything he could to make them the best they could be, and to give them a little bit of magic.

He didn't make things easy for software developers, or for hardware engineers, or for component manufacturers, or for IT managers. He simply did not care about their problems. His goal was to bring the amazing power of computers to normal people. He wanted normal people to have machines that they could use to create things and to enjoy themselves, without expecting them to become programmers and hardware techs.

A lot of business-type people wish that they were Steve Jobs. Not me. I would not have wanted to be Steve Jobs, but I would have loved to work for Steve Jobs. Even if I was the recipient of his famously colorful criticism, I'd know that I was getting advice from someone who expected me to do great things.

I missed out on the Apple II era (having an Atari 800 instead), so my first Apple product was a Macintosh SE. I loved that cute, beautiful little computer. I learned Pascal, C, and Motorola 68000 assembly programming on that computer. I drew pictures, I played games, I stuck it in the back seat of my car when going from place to place.

But you know what it is about that Mac that really sticks out in my memory? If you opened the case, instead of finding just metal and wires, you found engraved signatures of Steve Jobs and other members of the Macintosh team. They had created something special, and they were very proud of their work. That inspired me to be proud of the things I make, and to make them as beautiful and magical as I am able.

Sometimes I forget to do those things. I need Steve Jobs around to occasionally remind me.

It's OK to have heroes, right?

My Visit to Android-land

I've been a happy iPhone and iPad user for a while, but I'm currently involved in developing Android applications, so I decided to buy an Android phone to use for a few weeks. It's important to understand the user-interface conventions and user expectations for whatever platform you are developing for, and I figured that the only way to learn those things would be to take the plunge and immerse myself in Android for a while.

The following is a self-indulgent review of what I've found. If you are an Android fan, you will probably summarize it as "Surprise! Apple fanboy doesn't like Android," and you can just stop reading here.

KJMenuTableViewController - iOS Menus Made Easy

I have a new open-source library on Github for use by iOS developers: KJMenuTableViewController.

KJMenuTableViewController is an Xcode project that contains set of classes that simplifies the
creation of "menus" in iOS applications using UITableViewController.

The UITableViewController class is a generic mechanism for presenting a scrollable list of
rows of items. It is powerful and extensible, but it can be a chore to present a simple
list of button-like objects that react when tapped. One must provide implementations of several
methods of the UITableViewDataSource and UITableViewDelegate classes, each of which will
probably have a case statement to handle each of the individual items. It's not difficult, but it is
tedious and error-prone.

The KJMenuTableViewController classes simplify this usage case. One simply defines a subclass of
KJMenuTableViewController and overrides the viewDidLoad method to create sections and row items.
KJMenuTableViewController implements the table view delegate and data source methods to
appropriately display the sections and rows, and will take action when a row is tapped.

The code to be executed when an item is tapped are written as a block.
When the block is invoked, a KJMenuItemInvocation structure is passed to it. This structure
contains pointers to the menu item, cell, and controller, so there is no reason for the block
to retain any of these objects itself. (Beware of retain cycles if the block does reference
the menu item, cell, or controller.)

Beware the Lure of the iOS UIWebView

Apple's iOS SDK provides a class, UIWebView, that provides a simple way to display HTML content in an iOS application. Many apps use UIWebView to display web pages, online help, and other formatted content.

For the basic purpose of displaying HTML content, it works pretty well. However, as it appears to just be a wrapper around WebKit, one might be tempted to use it to try to implement a full-fledged web browser embedded in an application. Don't do this! UIWebView has many limitations that make it unsuitable for this purpose:

  • Many commonly used JavaScript functions, such as alert() and window.open(), don't work at all or only work in limited ways in a UIWebView. So many of the web sites one would try to visit do not work in a UIWebView.
  • There are limited hooks for customizing the behavior. There are a few delegate methods that notify your app when the web view starts loading or finishes loading, but you can't detect many of the events you'd really want to detect.
  • UIWebView does not send the same browser identification info that Mobile Safari does, so some servers will treat it as an unknown browser and return limited content.

Of course, some intrepid developers have found ingenious ways to work around some of these limitations. If you really want to try it, or if you are curious about what kinds of hackery are needed to use UIWebView as a web browser, check out these links:

The lesson I learned (after several hours of banging my head against the wall) is to pass web URLs over to Safari, rather than trying to display them in a UIWebView within my app. It's just not the right tool for this job.


Update (2011/10/1): My original post included this bullet point:

  • You can't control authentication. UIWebView can open an HTTPS connection, but if the server-side certificate is self-signed, there is no way to get it to ignore the certificate, and so it just fails with an error message.

I've been informed that this is not entirely true. It is possible to somehow "preconnect" to the server with an NSConnection, deal with authentication, and then pass the credentials to the UIWebView. More information is available here: http://stackoverflow.com/questions/11573164/uiwebview-to-view-self-signe...

My JavaScript Cheatsheet

A couple of times per year, I have to work on something that requires me to write some JavaScript. When I do, I have to reacquaint myself with the language by skimming through JavaScript: The Good Parts and finding some good online reference documentation.

In an effort to reduce the time needed to do this next time, I'm recording the little things that I ran across that I didn't remember or wished I could have found faster. So this is my own personal refresher for JavaScript. It may not help you at all.

Also see my Node.js Cheatsheet.

Reference Links

(268242) Pebble

My wife has had a minor planet named after her. This is the official naming citation, written by the planet's discoverer, Jim Bedient:

(268242) Pebble = 2005 JW1

Discovered 2005 May 4 by J. Bedient at Haleakala-Faulkes Telescope North.

Pebble Johnson (b. 19xx) is an innovative teacher of middle-school science and technology in Forsyth County, Georgia. She uses astronomy to heighten interest and excitement in the physical sciences among her students.

The naming citation was published in MPC # 75106:
http://www.minorplanetcenter.net/iau/ECS/MPCArchive/2011/MPC_20110517.pdf

Jim also wrote the following in an email::

On May 4, 2005, I was observing with the 2.2m Faulkes Telescope-North on Haleakala, Maui.  While observing an asteroid on the NEO list, I observed another object in the background.  I performed astrometric observations on succeeding nights, and it received the provisional designation 2005 JW1. After following it for several years, its orbit has been accurately determined, and a few months ago it received its permanent designation, (268242). Under International Astronomical Union rules, this made it eligible for naming, and of the privileges of discovering a minor planet is naming it. A couple of months ago I submitted a name proposal to the IAU's Working Group for Small Body Nomenclature. Last week I was informed that the proposed name had been approved, and it is hereafter known as (268242) Pebble, in honor of my good friend Dr. Pebble Johnson.

...

With the boom in minor planet discoveries in the last decade, due the advent of more sophisticated techniques, particularly larger telescopes and electronic detectors, naming citations are required to be considerably briefer than they once were allowed to be.  I really couldn't wax poetic about meeting Pebble ten years ago at Dr. Karen Meech's TOPS workshops in Hawaii, how she became a very dear friend, how I helped with her Ph.D thesis, and all the things, small and large, that go into forming a strong friendship. Placing her name in perpetuity on this small piece of solar system real estate is just an insignificant monument to a person with a very large heart.  I am honored to know her and to be able to make this gesture.

Syndicate content