我正在玩GCDWebServer
类(很棒!)并且想知道是否可以通过远程IP限制允许的连接。
server.addDefaultHandlerForMethod("GET", requestClass: GCDWebServerRequest.self, processBlock: {request in
let remote = request.remoteAddressString.substringToIndex(request.remoteAddressString.lastIndexOf(":")!)
let local = request.localAddressString.substringToIndex(request.localAddressString.lastIndexOf(":")!)
print("\(remote) vs \(local)")
if remote == local {
return nil
}
//Forbidden
return GCDWebServerResponse(statusCode: 403);
})
我希望上面会有效,但不是,我的意思是当IP不同时它会发送403 Forbidden,否则它会以最终结果:
[DEBUG] Connection aborted with status code 500 on socket 13
任何人都可以帮我吗?
P.S。以下也是最重要的工作
server.addDefaultHandlerForMethod("GET",
requestClass: GCDWebServerRequest.self,
processBlock: {request in
print(request.remoteAddressString)
print(request.localAddressString)
let remote = request.remoteAddressString.substringToIndex(request.remoteAddressString.lastIndexOf(":")!)
let local = request.localAddressString.substringToIndex(request.localAddressString.lastIndexOf(":")!)
print("\(remote) vs \(local)")
if remote == local {
return GCDWebServerResponse(statusCode: 200)
}
//Forbidden
return GCDWebServerResponse(statusCode: 403);
})
答案 0 :(得分:0)
GCDWebServer.h
见GCDWebServerProcessBlock
:
该块必须在出错时返回GCDWebServerResponse或nil,这将导致返回给客户端的500 HTTP状态代码。