1 | <? |
---|
2 | /** |
---|
3 | * Access the HTTP Request |
---|
4 | */ |
---|
5 | class HttpRequest { |
---|
6 | |
---|
7 | /** additional HTTP headers not prefixed with HTTP_ in $_SERVER superglobal */ |
---|
8 | var $add_headers = array('CONTENT_TYPE', 'CONTENT_LENGTH'); |
---|
9 | |
---|
10 | /** |
---|
11 | * Construtor |
---|
12 | * Retrieve HTTP Body |
---|
13 | * @param Array Additional Headers to retrieve |
---|
14 | */ |
---|
15 | function HttpRequest($add_headers = false) { |
---|
16 | |
---|
17 | $this->retrieve_headers($add_headers); |
---|
18 | $this->body = @file_get_contents('php://input'); |
---|
19 | } |
---|
20 | |
---|
21 | /** |
---|
22 | * Retrieve the HTTP request headers from the $_SERVER superglobal |
---|
23 | * @param Array Additional Headers to retrieve |
---|
24 | */ |
---|
25 | function retrieve_headers($add_headers = false) { |
---|
26 | |
---|
27 | if ($add_headers) { |
---|
28 | $this->add_headers = array_merge($this->add_headers, $add_headers); |
---|
29 | } |
---|
30 | |
---|
31 | if (isset($_SERVER['HTTP_METHOD'])) { |
---|
32 | $this->method = $_SERVER['HTTP_METHOD']; |
---|
33 | unset($_SERVER['HTTP_METHOD']); |
---|
34 | } else { |
---|
35 | $this->method = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : false; |
---|
36 | } |
---|
37 | $this->protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : false; |
---|
38 | $this->request_method = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : false; |
---|
39 | |
---|
40 | $this->headers = array(); |
---|
41 | foreach($_SERVER as $i=>$val) { |
---|
42 | if (strpos($i, 'HTTP_') === 0 || in_array($i, $this->add_headers)) { |
---|
43 | $name = str_replace(array('HTTP_', '_'), array('', '-'), $i); |
---|
44 | $this->headers[$name] = $val; |
---|
45 | } |
---|
46 | } |
---|
47 | } |
---|
48 | |
---|
49 | /** |
---|
50 | * Retrieve HTTP Method |
---|
51 | */ |
---|
52 | function method() { |
---|
53 | return $this->method; |
---|
54 | } |
---|
55 | |
---|
56 | /** |
---|
57 | * Retrieve HTTP Body |
---|
58 | */ |
---|
59 | function body() { |
---|
60 | return $this->body; |
---|
61 | } |
---|
62 | |
---|
63 | /** |
---|
64 | * Retrieve an HTTP Header |
---|
65 | * @param string Case-Insensitive HTTP Header Name (eg: "User-Agent") |
---|
66 | */ |
---|
67 | function header($name) { |
---|
68 | $name = strtoupper($name); |
---|
69 | return isset($this->headers[$name]) ? $this->headers[$name] : false; |
---|
70 | } |
---|
71 | |
---|
72 | /** |
---|
73 | * Retrieve all HTTP Headers |
---|
74 | * @return array HTTP Headers |
---|
75 | */ |
---|
76 | function headers() { |
---|
77 | return $this->headers; |
---|
78 | } |
---|
79 | |
---|
80 | /** |
---|
81 | * Return Raw HTTP Request (note: This is incomplete) |
---|
82 | * @param bool ReBuild the Raw HTTP Request |
---|
83 | */ |
---|
84 | function raw($refresh = false) { |
---|
85 | |
---|
86 | if (isset($this->raw) && !$refresh) { |
---|
87 | return $this->raw; // return cached |
---|
88 | } |
---|
89 | |
---|
90 | $headers = $this->headers(); |
---|
91 | $this->raw = "{$this->method}\r\n"; |
---|
92 | |
---|
93 | foreach($headers as $i=>$header) { |
---|
94 | $this->raw .= "$i: $header\r\n"; |
---|
95 | } |
---|
96 | |
---|
97 | $this->raw .= "\r\n{$this->body}"; |
---|
98 | |
---|
99 | return $this->raw; |
---|
100 | } |
---|
101 | |
---|
102 | } |
---|
103 | ?> |
---|