release 3.2.3
[m6w6/ext-http] / src / php_http_exception.c
1 /*
2 +--------------------------------------------------------------------+
3 | PECL :: http |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2004-2014, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 #include "php_http_api.h"
14
15 #include "ext/spl/spl_exceptions.h"
16
17 #ifndef PHP_HTTP_DBG_EXCEPTIONS
18 # define PHP_HTTP_DBG_EXCEPTIONS 0
19 #endif
20
21 #if PHP_HTTP_DBG_EXCEPTIONS
22 static void php_http_exception_hook(zval *ex)
23 {
24 if (ex) {
25 zval *m = zend_read_property(Z_OBJCE_P(ex), ex, "message", lenof("message"), 0);
26 fprintf(stderr, "*** Threw exception '%s'\n", Z_STRVAL_P(m));
27 } else {
28 fprintf(stderr, "*** Threw NULL exception\n");
29 }
30 }
31 #endif
32
33 static zend_class_entry *php_http_exception_interface_class_entry;
34 zend_class_entry *php_http_get_exception_interface_class_entry(void)
35 {
36 return php_http_exception_interface_class_entry;
37 }
38 static zend_class_entry *php_http_exception_runtime_class_entry;
39 zend_class_entry *php_http_get_exception_runtime_class_entry(void)
40 {
41 return php_http_exception_runtime_class_entry;
42 }
43 static zend_class_entry *php_http_exception_unexpected_val_class_entry;
44 zend_class_entry *php_http_get_exception_unexpected_val_class_entry(void)
45 {
46 return php_http_exception_unexpected_val_class_entry;
47 }
48 static zend_class_entry *php_http_exception_bad_method_call_class_entry;
49 zend_class_entry *php_http_get_exception_bad_method_call_class_entry(void)
50 {
51 return php_http_exception_bad_method_call_class_entry;
52 }
53 static zend_class_entry *php_http_exception_invalid_arg_class_entry;
54 zend_class_entry *php_http_get_exception_invalid_arg_class_entry(void)
55 {
56 return php_http_exception_invalid_arg_class_entry;
57 }
58 static zend_class_entry *php_http_exception_bad_header_class_entry;
59 zend_class_entry *php_http_get_exception_bad_header_class_entry(void)
60 {
61 return php_http_exception_bad_header_class_entry;
62 }
63 static zend_class_entry *php_http_exception_bad_url_class_entry;
64 zend_class_entry *php_http_get_exception_bad_url_class_entry(void)
65 {
66 return php_http_exception_bad_url_class_entry;
67 }
68 static zend_class_entry *php_http_exception_bad_message_class_entry;
69 zend_class_entry *php_http_get_exception_bad_message_class_entry(void)
70 {
71 return php_http_exception_bad_message_class_entry;
72 }
73 static zend_class_entry *php_http_exception_bad_conversion_class_entry;
74 zend_class_entry *php_http_get_exception_bad_conversion_class_entry(void)
75 {
76 return php_http_exception_bad_conversion_class_entry;
77 }
78 static zend_class_entry *php_http_exception_bad_querystring_class_entry;
79 zend_class_entry *php_http_get_exception_bad_querystring_class_entry(void)
80 {
81 return php_http_exception_bad_querystring_class_entry;
82 }
83
84 PHP_MINIT_FUNCTION(http_exception)
85 {
86 zend_class_entry *cep, ce = {0};
87
88 INIT_NS_CLASS_ENTRY(ce, "http", "Exception", NULL);
89 php_http_exception_interface_class_entry = zend_register_internal_interface(&ce);
90
91 /*
92 * Would be great to only have a few exceptions and rather more identifying
93 * error codes, but zend_replace_error_handling() does not accept any codes.
94 */
95
96 memset(&ce, 0, sizeof(ce));
97 INIT_NS_CLASS_ENTRY(ce, "http\\Exception", "RuntimeException", NULL);
98 cep = zend_register_internal_class_ex(&ce, spl_ce_RuntimeException);
99 zend_class_implements(cep, 1, php_http_exception_interface_class_entry);
100 php_http_exception_runtime_class_entry = cep;
101
102 memset(&ce, 0, sizeof(ce));
103 INIT_NS_CLASS_ENTRY(ce, "http\\Exception", "UnexpectedValueException", NULL);
104 cep = zend_register_internal_class_ex(&ce, spl_ce_UnexpectedValueException);
105 zend_class_implements(cep, 1, php_http_exception_interface_class_entry);
106 php_http_exception_unexpected_val_class_entry = cep;
107
108 memset(&ce, 0, sizeof(ce));
109 INIT_NS_CLASS_ENTRY(ce, "http\\Exception", "BadMethodCallException", NULL);
110 cep = zend_register_internal_class_ex(&ce, spl_ce_BadMethodCallException);
111 zend_class_implements(cep, 1, php_http_exception_interface_class_entry);
112 php_http_exception_bad_method_call_class_entry = cep;
113
114 memset(&ce, 0, sizeof(ce));
115 INIT_NS_CLASS_ENTRY(ce, "http\\Exception", "InvalidArgumentException", NULL);
116 cep = zend_register_internal_class_ex(&ce, spl_ce_InvalidArgumentException);
117 zend_class_implements(cep, 1, php_http_exception_interface_class_entry);
118 php_http_exception_invalid_arg_class_entry = cep;
119
120 memset(&ce, 0, sizeof(ce));
121 INIT_NS_CLASS_ENTRY(ce, "http\\Exception", "BadHeaderException", NULL);
122 cep = zend_register_internal_class_ex(&ce, spl_ce_DomainException);
123 zend_class_implements(cep, 1, php_http_exception_interface_class_entry);
124 php_http_exception_bad_header_class_entry = cep;
125
126 memset(&ce, 0, sizeof(ce));
127 INIT_NS_CLASS_ENTRY(ce, "http\\Exception", "BadUrlException", NULL);
128 cep = zend_register_internal_class_ex(&ce, spl_ce_DomainException);
129 zend_class_implements(cep, 1, php_http_exception_interface_class_entry);
130 php_http_exception_bad_url_class_entry = cep;
131
132 memset(&ce, 0, sizeof(ce));
133 INIT_NS_CLASS_ENTRY(ce, "http\\Exception", "BadMessageException", NULL);
134 cep = zend_register_internal_class_ex(&ce, spl_ce_DomainException);
135 zend_class_implements(cep, 1, php_http_exception_interface_class_entry);
136 php_http_exception_bad_message_class_entry = cep;
137
138 memset(&ce, 0, sizeof(ce));
139 INIT_NS_CLASS_ENTRY(ce, "http\\Exception", "BadConversionException", NULL);
140 cep = zend_register_internal_class_ex(&ce, spl_ce_DomainException);
141 zend_class_implements(cep, 1, php_http_exception_interface_class_entry);
142 php_http_exception_bad_conversion_class_entry = cep;
143
144 memset(&ce, 0, sizeof(ce));
145 INIT_NS_CLASS_ENTRY(ce, "http\\Exception", "BadQueryStringException", NULL);
146 cep = zend_register_internal_class_ex(&ce, spl_ce_DomainException);
147 zend_class_implements(cep, 1, php_http_exception_interface_class_entry);
148 php_http_exception_bad_querystring_class_entry = cep;
149
150 #if PHP_HTTP_DBG_EXCEPTIONS
151 zend_throw_exception_hook = php_http_exception_hook;
152 #endif
153
154 return SUCCESS;
155 }
156
157 /*
158 * Local variables:
159 * tab-width: 4
160 * c-basic-offset: 4
161 * End:
162 * vim600: noet sw=4 ts=4 fdm=marker
163 * vim<600: noet sw=4 ts=4
164 */
165