robbiehanson / CocoaHTTPServer

A small, lightweight, embeddable HTTP server for Mac OS X or iOS applications
Other
5.61k stars 1.31k forks source link

HTTPRouter #23

Open Nub opened 12 years ago

Nub commented 12 years ago

Hi Robbie!

I was looking into web services for OBJ-C recently and someone mentioned this, after I had found froth kit, I was deciding to go and try to modernize froth kit, but it was a mess and super old. Gladly I was able to find this awesome project which seems to be nice light weight and active!!

So now my presentation I worked on an awesome sub Class of HTTPConnection and added blocked based routing! It makes writing a server crazy simple now! I would love to have a chat and see where you are and how we can move all of CHS more in this direction, next I was going to add a way to link in automatic session management and basic user logins, I'm going for a lean and lightweight webstack for making api's and w/e else you can think of.

So without further ado here is simple server using my Routing class

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Initalize our http server
    httpServer = [[HTTPServer alloc] init];
    [httpServer setType:@"_http._tcp."];                //Bounjour
    [httpServer setPort:12345];
    [httpServer setConnectionClass:[HTTPRouter class]]; // User router

    //Setup some sample routes

    //Regex? o.O and query params why not?
    [HTTPRouter handleRoute:@"[tb]est" withBlock:^(HTTPMessage* message)
    {
        return [NSString stringWithFormat:@"Test Params:%@", message.params];
    }];

    //Static route
    [HTTPRouter handleRoute:@"test/hello" withBlock:^(HTTPMessage* message)
    {
        return @"HAHAHAH WHAT ARE U DOING? Hello world is so old school";
    }];

    //Dynamic routes, stored as params :key, sinartra anyone? oops don't tell them i did this
    [HTTPRouter handleRoute:@"laughs/:times" withBlock:^(HTTPMessage* message)
    {
        NSInteger times = [[message.params valueForKey:@"times"] integerValue];
        NSMutableString *response = [NSMutableString string];

        for (int i = 0; i < times; i++)
        {
            [response appendString:@"HA"];
        }

        return response;
    }];

    // Start the server (and check for problems)
    NSError *error;
    BOOL success = [httpServer start:&error];
    NSAssert(success, @"Error starting HTTP Server: %@", error);

}

And here is it's current implementation https://gist.github.com/2650888

I would love for this to become part of CocoaHTTPServer and maybe influence it's future! I look forward to talking with you! I would also like to inform you, this was whipped together, so there are still some things to resolve, and bug test. Such as freeing the yucky global used to store the blocks and their routes. But this can all be addressed :) It may also be useful to note the return types currently supported by blocks are NSString, NSData, NSDictionary, NSArray, the later 2 will be serialized using NSJSONSerialization.

robbiehanson commented 12 years ago

This is an idea that should absolutely become part of CocoaHTTPServer. I've seen others implement similar extensions in the past:

Blackbox (https://github.com/mattpat/Blackbox) was written on top of version 1.X of CHS. RoutingHTTPServer (https://github.com/mattstevens/RoutingHTTPServer) is more recent.

I've been meaning to add something like this into the repository. Probably adding it as an optional extension.

Take a look at RoutingHTTPServer and tell me what you think of it's architecture. (I haven't had a chance to look deeply into it yet.)

-Robbie Hanson

On May 9, 2012, at 8:36 PM, Zach Thayer wrote:

Hi Robbie!

I was looking into web services for OBJ-C recently and someone mentioned this, after I had found froth kit, I was deciding to go and try to modernize froth kit, but it was a mess and super old. Gladly I was able to find this awesome project which seems to be nice light weight and active!!

So now my presentation I worked on an awesome sub Class of HTTPConnection and added blocked based routing! It makes writing a server crazy simple now! I would love to have a chat and see where you are and how we can move all of CHS more in this direction, next I was going to add a way to link in automatic session management and basic user logins, I'm going for a lean and lightweight webstack for making api's and w/e else you can think of.

So without further ado here is how my class works https://gist.github.com/2650862

And here is it's current implementation https://gist.github.com/2650888

I would love for this to become part of CocoaHTTPServer and maybe influence it's future! I look forward to talking with you!


Reply to this email directly or view it on GitHub: https://github.com/robbiehanson/CocoaHTTPServer/issues/23

Kefka commented 12 years ago

There is at least one other implementation of a Routing Server based on CHS. Some info on it can be had here. I believe this was one of the more recently active projects. In any case, would very much like to see routing support rolled into CHS. :)

Nub commented 12 years ago

Hmm I did not know, however after a brief look through at, what time is it right now? 3:30am ack. Umm well i should review it further, but from what I see I have implemented nearly the same exact thing in ~230lines of code and a single subclass of HTTPConnection and a slight external mod of HTTPMessage. Mine is meant to me as simple as possible, it allows for regex without any special characters to signify it. I do plan to continue mine forward, or work with someone on a awesome solution, but I'll have to do some more reviewing first.

My goal was to add a routing layer without altering CHS and keeping it as simple as possible, which I managed to do :)

I'd appreciate any feedback :)

mattstevens commented 12 years ago

I would love to see a capability like this brought into the main project. I'll leave it to others to determine how best to implement it, but if there is anything useful in my extension you are welcome to it. I've been using the code to drive a DLNA media server for about a year and been pretty happy with it.

Nub commented 12 years ago

Hey matt thanks for the response! Your implementation does look rather swell, however I feel if CHS was target a bit more to such solutions we could optimize it all and minimize the source footprint, like tailoring HTTPMessage or tweaking the original design in some way to not have to layer in a bunch of new code. Perhaps this router or yours or something new all together replaces a module, I am not sure.

Robbie, if you get some free time to have a chat let me know, I'd be interested in helping to design and implement a solution that makes everyone happy :)

I think we should all start by compiling a list of features we would like to see in future versions of CHS.

Here is my list, somewhat in order of preference.

My wanted features

mAu888 commented 12 years ago

Very good. Thats what I needed for a prototype I'm developing.

Nub commented 12 years ago

Glad I could help, let me know if you have any suggestions/changes

mAu888 commented 12 years ago

I forked your gist and pushed an update. I had the problem, that when adding two routes foo and foo/:id, the app sometimes crashed when calling /foo. This was because it matched the route foo/:id for the requested url /foo.

Now I added a feature, that the routes will be evaluated in the order they have been added.

https://gist.github.com/3104696

Nub commented 12 years ago

Oh, awesome! And thanks for the fix! If I ever get free time again I'm going to fork this project and migrate it in.