+ if (strcmp(ccontrol, "public") && strcmp(ccontrol, "private") && strcmp(ccontrol, "no-cache")) {
+ http_error_ex(HE_WARNING, HTTP_E_INVALID_PARAM, "Cache-Control '%s' doesn't match public, private or no-cache", ccontrol);
+ RETURN_FALSE;
+ } else {
+ size_t cctl_len = spprintf(&cctl, 0, "%s,%s max-age=%ld", ccontrol, must_revalidate?" must-revalidate,":"", max_age);
+ RETVAL_SUCCESS(zend_update_static_property_stringl(THIS_CE, ZEND_STRS("cacheControl")-1, cctl, cctl_len TSRMLS_CC));
+ efree(cctl);
+ }
+}
+/* }}} */
+
+/* {{{ proto static string HttpResponse::getCacheControl()
+ Get current Cache-Control header setting. */
+PHP_METHOD(HttpResponse, getCacheControl)
+{
+ NO_ARGS;
+
+ if (return_value_used) {
+ zval *cctl = http_zsep(IS_STRING, *(zend_std_get_static_property(THIS_CE, ZEND_STRS("cacheControl")-1, 0 TSRMLS_CC)));
+ RETVAL_ZVAL(cctl, 1, 1);
+ }
+}
+/* }}} */
+
+/* {{{ proto static bool HttpResponse::setContentType(string content_type)
+ Set the content-type of the sent entity. */
+PHP_METHOD(HttpResponse, setContentType)
+{
+ char *ctype;
+ int ctype_len;
+
+ if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &ctype, &ctype_len)) {
+ RETURN_FALSE;
+ }
+
+ HTTP_CHECK_CONTENT_TYPE(ctype, RETURN_FALSE);
+ RETURN_SUCCESS(zend_update_static_property_stringl(THIS_CE, ZEND_STRS("contentType")-1, ctype, ctype_len TSRMLS_CC));
+}
+/* }}} */
+
+/* {{{ proto static string HttpResponse::getContentType()
+ Get current Content-Type header setting. */
+PHP_METHOD(HttpResponse, getContentType)
+{
+ NO_ARGS;
+
+ if (return_value_used) {
+ zval *ctype = http_zsep(IS_STRING, *(zend_std_get_static_property(THIS_CE, ZEND_STRS("contentType")-1, 0 TSRMLS_CC)));
+ RETVAL_ZVAL(ctype, 1, 1);
+ }
+}
+/* }}} */
+
+/* {{{ proto static string HttpResponse::guessContentType(string magic_file[, int magic_mode = MAGIC_MIME])
+ Attempts to guess the content type of supplied payload through libmagic. */
+PHP_METHOD(HttpResponse, guessContentType)
+{
+#ifdef HTTP_HAVE_MAGIC
+ char *magic_file, *ct = NULL;
+ int magic_file_len;
+ long magic_mode = MAGIC_MIME;
+
+ RETVAL_FALSE;
+ SET_EH_THROW_HTTP();
+ if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &magic_file, &magic_file_len, &magic_mode)) {
+ switch (Z_LVAL_P(*zend_std_get_static_property(THIS_CE, ZEND_STRS("mode")-1, 0 TSRMLS_CC))) {
+ case SEND_DATA:
+ {
+ zval *data = *zend_std_get_static_property(THIS_CE, ZEND_STRS("data")-1, 0 TSRMLS_CC);
+ ct = http_guess_content_type(magic_file, magic_mode, Z_STRVAL_P(data), Z_STRLEN_P(data), SEND_DATA);
+ break;
+ }
+
+ case SEND_RSRC:
+ {
+ php_stream *s;
+ zval *z = *zend_std_get_static_property(THIS_CE, ZEND_STRS("stream")-1, 0 TSRMLS_CC);
+ z->type = IS_RESOURCE;
+ php_stream_from_zval(s, &z);
+ ct = http_guess_content_type(magic_file, magic_mode, s, 0, SEND_RSRC);
+ break;
+ }
+
+ default:
+ ct = http_guess_content_type(magic_file, magic_mode, Z_STRVAL_P(*zend_std_get_static_property(THIS_CE, ZEND_STRS("file")-1, 0 TSRMLS_CC)), 0, -1);
+ break;
+ }
+ if (ct) {
+ zend_update_static_property_string(THIS_CE, ZEND_STRS("contentType")-1, ct TSRMLS_CC);
+ RETVAL_STRING(ct, 0);
+ }
+ }
+ SET_EH_NORMAL();
+#else
+ http_error(HE_THROW, HTTP_E_RUNTIME, "Cannot guess Content-Type; libmagic not available");
+ RETURN_FALSE;
+#endif
+}
+/* }}} */
+
+/* {{{ proto static bool HttpResponse::setContentDisposition(string filename[, bool inline = false])
+ Set the Content-Disposition. */
+PHP_METHOD(HttpResponse, setContentDisposition)
+{
+ char *file, *cd;
+ int file_len;
+ size_t cd_len;
+ zend_bool send_inline = 0;
+
+ if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &file, &file_len, &send_inline)) {
+ RETURN_FALSE;
+ }
+
+ cd_len = spprintf(&cd, 0, "%s; filename=\"%s\"", send_inline ? "inline" : "attachment", file);
+ RETVAL_SUCCESS(zend_update_static_property_stringl(THIS_CE, ZEND_STRS("contentDisposition")-1, cd, cd_len TSRMLS_CC));
+ efree(cd);
+}
+/* }}} */
+
+/* {{{ proto static string HttpResponse::getContentDisposition()
+ Get current Content-Disposition setting. */
+PHP_METHOD(HttpResponse, getContentDisposition)
+{
+ NO_ARGS;
+
+ if (return_value_used) {
+ zval *cdisp = http_zsep(IS_STRING, *(zend_std_get_static_property(THIS_CE, ZEND_STRS("contentDisposition")-1, 0 TSRMLS_CC)));
+ RETVAL_ZVAL(cdisp, 1, 1);
+ }
+}
+/* }}} */
+
+/* {{{ proto static bool HttpResponse::setETag(string etag)
+ Set a custom ETag. Use this only if you know what you're doing. */
+PHP_METHOD(HttpResponse, setETag)
+{
+ char *etag;
+ int etag_len;
+
+ if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &etag, &etag_len)) {
+ RETURN_FALSE;
+ }
+
+ RETURN_SUCCESS(zend_update_static_property_stringl(THIS_CE, ZEND_STRS("eTag")-1, etag, etag_len TSRMLS_CC));
+}
+/* }}} */
+
+/* {{{ proto static string HttpResponse::getETag()
+ Get calculated or previously set custom ETag. */
+PHP_METHOD(HttpResponse, getETag)
+{
+ NO_ARGS;
+
+ if (return_value_used) {
+ zval *etag = http_zsep(IS_STRING, *(zend_std_get_static_property(THIS_CE, ZEND_STRS("eTag")-1, 0 TSRMLS_CC)));
+ RETVAL_ZVAL(etag, 1, 1);
+ }
+}
+/* }}} */
+
+/* {{{ proto static bool HttpResponse::setLastModified(int timestamp)
+ Set a custom Last-Modified date. */
+PHP_METHOD(HttpResponse, setLastModified)
+{
+ long lm;
+
+ if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &lm)) {
+ RETURN_FALSE;
+ }
+
+ RETURN_SUCCESS(zend_update_static_property_long(THIS_CE, ZEND_STRS("lastModified")-1, lm TSRMLS_CC));
+}
+/* }}} */
+
+/* {{{ proto static int HttpResponse::getLastModified()
+ Get calculated or previously set custom Last-Modified date. */
+PHP_METHOD(HttpResponse, getLastModified)
+{
+ NO_ARGS;
+
+ if (return_value_used) {
+ zval *lmod = http_zsep(IS_LONG, *(zend_std_get_static_property(THIS_CE, ZEND_STRS("lastModified")-1, 0 TSRMLS_CC)));
+ RETVAL_ZVAL(lmod, 1, 1);
+ }
+}
+/* }}} */
+
+/* {{{ proto static bool HttpResponse::setThrottleDelay(double seconds)
+ Sets the throttle delay for use with HttpResponse::setBufferSize(). */
+PHP_METHOD(HttpResponse, setThrottleDelay)
+{
+ double seconds;
+
+ if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d", &seconds)) {
+ RETURN_FALSE;
+ }
+ RETURN_SUCCESS(zend_update_static_property_double(THIS_CE, ZEND_STRS("throttleDelay")-1, seconds TSRMLS_CC));
+}
+/* }}} */
+
+/* {{{ proto static double HttpResponse::getThrottleDelay()
+ Get the current throttle delay. */
+PHP_METHOD(HttpResponse, getThrottleDelay)
+{
+ NO_ARGS;
+
+ if (return_value_used) {
+ zval *tdel = http_zsep(IS_DOUBLE, *(zend_std_get_static_property(THIS_CE, ZEND_STRS("throttleDelay")-1, 0 TSRMLS_CC)));
+ RETVAL_ZVAL(tdel, 1, 1);
+ }
+}
+/* }}} */
+
+/* {{{ proto static bool HttpResponse::setBufferSize(int bytes)
+ Sets the send buffer size for use with HttpResponse::setThrottleDelay(). */
+PHP_METHOD(HttpResponse, setBufferSize)
+{
+ long bytes;
+
+ if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &bytes)) {
+ RETURN_FALSE;
+ }
+ RETURN_SUCCESS(zend_update_static_property_long(THIS_CE, ZEND_STRS("bufferSize")-1, bytes TSRMLS_CC));
+}
+/* }}} */
+
+/* {{{ proto static int HttpResponse::getBufferSize()
+ Get current buffer size. */
+PHP_METHOD(HttpResponse, getBufferSize)
+{
+ NO_ARGS;
+
+ if (return_value_used) {
+ zval *bsize = http_zsep(IS_LONG, *(zend_std_get_static_property(THIS_CE, ZEND_STRS("bufferSize")-1, 0 TSRMLS_CC)));
+ RETVAL_ZVAL(bsize, 1, 1);
+ }
+}
+/* }}} */
+
+/* {{{ proto static bool HttpResponse::setData(mixed data)
+ Set the data to be sent. */
+PHP_METHOD(HttpResponse, setData)
+{
+ char *etag;
+ zval *the_data;
+
+ if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z/", &the_data)) {
+ RETURN_FALSE;
+ }
+ if (Z_TYPE_P(the_data) != IS_STRING) {
+ convert_to_string(the_data);
+ }
+
+ if ( (SUCCESS != zend_update_static_property(THIS_CE, ZEND_STRS("data")-1, the_data TSRMLS_CC)) ||
+ (SUCCESS != zend_update_static_property_long(THIS_CE, ZEND_STRS("mode")-1, SEND_DATA TSRMLS_CC))) {
+ RETURN_FALSE;
+ }
+
+ zend_update_static_property_long(THIS_CE, ZEND_STRS("lastModified")-1, http_last_modified(the_data, SEND_DATA) TSRMLS_CC);
+ if ((etag = http_etag(Z_STRVAL_P(the_data), Z_STRLEN_P(the_data), SEND_DATA))) {
+ zend_update_static_property_string(THIS_CE, ZEND_STRS("eTag")-1, etag TSRMLS_CC);
+ efree(etag);
+ }
+
+ RETURN_TRUE;
+}
+/* }}} */
+
+/* {{{ proto static string HttpResponse::getData()
+ Get the previously set data to be sent. */
+PHP_METHOD(HttpResponse, getData)
+{
+ NO_ARGS;
+
+ if (return_value_used) {
+ zval *the_data = *zend_std_get_static_property(THIS_CE, ZEND_STRS("data")-1, 0 TSRMLS_CC);
+
+ RETURN_ZVAL(the_data, 1, 0);
+ }
+}
+/* }}} */
+
+/* {{{ proto static bool HttpResponse::setStream(resource stream)
+ Set the resource to be sent. */
+PHP_METHOD(HttpResponse, setStream)
+{
+ char *etag;
+ zval *the_stream;
+ php_stream *the_real_stream;
+ php_stream_statbuf ssb;
+
+ if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &the_stream)) {
+ RETURN_FALSE;
+ }
+
+ php_stream_from_zval(the_real_stream, &the_stream);
+ if (php_stream_stat(the_real_stream, &ssb)) {
+ RETURN_FALSE;
+ }
+
+ if ( (SUCCESS != zend_update_static_property_long(THIS_CE, ZEND_STRS("stream")-1, Z_LVAL_P(the_stream) TSRMLS_CC)) ||
+ (SUCCESS != zend_update_static_property_long(THIS_CE, ZEND_STRS("mode")-1, SEND_RSRC TSRMLS_CC))) {
+ RETURN_FALSE;
+ }
+ zend_list_addref(Z_LVAL_P(the_stream));
+
+ zend_update_static_property_long(THIS_CE, ZEND_STRS("lastModified")-1, http_last_modified(the_real_stream, SEND_RSRC) TSRMLS_CC);
+ if ((etag = http_etag(the_real_stream, 0, SEND_RSRC))) {
+ zend_update_static_property_string(THIS_CE, ZEND_STRS("eTag")-1, etag TSRMLS_CC);
+ efree(etag);
+ }
+
+ RETURN_TRUE;
+}
+/* }}} */
+
+/* {{{ proto static resource HttpResponse::getStream()
+ Get the previously set resource to be sent. */
+PHP_METHOD(HttpResponse, getStream)
+{
+ NO_ARGS;
+
+ if (return_value_used) {
+ zval *stream = http_zsep(IS_LONG, *(zend_std_get_static_property(THIS_CE, ZEND_STRS("stream")-1, 0 TSRMLS_CC)));
+ RETVAL_RESOURCE(Z_LVAL_P(stream));
+ }
+}
+/* }}} */
+
+/* {{{ proto static bool HttpResponse::setFile(string file)
+ Set the file to be sent. */
+PHP_METHOD(HttpResponse, setFile)
+{
+ char *the_file, *etag;
+ int file_len;
+ php_stream_statbuf ssb;
+
+ if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &the_file, &file_len)) {
+ RETURN_FALSE;
+ }
+
+ if (php_stream_stat_path(the_file, &ssb)) {
+ RETURN_FALSE;
+ }
+
+ if ( (SUCCESS != zend_update_static_property_stringl(THIS_CE, ZEND_STRS("file")-1, the_file, file_len TSRMLS_CC)) ||
+ (SUCCESS != zend_update_static_property_long(THIS_CE, ZEND_STRS("mode")-1, -1 TSRMLS_CC))) {
+ RETURN_FALSE;
+ }
+
+ zend_update_static_property_long(THIS_CE, ZEND_STRS("lastModified")-1, http_last_modified(the_file, -1) TSRMLS_CC);
+ if ((etag = http_etag(the_file, 0, -1))) {
+ zend_update_static_property_string(THIS_CE, ZEND_STRS("eTag")-1, etag TSRMLS_CC);
+ efree(etag);
+ }
+
+ RETURN_TRUE;
+}
+/* }}} */
+
+/* {{{ proto static string HttpResponse::getFile()
+ Get the previously set file to be sent. */
+PHP_METHOD(HttpResponse, getFile)
+{
+ NO_ARGS;
+
+ if (return_value_used) {
+ zval *file = http_zsep(IS_STRING, *(zend_std_get_static_property(THIS_CE, ZEND_STRS("file")-1, 0 TSRMLS_CC)));
+ RETVAL_ZVAL(file, 1, 1);
+ }
+}
+/* }}} */
+
+/* {{{ proto static bool HttpResponse::send([bool clean_ob = true])
+ Finally send the entity. */
+PHP_METHOD(HttpResponse, send)
+{
+ zval *sent;
+ zend_bool clean_ob = 1;
+
+ if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &clean_ob)) {
+ RETURN_FALSE;
+ }
+
+ HTTP_CHECK_HEADERS_SENT(RETURN_FALSE);
+
+ sent = *zend_std_get_static_property(THIS_CE, ZEND_STRS("sent")-1, 0 TSRMLS_CC);
+ if (Z_LVAL_P(sent)) {
+ http_error(HE_WARNING, HTTP_E_RESPONSE, "Cannot send HttpResponse, response has already been sent");
+ RETURN_FALSE;
+ } else {
+ Z_LVAL_P(sent) = 1;
+ }
+
+ /* capture mode */
+ if (i_zend_is_true(*zend_std_get_static_property(THIS_CE, ZEND_STRS("catch")-1, 0 TSRMLS_CC))) {
+ zval *zetag, *the_data;
+
+ MAKE_STD_ZVAL(the_data);
+ php_ob_get_buffer(the_data TSRMLS_CC);
+ zend_update_static_property(THIS_CE, ZEND_STRS("data")-1, the_data TSRMLS_CC);
+ ZVAL_LONG(*zend_std_get_static_property(THIS_CE, ZEND_STRS("mode")-1, 0 TSRMLS_CC), SEND_DATA);
+
+ zetag = http_zsep(IS_STRING, *(zend_std_get_static_property(THIS_CE, ZEND_STRS("eTag")-1, 0 TSRMLS_CC)));
+ if (!Z_STRLEN_P(zetag)) {
+ char *etag = http_etag(Z_STRVAL_P(the_data), Z_STRLEN_P(the_data), SEND_DATA);
+ if (etag) {
+ zend_update_static_property_string(THIS_CE, ZEND_STRS("eTag")-1, etag TSRMLS_CC);
+ efree(etag);
+ }
+ }
+ zval_ptr_dtor(&the_data);
+ zval_ptr_dtor(&zetag);
+
+ clean_ob = 1;
+ }