forked from whiteblock/hobbits
-
Notifications
You must be signed in to change notification settings - Fork 7
/
parser.swift
51 lines (41 loc) · 1.4 KB
/
parser.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import Foundation
struct Message {
let version: String // @todo Version type
let proto: String
let compression: String
let encoding: String
let headers: [UInt8]
let body: [UInt8]
}
extension Message: CustomStringConvertible {
public var description: String {
return "EWP " + version
+ " " + proto.rawValue
+ " " + compression
+ " " + encoding
+ " " + String(headers.count)
+ " " + String(body.count)
+ "\n" + String(bytes: headers, encoding: .utf8)!
+ String(bytes: body, encoding: .utf8)!
}
}
extension Message {
init(serializedData input: String) {
var lines = input.split(separator: "\n")
if lines.count == 1 {
lines.append("")
}
let payload = lines[1]
var request = lines[0].split(separator: " ")
assert(request.count >= 7) // @todo exception handling
let headersLength = Int(request[5])!
let bodyLength = Int(request[6])!
version = String(request[1])
proto = String(request[2])
compression = String(request[3])
encoding = String(request[4])
headers = payload.substring(start: 0, end: headersLength).bytes
body = payload.substring(start: headersLength, end: bodyLength).bytes
}
}
print(Message(serializedData: "EWP 0.2 RPC none json 0 25\n{'id':1,'method_id':0x00}"))