-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathlib_guzzle_common.c
More file actions
286 lines (234 loc) · 8.39 KB
/
Copy pathlib_guzzle_common.c
File metadata and controls
286 lines (234 loc) · 8.39 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
* Copyright 2020 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/
#include "php_agent.h"
#include "php_call.h"
#include "php_user_instrument.h"
#include "php_hash.h"
#include "php_wrapper.h"
#include "lib_guzzle_common.h"
#include "lib_guzzle4.h"
#include "lib_guzzle6.h"
#include "nr_header.h"
#include "util_logging.h"
#include "util_memory.h"
#include "util_strings.h"
char* nr_guzzle_create_async_context_name(const char* prefix, const zval* obj) {
if (!nr_php_is_zval_valid_object(obj)) {
return NULL;
}
return nr_formatf("%s #%d", prefix, Z_OBJ_HANDLE_P(obj));
}
static int nr_guzzle_stack_iterator(zval* frame,
int* in_guzzle_ptr,
zend_hash_key* key NRUNUSED TSRMLS_DC) {
int idx;
zval* klass = NULL;
NR_UNUSED_TSRMLS;
if (0 == nr_php_is_zval_valid_array(frame)) {
return ZEND_HASH_APPLY_KEEP;
}
if (NULL == in_guzzle_ptr) {
return ZEND_HASH_APPLY_KEEP;
}
klass = nr_php_zend_hash_find(Z_ARRVAL_P(frame), "class");
if (0 == nr_php_is_zval_non_empty_string(klass)) {
return ZEND_HASH_APPLY_KEEP;
}
idx = nr_strncaseidx(Z_STRVAL_P(klass), "guzzle", Z_STRLEN_P(klass));
if (idx >= 0) {
*in_guzzle_ptr = 1;
}
return ZEND_HASH_APPLY_KEEP;
}
int nr_guzzle_in_call_stack(TSRMLS_D) {
int in_guzzle = 0;
zval* stack = NULL;
if (0 == NRINI(guzzle_enabled)) {
return 0;
}
stack = nr_php_backtrace(TSRMLS_C);
if (nr_php_is_zval_valid_array(stack)) {
nr_php_zend_hash_zval_apply(Z_ARRVAL_P(stack),
(nr_php_zval_apply_t)nr_guzzle_stack_iterator,
&in_guzzle TSRMLS_CC);
}
nr_php_zval_free(&stack);
return in_guzzle;
}
int nr_guzzle_does_zval_implement_has_emitter(zval* obj TSRMLS_DC) {
return nr_php_object_instanceof_class(
obj, "GuzzleHttp\\Event\\HasEmitterInterface" TSRMLS_CC);
}
nr_segment_t* nr_guzzle_obj_add(const zval* obj,
const char* async_context_prefix TSRMLS_DC) {
nr_segment_t* segment = NULL;
char* async_context = NULL;
int old_context = 0;
/*
* Create the async context, in case there was parallelism.
*/
async_context
= nr_guzzle_create_async_context_name(async_context_prefix, obj);
/*
* This is a special case where we don't want the context of the newly
* created segment to be set on the txn because this segment is tracked
* separately based off of the unique created guzzle async context. We need to
* save the actual context so we can restore it later.
*/
old_context = NRTXN(current_async_context);
/*
* Must explicitly be parented to the current segment of the txn context;
* otherwise, it will be parented to the main NULL context. In non-fiber
* cases, this will be a segment on the default (NULL) context; otherwise, it
* will be the current segment on the actively executing fiber.
*
* Similar to curl_multi_exec curl handle segments, because async guzzle
* segments are stored separately in a hashmap, we don't want to this segment
* creation to affect the txn context.
*/
segment = nr_segment_start(NRPRG(txn),
nr_txn_get_current_segment_txn_context(NRPRG(txn)),
async_context);
nr_free(async_context);
NRTXN(current_async_context) = old_context;
/*
* Create the guzzle_objs hash table if we haven't already done so.
*/
if (NULL == NRTXNGLOBAL(guzzle_objs)) {
NRTXNGLOBAL(guzzle_objs) = nr_hashmap_create(NULL);
}
/*
* We store the start times indexed by the object handle for the Request
* object: Zend object handles are unsigned ints while HashTable objects
* support unsigned longs as indexes, so this is safe regardless of
* architecture, and saves us having to transform the object handle into a
* string to use string keys.
*/
nr_hashmap_index_update(NRTXNGLOBAL(guzzle_objs),
(uint64_t)Z_OBJ_HANDLE_P(obj), segment);
return segment;
}
nr_status_t nr_guzzle_obj_find_and_remove(const zval* obj,
nr_segment_t** segment_ptr
TSRMLS_DC) {
if (NULL != NRTXNGLOBAL(guzzle_objs)) {
uint64_t index = (uint64_t)Z_OBJ_HANDLE_P(obj);
nr_segment_t* segment = NULL;
segment
= (nr_segment_t*)nr_hashmap_index_get(NRTXNGLOBAL(guzzle_objs), index);
*segment_ptr = segment;
if (segment) {
/*
* Remove the object handle from the hashmap containing active requests.
*/
nr_hashmap_index_delete(NRTXNGLOBAL(guzzle_objs), index);
return NR_SUCCESS;
}
}
nrl_verbosedebug(NRL_INSTRUMENT,
"Guzzle: object %d not found in tracked list",
Z_OBJ_HANDLE_P(obj));
return NR_FAILURE;
}
/*
* Purpose : Sets a header on an object implementing either the Guzzle 3 or 4
* MessageInterface.
*
* Params : 1. The header to set.
* 2. The value to set the header to.
* 3. The request object.
*/
static void nr_guzzle_request_set_header(const char* header,
const char* value,
zval* request TSRMLS_DC) {
zval* header_param = NULL;
zval* retval = NULL;
zval* value_param = NULL;
if ((NULL == header) || (NULL == value) || (NULL == request)) {
return;
}
header_param = nr_php_zval_alloc();
nr_php_zval_str(header_param, header);
value_param = nr_php_zval_alloc();
nr_php_zval_str(value_param, value);
retval = nr_php_call(request, "setHeader", header_param, value_param);
nr_php_zval_free(&header_param);
nr_php_zval_free(&retval);
nr_php_zval_free(&value_param);
}
void nr_guzzle_request_set_outbound_headers(zval* request,
nr_segment_t* segment TSRMLS_DC) {
nr_hashmap_t* outbound_headers = NULL;
nr_vector_t* header_keys = NULL;
char* header = NULL;
char* value = NULL;
size_t i;
size_t header_count;
outbound_headers = nr_header_outbound_request_create(NRPRG(txn), segment);
if (NULL == outbound_headers) {
return;
}
if (NRPRG(txn) && NRTXN(special_flags.debug_cat)) {
nrl_verbosedebug(
NRL_CAT,
"CAT: outbound request: transport='Guzzle' %s=" NRP_FMT " %s=" NRP_FMT,
X_NEWRELIC_ID,
NRP_CAT((char*)nr_hashmap_get(outbound_headers, X_NEWRELIC_ID,
nr_strlen(X_NEWRELIC_ID))),
X_NEWRELIC_TRANSACTION,
NRP_CAT((char*)nr_hashmap_get(outbound_headers, X_NEWRELIC_TRANSACTION,
nr_strlen(X_NEWRELIC_TRANSACTION))));
}
header_keys = nr_hashmap_keys(outbound_headers);
header_count = nr_vector_size(header_keys);
for (i = 0; i < header_count; i++) {
header = nr_vector_get(header_keys, i);
value = (char*)nr_hashmap_get(outbound_headers, header, nr_strlen(header));
nr_guzzle_request_set_header(header, value, request TSRMLS_CC);
}
nr_vector_destroy(&header_keys);
nr_hashmap_destroy(&outbound_headers);
}
char* nr_guzzle_response_get_header(const char* header,
zval* response TSRMLS_DC) {
zval* param = nr_php_zval_alloc();
zval* retval = NULL;
char* value = NULL;
nr_php_zval_str(param, header);
retval = nr_php_call(response, "getHeader", param);
if (NULL == retval) {
nrl_verbosedebug(NRL_INSTRUMENT,
"Guzzle: Response::getHeader() returned NULL");
} else if (nr_php_is_zval_valid_string(retval)) {
/*
* Guzzle 4 and 5 return an empty string if the header could not be found.
*/
if (Z_STRLEN_P(retval) > 0) {
value = nr_strndup(Z_STRVAL_P(retval), Z_STRLEN_P(retval));
}
} else {
nrl_verbosedebug(
NRL_INSTRUMENT,
"Guzzle: unexpected Response::getHeader() return of type %d",
Z_TYPE_P(retval));
}
nr_php_zval_free(¶m);
nr_php_zval_free(&retval);
return value;
}
NR_PHP_WRAPPER_START(nr_guzzle_client_construct) {
int is_guzzle_45 = 0;
zval* this_var = nr_php_scope_get(NR_EXECUTE_ORIG_ARGS TSRMLS_CC);
(void)wraprec;
NR_UNUSED_SPECIALFN;
is_guzzle_45 = nr_guzzle_does_zval_implement_has_emitter(this_var TSRMLS_CC);
nr_php_scope_release(&this_var);
if (is_guzzle_45) {
NR_PHP_WRAPPER_DELEGATE(nr_guzzle4_client_construct);
} else {
NR_PHP_WRAPPER_DELEGATE(nr_guzzle6_client_construct);
}
}
NR_PHP_WRAPPER_END