srchub

srchub Git Source Tree


Root/pluf/src/Pluf/HTTP/Response.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of Plume Framework, a simple PHP Application Framework.
# Copyright (C) 2001-2007 Loic d'Anterroches and contributors.
#
# Plume Framework is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Plume Framework is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
# ***** END LICENSE BLOCK ***** */
 
/**
 * Response object to be constructed by the views.
 *
 * When constructing a view, the response object must be populated and
 * returned. The response is then displayed to the visitor.
 * The interest of using a response object is that we can run a post
 * filter action on the response. For example you can run a filter that
 * is checking that all the output is valid HTML and write a logfile if
 * this is not the case.
 */
class Pluf_HTTP_Response
{
    /**
     * Content of the response.
     */
    public $content = '';
 
    /**
     * Array of the headers to add.
     *
     * For example $this->headers['Content-Type'] = 'text/html; charset=utf-8';
     */
    public $headers = array();
 
    /**
     * Status code of the answer.
     */
    public $status_code = 200;
 
    /**
     * Cookies to send.
     *
     * $this->cookies['my_cookie'] = 'content of the cookie';
     */
    public $cookies = array();
 
    /**
     * Status code list.
     *
     */
    public $status_code_list = array(
                                     '100' => 'CONTINUE',
                                     '101' => 'SWITCHING PROTOCOLS',
                                     '200' => 'OK',
                                     '201' => 'CREATED',
                                     '202' => 'ACCEPTED',
                                     '203' => 'NON-AUTHORITATIVE INFORMATION',
                                     '204' => 'NO CONTENT',
                                     '205' => 'RESET CONTENT',
                                     '206' => 'PARTIAL CONTENT',
                                     '300' => 'MULTIPLE CHOICES',
                                     '301' => 'MOVED PERMANENTLY',
                                     '302' => 'FOUND',
                                     '303' => 'SEE OTHER',
                                     '304' => 'NOT MODIFIED',
                                     '305' => 'USE PROXY',
                                     '306' => 'RESERVED',
                                     '307' => 'TEMPORARY REDIRECT',
                                     '400' => 'BAD REQUEST',
                                     '401' => 'UNAUTHORIZED',
                                     '402' => 'PAYMENT REQUIRED',
                                     '403' => 'FORBIDDEN',
                                     '404' => 'NOT FOUND',
                                     '405' => 'METHOD NOT ALLOWED',
                                     '406' => 'NOT ACCEPTABLE',
                                     '407' => 'PROXY AUTHENTICATION REQUIRED',
                                     '408' => 'REQUEST TIMEOUT',
                                     '409' => 'CONFLICT',
                                     '410' => 'GONE',
                                     '411' => 'LENGTH REQUIRED',
                                     '412' => 'PRECONDITION FAILED',
                                     '413' => 'REQUEST ENTITY TOO LARGE',
                                     '414' => 'REQUEST-URI TOO LONG',
                                     '415' => 'UNSUPPORTED MEDIA TYPE',
                                     '416' => 'REQUESTED RANGE NOT SATISFIABLE',
                                     '417' => 'EXPECTATION FAILED',
                                     '500' => 'INTERNAL SERVER ERROR',
                                     '501' => 'NOT IMPLEMENTED',
                                     '502' => 'BAD GATEWAY',
                                     '503' => 'SERVICE UNAVAILABLE',
                                     '504' => 'GATEWAY TIMEOUT',
                                     '505' => 'HTTP VERSION NOT SUPPORTED'
                                     );
 
  
    /**
     * Constructor of the response.
     *
     * @param string Content of the response ('')
     * @param string MimeType of the response (null) if not given will
     * default to the one given in the configuration 'mimetype'
     */
    function __construct($content='', $mimetype=null)
    {
        if (is_null($mimetype)) {
            $mimetype = Pluf::f('mimetype', 'text/html').'; charset=utf-8';
        }
        $this->content = $content;
        $this->headers['Content-Type'] = $mimetype;
        $this->headers['X-Powered-By'] = 'Pluf - http://pluf.org/';
        $this->status_code = 200;
        $this->cookies = array();
    }
 
    /**
     * Render a response object.
     */
    function render($output_body=true)
    {
        if ($this->status_code >= 200
            && $this->status_code != 204
            && $this->status_code != 304) {
            $this->headers['Content-Length'] = strlen($this->content);
        }
        $this->outputHeaders();
        if ($output_body) {
            echo $this->content;
        }
    }
 
    /**
     * Output headers.
     */
    function outputHeaders()
    {
        if (!defined('IN_UNIT_TESTS')) {
            header('HTTP/1.0 '.$this->status_code.' '
                   .$this->status_code_list[$this->status_code],
                   true, $this->status_code);
            foreach ($this->headers as $header => $ch) {
                header($header.': '.$ch);
            }
            foreach ($this->cookies as $cookie => $data) {
                // name, data, expiration, path, domain, secure, http only
                $expire = (null == $data) ? time()-31536000 : time()+31536000;
                $data = (null == $data) ? '' : $data;
                setcookie($cookie, $data, $expire,
                          Pluf::f('cookie_path', '/'),
                          Pluf::f('cookie_domain', null),
                          Pluf::f('cookie_secure', false),
                          Pluf::f('cookie_httponly', true));
            }
        } else {
            $_COOKIE = array();
            foreach ($this->cookies as $cookie => $data) {
                $_COOKIE[$cookie] = $data;
            }
        }
    }
}

Archive Download this file

Branches

Number of commits:
Page rendered in 0.10231s using 11 queries.