-
Notifications
You must be signed in to change notification settings - Fork 12
/
server.php
executable file
·98 lines (89 loc) · 3.57 KB
/
server.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
include_once "../src/Rest/Server.php";
class Foobar implements Rest\Controller {
public function execute(Rest\Server $rest) {
if($rest->getRequest()->isPost()) {
$rest->getResponse()->addHeader("HTTP/1.1 201 Created");
$rest->getResponse()->setResponse($rest->getBaseUrl()."/server.php?q=/Foo/bar");
} else {
$rest->getResponse()->setResponse("Hello World!");
}
return $rest;
}
public function foo(Rest\Server $rest) {
if(($name = $rest->getQuery(3)) == null && ($name = $rest->getRequest()->getPost("name")) == null) {
$name = "RestServer";
} else {
}
$rest->getResponse()->setResponse("Hello , ".ucfirst($name).".");
return $rest;
}
public function bar(Rest\Server $rest) {
return new Echoer ;
}
public function auth(Rest\Server $rest) {
$rest->getResponse()->setResponse($rest->getAuthenticator()->getUser());
}
public function bench(Rest\Server $rest) {
$rest->getResponse()->appendResponse("It took ".round(xdebug_time_index(),5)." seconds\n");
$rest->getResponse()->appendResponse("Used ".round(xdebug_memory_usage()/1024,5)."Kb of Memory\n");
$rest->getResponse()->appendResponse("Used at peak ".round(xdebug_peak_memory_usage()/1024,5)."Kb of Memory\n");
return $rest;
}
}
class Echoer implements Rest\View {
public function execute(Rest\Server $rest) {
$r = "Method=".$rest->getRequest()->getMethod()."\n";
$r .= "URI=".$rest->getRequest()->getRequestURI()."\n";
foreach($rest->getRequest()->getGET() as $key=>$value) {
$r .= "GET[".$key."]=".$value."\n";
}
foreach($rest->getRequest()->getPOST() as $key=>$value) {
$r .= "POST[".$key."]=".$value."\n";
}
$rest->getResponse()->setResponse(nl2br($r));
return $rest;
}
}
$q = (isset($_GET["q"]))?$_GET["q"]:"";
$r = new Rest\Server($q) ;
$r->addMap("GET","/Foo","Foobar");
$r->addMap("POST","/Foo","Foobar");
$r->addMap("GET","/Foo/bar","Foobar::bar");
$r->addMap("GET","/Foo/hello","Foobar::foo");
$r->addMap("POST","/Foo/hello","Foobar::foo");
$r->addMap("GET","/Foo/hello/[\w]*","Foobar::foo");
$r->addMap("GET","/Foo/restricted/basic","Foobar::auth");
$r->addMap("GET","/Foo/restricted/digest","Foobar::auth");
$r->addMap("GET","/Foo/bench","Foobar::bench");
$r->addMap("GET","/Lambda",function($rest) {
$rest->getResponse()->setResponse("Hello Closure!");
return $rest;
});
$r->addMap("GET","/hello/:name",function($rest) {
$rest->getResponse()->setResponse("Hello, ". $rest->getRequest()->getParameter("name")."!");
return $rest;
});
$r->addMap("GET","/mime",function($rest) {
$rest->getResponse()->setResponse("Hello, ". $rest->getRequest()->getExtension()."!");
return $rest;
}, array("*","text/html"));
$r->addMap("GET","/nomime",function($rest) {
$rest->getResponse()->setResponse("Hello, ". $rest->getRequest()->getExtension()."!");
return $rest;
}, array("text/html"));
if($r->getQuery(2) == "restricted") {
if($r->getQuery(3) == "basic") {
$r->getAuthenticator()->requireAuthentication(true);
if($r->getAuthenticator()->getUser() == "joe" && $r->getAuthenticator()->GetPassword() == "123") {
$r->getAuthenticator()->setAuthenticated(true);
}
} else if($r->getQuery(3) == "digest") {
$r->getAuthenticator()->forceDigest(true);
$user = $r->getAuthenticator()->getUser();
$pass = "123";
$r->getAuthenticator()->validate($user,$pass);
}
}
$r->execute();
?>