TestsTested | ✗ |
LangLanguage | Obj-CObjective C |
License | BSD |
ReleasedLast Release | Dec 2014 |
Maintained by Unclaimed.
OCFWebServer is a lightweight, modern and asynchronous HTTP (version 1.1) server. It was forked from GCDWebServer and modified to fit the needs of Objective-Cloud.com and hopefully other people's needs as well.
OCFWebServer is used by OCFWeb which is a framework for developing web applications with Objective-C. OCFWeb and OCFWebServer are both used by Objective-Cloud.com. Are you using OCFWebServer as well? Let us know and we will link your app/project right here.
OCFWebServer was developed to be used for Objective-Cloud.com. This does not mean that the goals we had while developing it are incompatible with the needs of developers of regular
apps. These are the goals we had in mind while working on OCFWebServer:
You can simply download the source code of OCFWebServer and add every header and implementation file to your own project.
Remark: All of the following examples are adapted from the GCDWebServer README file and slightly modified to reflect the changes made by OCFWebServer. Some of the explaining texts have also been adopted. Credits: Pierre-Olivier Latour (Thank you so much Pierre!)
Setting up OCFWebServer is easy:
#import "OCFWebServer.h"
int main(int argc, const char* argv[]) {
@autoreleasepool {
OCFWebServer *server = [OCFWebServer new];
// Add a request handler for every possible GET request
[server addDefaultHandlerForMethod:@"GET"
requestClass:[OCFWebServerRequest class]
processBlock:^void(OCFWebServerRequest *request,
OCFWebServerResponseBlock respondWith) {
// Create your response and pass it to respondWith(...)
OCFWebServerResponse *response = [OCFWebServerDataResponse responseWithHTML:@"Hello World"];
[request respondWith:response];
}];
// Run the server on port 8080
[server runWithPort:8080];
}
return EXIT_SUCCESS;
The example above assumes that you have a console based application. If you have a Cocoa or Cocoa Touch application then you might want to have a @property (nonatomic, strong) OCFWebServer *server
in one of your controllers and use one of the start
methods instead of runWithPort:
. If you pass 0
as the port then OCFWebServer will automatically ask the operating system for a free port and use that.
Here's an example handler that redirects /
to /index.html
using the convenience method on 'OCFWebServerResponse' (it sets the HTTP status code and 'Location' header automatically):
[self addHandlerForMethod:@"GET"
path:@"/"
requestClass:[OCFWebServerRequest class]
processBlock:^void(OCFWebServerRequest* request) {
NSURL *toURL = [NSURL URLWithString:@"index.html" relativeToURL:request.URL];
respondWith([OCFWebServerResponse responseWithRedirect:toURL
permanent:NO]);
}];
To implement an HTTP form, you need a pair of handlers:
Here we go:
[server addHandlerForMethod:@"GET"
path:@"/"
requestClass:[OCFWebServerRequest class]
processBlock:^void(OCFWebServerRequest* request) {
NSString* html = @"<html><body> \
<form name=\"input\" action=\"/\" \
method=\"post\" enctype=\"application/x-www-form-urlencoded\"> \
Value: <input type=\"text\" name=\"value\"> \
<input type=\"submit\" value=\"Submit\"> \
</form> \
</body></html>";
[request respondWith:[OCFWebServerDataResponse responseWithHTML:html]];
}];
[server addHandlerForMethod:@"POST"
path:@"/"
requestClass:[OCFWebServerURLEncodedFormRequest class]
processBlock:^void(OCFWebServerRequest* request) {
NSString *value = [(OCFWebServerURLEncodedFormRequest*)request arguments][@"value"];
NSString* html = [NSString stringWithFormat:@"<p>%@</p>", value];
[request respondWith:[OCFWebServerDataResponse responseWithHTML:html]];
}];
As shown in the examples, you can add more than one handler to an instance of OCFWebServer. The handlers are sorted and matched in a last in, first out fashion.
OCFWebServer runs on
and has no third party dependencies.
OCFWebServer is a fork of GCDWebServer. The author of GCDWebServer has done a fantastic job. That is why we picked GCDWebServer as the foundation for OCFWebServer. In the process of making Objective-Cloud.com we realized that GCDWebServer in an incompatible fashion in order to work better. That is why we have forked GCDWebServer and improved it. OCFWebServer is not inherently better than GCDWebServer. It is different.
If you want to learn more about the architecture of OCFWebServer you can have a look at the README of GCDWebServer. OCFWebServer has almost the same architecture than GCDWebServer.
In OCFWebServer your request handler does not have to return anything immediately. OCFWebServer will pass the request to your request handler. The request gives you access to a lot of HTTP request specific properties. Now it is your turn to compute a response. Once that is done you should let the request object know about your response by calling -respondWith:
(class: OCFRequest) and pass it the response. Here is an example:
[server addDefaultHandlerForMethod:@"GET"
requestClass:[OCFWebServerRequest class]
processBlock:^void(OCFWebServerRequest *request) {
dispatch_async(myQueue, ^() {
OCFWebServerDataResponse *response = [OCFWebServerDataResponse responseWithHTML:@"Hello World"];
[request respondWith:response];
});
}];
As you can see your request handler can do anything it wants. You do not have to call dispatch_async
but you can if you need to. Some APIs require you to do something asynchronously (NSURLConnection, XPC, …).
By the way: Migrating your GCDWebServer related code to OCFWebServer is very easy: Simply replace return response;
with [request respondWith:response], return;
and you are done.
At the time of writing GCDWebServer can only handle 16 concurrent requests. You can increase that by changing a constant in GCDWebServer's source code but in OCFWebServer the default maximum number of concurrent request is automatically set to the maximum of what is possible. If you are running OS X and not fine tune the settings this will mean that OCFWebServer can handle up to 128 concurrent requests at a time. If you tune the settings of OS X then this value can be increased and we are already working on a better queuing system which should further increase the number of concurrent requests.
True: This is an implementation detail but important to mention. OCFWebServer is using ARC, dispatch objects (OS_OBJECT_USE_OBJC
), modern runtime and the existing code base of GCDWebServer has been cleaned up and made more POSIX compatible.
OCFWebServer does only support OS X 10.8+ and iOS 6+. If you want to use it on older versions of OS X/iOS then you should use GCDWebServer.
If you want even more convenience for your HTTP server related needs you should also have a look at OCFWeb. OCFWeb is a framework that let's you develop web applications in Objective-C. OCFWeb is using OCFWebServer internally and adds a lot of nice stuff to it like a template engine, nicer syntax for handlers and a lot more.
Development of OCFWebServer takes place on GitHub. If you find a bug, suspect a bug or have a question feel free to open an issue. Pull requests are very welcome and will be accepted as fast as possible.
OCFWebServer is available under the New BSD License - just like GCDWebServer.
This file belongs to the OCFWebServer project.
OCFWebServer is a fork of GCDWebServer (originally developed by
Pierre-Olivier Latour).
We have forked GCDWebServer because we made extensive and
incompatible changes to it.
Copyright (c) 2013, Christian Kienle / [email protected]
All rights reserved.
Original Copyright Statement:
Copyright (c) 2012-2013, Pierre-Olivier Latour
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 the <organization> 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 <COPYRIGHT HOLDER> 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.