Skip to content

Instantly share code, notes, and snippets.

@jaytaph
Created July 12, 2013 19:12
Show Gist options
  • Save jaytaph/5986945 to your computer and use it in GitHub Desktop.
Save jaytaph/5986945 to your computer and use it in GitHub Desktop.
import saffire;
import http::request;
import saffire::sapi;
class http {
protected property sapi = null;
public method detectSapi() {
switch (saffire.sapi()) {
case "fastcgi" :
return saffire.sapi.fastcgi();
case "cli" :
return saffire.sapi.dummy();
default :
throw new Exception("Cannot find interface for this sapi");
}
}
public method setSapi(Sapi sapi) {
self.sapi = sapi;
}
public method getSapi() {
if (! self.sapi) self.sapi = self.detectSapi();
return self.sapi;
}
// Returns a fileld request
public method getMainRequest() {
req = self.getRequest();
sapi = self.getSapi();
sapi.fillRequest(req);
return req;
}
// Just returns an empty request
public method getRequest() {
return new request();
}
}
import net::uri; // as example, we use different modules for IP and URI parsing (getQueryString getHostname etc)
import net::ip; // GetHostByName, getHostByAddress, isvalidIp() etc
class Request {
protected property client_ip;
protected property method;
public method getMethod() {
return self.method;
}
public method setMethod(method) {
self.method = method.uppercase;
}
public method setClientIp(ip) {
if (ip.isValidIp(ip)) self.client_ip = ip;
}
....
}
import ::http::request;
import ::_sfl::fastcgi as fcgi; // Internal fastcgi module
class fastcgi implements sapi {
public method fillRequest(Request req) {
req.client_ip = self.getParameter("CLIENT_IP", "");
req.method = self.getParameter("METHOD", "");
}
public method getParameter(key, default) {
// fcgi.env is a hash object
return fcgi.env[key] ?? default; // coalesce. Sweet!
}
public method hasParameter(key) {
return fcgi.env.has(key);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment