Skip to content

Commit

Permalink
initial import
Browse files Browse the repository at this point in the history
  • Loading branch information
face committed Jun 15, 2009
0 parents commit 1496141
Show file tree
Hide file tree
Showing 6 changed files with 4,336 additions and 0 deletions.
29 changes: 29 additions & 0 deletions LICENSE
@@ -0,0 +1,29 @@
#
# Copyright (c) 2009, Rama McIntosh All rights reserved.
#
# Redistribution and use 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.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of Rama McIntosh nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# 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.
#
50 changes: 50 additions & 0 deletions MongooseDaemon.h
@@ -0,0 +1,50 @@
//
// MongooseDaemon.h
//
// Created by Rama McIntosh on 3/4/09.
// Copyright Rama McIntosh 2009. All rights reserved.
//


//
// Copyright (c) 2009, Rama McIntosh All rights reserved.
//
// Redistribution and use 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.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of Rama McIntosh nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// 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.
//
//

#import "mongoose.h"

@interface MongooseDaemon : NSObject {
NSThread *httpThread;
struct mg_context *mongoose;
}

- (void)startMongooseDaemon:(NSString *)ports;
- (void)stopMongooseDaemon;

@end
88 changes: 88 additions & 0 deletions MongooseDaemon.m
@@ -0,0 +1,88 @@
//
// MongooseDaemon.m
//
// Created by Rama McIntosh on 3/4/09.
// Copyright Rama McIntosh 2009. All rights reserved.
//

//
// Copyright (c) 2009, Rama McIntosh All rights reserved.
//
// Redistribution and use 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.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of Rama McIntosh nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// 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.
//

// MongooseDaemon is a small wrapper to make ingetrating mongoose
// with iPhone apps super easy

#import "MongooseDaemon.h"

//#define DOCUMENTS_FOLDER [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]
#define DOCUMENTS_FOLDER NSHomeDirectory()

@implementation MongooseDaemon

- (void)threadMain
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSRunLoop *runLoop = [NSRunLoop currentRunLoop];

[runLoop addPort:[NSMachPort port] forMode:NSDefaultRunLoopMode];
while(true) {
[runLoop run];
}
[pool release];
}

- (void)startHTTP:(NSString *)ports
{
mongoose = mg_start(); // Start Mongoose serving thread
mg_set_option(mongoose, "root", [DOCUMENTS_FOLDER UTF8String]); // Set document root
mg_set_option(mongoose, "ports", [ports UTF8String]); // Listen on port XXXX
//mg_bind_to_uri(mongoose, "/foo", &bar, NULL); // Setup URI handler

// Now Mongoose is up, running and configured.
// Serve until somebody terminates us
NSLog(@"Server should be running");
for (;;)
getchar();
}

- (void)startMongooseDaemon:(NSString *)ports;
{
httpThread = [[NSThread alloc] initWithTarget:self selector:@selector(threadMain) object:nil];
[httpThread start];
[self performSelector:@selector(startHTTP:) onThread:httpThread withObject:ports waitUntilDone:NO];
}

- (void)stopMongooseDaemon
{
mg_stop(mongoose);
[httpThread cancel];
[httpThread release];
}

@end
80 changes: 80 additions & 0 deletions README.rdoc
@@ -0,0 +1,80 @@
MongooseDaemon
===============

MongooseDaemon is an objective C wrapper around
the embedable mongoose http server for iPhone
development. It is offered under a BSD
style license.

MongooseDaemon is ideal for both embedded http services
as well as simply debugging your iPhone applications
file structure.

Mongoose is a lightweight embedable http server
written by Sergey Lyubka and offered under
a BSD style license. More information on
mongoose can be found at the project's
site here: http://code.google.com/p/mongoose/

NOTE: The mongoose.h and mongoose.c are taken
directly from mongoose-2.6 and are unmodified.
They are simply forked in this package for
convenience and stability.

Usage
=======


Clone the mongoose directory and add it to your X-Code project.

Then you can start it within one of you classes to
provide http access to your iPhone application.


For example, to start MongooseDaemon when your application
starts on port 8080:

Add the following to MyAppDelegate.h:
...
#import "MongooseDaemon.h"
....
@class MongooseDaemon

@interface MyAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {
...
MongooseDaemon *mongooseDaemon;
...
}

And add the following to MyAppDelegate.m:
...
@implementation LightyAppDelegate
...
- (void)applicationDidFinishLaunching:(UIApplication *)application {
mongooseDaemon = [[MongooseDaemon alloc] init];
[mongooseDaemon startMongooseDaemon:@"8080"];

...
- (void)dealloc {
...
[mongooseDaemon stopMongooseDaemon];
[mongooseDaemon release];
...
}

And that's it...an embeded http server with only
about 9 lines of code!

TODO
=======
1) Add a helper method to return the server's URL for
convience

2) Add support for some of mongoose's rich feature set.

3) Investigate if there is anyway to server files over endge/3g instead of just WiFi.

==============

Copyright (c) 2008 Rama McIntosh
Released under the BSD license found in the file LICENSE

0 comments on commit 1496141

Please sign in to comment.