+ char *file, *cd;
+ int file_len;
+ zend_bool send_inline = 0;
+
+#define HTTP_CONTENTDISPOSITION_TEMPLATE "%s; filename=\"%s\""
+
+ if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &file, &file_len, &send_inline)) {
+ RETURN_FALSE;
+ }
+
+ spprintf(&cd, 0, HTTP_CONTENTDISPOSITION_TEMPLATE, send_inline ? "inline" : "attachment", file);
+ SET_STATIC_PROP_STRING(contentDisposition, cd, 0);
+ RETURN_TRUE;
+}
+/* }}} */
+
+/* {{{ proto string HttpResponse::getContentDisposition()
+ *
+ * Get current Content-Disposition setting.
+ */
+PHP_METHOD(HttpResponse, getContentDisposition)
+{
+ NO_ARGS;
+
+ IF_RETVAL_USED {
+ zval *cd = GET_STATIC_PROP(contentDisposition);
+ RETURN_STRINGL(Z_STRVAL_P(cd), Z_STRLEN_P(cd), 1);
+ }
+}
+/* }}} */
+
+/* {{{ proto 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;
+ }
+
+ USE_STATIC_PROP();
+ SET_STATIC_PROP_STRINGL(eTag, etag, etag_len, 1);
+ RETURN_TRUE;
+}
+/* }}} */
+
+/* {{{ proto string HttpResponse::getETag()
+ *
+ * Get the previously set custom ETag.
+ */
+PHP_METHOD(HttpResponse, getETag)
+{
+ NO_ARGS;
+
+ IF_RETVAL_USED {
+ zval *etag = GET_STATIC_PROP(eTag);
+ RETURN_STRINGL(Z_STRVAL_P(etag), Z_STRLEN_P(etag), 1);
+ }
+}
+/* }}} */
+
+/* {{{ proto void HttpResponse::setThrottleDelay(double seconds)
+ *
+ */
+PHP_METHOD(HttpResponse, setThrottleDelay)
+{
+ double seconds;
+
+ if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d", &seconds)) {
+ ZVAL_DOUBLE(GET_STATIC_PROP(throttleDelay), seconds);
+ }
+}
+/* }}} */
+
+/* {{{ proto double HttpResponse::getThrottleDelay()
+ *
+ */
+PHP_METHOD(HttpResponse, getThrottleDelay)
+{
+ NO_ARGS;
+
+ IF_RETVAL_USED {
+ RETURN_DOUBLE(Z_DVAL_P(GET_STATIC_PROP(throttleDelay)));
+ }
+}
+/* }}} */
+
+/* {{{ proto void HttpResponse::setBufferSize(long bytes)
+ *
+ */
+PHP_METHOD(HttpResponse, setBufferSize)
+{
+ long bytes;
+
+ if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &bytes)) {
+ ZVAL_LONG(GET_STATIC_PROP(bufferSize), bytes);
+ }
+}
+/* }}} */
+
+/* {{{ proto long HttpResponse::getBufferSize()
+ *
+ */
+PHP_METHOD(HttpResponse, getBufferSize)
+{
+ NO_ARGS;
+
+ IF_RETVAL_USED {
+ RETURN_LONG(Z_LVAL_P(GET_STATIC_PROP(bufferSize)));
+ }
+}
+/* }}} */
+
+/* {{{ proto bool HttpResponse::setData(string data)
+ *
+ * Set the data to be sent.
+ */
+PHP_METHOD(HttpResponse, setData)
+{
+ zval *the_data, **data;
+
+ if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &the_data)) {
+ RETURN_FALSE;
+ }
+ convert_to_string_ex(&the_data);
+
+ USE_STATIC_PROP();
+ SET_STATIC_PROP(data, the_data);
+ ZVAL_LONG(GET_STATIC_PROP(lastModified), http_last_modified(the_data, SEND_DATA));
+ ZVAL_LONG(GET_STATIC_PROP(mode), SEND_DATA);
+ if (!Z_STRLEN_P(GET_STATIC_PROP(eTag))) {
+ SET_STATIC_PROP_STRING(eTag, http_etag(Z_STRVAL_P(the_data), Z_STRLEN_P(the_data), SEND_DATA), 0);
+ }
+
+ RETURN_TRUE;
+}
+/* }}} */
+
+/* {{{ proto string HttpResponse::getData()
+ *
+ * Get the previously set data to be sent.
+ */
+PHP_METHOD(HttpResponse, getData)
+{
+ NO_ARGS;
+
+ IF_RETVAL_USED {
+ zval *the_data = GET_STATIC_PROP(data);
+ RETURN_STRINGL(Z_STRVAL_P(the_data), Z_STRLEN_P(the_data), 1);
+ }
+}
+/* }}} */
+
+/* {{{ proto bool HttpResponse::setStream(resource stream)
+ *
+ * Set the resource to be sent.
+ */
+PHP_METHOD(HttpResponse, setStream)
+{
+ zval *the_stream;
+ php_stream *the_real_stream;
+
+ if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &the_stream)) {
+ RETURN_FALSE;
+ }
+ zend_list_addref(Z_LVAL_P(the_stream));
+ php_stream_from_zval(the_real_stream, &the_stream);
+
+ USE_STATIC_PROP();
+ ZVAL_LONG(GET_STATIC_PROP(stream), Z_LVAL_P(the_stream));
+ ZVAL_LONG(GET_STATIC_PROP(lastModified), http_last_modified(the_real_stream, SEND_RSRC));
+ ZVAL_LONG(GET_STATIC_PROP(mode), SEND_RSRC);
+ if (!Z_STRLEN_P(GET_STATIC_PROP(eTag))) {
+ SET_STATIC_PROP_STRING(eTag, http_etag(the_real_stream, 0, SEND_RSRC), 0);
+ }
+
+ RETURN_TRUE;
+}
+/* }}} */
+
+/* {{{ proto resource HttpResponse::getStream()
+ *
+ * Get the previously set resource to be sent.
+ */
+PHP_METHOD(HttpResponse, getStream)
+{
+ NO_ARGS;
+
+ IF_RETVAL_USED {
+ RETURN_RESOURCE(Z_LVAL_P(GET_STATIC_PROP(stream)));
+ }
+}
+/* }}} */
+
+/* {{{ proto bool HttpResponse::setFile(string file)
+ *
+ * Set the file to be sent.
+ */
+PHP_METHOD(HttpResponse, setFile)
+{
+ zval *the_file;
+
+ if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &the_file)) {
+ RETURN_FALSE;
+ }
+
+ convert_to_string_ex(&the_file);
+ USE_STATIC_PROP();
+ SET_STATIC_PROP(file, the_file);
+ ZVAL_LONG(GET_STATIC_PROP(lastModified), http_last_modified(the_file, -1));
+ ZVAL_LONG(GET_STATIC_PROP(mode), -1);
+ if (!Z_STRLEN_P(GET_STATIC_PROP(eTag))) {
+ SET_STATIC_PROP_STRING(eTag, http_etag(the_file, 0, -1), 0);
+ }
+
+ RETURN_TRUE;
+}
+/* }}} */
+
+/* {{{ proto string HttpResponse::getFile()
+ *
+ * Get the previously set file to be sent.
+ */
+PHP_METHOD(HttpResponse, getFile)
+{
+ NO_ARGS;
+
+ IF_RETVAL_USED {
+ zval *the_file = GET_STATIC_PROP(file);
+ RETURN_STRINGL(Z_STRVAL_P(the_file), Z_STRLEN_P(the_file), 1);
+ }
+}
+/* }}} */
+
+/* {{{ proto bool HttpResponse::send([bool clean_ob = true])
+ *
+ * Finally send the entity.
+ *
+ * Example:
+ * <pre>
+ * <?php
+ * HttpResponse::setCache(true);
+ * HttpResponse::setContentType('application/pdf');
+ * HttpResponse::setContentDisposition("$user.pdf", false);
+ * HttpResponse::setFile('sheet.pdf');
+ * HttpResponse::send();
+ * ?>
+ * </pre>
+ */
+PHP_METHOD(HttpResponse, send)
+{
+ zval *do_cache, *do_gzip;
+ zend_bool clean_ob = 1;
+
+ if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &clean_ob)) {
+ RETURN_FALSE;
+ }
+ if (SG(headers_sent)) {
+ http_error(E_WARNING, HTTP_E_HEADER, "Cannot send HttpResponse, headers have already been sent");
+ RETURN_FALSE;
+ }
+
+ if (clean_ob) {
+ /* interrupt on-the-fly etag generation */
+ HTTP_G(etag).started = 0;
+ /* discard previous output buffers */
+ php_end_ob_buffers(0 TSRMLS_CC);
+ }
+
+ /* gzip */
+ if (Z_LVAL_P(GET_STATIC_PROP(gzip))) {
+ php_start_ob_buffer_named("ob_gzhandler", 0, 0 TSRMLS_CC);
+ } else {
+ php_start_ob_buffer(NULL, 0, 0 TSRMLS_CC);
+ }
+
+ /* caching */
+ if (Z_LVAL_P(GET_STATIC_PROP(cache))) {
+ char *cc_hdr;
+ int cc_len;
+ zval *cctl, *etag, *lmod;
+
+ etag = GET_STATIC_PROP(eTag);
+ lmod = GET_STATIC_PROP(lastModified);
+ cctl = GET_STATIC_PROP(cacheControl);
+
+ http_cache_etag(Z_STRVAL_P(etag), Z_STRLEN_P(etag), Z_STRVAL_P(cctl), Z_STRLEN_P(cctl));
+ http_cache_last_modified(Z_LVAL_P(lmod), Z_LVAL_P(lmod) ? Z_LVAL_P(lmod) : time(NULL), Z_STRVAL_P(cctl), Z_STRLEN_P(cctl));
+ }
+
+ /* content type */
+ {
+ zval *ctype = GET_STATIC_PROP(contentType);
+ if (Z_STRLEN_P(ctype)) {
+ http_send_content_type(Z_STRVAL_P(ctype), Z_STRLEN_P(ctype));
+ } else {
+ char *ctypes = INI_STR("default_mimetype");
+ size_t ctlen = ctypes ? strlen(ctypes) : 0;
+
+ if (ctlen) {
+ http_send_content_type(ctypes, ctlen);
+ } else {
+ http_send_content_type("application/x-octetstream", lenof("application/x-octetstream"));
+ }
+ }
+ }
+
+ /* content disposition */
+ {
+ zval *cd = GET_STATIC_PROP(contentDisposition);
+ if (Z_STRLEN_P(cd)) {
+ char *cds;
+
+ spprintf(&cds, 0, "Content-Disposition: %s", Z_STRVAL_P(cd));
+ http_send_header(cds);
+ efree(cds);
+ }
+ }
+
+ /* throttling */
+ {
+ HTTP_G(send).buffer_size = Z_LVAL_P(GET_STATIC_PROP(bufferSize));
+ HTTP_G(send).throttle_delay = Z_DVAL_P(GET_STATIC_PROP(throttleDelay));
+ }