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