php_stream_context.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | https://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Author: Wez Furlong <wez@thebrainroom.com> |
  14. +----------------------------------------------------------------------+
  15. */
  16. /* Stream context and status notification related definitions */
  17. /* callback for status notifications */
  18. typedef void (*php_stream_notification_func)(php_stream_context *context,
  19. int notifycode, int severity,
  20. char *xmsg, int xcode,
  21. size_t bytes_sofar, size_t bytes_max,
  22. void * ptr);
  23. #define PHP_STREAM_NOTIFIER_PROGRESS 1
  24. /* Attempt to fetch context from the zval passed,
  25. If no context was passed, use the default context
  26. The default context has not yet been created, do it now. */
  27. #define php_stream_context_from_zval(zcontext, nocontext) ( \
  28. (zcontext) ? zend_fetch_resource_ex(zcontext, "Stream-Context", php_le_stream_context()) : \
  29. (nocontext) ? NULL : \
  30. FG(default_context) ? FG(default_context) : \
  31. (FG(default_context) = php_stream_context_alloc()) )
  32. #define php_stream_context_to_zval(context, zval) { ZVAL_RES(zval, (context)->res); GC_ADDREF((context)->res); }
  33. typedef struct _php_stream_notifier php_stream_notifier;
  34. struct _php_stream_notifier {
  35. php_stream_notification_func func;
  36. void (*dtor)(php_stream_notifier *notifier);
  37. zval ptr;
  38. int mask;
  39. size_t progress, progress_max; /* position for progress notification */
  40. };
  41. struct _php_stream_context {
  42. php_stream_notifier *notifier;
  43. zval options; /* hash keyed by wrapper family or specific wrapper */
  44. zend_resource *res; /* used for auto-cleanup */
  45. };
  46. BEGIN_EXTERN_C()
  47. PHPAPI void php_stream_context_free(php_stream_context *context);
  48. PHPAPI php_stream_context *php_stream_context_alloc(void);
  49. PHPAPI zval *php_stream_context_get_option(php_stream_context *context,
  50. const char *wrappername, const char *optionname);
  51. PHPAPI int php_stream_context_set_option(php_stream_context *context,
  52. const char *wrappername, const char *optionname, zval *optionvalue);
  53. PHPAPI php_stream_notifier *php_stream_notification_alloc(void);
  54. PHPAPI void php_stream_notification_free(php_stream_notifier *notifier);
  55. END_EXTERN_C()
  56. /* not all notification codes are implemented */
  57. #define PHP_STREAM_NOTIFY_RESOLVE 1
  58. #define PHP_STREAM_NOTIFY_CONNECT 2
  59. #define PHP_STREAM_NOTIFY_AUTH_REQUIRED 3
  60. #define PHP_STREAM_NOTIFY_MIME_TYPE_IS 4
  61. #define PHP_STREAM_NOTIFY_FILE_SIZE_IS 5
  62. #define PHP_STREAM_NOTIFY_REDIRECTED 6
  63. #define PHP_STREAM_NOTIFY_PROGRESS 7
  64. #define PHP_STREAM_NOTIFY_COMPLETED 8
  65. #define PHP_STREAM_NOTIFY_FAILURE 9
  66. #define PHP_STREAM_NOTIFY_AUTH_RESULT 10
  67. #define PHP_STREAM_NOTIFY_SEVERITY_INFO 0
  68. #define PHP_STREAM_NOTIFY_SEVERITY_WARN 1
  69. #define PHP_STREAM_NOTIFY_SEVERITY_ERR 2
  70. BEGIN_EXTERN_C()
  71. PHPAPI void php_stream_notification_notify(php_stream_context *context, int notifycode, int severity,
  72. char *xmsg, int xcode, size_t bytes_sofar, size_t bytes_max, void * ptr);
  73. PHPAPI php_stream_context *php_stream_context_set(php_stream *stream, php_stream_context *context);
  74. END_EXTERN_C()
  75. #define php_stream_notify_info(context, code, xmsg, xcode) do { if ((context) && (context)->notifier) { \
  76. php_stream_notification_notify((context), (code), PHP_STREAM_NOTIFY_SEVERITY_INFO, \
  77. (xmsg), (xcode), 0, 0, NULL); } } while (0)
  78. #define php_stream_notify_progress(context, bsofar, bmax) do { if ((context) && (context)->notifier) { \
  79. php_stream_notification_notify((context), PHP_STREAM_NOTIFY_PROGRESS, PHP_STREAM_NOTIFY_SEVERITY_INFO, \
  80. NULL, 0, (bsofar), (bmax), NULL); } } while(0)
  81. #define php_stream_notify_progress_init(context, sofar, bmax) do { if ((context) && (context)->notifier) { \
  82. (context)->notifier->progress = (sofar); \
  83. (context)->notifier->progress_max = (bmax); \
  84. (context)->notifier->mask |= PHP_STREAM_NOTIFIER_PROGRESS; \
  85. php_stream_notify_progress((context), (sofar), (bmax)); } } while (0)
  86. #define php_stream_notify_progress_increment(context, dsofar, dmax) do { if ((context) && (context)->notifier && ((context)->notifier->mask & PHP_STREAM_NOTIFIER_PROGRESS)) { \
  87. (context)->notifier->progress += (dsofar); \
  88. (context)->notifier->progress_max += (dmax); \
  89. php_stream_notify_progress((context), (context)->notifier->progress, (context)->notifier->progress_max); } } while (0)
  90. #define php_stream_notify_file_size(context, file_size, xmsg, xcode) do { if ((context) && (context)->notifier) { \
  91. php_stream_notification_notify((context), PHP_STREAM_NOTIFY_FILE_SIZE_IS, PHP_STREAM_NOTIFY_SEVERITY_INFO, \
  92. (xmsg), (xcode), 0, (file_size), NULL); } } while(0)
  93. #define php_stream_notify_error(context, code, xmsg, xcode) do { if ((context) && (context)->notifier) {\
  94. php_stream_notification_notify((context), (code), PHP_STREAM_NOTIFY_SEVERITY_ERR, \
  95. (xmsg), (xcode), 0, 0, NULL); } } while(0)