CCLHTTPServer 0.9.0

CCLHTTPServer 0.9.0

TestsTested
LangLanguage Obj-CObjective C
License BSD
ReleasedLast Release Dec 2014

Maintained by Kyle Fuller.


Downloads

Total1,231
Week0
Month0

Installs

Apps75
Test Targets1
powered by Segment

GitHub

Stars39
Watchers7
Forks9
Issues1
Contributors1
Pull Requests0

Code

Files7
LOCLines of Code 367


CCLHTTPServer is a simple HTTP library server for iOS and OS X.

Usage

You can create a basic HTTP server listening on a port using the CCLHTTPServer class, simply pass it a handler and it will start listening.

CCLHTTPServer *server = [[CCLHTTPServer alloc] initWithInterface:nil port:8080 handler:^id<CCLHTTPServerResponse>(id<CCLHTTPServerRequest> request) {
    NSDictionary *headers = @{
        @"Content-Type": @"text/plain; charset=utf8",
    };

    NSData *body = [@"Hello World" dataUsingEncoding:NSUTF8StringEncoding];

    return [[CCLHTTPServerResponse alloc] initWithStatusCode:200 headers:headers body:body];
}];

Your handler will accept a request conforming to the CCLHTTPServerRequest protocol. You must return an object which conforms to the CCLHTTPServerResponse protocol, we've provided a standard response class called CCLHTTPServerResponse which has various convinience methods for creating responses of different types.

CCLHTTPServer was designed to expose a standard interface for creating a HTTP server in Objective-C though the CCLHTTPServer/Interface pod. This means that other HTTP servers are able to follow the same interface and allows developers to write frameworks around this standard interface without being tied down to a single server implementation.

Requests

A request is an object conforming to the CCLHTTPServerRequest protocol which exposes basic information about the request such as the method, path, HTTPVersion, headers and the body.

@protocol CCLHTTPServerRequest <NSObject>
- (NSString *)method;
- (NSString *)path;
- (NSString *)HTTPVersion;
- (NSDictionary *)headers;
- (NSData *)body;
@end

Responses

You can make your own response class or make an existing class conform to the CCLHTTPServerResponse protcol. However for most cases you can use CCLHTTPServerResponse class.

Response with data

[CCLHTTPServerResponse responseWithStatusCode:204 headers:nil body:nil];

Text response

[CCLHTTPServerResponse responseWithStatusCode:200
                                      headers:nil
                                      content:@"Hello World"
                                  contentType:@"plain/text"];

Form encoded response

[CCLHTTPServerResponse formURLEncodedResponseWithStatusCode:200
                                                    headers:nil
                                                 parameters:@{@"name": @"Kyle"}];

JSON response

[CCLHTTPServerResponse JSONResponseWithStatusCode:200
                                          headers:nil
                                       parameters:@{@"name": @"Kyle"}];

Installation

Podfile

pod 'CCLHTTPServer'

License

CCLHTTPServer is released under the BSD license. See LICENSE.