Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
readfile
  • Loading branch information
shalyf committed Jul 17, 2017
commit 931c6abb4b8ba032e68ec200ed60d7b1658e644b
22 changes: 21 additions & 1 deletion Sources/HttpParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ public class HttpParser {
request.queryParams = extractQueryParams(request.path)
request.headers = try readHeaders(socket)
if let contentLength = request.headers["content-length"], let contentLengthValue = Int(contentLength) {
request.body = try readBody(socket, size: contentLengthValue)
if request.headers["content-type"] == "application/octet-stream" {
request.tempFile = try readFile(socket, length: contentLengthValue)
} else {
request.body = try readBody(socket, size: contentLengthValue)
}
}
return request
}
Expand Down Expand Up @@ -75,6 +79,22 @@ public class HttpParser {
// }
}

private func readFile(_ socket: Socket, length: Int) throws -> String {
var offset = 0
let bufferLength = 1024
let filePath = NSTemporaryDirectory() + "/" + NSUUID().uuidString
let file = try filePath.openNewForWriting()

while offset < length {
let length = offset + bufferLength < length ? bufferLength : length - offset
let result = try socket.read(length: length)
try file.write(result.buffer, count: result.count)
offset += result.count
}
file.close()
return filePath
}

private func readBody(_ socket: Socket, size: Int) throws -> [UInt8] {
var body = [UInt8]()
for _ in 0..<size { body.append(try socket.read()) }
Expand Down
7 changes: 7 additions & 0 deletions Sources/HttpRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,16 @@ public class HttpRequest {
public var body: [UInt8] = []
public var address: String? = ""
public var params: [String: String] = [:]
public var tempFile: String?

public init() {}

public func removeTempFileIfExists() throws {
if let path = tempFile, try path.exists() {
try FileManager.default.removeItem(atPath: path)
}
}

public func hasTokenForHeader(_ headerName: String, token: String) -> Bool {
guard let headerValue = headers[headerName] else {
return false
Expand Down
1 change: 1 addition & 0 deletions Sources/HttpServerIO.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ public class HttpServerIO {
let (params, handler) = self.dispatch(request)
request.params = params
let response = handler(request)
try? request.removeTempFileIfExists()
var keepConnection = parser.supportsKeepAlive(request.headers)
do {
if self.operating {
Expand Down
11 changes: 11 additions & 0 deletions Sources/Socket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,17 @@ open class Socket: Hashable, Equatable {
}
}

public typealias SocketReadResult = (count: Int, buffer: [UInt8])

open func read(length: Int) throws -> SocketReadResult {
var buffer = [UInt8](repeating: 0, count: length)
let count = recv(self.socketFileDescriptor as Int32, &buffer, buffer.count, 0)
if count <= 0 {
throw SocketError.recvFailed(Errno.description())
}
return (count, buffer)
}

open func read() throws -> UInt8 {
var buffer = [UInt8](repeating: 0, count: 1)
let next = recv(self.socketFileDescriptor as Int32, &buffer, Int(buffer.count), 0)
Expand Down
11 changes: 11 additions & 0 deletions Sources/String+File.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@ extension String {
}
}

public func write(_ data: [UInt8], count: Int) throws -> Void {
if count <= 0 || data.count <= 0 {
return
}
try data.withUnsafeBufferPointer {
if fwrite($0.baseAddress, 1, count, self.pointer) != count {
throw FileError.error(errno)
}
}
}

public static func currentWorkingDirectory() throws -> String {
guard let path = getcwd(nil, 0) else {
throw FileError.error(errno)
Expand Down