Line data Source code
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 374 : PHP_MINIT_FUNCTION(http_exception)
45 : {
46 374 : zend_class_entry *cep, ce = {0};
47 :
48 374 : INIT_NS_CLASS_ENTRY(ce, "http", "Exception", NULL);
49 374 : 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 374 : memset(&ce, 0, sizeof(ce));
57 374 : INIT_NS_CLASS_ENTRY(ce, "http\\Exception", "RuntimeException", NULL);
58 374 : cep = zend_register_internal_class_ex(&ce, spl_ce_RuntimeException, NULL TSRMLS_CC);
59 374 : zend_class_implements(cep TSRMLS_CC, 1, php_http_exception_interface_class_entry);
60 374 : php_http_exception_runtime_class_entry = cep;
61 :
62 374 : memset(&ce, 0, sizeof(ce));
63 374 : INIT_NS_CLASS_ENTRY(ce, "http\\Exception", "UnexpectedValueException", NULL);
64 374 : cep = zend_register_internal_class_ex(&ce, spl_ce_UnexpectedValueException, NULL TSRMLS_CC);
65 374 : zend_class_implements(cep TSRMLS_CC, 1, php_http_exception_interface_class_entry);
66 374 : php_http_exception_unexpected_val_class_entry = cep;
67 :
68 374 : memset(&ce, 0, sizeof(ce));
69 374 : INIT_NS_CLASS_ENTRY(ce, "http\\Exception", "BadMethodCallException", NULL);
70 374 : cep = zend_register_internal_class_ex(&ce, spl_ce_BadMethodCallException, NULL TSRMLS_CC);
71 374 : zend_class_implements(cep TSRMLS_CC, 1, php_http_exception_interface_class_entry);
72 374 : php_http_exception_bad_method_call_class_entry = cep;
73 :
74 374 : memset(&ce, 0, sizeof(ce));
75 374 : INIT_NS_CLASS_ENTRY(ce, "http\\Exception", "InvalidArgumentException", NULL);
76 374 : cep = zend_register_internal_class_ex(&ce, spl_ce_InvalidArgumentException, NULL TSRMLS_CC);
77 374 : zend_class_implements(cep TSRMLS_CC, 1, php_http_exception_interface_class_entry);
78 374 : php_http_exception_invalid_arg_class_entry = cep;
79 :
80 374 : memset(&ce, 0, sizeof(ce));
81 374 : INIT_NS_CLASS_ENTRY(ce, "http\\Exception", "BadHeaderException", NULL);
82 374 : cep = zend_register_internal_class_ex(&ce, spl_ce_DomainException, NULL TSRMLS_CC);
83 374 : zend_class_implements(cep TSRMLS_CC, 1, php_http_exception_interface_class_entry);
84 374 : php_http_exception_bad_header_class_entry = cep;
85 :
86 374 : memset(&ce, 0, sizeof(ce));
87 374 : INIT_NS_CLASS_ENTRY(ce, "http\\Exception", "BadUrlException", NULL);
88 374 : cep = zend_register_internal_class_ex(&ce, spl_ce_DomainException, NULL TSRMLS_CC);
89 374 : zend_class_implements(cep TSRMLS_CC, 1, php_http_exception_interface_class_entry);
90 374 : php_http_exception_bad_url_class_entry = cep;
91 :
92 374 : memset(&ce, 0, sizeof(ce));
93 374 : INIT_NS_CLASS_ENTRY(ce, "http\\Exception", "BadMessageException", NULL);
94 374 : cep = zend_register_internal_class_ex(&ce, spl_ce_DomainException, NULL TSRMLS_CC);
95 374 : zend_class_implements(cep TSRMLS_CC, 1, php_http_exception_interface_class_entry);
96 374 : php_http_exception_bad_message_class_entry = cep;
97 :
98 374 : memset(&ce, 0, sizeof(ce));
99 374 : INIT_NS_CLASS_ENTRY(ce, "http\\Exception", "BadConversionException", NULL);
100 374 : cep = zend_register_internal_class_ex(&ce, spl_ce_DomainException, NULL TSRMLS_CC);
101 374 : zend_class_implements(cep TSRMLS_CC, 1, php_http_exception_interface_class_entry);
102 374 : php_http_exception_bad_conversion_class_entry = cep;
103 :
104 374 : memset(&ce, 0, sizeof(ce));
105 374 : INIT_NS_CLASS_ENTRY(ce, "http\\Exception", "BadQueryStringException", NULL);
106 374 : cep = zend_register_internal_class_ex(&ce, spl_ce_DomainException, NULL TSRMLS_CC);
107 374 : zend_class_implements(cep TSRMLS_CC, 1, php_http_exception_interface_class_entry);
108 374 : 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 374 : 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 :
|