[swift-users] Instantiate Swift class from string

Jens Alfke jens at mooseyard.com
Thu Dec 10 17:25:14 CST 2015

> On Dec 10, 2015, at 1:01 PM, Matthew Davies via swift-users <swift-users at swift.org> wrote:
>   func get(url: String, ctrl: Controller.Type, method: String) {
>     let inst = ctrl.init()
>     // Run the method that is passed in here
>   }

Since HTTP only has a handful of standard methods/verbs, you can just define a Swift method for each one in your protocol (get, put, delete, etc.) and use a switch statement on the `method` parameter to dispatch to the right one.

This is a case where you wouldn’t want to use dynamic lookup anyway, since the method name you’d be looking up and calling would be chosen by the remote client. That’s the sort of thing that’s just begging to be exploited (I.e. someone opens a TCP socket and sends “getWithoutCheckingAuth /admin/secretdata.txt HTTP/1.1”…)

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20151210/de1bdf2f/attachment.html>

More information about the swift-users mailing list