I've never seen a simpler IOS HTTP server than Cclhttpserver. You don't even have to create any subclasses to use it. For most tasks, this small but powerful server can meet your needs.
It's simply outrageous, so I think it's enough to look at its homepage introduction:
Https://github.com/cocodelabs/CCLHTTPServer
Cclhttpserver is a simple HTTP server framework under a IOS/OSX.
First, usage
The following code creates a simple HTTP server that listens on the specified port and responds with the specified Cclhttpserverresponse class (subclass).
Objective-c:
Cclhttpserver*server = [[Cclhttpserver alloc] Initwithinterface:nil port:8080handler:^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];
}];
SWITF:
Varserver:cclhttpserver=cclhttpserver (Interface:nil, port:7433, Handler:
{Request in
Let headers =["Content-type": "Text/plain; Charset=utf8 "]
Let BODY = "Hello World". datausingencoding (nsutf8stringencoding);
Returncclhttpserverresponse (statuscode:200, Headers:headers, Body:body)
})
The cclhttpserverresponse must handle the request parameter passed to it. The request parameter is an object that implements the Cclhttpserverrequest protocol, which encapsulates a variety of useful methods that you can use to create a variety of response (which are described in more detail below).
The purpose of Cclhttpserver is to expose a standard o-c interface for creating HTTP servers, see the Cllhttpserver/interface directory. That is, other HTTP servers can use these interfaces, and developers can use the interface to write their own framework without having to control the implementation of a particular server.
Ii. Request for requests
Request is an object that implements the Cclhttpserverrequest protocol, which exposes basic details such as the request method, path, HTTP version, headers, and the body of the HTTP request.
@protocol cclhttpserverrequest<nsobject>
-(NSString *) method;
-(NSString *) path;
-(NSString *) httpversion;
-(Nsdictionary *) headers;
-(NSData *) body;
@end
Third, response responses
You can create a new response class, or you can use any existing classes that have implemented the Cclhttpserverresponse protocol. Of course, in most cases, the use of cclhttpserverresponse is sufficient.
1. Return NSData Data
[cclhttpserverresponseresponsewithstatuscode:204 Headers:nil Body:nil];
2. Return text data
[cclhttpserverresponseresponsewithstatuscode:200
Headers:nil
content:@ "Hello World"
contenttype:@ "Plain/text"];
3. Return URL-encoded name value pairs
This form is like: Name=kyle&sex=female
[cclhttpserverresponseformurlencodedresponsewithstatuscode:200
Headers:nil
parameters:@{@ "name": @ "Kyle"}];
4. Return JSON data
[cclhttpserverresponsejsonresponsewithstatuscode:200
Headers:nil
parameters:@{@ "name": @ "Kyle"}];
Four, installation
Pod ' Cclhttpserver '
"iOS Open Source Code" (1): Cclhttpserver