add stat and etag methods
authorMichael Wallner <mike@php.net>
Sat, 14 Jan 2012 10:49:39 +0000 (10:49 +0000)
committerMichael Wallner <mike@php.net>
Sat, 14 Jan 2012 10:49:39 +0000 (10:49 +0000)
php_http_message_body.c
php_http_message_body.h

index 4ff36b5666c73bd9af3a467f933d7e16236cc0d8..b2d4ff8fb98efacc3c6940a8844545d2aec53021 100644 (file)
@@ -115,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);
@@ -415,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[] = {
@@ -425,6 +430,8 @@ 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;
@@ -610,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
index 74936c0200b215e1685978517a5b57342d6c9bf0..9147cbfde4e8aa3bac826e6b7ddda3937dbee135 100644 (file)
@@ -67,6 +67,8 @@ PHP_METHOD(HttpMessageBody, toStream);
 PHP_METHOD(HttpMessageBody, toCallback);
 PHP_METHOD(HttpMessageBody, append);
 PHP_METHOD(HttpMessageBody, add);
+PHP_METHOD(HttpMessageBody, etag);
+PHP_METHOD(HttpMessageBody, stat);
 
 #endif