[IOS open source code] (1): CCLHTTPServer, ioscclhttpserver

Source: Internet
Author: User

[IOS open source code] (1): CCLHTTPServer, ioscclhttpserver

I have never seen any iOS HTTP server that is simpler than the CCLHTTPServer. You can use it without creating any subclass. For most tasks, this small but powerful server can meet your needs.

It is as simple as it is, so I think it is enough to look at its home page:

Https://github.com/cocodelabs/CCLHTTPServer

 

CCLHTTPServer is a simple HTTP Server framework under iOS/OSX.

 

I. Usage

 

The following code creates a simple HTTP server, listens to the specified port, and responds with the specified CCLHTTPServerResponse class (subclass.

Objective-C:

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 [[carbon httpserverresponse 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)

})

CCLHTTPServerResponse must process the request parameters passed to it. The request parameter is an object that implements the CCLHTTPServerRequest protocol. This protocol encapsulates various useful methods. You can use these methods to create a variety of response (this protocol will be described in detail below ).

The purpose of CCLHTTPServer is to expose a standard O-C Interface for creating an HTTP server, see the CLLHTTPServer/Interface directory. That is to say, other HTTP servers can also use these interfaces. developers can use this interface to write their own frameworks without having to worry about the specific implementation of a server.

 

Ii. Request

 

Request is an object that implements the CCLHTTPServerRequest protocol. This protocol exposes the basic details of HTTP requests, such as the request method, path, HTTP Version, headers, and body.

@ Protocol CCLHTTPServerRequest <NSObject>

-(NSString *) method;

-(NSString *) path;

-(NSString *) HTTPVersion;

-(NSDictionary *) headers;

-(NSData *) body;

@ End

3. Response to Responses

You can create a response class or use any existing class that has implemented the CCLHTTPServerResponse protocol. Of course, in most cases, it is sufficient to use CCLHTTPServerResponse.

1. Return NSData data

[CCLHTTPServerResponseresponseWithStatusCode: 204 headers: nil body: nil];

2. return text data

[Maid: 200

Headers: nil

Content: @ "Hello World"

ContentType: @ "plain/text"];

3. Return the name-value pair of URL encoding.

This format is like: name = Kyle & sex = female

[CCLHTTPServerResponseformURLEncodedResponseWithStatusCode: 200

Headers: nil

Parameters: @ {@ "name": @ "Kyle"}];

 

4. Return JSON data

[Maid: 200

Headers: nil

Parameters: @ {@ "name": @ "Kyle"}];

Iv. Installation

Pod 'cchttpserver'


For Android and ios, one is open source code and the other is closed source code, except that Apple software should be released through the store andriod

Ios software is stable and secure. Many Android users
 
What is the relationship between ios macosx and unix? Unix is not open source code. Why does apple promise open source?

MacOSX is the composite core, which is composed of three parts. The composite kernel named XNT is an open-source project. XNT = X is Not Unix, which means "OSX is Not UNIX". In fact, OSX does Not simply bring FreeBSD to the GUI, but changes the entire kernel. It is still a UNIX-like system. Note that it is not unix, but unix-like.
XNT is open-source. With some drivers and system programs, XNT is known as Darwin. It was once an open-source project. After OSX switched to the x86 architecture, open-source software for x86 was stopped, but it was still open-source software for PowerPC. (This is mainly because of Steve Jobs's own personality. In addition, Apple did not need the open-source community to find bugs)

Linux is also a type of unix. Although its implementation principle is completely different from unix, It inherits the unix spirit.
Deb is an installation package format of the debian system, and debian is a linux system with many branches. The main branch is based on the Linux macro kernel and GUN, which is typical of ubuntu. Deb cannot be installed for non-debian, such as fedora. For general linux software installation methods, only code compilation is required. The deb package is not applicable to different linux systems. Therefore, Android is only a Java virtual machine on the Linux kernel and cannot install the deb package.

The operating system of IOS jobs is partly originated from darwin, which is a UNIX-like system based on the XNT kernel. Therefore, deb of linux debian cannot be installed on IOS. However, the debian apt system exists on IOS, that is, the deb package for IOS packets can be installed, and deb can be installed using the APT with UI such as Cydia.

Although Steve Jobs himself had a bad attitude towards open source and had a quarrel with Mr Linus, another big character similar to him,
However, Apple is one of the world's largest companies with the key strategy of open source, which is undeniable. Its open-source project also includes the webkit browser kernel, which is now used in chrome, safari, various third-party browsers, and almost all ARM devices. Apple also has several very important open-source projects in more professional fields, such as the compiler framework LLVM, the heterogeneous programming framework OpenCL, and the implementation of the Service Discovery Protocol Bonjour. These are all well-known in various fields, LLVM has become a hotbed of innovation. Many projects depend on Apple's open-source projects.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.