Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

An embeddable Cocoa HTTP server, powered by the CocoaHTTPServer project.

branch: master
Octocat-spinner-32 Blackbox.xcodeproj Removed some unnecessary constants November 28, 2010
Octocat-spinner-32 English.lproj A few bugfixes to the demo project October 19, 2010
Octocat-spinner-32 .gitignore clean start March 15, 2010
Octocat-spinner-32 AsyncSocket.h Updated to latest trunk version of CocoaHTTPServer August 24, 2010
Octocat-spinner-32 AsyncSocket.m Updated to latest trunk version of CocoaHTTPServer August 24, 2010
Octocat-spinner-32 BBConnection.h Added basis of HaleBopp responder August 25, 2010
Octocat-spinner-32 BBConnection.m Fixed a couple of small potential leaks September 09, 2010
Octocat-spinner-32 BBDataResponse.h Updated to latest trunk version of CocoaHTTPServer August 24, 2010
Octocat-spinner-32 BBDataResponse.m Updated to latest trunk version of CocoaHTTPServer August 24, 2010
Octocat-spinner-32 BBDemoController.h completed demo project for Blackbox March 16, 2010
Octocat-spinner-32 BBDemoController.m Modified blocks syntax so the block is always the last parameter September 07, 2010
Octocat-spinner-32 BBDemoLiveResponder.h completed demo project for Blackbox March 16, 2010
Octocat-spinner-32 BBDemoLiveResponder.m completed demo project for Blackbox March 16, 2010
Octocat-spinner-32 BBDirectoryResponder.h Added support for a directory responder to create a more "conventiona… September 07, 2010
Octocat-spinner-32 BBDirectoryResponder.m Added support for a directory responder to create a more "conventiona… September 07, 2010
Octocat-spinner-32 BBRequest.h Added support for a directory responder to create a more "conventiona… September 07, 2010
Octocat-spinner-32 BBRequest.m Added support for a directory responder to create a more "conventiona… September 07, 2010
Octocat-spinner-32 BBResponder.h Added basis of HaleBopp responder August 25, 2010
Octocat-spinner-32 BBServer.h Modified blocks syntax so the block is always the last parameter September 07, 2010
Octocat-spinner-32 BBServer.m Modified blocks syntax so the block is always the last parameter September 07, 2010
Octocat-spinner-32 Blackbox-Info.plist built beginnings of a demonstration project to go with Blackbox March 16, 2010
Octocat-spinner-32 Blackbox_Prefix.pch built beginnings of a demonstration project to go with Blackbox March 16, 2010
Octocat-spinner-32 DDData.h built beginnings of a demonstration project to go with Blackbox March 16, 2010
Octocat-spinner-32 DDData.m Removed dependency on SSCrypto/libcrypto, created target for HaleBopp August 24, 2010
Octocat-spinner-32 DDNumber.h built beginnings of a demonstration project to go with Blackbox March 16, 2010
Octocat-spinner-32 DDNumber.m built beginnings of a demonstration project to go with Blackbox March 16, 2010
Octocat-spinner-32 DDRange.h built beginnings of a demonstration project to go with Blackbox March 16, 2010
Octocat-spinner-32 DDRange.m built beginnings of a demonstration project to go with Blackbox March 16, 2010
Octocat-spinner-32 HBCDemoController.h A few bugfixes to the demo project October 19, 2010
Octocat-spinner-32 HBCDemoController.m A few bugfixes to the demo project October 19, 2010
Octocat-spinner-32 HBDemoController.h HaleBopp implementation that will likely be entirely thrown out October 19, 2010
Octocat-spinner-32 HBDemoController.m HaleBopp implementation that will likely be entirely thrown out October 19, 2010
Octocat-spinner-32 HBResponder.h Removed some unnecessary constants November 28, 2010
Octocat-spinner-32 HBResponder.m Added channel-based push model in HaleBopp October 19, 2010
Octocat-spinner-32 HBResponderDelegate.h Added channel-based push model in HaleBopp October 19, 2010
Octocat-spinner-32 HTTPAsyncFileResponse.h Updated to latest trunk version of CocoaHTTPServer August 24, 2010
Octocat-spinner-32 HTTPAsyncFileResponse.m Updated to latest trunk version of CocoaHTTPServer August 24, 2010
Octocat-spinner-32 HTTPAuthenticationRequest.h built beginnings of a demonstration project to go with Blackbox March 16, 2010
Octocat-spinner-32 HTTPAuthenticationRequest.m Updated to latest trunk version of CocoaHTTPServer August 24, 2010
Octocat-spinner-32 HTTPConnection.h Updated to latest trunk version of CocoaHTTPServer August 24, 2010
Octocat-spinner-32 HTTPConnection.m Updated to latest trunk version of CocoaHTTPServer August 24, 2010
Octocat-spinner-32 HTTPDynamicFileResponse.h Updated to latest trunk version of CocoaHTTPServer August 24, 2010
Octocat-spinner-32 HTTPDynamicFileResponse.m Updated to latest trunk version of CocoaHTTPServer August 24, 2010
Octocat-spinner-32 HTTPResponse.h Updated to latest trunk version of CocoaHTTPServer August 24, 2010
Octocat-spinner-32 HTTPResponse.m Updated to latest trunk version of CocoaHTTPServer August 24, 2010
Octocat-spinner-32 HTTPServer.h Added support for localhost-only servers August 25, 2010
Octocat-spinner-32 HTTPServer.m Added support for localhost-only servers August 25, 2010
Octocat-spinner-32 HaleBopp Channel Demo-Info.plist Added channel-based push model in HaleBopp October 19, 2010
Octocat-spinner-32 HaleBopp Demo-Info.plist Added basis of HaleBopp responder August 25, 2010
Octocat-spinner-32 README.md Modified blocks syntax so the block is always the last parameter September 07, 2010
Octocat-spinner-32 comet.html Fleshed out HaleBopp (Comet) example project August 25, 2010
Octocat-spinner-32 main.m built beginnings of a demonstration project to go with Blackbox March 16, 2010
Octocat-spinner-32 off.tiff built beginnings of a demonstration project to go with Blackbox March 16, 2010
Octocat-spinner-32 on.tiff built beginnings of a demonstration project to go with Blackbox March 16, 2010
README.md

Blackbox

Blackbox is an embeddable Cocoa HTTP server -- sort of: strictly speaking, it's a set of classes that wraps the terrific CocoaHTTPServer project, which deserves all of the credit for the code that does the heavy-lifting. Blackbox allows you to associate HTTP resources with Cocoa "responder" objects (kind of like Twisted Web does for Python) rather than files on your filesystem.

With Blackbox, you can create personal file sharers in a snap, write applications that communicate with each other over HTTP, and easily create web control interfaces for headless applications.

Want a demo? Check out this (old) video, which shows how you can build a small personal web server in under 15 minutes with Blackbox. (Note: don't rely on the API demonstrated in the video; the project works more or less the same way, but the method names have changed considerably since the video was created).

Documentation

Documentation is forthcoming, but not yet available. :) Here are the basics though:

You create your server by alloc/init'ing a BBServer object, which inherits most of its functionality from the HTTPServer class (part of CocoaHTTPServer). See HTTPServer.h for configuration options (ie, server port, Bonjour service name/type, etc.). Once your server is configured to your liking, set it into motion with the start: method.

When a request comes in, Blackbox looks for the appropriate "responder" object to handle the request. A responder (in this context) is any Objective-C object that conforms to the BBResponder protocol (in BBResponder.h) -- currently, this is only the handleRequest: method. You associate responders with URL paths using BBServer's setResponder:forPath: method.

For example, if I wanted the object myObject to handle any calls below /coolbeans, I would do this:

[myServer setResponder:myObject forPath:@"/coolbeans"];

Now, requests to /coolbeans, /coolbeans/yummy, and /coolbeans/photos/garbanzo.jpg would all be directed to myObject's handleRequest: method. You respond to a request using either the setResponseBody: or setResponseString: method of the provided BBRequest object. You use the BBRequest object to get information (ie, POST params) out of the request, and to change other properties of the response, including content type and HTTP status code -- see BBRequest.h for more details. The response is sent when your responder's handleRequest: method returns.

Requests to paths without an associated responder object are tacitly ignored by default, and given a response of "200 OK." If you'd like, you can set a "catch-all" responder using the setDefaultResponder: method.

If you want to play around with it, start with the Blackbox Demo app, included in the project. Please note that the demo application links against the SystemConfiguration framework; this is not necessary to use Blackbox, the framework is only used to get the computer name to display as the placeholder text for "Bonjour Name." This value is obtained automatically if you set the Bonjour name to an empty string (@"").

Using Blocks as Handlers

If you're using Blackbox on 10.6+, you can also use blocks to respond to requests rather than having dedicated responder objects:

[myServer setHandlerForPath:@"/coolbeans" handler:^(BBRequest *theRequest){
    // do stuff with theRequest
}];

Note, however, that block handlers are always processed before responders. It is generally inadvisable to use a mixed environment of responder objects and block handlers, but if you do, it is strongly recommended that handlers and responders not share common path prefixes. For example:

[myServer setHandlerForPath:@"/generic" handler:^{...}];
[myServer setResponder:myObj forPath:@"/generic/specific"];

In the above, requests made to /generic/specific would in fact be handled by the block handler, not the myObj responder, because /generic/specific does indeed fall underneath /generic, and the handlers are searched for a match first. For this reason, setting a default handler (with setDefaultHandler:, like setDefaultResponder:) will prevent any responder from ever being used.

Projects That Use Blackbox

Currently the only known project that uses Blackbox is Bowtie -- my themeable iTunes controller -- which uses Blackbox to communicate with Bowtie for iPhone and other remote clients.

Do you use Blackbox in your project? Drop me a note at m@13bold.com, and I'll add you to this list!

License

Blackbox is licensed under the MIT license, excerpted below.

Copyright (C) 2010 Matt Patenaude.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

This project is a wrapper around the CocoaHTTPServer project, by Deusty Designs.

Copyright (c) 2006, Deusty Designs, LLC
All rights reserved.

Redistribution and use of this software in source and binary forms,
with or without modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above
  copyright notice, this list of conditions and the
  following disclaimer.

* Neither the name of Desuty Designs nor the names of its
  contributors may be used to endorse or promote products
  derived from this software without specific prior
  written permission of Deusty Designs, LLC.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Something went wrong with that request. Please try again.