-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhttp_response.class.php
142 lines (127 loc) · 5.42 KB
/
http_response.class.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
/**
* Colourphon
*
* Class providing basic but repetative header content designation.
* Could also add content-type headers in here.
* Perhaps this becomes a header factory?
*
*/
class http_response extends ColourphonRdf {
private $content;
private $content_type;
private $status;
private $new_location ;
private $extensions = array('.rdf', '.ttl', '.ntriples', '.html');
public function __construct($status='', $content_type='', $content='', $new_location=''){
$this->content_type = $content_type;
$this->status = $status;
$this->new_location = $new_location;
$this->send_response($content);
}
public function set_content_type($content_type){
if(isset($content_type)){
$this->content_type = $content_type;
}
}
public function set_content($content){
if(isset($content)){
$this->content = $content;
}
}
public function set_status($status){
if(isset($status)){
$this->status = $status;
}
}
public function send_headers(){
if(isset($this->status)){
switch ( $this->status ) {
case "200":
header("HTTP/1.0 200 OK", false);
header("Status: 200 OK", false);
break;
case "201" :
header("HTTP/1.0 201 Created", false);
header("Status: 201 Created", false);
break;
case "202" :
header("HTTP/1.0 202 Accepted",false);
header("Status: 202 Accepted", false);
break;
case "302" :
header("HTTP/1.0 302 Found", false);
header("Status: 302 Found", false);
break;
case "303" :
$ext = substr($_SERVER['REQUEST_URI'], (strrpos($_SERVER['REQUEST_URI'],'.')));
// ??? Check to see that we don't come back in and loop.
// Note: this is handled by parsing the REQUEST_URI in ColourphonRdf
//if (in_array($ext, $this->extensions)) $this->do_404();
// Inspect accept headers
if (isset($_SERVER['HTTP_ACCEPT']) && strpos($_SERVER['HTTP_ACCEPT'], 'application/rdf+xml') !== false) {
header('HTTP/1.1 303 See Other');
header('Location: http://' . $_SERVER['SERVER_NAME'] . $this->new_location . '.rdf', 303);
ob_end_flush();
die;
} else if (isset($_SERVER['HTTP_ACCEPT']) && strpos($_SERVER['HTTP_ACCEPT'], 'text/turtle') !== false) {
header('HTTP/1.1 303 See Other');
header('Location: http://' . $_SERVER['SERVER_NAME'] . $this->new_location . '.ttl', 303);
ob_end_flush();
die;
} else if (isset($_SERVER['HTTP_ACCEPT']) && (
strpos($_SERVER['HTTP_ACCEPT'], 'text/n3') !== false
|| strpos($_SERVER['HTTP_ACCEPT'], 'text/ntriples') !== false
)
){
header('HTTP/1.1 303 See Other');
header('Location: http://' . $_SERVER['SERVER_NAME'] . $this->new_location . '.ntriples', 303);
ob_end_flush();
die;
} else {
// otherwise redirect to human-readable representation
// Check we are not looking for a non-existent html file.
header('HTTP/1.1 303 See Other');
header('Location: http://' . $_SERVER['SERVER_NAME'] . $this->new_location . '.html', 303);
ob_end_flush();
die;
}
break;
case "400" :
header("HTTP/1.0 400 Bad Request", false);
header("Status: 400 Bad Request", false);
break;
case "404" :
header("HTTP/1.0 404 Not Found", false);
header("Status: 404 Not Found", false);
break;
default:
header("HTTP/1.0 404 Not Found");
header("Status: 404 Not Found", false);
break;
}
}
if(isset($this->content_type)){
header("Content-type: {$this->content_type}");
}
}
public function send_response($content=""){
if(isset($content)){
$this->content = $content;
}
ob_start();
$this->send_headers();
echo $this->content;
ob_end_flush();
}
public function do_404(){
header('HTTP/1.1 404 Not Found');
echo '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <html><head> <title>404 Not Found</title> </head><body> <h1>Not Found</h1> <p>The requested URL ';
echo $_SERVER['REQUEST_URI'];
echo " was not found on this server.</p></body></html>\n";
ob_end_flush();
die;
}
}
?>