Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixing the dangling pointers. #54

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 11 additions & 4 deletions Sources/PerfectHTTPServer/HTTP11/HTTP11Request.swift
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,11 @@ class HTTP11Request: HTTPRequest {
workingBuffer.removeAll()
case .headerField:
workingBuffer.append(0)
lastHeaderName = String(validatingUTF8: UnsafeMutableRawPointer(mutating: workingBuffer).assumingMemoryBound(to: Int8.self))
lastHeaderName = workingBuffer.withUnsafeBufferPointer {
return $0.baseAddress?.withMemoryRebound(to: Int8.self, capacity: workingBuffer.count) {
return String(validatingUTF8: $0)
}
}
workingBuffer.removeAll()
case .headerValue:
if let name = lastHeaderName {
Expand Down Expand Up @@ -464,10 +468,13 @@ class HTTP11Request: HTTPRequest {
// false indicates that the request either was fully read and is being processed or that the request failed
// either way no further action should be taken
func didReadSomeBytes(_ b: [UInt8], callback: @escaping StatusCallback) -> Bool {
_ = UnsafePointer(b).withMemoryRebound(to: Int8.self, capacity: b.count) {
http_parser_execute(&parser, &parserSettings, $0, b.count)
var mutableB = b
_ = mutableB.withUnsafeMutableBufferPointer { buffered in
buffered.baseAddress?.withMemoryRebound(to: Int8.self, capacity: b.count) {
http_parser_execute(&parser, &parserSettings, $0, b.count)
}
}

let http_errno = parser.http_errno
guard HPE_HEADER_OVERFLOW.rawValue != http_errno else {
callback(.requestEntityTooLarge)
Expand Down
7 changes: 6 additions & 1 deletion Sources/PerfectHTTPServer/HTTPContentCompression.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ class ZlibStream {
dest.deallocate()
}
if !bytes.isEmpty {
stream.next_in = UnsafeMutablePointer(mutating: bytes)
var byts = bytes
byts.withUnsafeMutableBufferPointer { buffered in
buffered.baseAddress?.withMemoryRebound(to: Bytef.self, capacity: bytes.count) {
stream.next_in = $0
}
}
stream.avail_in = uInt(bytes.count)
} else {
stream.next_in = nil
Expand Down