back to dev
[m6w6/ext-http] / 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 TSRMLS_DC)
23 {
24 if (ex) {
25 zval *m = zend_read_property(Z_OBJCE_P(ex), ex, "message", lenof("message"), 0 TSRMLS_CC);
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 zend_class_entry *php_http_exception_interface_class_entry;
34 zend_class_entry *php_http_exception_runtime_class_entry;
35 zend_class_entry *php_http_exception_unexpected_val_class_entry;
36 zend_class_entry *php_http_exception_bad_method_call_class_entry;
37 zend_class_entry *php_http_exception_invalid_arg_class_entry;
38 zend_class_entry *php_http_exception_bad_header_class_entry;
39 zend_class_entry *php_http_exception_bad_url_class_entry;
40 zend_class_entry *php_http_exception_bad_message_class_entry;
41 zend_class_entry *php_http_exception_bad_conversion_class_entry;
42 zend_class_entry *php_http_exception_bad_querystring_class_entry;
43
44 PHP_MINIT_FUNCTION(http_exception)
45 {
46 zend_class_entry *cep, ce = {0};
47
48 INIT_NS_CLASS_ENTRY(ce, "http", "Exception", NULL);
49 php_http_exception_interface_class_entry = zend_register_internal_interface(&ce TSRMLS_CC);
50
51 /*
52 * Would be great to only have a few exceptions and rather more identifying
53 * error codes, but zend_replace_error_handling() does not accept any codes.
54 */
55
56 memset(&ce, 0, sizeof(ce));
57 INIT_NS_CLASS_ENTRY(ce, "http\\Exception", "RuntimeException", NULL);
58 cep = zend_register_internal_class_ex(&ce, spl_ce_RuntimeException, NULL TSRMLS_CC);
59 zend_class_implements(cep TSRMLS_CC, 1, php_http_exception_interface_class_entry);
60 php_http_exception_runtime_class_entry = cep;
61
62 memset(&ce, 0, sizeof(ce));
63 INIT_NS_CLASS_ENTRY(ce, "http\\Exception", "UnexpectedValueException", NULL);
64 cep = zend_register_internal_class_ex(&ce, spl_ce_UnexpectedValueException, NULL TSRMLS_CC);
65 zend_class_implements(cep TSRMLS_CC, 1, php_http_exception_interface_class_entry);
66 php_http_exception_unexpected_val_class_entry = cep;
67
68 memset(&ce, 0, sizeof(ce));
69 INIT_NS_CLASS_ENTRY(ce, "http\\Exception", "BadMethodCallException", NULL);
70 cep = zend_register_internal_class_ex(&ce, spl_ce_BadMethodCallException, NULL TSRMLS_CC);
71 zend_class_implements(cep TSRMLS_CC, 1, php_http_exception_interface_class_entry);
72 php_http_exception_bad_method_call_class_entry = cep;
73
74 memset(&ce, 0, sizeof(ce));
75 INIT_NS_CLASS_ENTRY(ce, "http\\Exception", "InvalidArgumentException", NULL);
76 cep = zend_register_internal_class_ex(&ce, spl_ce_InvalidArgumentException, NULL TSRMLS_CC);
77 zend_class_implements(cep TSRMLS_CC, 1, php_http_exception_interface_class_entry);
78 php_http_exception_invalid_arg_class_entry = cep;
79
80 memset(&ce, 0, sizeof(ce));
81 INIT_NS_CLASS_ENTRY(ce, "http\\Exception", "BadHeaderException", NULL);
82 cep = zend_register_internal_class_ex(&ce, spl_ce_DomainException, NULL TSRMLS_CC);
83 zend_class_implements(cep TSRMLS_CC, 1, php_http_exception_interface_class_entry);
84 php_http_exception_bad_header_class_entry = cep;
85
86 memset(&ce, 0, sizeof(ce));
87 INIT_NS_CLASS_ENTRY(ce, "http\\Exception", "BadUrlException", NULL);
88 cep = zend_register_internal_class_ex(&ce, spl_ce_DomainException, NULL TSRMLS_CC);
89 zend_class_implements(cep TSRMLS_CC, 1, php_http_exception_interface_class_entry);
90 php_http_exception_bad_url_class_entry = cep;
91
92 memset(&ce, 0, sizeof(ce));
93 INIT_NS_CLASS_ENTRY(ce, "http\\Exception", "BadMessageException", NULL);
94 cep = zend_register_internal_class_ex(&ce, spl_ce_DomainException, NULL TSRMLS_CC);
95 zend_class_implements(cep TSRMLS_CC, 1, php_http_exception_interface_class_entry);
96 php_http_exception_bad_message_class_entry = cep;
97
98 memset(&ce, 0, sizeof(ce));
99 INIT_NS_CLASS_ENTRY(ce, "http\\Exception", "BadConversionException", NULL);
100 cep = zend_register_internal_class_ex(&ce, spl_ce_DomainException, NULL TSRMLS_CC);
101 zend_class_implements(cep TSRMLS_CC, 1, php_http_exception_interface_class_entry);
102 php_http_exception_bad_conversion_class_entry = cep;
103
104 memset(&ce, 0, sizeof(ce));
105 INIT_NS_CLASS_ENTRY(ce, "http\\Exception", "BadQueryStringException", NULL);
106 cep = zend_register_internal_class_ex(&ce, spl_ce_DomainException, NULL TSRMLS_CC);
107 zend_class_implements(cep TSRMLS_CC, 1, php_http_exception_interface_class_entry);
108 php_http_exception_bad_querystring_class_entry = cep;
109
110 #if PHP_HTTP_DBG_EXCEPTIONS
111 zend_throw_exception_hook = php_http_exception_hook;
112 #endif
113
114 return SUCCESS;
115 }
116
117 /*
118 * Local variables:
119 * tab-width: 4
120 * c-basic-offset: 4
121 * End:
122 * vim600: noet sw=4 ts=4 fdm=marker
123 * vim<600: noet sw=4 ts=4
124 */
125