0

我正在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

谁能帮我这个?

PS以下也不起作用

    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);
})
4

1 回答 1

0

GCDWebServer.h参阅GCDWebServerProcessBlock

该块必须返回 GCDWebServerResponse 或出错时返回 nil,这将导致向客户端返回 500 HTTP 状态代码。

于 2016-10-11T21:02:00.280 回答