add stat and etag methods
[m6w6/ext-http] / php_http_message_body.c
index ad92a1747efe5dd5cfd6fe25a37bd1bf76445555..b2d4ff8fb98efacc3c6940a8844545d2aec53021 100644 (file)
@@ -6,17 +6,13 @@
     | modification, are permitted provided that the conditions mentioned |
     | in the accompanying LICENSE file are met.                          |
     +--------------------------------------------------------------------+
-    | Copyright (c) 2004-2010, Michael Wallner <mike@php.net>            |
+    | Copyright (c) 2004-2011, Michael Wallner <mike@php.net>            |
     +--------------------------------------------------------------------+
 */
 
-/* $Id: http_message_body.c 292841 2009-12-31 08:48:57Z mike $ */
+#include "php_http_api.h"
 
-#include "php_http.h"
-
-#include <libgen.h>
 #include <ext/standard/php_lcg.h>
-#include <ext/standard/php_string.h>
 
 #define BOUNDARY_OPEN(body) \
        do {\
@@ -119,7 +115,7 @@ PHP_HTTP_API char *php_http_message_body_etag(php_http_message_body_t *body)
        const php_stream_statbuf *ssb = php_http_message_body_stat(body);
 
        /* real file or temp buffer ? */
-       if (body->ssb.sb.st_mtime) {
+       if (ssb && ssb->sb.st_mtime) {
                char *etag;
 
                spprintf(&etag, 0, "%lx-%lx-%lx", ssb->sb.st_ino, ssb->sb.st_mtime, ssb->sb.st_size);
@@ -419,6 +415,11 @@ PHP_HTTP_BEGIN_ARGS(add, 0)
        PHP_HTTP_ARG_VAL(files, 0)
 PHP_HTTP_END_ARGS;
 
+PHP_HTTP_EMPTY_ARGS(etag);
+
+PHP_HTTP_BEGIN_ARGS(stat, 0)
+       PHP_HTTP_ARG_VAL(what, 0)
+PHP_HTTP_END_ARGS;
 
 zend_class_entry *php_http_message_body_class_entry;
 zend_function_entry php_http_message_body_method_entry[] = {
@@ -429,13 +430,15 @@ zend_function_entry php_http_message_body_method_entry[] = {
        PHP_HTTP_MESSAGE_BODY_ME(toCallback, ZEND_ACC_PUBLIC)
        PHP_HTTP_MESSAGE_BODY_ME(append, ZEND_ACC_PUBLIC)
        PHP_HTTP_MESSAGE_BODY_ME(add, ZEND_ACC_PUBLIC)
+       PHP_HTTP_MESSAGE_BODY_ME(etag, ZEND_ACC_PUBLIC)
+       PHP_HTTP_MESSAGE_BODY_ME(stat, ZEND_ACC_PUBLIC)
        EMPTY_FUNCTION_ENTRY
 };
 static zend_object_handlers php_http_message_body_object_handlers;
 
 PHP_MINIT_FUNCTION(http_message_body)
 {
-       PHP_HTTP_REGISTER_CLASS(http\\message, Body, http_message_body, php_http_object_class_entry, 0);
+       PHP_HTTP_REGISTER_CLASS(http\\Message, Body, http_message_body, php_http_object_class_entry, 0);
        php_http_message_body_class_entry->create_object = php_http_message_body_object_new;
        memcpy(&php_http_message_body_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
        php_http_message_body_object_handlers.clone_obj = php_http_message_body_object_clone;
@@ -614,6 +617,65 @@ PHP_METHOD(HttpMessageBody, add)
        }
        RETURN_FALSE;
 }
+
+PHP_METHOD(HttpMessageBody, etag)
+{
+       if (SUCCESS == zend_parse_parameters_none()) {
+               php_http_message_body_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+               char *etag = php_http_message_body_etag(obj->body);
+
+               if (etag) {
+                       RETURN_STRING(etag, 0);
+               }
+       }
+       RETURN_FALSE;
+}
+
+PHP_METHOD(HttpMessageBody, stat)
+{
+       char *field_str = NULL;
+       int field_len = 0;
+
+       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &field_str, &field_len)) {
+               php_http_message_body_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+               const php_stream_statbuf *sb = php_http_message_body_stat(obj->body);
+
+               if (sb) {
+                       if (field_str && field_len) {
+                                       switch (*field_str) {
+                                               case 's':
+                                               case 'S':
+                                                       RETURN_LONG(sb->sb.st_size);
+                                                       break;
+                                               case 'a':
+                                               case 'A':
+                                                       RETURN_LONG(sb->sb.st_atime);
+                                                       break;
+                                               case 'm':
+                                               case 'M':
+                                                       RETURN_LONG(sb->sb.st_mtime);
+                                                       break;
+                                               case 'c':
+                                               case 'C':
+                                                       RETURN_LONG(sb->sb.st_ctime);
+                                                       break;
+                                               default:
+                                                       php_http_error(HE_WARNING, PHP_HTTP_E_MESSAGE_BODY, "unknown stat field: '%s' (should be one of [s]ize, [a]time, [m]time or [c]time)", field_str);
+                                                       break;
+                                       }
+                       } else {
+                               array_init(return_value);
+                               add_assoc_long_ex(return_value, ZEND_STRS("size"), sb->sb.st_size);
+                               add_assoc_long_ex(return_value, ZEND_STRS("atime"), sb->sb.st_atime);
+                               add_assoc_long_ex(return_value, ZEND_STRS("mtime"), sb->sb.st_mtime);
+                               add_assoc_long_ex(return_value, ZEND_STRS("ctime"), sb->sb.st_ctime);
+                               return;
+                       }
+               }
+       }
+       RETURN_FALSE;
+}
+
 /*
  * Local variables:
  * tab-width: 4