18
18
19
19
#define LOGARGS (instance , lvl ) LCB_LOG_##lvl, instance, "pcbc/http", __FILE__, __LINE__
20
20
21
+ extern zend_class_entry * pcbc_default_exception_ce ;
22
+ extern zend_class_entry * pcbc_http_exception_ce ;
23
+
21
24
typedef struct {
22
25
opcookie_res header ;
23
26
zval bytes ;
@@ -58,7 +61,7 @@ void http_callback(lcb_INSTANCE *instance, int cbtype, const lcb_RESPHTTP *resp)
58
61
opcookie_push (cookie , & result -> header );
59
62
}
60
63
61
- static lcb_STATUS proc_http_results (zval * return_value , opcookie * cookie TSRMLS_DC )
64
+ static lcb_STATUS proc_http_results (zval * return_value , opcookie * cookie , void ( httpcb )( zval * , zval * ) TSRMLS_DC )
62
65
{
63
66
opcookie_http_res * res ;
64
67
lcb_STATUS err = LCB_SUCCESS ;
@@ -71,7 +74,42 @@ static lcb_STATUS proc_http_results(zval *return_value, opcookie *cookie TSRMLS_
71
74
FOREACH_OPCOOKIE_RES (opcookie_http_res , res , cookie )
72
75
{
73
76
if (has_value == 0 ) {
74
- ZVAL_ZVAL (return_value , & res -> bytes , 1 , 0 );
77
+ if (Z_TYPE (res -> bytes ) == IS_ARRAY ) {
78
+ HashTable * marr = Z_ARRVAL (res -> bytes );
79
+ zval * mval ;
80
+ mval = zend_symtable_str_find (marr , ZEND_STRL ("errors" ));
81
+ if (mval && Z_TYPE_P (mval ) == IS_ARRAY ) {
82
+ smart_str buf = {0 };
83
+ zend_string * string_key = NULL ;
84
+ zval * entry ;
85
+ ZEND_HASH_FOREACH_STR_KEY_VAL (HASH_OF (mval ), string_key , entry )
86
+ {
87
+ smart_str_append_ex (& buf , string_key , 0 );
88
+ if (Z_TYPE_P (entry ) == IS_STRING ) {
89
+ smart_str_appends (& buf , ": " );
90
+ smart_str_append_ex (& buf , Z_STR_P (entry ), 0 );
91
+ }
92
+ smart_str_appends (& buf , ", " );
93
+ }
94
+ ZEND_HASH_FOREACH_END ();
95
+ if (buf .s && ZSTR_LEN (buf .s ) > 2 ) {
96
+ ZSTR_LEN (buf .s ) -= 2 ;
97
+ ZSTR_VAL (buf .s )[ZSTR_LEN (buf .s )] = '\0' ;
98
+ }
99
+ object_init_ex (return_value , pcbc_http_exception_ce );
100
+ zend_update_property_str (pcbc_default_exception_ce , return_value , ZEND_STRL ("message" ),
101
+ buf .s TSRMLS_CC );
102
+ smart_str_free (& buf );
103
+ err = LCB_ERR_HTTP ;
104
+ }
105
+ }
106
+ if (err == LCB_SUCCESS ) {
107
+ if (httpcb ) {
108
+ httpcb (return_value , & res -> bytes );
109
+ } else {
110
+ ZVAL_ZVAL (return_value , & res -> bytes , 1 , 0 );
111
+ }
112
+ }
75
113
has_value = 1 ;
76
114
} else {
77
115
err = LCB_ERR_GENERIC ;
@@ -88,7 +126,8 @@ static lcb_STATUS proc_http_results(zval *return_value, opcookie *cookie TSRMLS_
88
126
return err ;
89
127
}
90
128
91
- void pcbc_http_request (zval * return_value , lcb_INSTANCE * conn , lcb_CMDHTTP * cmd , int json_response TSRMLS_DC )
129
+ void pcbc_http_request (zval * return_value , lcb_INSTANCE * conn , lcb_CMDHTTP * cmd , int json_response ,
130
+ void (httpcb )(zval * , zval * ) TSRMLS_DC )
92
131
{
93
132
lcb_STATUS err ;
94
133
opcookie * cookie ;
@@ -99,10 +138,15 @@ void pcbc_http_request(zval *return_value, lcb_INSTANCE *conn, lcb_CMDHTTP *cmd,
99
138
lcb_cmdhttp_destroy (cmd );
100
139
if (err == LCB_SUCCESS ) {
101
140
lcb_wait (conn , LCB_WAIT_DEFAULT );
102
- err = proc_http_results (return_value , cookie TSRMLS_CC );
141
+ err = proc_http_results (return_value , cookie , httpcb TSRMLS_CC );
103
142
}
104
143
opcookie_destroy (cookie );
105
- if (err != LCB_SUCCESS ) {
144
+
145
+ if (Z_TYPE_P (return_value ) == IS_OBJECT &&
146
+ instanceof_function (Z_OBJCE_P (return_value ), pcbc_default_exception_ce TSRMLS_CC )) {
147
+ zend_throw_exception_object (return_value TSRMLS_CC );
148
+ RETURN_NULL ();
149
+ } else if (err != LCB_SUCCESS ) {
106
150
throw_lcb_exception (err , NULL );
107
151
}
108
152
}
0 commit comments