File: php_http_exception.c

package info (click to toggle)
php-pecl-http 2.0.4-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,436 kB
  • ctags: 1,839
  • sloc: ansic: 15,322; php: 726; xml: 254; makefile: 10; pascal: 3
file content (125 lines) | stat: -rw-r--r-- 5,363 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
    +--------------------------------------------------------------------+
    | PECL :: http                                                       |
    +--------------------------------------------------------------------+
    | Redistribution and use in source and binary forms, with or without |
    | modification, are permitted provided that the conditions mentioned |
    | in the accompanying LICENSE file are met.                          |
    +--------------------------------------------------------------------+
    | Copyright (c) 2004-2014, Michael Wallner <mike@php.net>            |
    +--------------------------------------------------------------------+
*/

#include "php_http_api.h"

#include <ext/spl/spl_exceptions.h>

#ifndef PHP_HTTP_DBG_EXCEPTIONS
#	define PHP_HTTP_DBG_EXCEPTIONS 0
#endif

#if PHP_HTTP_DBG_EXCEPTIONS
static void php_http_exception_hook(zval *ex TSRMLS_DC)
{
	if (ex) {
		zval *m = zend_read_property(Z_OBJCE_P(ex), ex, "message", lenof("message"), 0 TSRMLS_CC);
		fprintf(stderr, "*** Threw exception '%s'\n", Z_STRVAL_P(m));
	} else {
		fprintf(stderr, "*** Threw NULL exception\n");
	}
}
#endif

zend_class_entry *php_http_exception_interface_class_entry;
zend_class_entry *php_http_exception_runtime_class_entry;
zend_class_entry *php_http_exception_unexpected_val_class_entry;
zend_class_entry *php_http_exception_bad_method_call_class_entry;
zend_class_entry *php_http_exception_invalid_arg_class_entry;
zend_class_entry *php_http_exception_bad_header_class_entry;
zend_class_entry *php_http_exception_bad_url_class_entry;
zend_class_entry *php_http_exception_bad_message_class_entry;
zend_class_entry *php_http_exception_bad_conversion_class_entry;
zend_class_entry *php_http_exception_bad_querystring_class_entry;

PHP_MINIT_FUNCTION(http_exception)
{
	zend_class_entry *cep, ce = {0};

	INIT_NS_CLASS_ENTRY(ce, "http", "Exception", NULL);
	php_http_exception_interface_class_entry = zend_register_internal_interface(&ce TSRMLS_CC);
	
	/*
	 * Would be great to only have a few exceptions and rather more identifying
	 * error codes, but zend_replace_error_handling() does not accept any codes.
	 */

	memset(&ce, 0, sizeof(ce));
	INIT_NS_CLASS_ENTRY(ce, "http\\Exception", "RuntimeException", NULL);
	cep = zend_register_internal_class_ex(&ce, spl_ce_RuntimeException, NULL TSRMLS_CC);
	zend_class_implements(cep TSRMLS_CC, 1, php_http_exception_interface_class_entry);
	php_http_exception_runtime_class_entry = cep;

	memset(&ce, 0, sizeof(ce));
	INIT_NS_CLASS_ENTRY(ce, "http\\Exception", "UnexpectedValueException", NULL);
	cep = zend_register_internal_class_ex(&ce, spl_ce_UnexpectedValueException, NULL TSRMLS_CC);
	zend_class_implements(cep TSRMLS_CC, 1, php_http_exception_interface_class_entry);
	php_http_exception_unexpected_val_class_entry = cep;

	memset(&ce, 0, sizeof(ce));
	INIT_NS_CLASS_ENTRY(ce, "http\\Exception", "BadMethodCallException", NULL);
	cep = zend_register_internal_class_ex(&ce, spl_ce_BadMethodCallException, NULL TSRMLS_CC);
	zend_class_implements(cep TSRMLS_CC, 1, php_http_exception_interface_class_entry);
	php_http_exception_bad_method_call_class_entry = cep;

	memset(&ce, 0, sizeof(ce));
	INIT_NS_CLASS_ENTRY(ce, "http\\Exception", "InvalidArgumentException", NULL);
	cep = zend_register_internal_class_ex(&ce, spl_ce_InvalidArgumentException, NULL TSRMLS_CC);
	zend_class_implements(cep TSRMLS_CC, 1, php_http_exception_interface_class_entry);
	php_http_exception_invalid_arg_class_entry = cep;

	memset(&ce, 0, sizeof(ce));
	INIT_NS_CLASS_ENTRY(ce, "http\\Exception", "BadHeaderException", NULL);
	cep = zend_register_internal_class_ex(&ce, spl_ce_DomainException, NULL TSRMLS_CC);
	zend_class_implements(cep TSRMLS_CC, 1, php_http_exception_interface_class_entry);
	php_http_exception_bad_header_class_entry = cep;

	memset(&ce, 0, sizeof(ce));
	INIT_NS_CLASS_ENTRY(ce, "http\\Exception", "BadUrlException", NULL);
	cep = zend_register_internal_class_ex(&ce, spl_ce_DomainException, NULL TSRMLS_CC);
	zend_class_implements(cep TSRMLS_CC, 1, php_http_exception_interface_class_entry);
	php_http_exception_bad_url_class_entry = cep;

	memset(&ce, 0, sizeof(ce));
	INIT_NS_CLASS_ENTRY(ce, "http\\Exception", "BadMessageException", NULL);
	cep = zend_register_internal_class_ex(&ce, spl_ce_DomainException, NULL TSRMLS_CC);
	zend_class_implements(cep TSRMLS_CC, 1, php_http_exception_interface_class_entry);
	php_http_exception_bad_message_class_entry = cep;

	memset(&ce, 0, sizeof(ce));
	INIT_NS_CLASS_ENTRY(ce, "http\\Exception", "BadConversionException", NULL);
	cep = zend_register_internal_class_ex(&ce, spl_ce_DomainException, NULL TSRMLS_CC);
	zend_class_implements(cep TSRMLS_CC, 1, php_http_exception_interface_class_entry);
	php_http_exception_bad_conversion_class_entry = cep;

	memset(&ce, 0, sizeof(ce));
	INIT_NS_CLASS_ENTRY(ce, "http\\Exception", "BadQueryStringException", NULL);
	cep = zend_register_internal_class_ex(&ce, spl_ce_DomainException, NULL TSRMLS_CC);
	zend_class_implements(cep TSRMLS_CC, 1, php_http_exception_interface_class_entry);
	php_http_exception_bad_querystring_class_entry = cep;

#if PHP_HTTP_DBG_EXCEPTIONS
	zend_throw_exception_hook = php_http_exception_hook;
#endif
	
	return SUCCESS;
}

/*
 * Local variables:
 * tab-width: 4
 * c-basic-offset: 4
 * End:
 * vim600: noet sw=4 ts=4 fdm=marker
 * vim<600: noet sw=4 ts=4
 */