<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>~james/developer/blog &#187; Uncategorized</title>
	<atom:link href="http://jameswilliams.me/developer/blog/category/uncategorized/feed/" rel="self" type="application/rss+xml" />
	<link>http://jameswilliams.me/developer/blog</link>
	<description>A development blog</description>
	<lastBuildDate>Tue, 23 Aug 2011 19:17:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Pragmatic PHP</title>
		<link>http://jameswilliams.me/developer/blog/2008/12/pragmatic-php/</link>
		<comments>http://jameswilliams.me/developer/blog/2008/12/pragmatic-php/#comments</comments>
		<pubDate>Mon, 08 Dec 2008 23:13:16 +0000</pubDate>
		<dc:creator>James Williams</dc:creator>
				<category><![CDATA[for_export]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://jameswilliams.me/developer/blog/?p=79</guid>
		<description><![CDATA[I wrote some PHP today. I didn&#8217;t mean to. All I wanted was a way to integrate strategically tagged pictures from flickr into a website. I couldn&#8217;t find any ready-made solutions to do what I wanted (making sure the page blends in with the rest of the site is the number one requirement (aside from [...]]]></description>
			<content:encoded><![CDATA[<p>I wrote some PHP today. I didn&#8217;t mean to.</p>

<p>All I wanted was a way to integrate strategically tagged pictures from flickr into a website. I couldn&#8217;t find any ready-made solutions to do what I wanted (making sure the page blends in with the rest of the site is the number one requirement (aside from actually pulling in the pictures)), so I had to write it myself. And while my <a href="http://dreamhost.com">host</a> is certainly capable of running ruby or python&#8230;I couldn&#8217;t figure out how to do it without running rails or something. With PHP, on the other hand, it&#8217;s as easy as creating a file with a .php extension. So PHP was definitely the path of least resistance.</p>

<p>I haven&#8217;t really done much with PHP. I used it in college once, but that project involved staying up for forty-eight hours while banging out bad code in the computer lab. I can&#8217;t really remember much from that time other than prefixing variables with dollar signs. Which is probably for the best. (Despite handing the project in a couple of weeks late, I got an A by hanging out with the professor for a few hours. It was that kind of class.)</p>

<p>So I banged out a web page to do what I wanted. And I realized just how hard it is to write good code in PHP. I&#8217;m sure it&#8217;s possible, but I certainly haven&#8217;t figured it out yet.</p>

<p>After just a couple of hours, I already had a mostly unmaintainable mess on my hands. There are global functions and it&#8217;s basically impossible to tell where my logic ends and my markup begins. I&#8217;ve already refactored things out once, but things have already deteriorated.</p>

<p>Until now, I&#8217;ve considered myself a fairly disciplined programmer. I&#8217;ve developed pretty good instincts for setting up class hierarchies, making sure that everything I write only has one responsibility, avoiding code smells, and things like that. Yet somehow, over the course of hacking out this little project, I&#8217;ve managed to churn out something more expected from a first year programming student. I&#8217;d like to think I did it a little faster than that hypothetical first-year would have, but still.</p>

<p>I think it&#8217;s because I don&#8217;t actually know PHP. In most languages, that would mean that I just couldn&#8217;t have done anything and would have had to learn the language first. But PHP has such a large (if somewhat disorganized) library and there are so many copy-and-paste examples on the Internet that not knowing the language wasn&#8217;t actually a barrier at all. PHP let me get it done anyway.</p>

<p>So, score one for immediate pragmatism but minus a thousand for technical debt, you know?</p>

<p>I&#8217;m sure it&#8217;s possible to write good, maintainable software if you know the language well. But I suspect it&#8217;s hard. It&#8217;s so easy to write crappy PHP code (and I know because I just did it) that I can&#8217;t really imagine it being any easier to write good code. I imagine, for the PHP programmer, it&#8217;s a constant struggle to do things the easy way and just get it done versus spending a few extra minutes to do it the right way.</p>

<p>But I&#8217;m glad I don&#8217;t have to do it.</p>

<p>So, I salute all of the hard working, smart PHP programmers who are writing solid code against fantastic odds. Better you than me.</p>
]]></content:encoded>
			<wfw:commentRss>http://jameswilliams.me/developer/blog/2008/12/pragmatic-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>First Steps With Ruby</title>
		<link>http://jameswilliams.me/developer/blog/2008/12/first-steps-with-ruby/</link>
		<comments>http://jameswilliams.me/developer/blog/2008/12/first-steps-with-ruby/#comments</comments>
		<pubDate>Tue, 02 Dec 2008 21:32:32 +0000</pubDate>
		<dc:creator>James Williams</dc:creator>
				<category><![CDATA[for_export]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://jameswilliams.me/developer/blog/?p=73</guid>
		<description><![CDATA[After watching two fantastic PDC videos, Deep Dive: Dynamic Languages in Microsoft .NET and IronRuby: The Right Language for the Right Job, I started doing some serious thinking about dynamic languages and .NET. At work (where we use VB.Net), we tend to throw around several different data types that are essentially duck-typed: objects with key/value [...]]]></description>
			<content:encoded><![CDATA[<p>After watching two fantastic <a href="http://channel9.msdn.com/pdc2008/">PDC</a> videos, <a href="http://channel9.msdn.com/pdc2008/TL10/">Deep Dive: Dynamic Languages in Microsoft .NET</a> and <a href="http://channel9.msdn.com/pdc2008/TL44/">IronRuby: The Right Language for the Right Job</a>, I started doing some serious thinking about dynamic languages and .NET. At work (where we use VB.Net), we tend to throw around several different data types that are essentially duck-typed: objects with key/value collections that if it has a certain set of keys, it&#8217;s of type A and if it has another set of keys, it&#8217;s of type B.</p>

<p>What would happen if we combined these objects with the capabilities of a dynamic language?</p>

<p>I have no idea. With the exception of Objective-C, I don&#8217;t have much experience with dynamic languages. And Objective-C, sitting on top of C like it does, is hardly a candidate for running on the <a href="http://en.wikipedia.org/wiki/Dynamic_Language_Runtime">DLR</a>. And it&#8217;s hardly as expressive as something like Python or Ruby, which are conveniently the first languages available on the DLR.</p>

<p>So before I could start experimenting with our stuff and dynamic languages, I had to actually learn a dynamic language. I picked ruby, mostly by coin flip. I&#8217;m not starting with IronRuby right away. I find I&#8217;m always happier when I can manage complexity and it seemed that adding a not-quite-finished runtime to the mix could cause me trouble that I wouldn&#8217;t be prepared to fix. So I installed the &#8220;official&#8221; Ruby package for Windows and got to work.</p>

<p>I started out by implementing <a href="http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life">Conway&#8217;s Game of Life</a> in Ruby. Over the course of an afternoon, I was able to whittle it down to 91 lines by swapping out the procedural statements I&#8217;d written for more Ruby-ish idioms. The Game of Life has such a simple set of requirements (there are just four basic rules) that I didn&#8217;t have to think about what the program had to actually do and could instead concentrate on the language I was writing it in. I think this is going to be my go-to first project when learning a new language from now on.</p>

<p>After that, I started implementing the first few <a href="http://projecteuler.net/">Project Euler</a> problems (the first one is doable with just one line of ruby). It didn&#8217;t take long for the math to get hard enough that I was no longer concentrating on the language, though. So I stopped pretty soon into the problem set.</p>

<p>I&#8217;m not really sure what I&#8217;m going to do next. Possibly something like Tetris, but it&#8217;s all up in the air. But even with this limited exposure to the language, I&#8217;m starting to look at the code I&#8217;m writing day-to-day and saying &#8220;How would this be easier in Ruby?&#8221;. I think that&#8217;s pretty valuable and so far, I&#8217;m quite pleased with the language and want to get to know it better.</p>
]]></content:encoded>
			<wfw:commentRss>http://jameswilliams.me/developer/blog/2008/12/first-steps-with-ruby/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>signtool.exe and capicom.dll</title>
		<link>http://jameswilliams.me/developer/blog/2008/08/signtoolexe-and-capicomdll/</link>
		<comments>http://jameswilliams.me/developer/blog/2008/08/signtoolexe-and-capicomdll/#comments</comments>
		<pubDate>Mon, 25 Aug 2008 15:10:32 +0000</pubDate>
		<dc:creator>James Williams</dc:creator>
				<category><![CDATA[for_export]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[windows]]></category>

		<guid isPermaLink="false">http://jameswilliams.me/developer/blog/?p=67</guid>
		<description><![CDATA[[Ed: I'm changing things up today and writing about Windows. Obviously, the day job is involved.] I&#8217;ve been consistently running into a problem with signtool.exe refusing to work at random points throughout my day. It would complain that it requires capicom.dll v. 2.1.0.1 or higher. Despite the fact that I had several capicom.dll files on [...]]]></description>
			<content:encoded><![CDATA[<p><b><i>[Ed: I'm changing things up today and writing about Windows. Obviously, the day job is involved.]</i></b></p>

<p>I&#8217;ve been consistently running into a problem with signtool.exe refusing to work at random points throughout my day. It would complain that it requires capicom.dll v. 2.1.0.1 or higher. Despite the fact that I had several capicom.dll files on my system (no, I don&#8217;t know why) and they&#8217;re all v 2.1.0.1, signtool.exe would refuse to use them.</p>

<p>I believe (and here I really want to stress that I&#8217;m not speaking from a position of authority!) that the problem stemmed from the dll registration system. When I tried to register capicom.dll (any of them) manually, regsvr32 would fail and report &#8220;invalid access to memory location&#8221;.</p>

<p>Some poking around on Google revealed the cause: Windows XP SP2 added support for the modern processor&#8217;s <a href="http://en.wikipedia.org/wiki/NX_bit">No Execute Bit</a> (which prevents the execution of code in data segments of memory and provides some much needed security from buffer overrun attacks). Apparantly, Microsoft considers regsvr32 an attack vector or something and it won&#8217;t work correctly with this extra protection turned on.</p>

<p>So the answer was to disable it. <a href="http://support.microsoft.com/kb/875352">This Microsoft Knowledgebase article</a> describes the functionality (called Data Execution Prevention, or DEP, in Microspeak) and how to control it. I went with changing my boot.ini&#8217;s noexecute parameter to AlwaysOff. And signtool.exe magically works!</p>

<p>This is obviously not a &#8220;correct&#8221; solution as my system is suddenly more vulnerable to machine-pwning attacks. But it works and it lets me get back to doing my job. So I&#8217;m not going to spend any more time on it, despite the fact that I&#8217;m somewhat unhappy about how it all turned out.</p>

<p>Also: &#8220;capicom.dll&#8221; is possibly the most ridiculous file name I&#8217;ve ever seen. Every time I say it out loud, I want to punch myself in the face.</p>

<p><b><i>[Added 2008-08-27]: So, the symptoms are happening again. My fix didn&#8217;t actually fix it. It&#8217;s such an intermittent problem, that it&#8217;s hard to diagnose. I hate intermittent problems. Anyway. Bear this in mind before you go an disable the DEP support on your system.</i></b></p>
]]></content:encoded>
			<wfw:commentRss>http://jameswilliams.me/developer/blog/2008/08/signtoolexe-and-capicomdll/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SOAP on Apple Platforms: Still Painful</title>
		<link>http://jameswilliams.me/developer/blog/2008/08/soap-on-apple-platforms-still-painful/</link>
		<comments>http://jameswilliams.me/developer/blog/2008/08/soap-on-apple-platforms-still-painful/#comments</comments>
		<pubDate>Fri, 15 Aug 2008 13:40:25 +0000</pubDate>
		<dc:creator>James Williams</dc:creator>
				<category><![CDATA[for_export]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://jameswilliams.me/developer/blog/?p=65</guid>
		<description><![CDATA[A Cocoa-devmailing list thread popped up recently wondering how to make SOAP calls on an Apple platform. If you follow it through to the conclusion, you&#8217;ll learn that Patrick decided to roll his own: he&#8217;s got a base class which communicates with the server and triggers the actual serialization via simple string patterns or simple [...]]]></description>
			<content:encoded><![CDATA[<p>A Cocoa-dev<a href="http://lists.apple.com/archives/Cocoa-dev/2008/Aug/msg00982.html">mailing list thread</a> popped up recently wondering how to make SOAP calls on an Apple platform. If you follow it through to the conclusion, you&#8217;ll learn that Patrick decided to roll his own: he&#8217;s got a base class which communicates with the server and triggers the actual serialization via simple string patterns or simple xml parsing.</p>

<p>This is almost exactly what I&#8217;m doing. The biggest difference is that he&#8217;s using <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSXMLParser_Class/Reference/Reference.html">NSXMLParser</a> and I&#8217;m using <a href="http://code.google.com/p/touchcode/wiki/TouchXML">TouchXML</a>.</p>

<p>My question: how many times are developers going to have to write the same code over and over again in isolation before Apple adds a decent SOAP API to all of their platforms? Web services aren&#8217;t going away. Microsoft isn&#8217;t going away. And as long as developers are using Microsoft&#8217;s stack to write web services, we&#8217;re going to have to consume SOAP. Microsoft&#8217;s tools just make it far too trivial to do SOAP for it to fade into obscurity any time soon.</p>
]]></content:encoded>
			<wfw:commentRss>http://jameswilliams.me/developer/blog/2008/08/soap-on-apple-platforms-still-painful/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>HTTP POST via the CFNetwork stack</title>
		<link>http://jameswilliams.me/developer/blog/2008/08/http-post-via-the-cfnetwork-stack/</link>
		<comments>http://jameswilliams.me/developer/blog/2008/08/http-post-via-the-cfnetwork-stack/#comments</comments>
		<pubDate>Mon, 11 Aug 2008 23:36:22 +0000</pubDate>
		<dc:creator>James Williams</dc:creator>
				<category><![CDATA[for_export]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://jameswilliams.me/developer/blog/?p=41</guid>
		<description><![CDATA[Introduction Apple&#8217;s CFNetwork documentation leaves a lot to be desired. At one point while figuring out how to use it, I believe I went on a rant where I described its contents as &#8220;vicious lies&#8221;. Now that I&#8217;ve actually figured it out, I&#8217;ve revised my opinion. The documentation doesn&#8217;t lie, exactly. It omits. And what&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p><h3>Introduction</h3>
Apple&#8217;s <a href="http://developer.apple.com/documentation/Networking/Conceptual/CFNetwork/Introduction/chapter_1_section_1.html">CFNetwork documentation</a> leaves a lot to be desired. At one point while figuring out how to use it, I believe I went on a rant where I described its contents as &#8220;vicious lies&#8221;. Now that I&#8217;ve actually figured it out, I&#8217;ve revised my opinion. The documentation doesn&#8217;t lie, exactly. It omits. And what&#8217;s worse is that I couldn&#8217;t find anything on the Internet which told the entire story. So, I&#8217;m using this blog post to try to fill that gap. With a little bit of luck, Google will find me. And with a lot of luck I&#8217;m not posting incredibly inaccurate information here. So. Caveat: I&#8217;m still learning OS X programming. Don&#8217;t take this as gospel; take it as a good place to start. Also, I&#8217;ve removed all error checking for brevity. You&#8217;ll need to add it back. Good luck. (We&#8217;re all counting on you.)</p>

<p>I&#8217;m going to talk about sending a very basic HTTP post request to a server which authenticates with <a href="http://en.wikipedia.org/wiki/NTLM">NTLM</a> and then reading the response data. My example makes the request synchronously (because my application currently makes the request synchronously). You could modify this to use threads, or you could <a href="http://developer.apple.com/documentation/Networking/Conceptual/CFNetwork/CFStreamTasks/chapter_4_section_4.html#//apple_ref/doc/uid/TP30001132-CH6-SW19">use polling or run loops</a>. I&#8217;m not doing any of these, but I believe the ideas are basically the same. Refer to the documentation for more details.</p>

<p>The basic strategy is: <br/>
<ol>
<li>Build a CFHTTPMessageRef containing the HTTP message to POST</li>
<li>Send it to the server</li> 
<li>Read the response back from the server</li>
<li>See if authentication is required</li>
<li>Create a <i>new</i> CFHTTPMessageRef that looks exactly like the one created in step 1</li>
<li>Add authentication credentials to the new CFHTTPMessageRef</li>
<li>Send it to the server</li>
<li>Read the response back from the server</li>
<li>Repeat steps 4 through 9, as necessary</li> 
<li>Retrieve the HTTP body from the server&#8217;s response and hand it off to the caller for further processing.</li> 
</ol></p>

<p>In higher level frameworks (like Microsoft&#8217;s .Net or even Apple&#8217;s <a href="http://developer.apple.com/documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html">NSURLConnection</a>), a lot of these steps are done for you. Indeed, if you don&#8217;t need a feature of the CFNetwork stack, I highly recommend using NSURLConnection. Unfortunately, NSURLConnection cannot do NTLM authentication. So if you&#8217;re talking to a Windows web-server that&#8217;s expecting domain-level credentials, you&#8217;re stuck. It&#8217;s not so bad, though.</p>

<p><h3>Build a CFHTTPMessageRef</h3>
This is easy enough that I&#8217;m just going to point you at the code:
<code>
-(CFHTTPMessageRef)buildMessage
{
&nbsp;&nbsp;&nbsp;NSURL *myURL = [NSURL URLWithString:@"http://myurl.com"];
&nbsp;&nbsp;&nbsp;NSData *dataToPost = [[NSString stringWithString:@"POST Data It Doesn't Matter What It Is"] dataUsingEncoding:NSUTF8StringEncoding];
<br/>
&nbsp;&nbsp;&nbsp;//Create with the default allocator (NULL), a post request, 
&nbsp;&nbsp;&nbsp;//the URL, and pick either
&nbsp;&nbsp;&nbsp;//kCFHTTPVersion1_0 or kCFHTTPVersion1_1
&nbsp;&nbsp;&nbsp;CFHTTPMessageRef request = CFHTTPMessageCreateRequest(NULL, CSTR("POST"), (CFURLRef)myURL, kCFHTTPVersion1_1);
<br/>
&nbsp;&nbsp;&nbsp;CFHTTPMessageSetBody(request, (CFDataRef)dataToPost);
<br/>
&nbsp;&nbsp;&nbsp;//Unfortunately, this isn't smart enough to set reasonable headers for you
&nbsp;&nbsp;&nbsp;CFHTTPMessageSetHeaderFieldValue(request, CFSTR("HOST"), (CFStringRef)[myURL host]);
&nbsp;&nbsp;&nbsp;CFHTTPMessageSetHeaderFieldValue(request, CFSTR("Content-Length"), (CFStringRef)[NSString stringWithFormat:"%d", [dataToPost length]);
&nbsp;&nbsp;&nbsp;CFHTTPMessageSetHeaderFieldValue(request, CFSTR("Content-Type"), CFSTR("charset=utf-8"));
<br/>
&nbsp;&nbsp;&nbsp;return [NSMakeCollectable(request) autorelease];
}
</code>
</p>

<p>
<h3>Send it to the server and read back the response</h3>
This one might require a little more explanation. We&#8217;re going to <br/>
<ol>
<li>Create a <a href="http://developer.apple.com/documentation/CoreFoundation/Reference/CFReadStreamRef/Reference/reference.html">CFReadStream</a> for the request</li>
<li>Open the stream<li>
<li>While bytes are available on the stream, read them</li>
<li><b>Once there are no more bytes available, copy the kCFStreamPropertyHTTPResponseHeader property from the stream</b> (This is the part I can&#8217;t find in the documentation: reading bytes from the stream won&#8217;t actually give you everything. The read stream keeps the HTTP response header bytes for itself and makes it available as a property. This is <i>entirely</i> unintuitive, especially since the documentation insinuates that you can append the bytes from the read stream onto an empty CFHTTPMessageRef to get the response. You can&#8217;t do this, because reading the bytes from the stream doesn&#8217;t give you everything you need. :( Also, you have to copy the property from the stream after you&#8217;ve read all the bytes. Otherwise, it might not be there. Again, none of this is in the documentation (or, if it is, it&#8217;s hidden. I couldn&#8217;t find it after a few weeks of searching.))</li> 
<li>Use the bytes read from the stream AND the information from the kCFStreamPropertyHTTPResponseHeader to actually construct a CFHTTPMessageRef containing the response</li>
</ol>
<code>
-(CFHTTPMessageRef)performHTTPRequest:(CFHTTPMessageRef)request
{
&nbsp;&nbsp;&nbsp;CFReadStreamRef requestStream = CFReadStreamCreateForHTTPRequest(NULL, request);
&nbsp;&nbsp;&nbsp;CFReadStreamOpen(requestStream);
<br/>
&nbsp;&nbsp;&nbsp;NSMutableData *responseBytes = [NSMutableData data];
<br/>
&nbsp;&nbsp;&nbsp;CFIndex numBytesRead = 0 ;
&nbsp;&nbsp;&nbsp;do
&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;UInt8 buf[1024];
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;numBytesRead = CFReadStreamRead(requestStream, buf, sizeof(buf));
<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(numBytesRead > 0)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[responseBytes appendBytes:buf length:numBytesRead];
<br/>
&nbsp;&nbsp;&nbsp;} while(numBytesRead > 0);
<br/>
&nbsp;&nbsp;&nbsp;CFHTTPMessageRef response = (CFHTTPMessageRef)CFReadStreamCopyProperty(requestStream, kCFStreamPropertyHTTPResponseHeader);
&nbsp;&nbsp;&nbsp;CFHTTPMessageSetBody(response, (CFDataRef)responseBytes);
<br/>
&nbsp;&nbsp;&nbsp;CFReadStreamClose(requestStream);
CFRelease(requestStream);
<br/>
&nbsp;&nbsp;&nbsp;return [NSMakeCollectable(response) autorelease];
}
</code>
</p>

<p>
<h3>Adding Authentication to an HTTP Request</h3>
I&#8217;m going to skip to step 6 for a moment, just so I can use this method in the next step (which combines steps 5 &#8211; 9 in a single loop). This is actually pretty simple (the only wrinkle is that you HAVE to use CFNetwork code and can&#8217;t use NSURLConnection) so I&#8217;ll skip to the code.
<code>
-(void)addAuthenticationToRequest:(CFHTTPMessageRef)request withResponse:(CFHTTPMessageRef)response
{
&nbsp;&nbsp;&nbsp;CFHTTPAuthenticationRef authentication = CFHTTPAuthenticationCreateFromResponse(NULL, response);
&nbsp;&nbsp;&nbsp;[NSMakeCollectable(authentication) autorelease];
<br/>
&nbsp;&nbsp;&nbsp;CFStreamError err;
&nbsp;&nbsp;&nbsp;Boolean success = CFHTTPMessageApplyCredentials(request, authentication, CFSTR("username"), CFSTR("password"), &#038;err);
}
</code>
</p>

<p>
<h3>Putting It All Together</h3>
Now, we&#8217;re going to actually make the HTTP request in a loop. The loop will let us see if we need to authenticate. (Since good HTTP authentication uses a challenge-response mechanism, you have to make multiple requests. It&#8217;s a shame the libraries don&#8217;t wrap this up for us&#8230;) Once we have a response back, we&#8217;ll get the body and use NSLog to print it to the console.

<code>
-(void)magicHappens
{
&nbsp;&nbsp;&nbsp;CFHTTPMessageRef request = [self buildMessage];
&nbsp;&nbsp;&nbsp;CFHTTPMessageRef response = [self performHTTPRequest: request];
<br/>
&nbsp;&nbsp;&nbsp;UInt32 statusCode;
&nbsp;&nbsp;&nbsp;statusCode = CFHTTPMessageGetResponseStatusCode(response);
<br/>
&nbsp;&nbsp;&nbsp;//An HTTP status code of 401 or 407 indicates that authentication is &nbsp;&nbsp;&nbsp;//required I use an auth count to make sure we don't get stuck in an &nbsp;&nbsp;&nbsp;//infinite loop if our credentials are bad. Sometimes, making the &nbsp;&nbsp;&nbsp;//request more than once lets it go through. 
&nbsp;&nbsp;&nbsp;//I admit I don't know why.
<br/>
&nbsp;&nbsp;&nbsp;int authCount = 0;
&nbsp;&nbsp;&nbsp;while((statusCode == 401 || statusCode == 407) &#038;&#038; authCount < 3)
&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;request = [self buildMessage];
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[self addAuthenticationToRequest:request withResponse:response];
<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;response = [self performHTTPRequest: request];
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;statusCode = CFHTTPMessageGetResponseStatusCode;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;authCount++;
&nbsp;&nbsp;&nbsp;}
<br/>
&nbsp;&nbsp;&nbsp; NSData *responseBodyData = [(NSData*)CFHTTPMessageCopyBody(response) autorelease];
&nbsp;&nbsp;&nbsp;NSString *responseBody = [[[NSString alloc] initWithData:responseBodyData encoding:NSUTF8StringEncoding] autorelease];
<br/>
&nbsp;&nbsp;&nbsp;NSLog(responseBody);
}
</code>
</p>

<p><h3>In Conclusion</h3>
I hope that was both clear and helpful. I tried to keep my commentary to a minimum because I&#8217;m a better coder than I am a writer (though, I&#8217;m not a particularly good coder when it comes to ObjC and Cocoa/Carbon). I wrote this inside a WordPress edit window so I could keep formatting mistakes to a minimum; but that means I haven&#8217;t actually compiled it. It may not work as presented; but it should be enough to get you the jist of what to do.</p>

<p>&lt;</p>

<p>p>References:</p>

<ul>
<li><a href="http://developer.apple.com/documentation/Networking/Conceptual/CFNetwork/Introduction/chapter_1_section_1.html">CFNetworkProgramming Guide</a></li>
<li><a href="http://developer.apple.com/documentation/CoreFoundation/Reference/CFMessageRef/Reference/reference.html">CFHTTPMessage</a></li>
<li><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSURL_Class/index.html">NSURL</a></li>
<li><a href="http://developer.apple.com/documentation/CoreFoundation/Reference/CFReadStreamRef/index.html">CFReadStream</a></li>
<li><a href="http://developer.apple.com/documentation/CoreFoundation/Reference/CFHTTPAuthenticationRef/index.html">CFHTTPAuthentication</a></li>
<li><a href="http://jameswilliams.me/developer/blog/2008/08/autoreleasing-cftyperefs/">My blog post about NSMakeCollectable</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://jameswilliams.me/developer/blog/2008/08/http-post-via-the-cfnetwork-stack/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Autoreleasing CFTypeRefs</title>
		<link>http://jameswilliams.me/developer/blog/2008/08/autoreleasing-cftyperefs/</link>
		<comments>http://jameswilliams.me/developer/blog/2008/08/autoreleasing-cftyperefs/#comments</comments>
		<pubDate>Fri, 08 Aug 2008 13:28:11 +0000</pubDate>
		<dc:creator>James Williams</dc:creator>
				<category><![CDATA[for_export]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://jameswilliams.me/developer/blog/?p=35</guid>
		<description><![CDATA[Over on Apple&#8217;s Cocoa-dev mailing list, Peter Lewis asked if there was a way to release a CGImageRef. It turns out that this is trivial. In a program which isn&#8217;t garbage collected, you can just cast any CFTypeRef to an id and autorelease it: [(id)myCarbonObject autorelease]. If your app might be run under the garbage [...]]]></description>
			<content:encoded><![CDATA[<p>Over on Apple&#8217;s <a href="http://lists.apple.com/mailman/listinfo/cocoa-dev">Cocoa-dev mailing list</a>, Peter Lewis asked <a href="http://lists.apple.com/archives/Cocoa-dev/2008/Aug/msg00388.html">if there was a way to release a CGImageRef</a>. It turns out that this is trivial. In a program which isn&#8217;t garbage collected, <a href="http://lists.apple.com/archives/Cocoa-dev/2008/Aug/msg00393.html">you can just cast any CFTypeRef to an <code>id</code> and autorelease it</a>: <code>[(id)myCarbonObject autorelease]</code>.</p>

<p>If your app might be run under the garbage collector, though, that&#8217;s not good enough. You need to <a href="http://lists.apple.com/archives/Cocoa-dev/2008/Aug/msg00465.html">run <code>NSMakeCollectable</code> on the CFTypeRef</a>: <code>[NSMakeCollectable(myCarbonObject) autorelease]</code>.</p>

<p>Fortunately, even though NSMakeCollectable is new in OS X 10.5 Leopard, it&#8217;s an inline function and is <a href="http://lists.apple.com/archives/Cocoa-dev/2008/Aug/msg00483.html">therefore usable in apps targeting 10.4 Tiger</a>.</p>

<p>I was excited to learn this because I had to jump through some hoops to release Carbon objects in the CFNetwork code I wrote recently. I can now happily just autorelease it.</p>
]]></content:encoded>
			<wfw:commentRss>http://jameswilliams.me/developer/blog/2008/08/autoreleasing-cftyperefs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Quick Status Update</title>
		<link>http://jameswilliams.me/developer/blog/2008/08/quick-status-update/</link>
		<comments>http://jameswilliams.me/developer/blog/2008/08/quick-status-update/#comments</comments>
		<pubDate>Wed, 06 Aug 2008 01:18:27 +0000</pubDate>
		<dc:creator>James Williams</dc:creator>
				<category><![CDATA[for_export]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://jameswilliams.me/developer/blog/?p=33</guid>
		<description><![CDATA[After several weeks of playing with it, pouring over Apple&#8217;s somewhat cryptic documentation, and trying every combination of every flag and order or operation I could think of&#8230;I&#8217;m finally making SOAP requests with CFHTTPMessage instead of WSMethodInvocation. I can now make the exact same requests (Ping, SearchListLoad, DataFormLoad) that I could before I started all [...]]]></description>
			<content:encoded><![CDATA[<p>After several weeks of playing with it, pouring over Apple&#8217;s somewhat cryptic documentation, and trying every combination of every flag and order or operation I could think of&#8230;I&#8217;m finally making SOAP requests with CFHTTPMessage instead of WSMethodInvocation.</p>

<p>I can now make the exact same requests (Ping, SearchListLoad, DataFormLoad) that I could before I started all of this&#8230;but I&#8217;m not using any Mac-only APIs. (It&#8217;s still OS X only, of course.)</p>

<p>So I consider that a victory. At some point in the near future, I will write a blog post about just how to make an HTTP post using CFHTTPMessage. I need to get this information into Google so no one else has to spend weeks figuring it out on their own.</p>

<p>But I&#8217;m just happy to have it working. I can now move on to the much easier and only slightly less-interesting task of implementing the rest of the web service methods I need.</p>
]]></content:encoded>
			<wfw:commentRss>http://jameswilliams.me/developer/blog/2008/08/quick-status-update/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Commenting Approach</title>
		<link>http://jameswilliams.me/developer/blog/2008/07/commenting-approach/</link>
		<comments>http://jameswilliams.me/developer/blog/2008/07/commenting-approach/#comments</comments>
		<pubDate>Wed, 30 Jul 2008 13:39:53 +0000</pubDate>
		<dc:creator>James Williams</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[meta]]></category>

		<guid isPermaLink="false">http://jameswilliams.me/developer/blog/?p=28</guid>
		<description><![CDATA[I&#8217;m unhappy with the FriendFeed comment plugin. I&#8217;m not knocking its author: I think it&#8217;s the best that can be done with the current FF API. But it just doesn&#8217;t work well as the only way to comment on a post. So I&#8217;ve integrated Disqus commenting. I think I like it better than the standard [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m unhappy with the FriendFeed comment plugin. I&#8217;m not knocking its author: I think it&#8217;s the best that can be done with the current FF API. But it just doesn&#8217;t work well as the only way to comment on a post.</p>

<p>So I&#8217;ve integrated <a href="http://disqus.com/">Disqus</a> commenting. I think I like it better than the standard WP commenting format. I&#8217;ll just have to see how it goes. Right now, it&#8217;s set up so registered Disqus users can comment immediately; unregistered folks will be tossed into a moderation queue.</p>

<p>I understand that this is mostly academic, of course. I don&#8217;t expect to drive much discussion. But it&#8217;s still amusing to try out.</p>
]]></content:encoded>
			<wfw:commentRss>http://jameswilliams.me/developer/blog/2008/07/commenting-approach/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Quick Note Regarding NSURLConnection and NTLM</title>
		<link>http://jameswilliams.me/developer/blog/2008/07/quick-note-regarding-nsurlconnection-and-ntlm/</link>
		<comments>http://jameswilliams.me/developer/blog/2008/07/quick-note-regarding-nsurlconnection-and-ntlm/#comments</comments>
		<pubDate>Wed, 30 Jul 2008 12:15:05 +0000</pubDate>
		<dc:creator>James Williams</dc:creator>
				<category><![CDATA[for_export]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Mac]]></category>
		<category><![CDATA[networking]]></category>

		<guid isPermaLink="false">http://jameswilliams.me/developer/blog/?p=26</guid>
		<description><![CDATA[As near as I can tell, NSURLConnection does not support NTLM authentication. So, if you are considering implementing something using NSURLConnection: take a moment to make sure you will never talk to a server expecting you to authenticate via NTLM (which is IIS as near as I can tell) before you waste two evenings doing [...]]]></description>
			<content:encoded><![CDATA[<p>As near as I can tell, <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSURLConnection_Class/Reference/Reference.html#">NSURLConnection</a> does not support <a href="http://www.innovation.ch/personal/ronald/ntlm.html">NTLM authentication</a>. </p>

<p>So, if you are considering implementing something using NSURLConnection: take a moment to make sure you will never talk to a server expecting you to authenticate via NTLM (which is <a href="http://en.wikipedia.org/wiki/Internet_Information_Services">IIS</a> as near as I can tell) before you waste two evenings doing it.</p>

<p>That being said, NSURLConnection is really cool and I wish I could use it.</p>

<p>For my next trick, figure out how to do this with <a href="http://developer.apple.com/documentation/CoreFoundation/Reference/CFMessageRef/Reference/reference.html">CFHTTPMessage</a>, which does provide <code>kCFHTTPAuthenticationSchemeNTLM</code> for authentication.</p>

<p>*sigh*</p>
]]></content:encoded>
			<wfw:commentRss>http://jameswilliams.me/developer/blog/2008/07/quick-note-regarding-nsurlconnection-and-ntlm/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Accessing Instance Variables in ObjC</title>
		<link>http://jameswilliams.me/developer/blog/2008/07/accessing-instance-variables-in-objc/</link>
		<comments>http://jameswilliams.me/developer/blog/2008/07/accessing-instance-variables-in-objc/#comments</comments>
		<pubDate>Mon, 28 Jul 2008 16:07:17 +0000</pubDate>
		<dc:creator>James Williams</dc:creator>
				<category><![CDATA[for_export]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Mac]]></category>

		<guid isPermaLink="false">http://jameswilliams.me/developer/blog/?p=22</guid>
		<description><![CDATA[A few months ago, I spent a few days searching around to see if I could figure out how to determine the instance variables on a class in ObjC. I knew it was possible because Key-Value-Coding will do it if it can't find the well-named accessor methods for a keypath. But all of my searching proved fruitless and I went with a different method of accomplishing my end goal.]]></description>
			<content:encoded><![CDATA[<p>A few months ago, I spent a few days searching around to see if I could figure out how to determine the instance variables on a class in ObjC. I knew it was possible because Key-Value-Coding will do it if it can&#8217;t find the well-named accessor methods for a keypath. But all of my searching proved fruitless and I went with a different method of accomplishing my end goal.</p>

<p>Well, it looks like the answer has appeared in my feed reader on an entry from Matt Gallagher&#8217;s Cocoa With Love blog:<a href="http://cocoawithlove.com/2008/07/key-value-information.html">Key Value Information</a>.</p>

<p>I&#8217;ll just quote him (from the very end of his post):
<blockquote>
Similarly, if you were interested in key value coding that may access instance variables directly, you could use class <code>_getClassVariable</code> to get any instance variables with the same name as the key.
</blockquote>
</p>

<p>The leading underscore on the method name indicates that it&#8217;s undocumented and internal to Apple&#8217;s implementation. So using it isn&#8217;t necessarily the smartest thing in the world. But sometimes, you&#8217;ve gotta do what you gotta do.</p>

<p>Now that I have a method name, I&#8217;ll probably resume my search for information on this technique and will obviously report any findings here.</p>

<p><b>Update: July 29, 2008</b></p>

<p>I did some poking around in the <a href="http://developer.apple.com/documentation/Cocoa/Reference/ObjCRuntimeRef/Reference/reference.html">Objective-C 2.0 Runtime Reference</a> and found the <code>class_copyIvarList</code> function which returns an array of <code>Ivar</code> types. <code>Ivar</code> is a struct containing fields for the ivar_name, ivar_type, and offset. Combined with <code>object_getIvar</code>, I think I would have been much closer to achieving my original goal. </p>

<p>I don&#8217;t know why I couldn&#8217;t find this before. I guess I was just looking in the wrong places.</p>
]]></content:encoded>
			<wfw:commentRss>http://jameswilliams.me/developer/blog/2008/07/accessing-instance-variables-in-objc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

