Skip to content

Commit 4f516b3

Browse files
committed
Split project into two modules allowing for HTTP to be imported into other projects
1 parent f0689b0 commit 4f516b3

File tree

4 files changed

+60
-49
lines changed

4 files changed

+60
-49
lines changed

Package.swift

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
import PackageDescription
22

33
let package = Package(
4-
name: "swift-http"
4+
name: "swift-http",
5+
targets: [
6+
Target(
7+
name: "example",
8+
dependencies: [
9+
.Target(name: "http")
10+
]
11+
),
12+
13+
Target(
14+
name: "http"
15+
)
16+
]
517
)

Sources/main.swift

-17
This file was deleted.

example/main.swift

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import Foundation
2+
3+
#if os(Linux)
4+
import Glibc
5+
#else
6+
import Darwin.C
7+
#endif
8+
9+
import http
10+
11+
let environment = NSProcessInfo.processInfo().environment
12+
var port = environment["PORT"]
13+
if port == nil || port!.isEmpty {
14+
port = "8080"
15+
}
16+
let portInt = UInt16(port!)
17+
18+
let server = HTTP(port: portInt!)
19+
20+
while (true) {
21+
if (listen(server.serverSocket, 10) < 0) {
22+
exit(1)
23+
}
24+
25+
let clientSocket = accept(server.serverSocket, nil, nil)
26+
27+
let msg = "Hello World"
28+
let contentLength = msg.utf8.count
29+
30+
server.echo(clientSocket, "HTTP/1.1 200 OK\n")
31+
server.echo(clientSocket, "Server: Swift Web Server\n")
32+
server.echo(clientSocket, "Content-length: \(contentLength)\n")
33+
server.echo(clientSocket, "Content-type: text-plain\n")
34+
server.echo(clientSocket, "Connection: close\n")
35+
server.echo(clientSocket, "\r\n")
36+
37+
server.echo(clientSocket, msg)
38+
39+
print("Response sent: '\(msg)' - Length: \(contentLength)")
40+
41+
shutdown(clientSocket, Int32(SHUT_RDWR))
42+
close(clientSocket)
43+
}

Sources/http.swift http/HTTP.swift

+4-31
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
#endif
66

77
public class HTTP {
8-
var serverSocket : Int32 = 0
9-
var serverAddress : sockaddr_in?
8+
public var serverSocket : Int32 = 0
9+
public var serverAddress : sockaddr_in?
1010
var bufferSize : Int = 1024
1111

1212
func sockaddr_cast(p: UnsafeMutablePointer<Void>) -> UnsafeMutablePointer<sockaddr> {
1313
return UnsafeMutablePointer<sockaddr>(p)
1414
}
1515

16-
func echo(socket: Int32, _ output: String) {
16+
public func echo(socket: Int32, _ output: String) {
1717
output.withCString { (bytes) in
1818
#if os(Linux)
1919
let flags = Int32(MSG_NOSIGNAL)
@@ -24,7 +24,7 @@ public class HTTP {
2424
}
2525
}
2626

27-
init(port: UInt16) {
27+
public init(port: UInt16) {
2828
#if os(Linux)
2929
serverSocket = socket(AF_INET, Int32(SOCK_STREAM.rawValue), 0)
3030
#else
@@ -63,33 +63,6 @@ public class HTTP {
6363
print("Server started at port \(port)")
6464
}
6565
}
66-
67-
func start() {
68-
while (true) {
69-
if (listen(serverSocket, 10) < 0) {
70-
exit(1)
71-
}
72-
73-
let clientSocket = accept(serverSocket, nil, nil)
74-
75-
let msg = "Hello World"
76-
let contentLength = msg.utf8.count
77-
78-
echo(clientSocket, "HTTP/1.1 200 OK\n")
79-
echo(clientSocket, "Server: Swift Web Server\n")
80-
echo(clientSocket, "Content-length: \(contentLength)\n")
81-
echo(clientSocket, "Content-type: text-plain\n")
82-
echo(clientSocket, "Connection: close\n")
83-
echo(clientSocket, "\r\n")
84-
85-
echo(clientSocket, msg)
86-
87-
print("Response sent: '\(msg)' - Length: \(contentLength)")
88-
89-
shutdown(clientSocket, Int32(SHUT_RDWR))
90-
close(clientSocket)
91-
}
92-
}
9366
}
9467

9568
extension CUnsignedShort {

0 commit comments

Comments
 (0)