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