-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTransaction.class.php
More file actions
556 lines (525 loc) · 16.2 KB
/
Transaction.class.php
File metadata and controls
556 lines (525 loc) · 16.2 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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
<?php
namespace VarnishlogParser;
/**
* Basis for every transaction in Varnish.
* Most useful when overrided...
*/
class DefaultTransaction {
public $vxid; // Transaction number
public $direction; // 'b', 'c' or '-'
public $children; // Transactions emitted by this one.
public $parent; // Vxid of parent
public $creation_reason; // Why this transaction has been created
/**
* Default constructor
* @param Integer $vxid Transaction ID.
* @param String $direction "c", "b" or "-"
* @param String $creation_payload Reason why it has been created.
*/
function __construct($vxid, $direction, $creation_payload){
$this->vxid = $vxid;
$this->direction = $direction;
$type = $parent = $reason = null;
@list($type, $parent, $reason) = explode(' ', $creation_payload);
$this->parent = $parent;
$this->creation_reason = $reason;
$this->children = array();
}
/**
* Add a new child transaction.
* @param Integer $vxid Transaction ID.
* @param String $tag Creation reason.
*/
function addChild($vxid, $tag){
$this->children[$vxid] = $tag;
}
/**
* add new information about transaction.
* @param Integer $vxid Transaction ID.
* @param String $payload Data provided.
*/
function addInformation($tag, $payload){
switch ($tag) {
case 'Link':
list($type, $vxid_dest, $tag_dest) = explode(' ',$payload);
$this->addChild($vxid_dest,$tag_dest);
break;
default:
$this->{$tag}[] = $payload;
break;
}
}
/**
* Simple representation of this transaction.
* @param String $parent_name Name for initiator.
* @return String Simple representation.
*/
function toString($parent_name=null){
return $this->toStringRequest($parent_name)."\n".$this->toStringResponse($parent_name);
}
/**
* Simple representation of input data.
* If it's a user session, output client IP.
* @param String $parent_name Name for initiator.
* @return String Simple representation.
*/
function toStringRequest($parent_name=null){
if(isset($this->SessOpen[0])){
list($remote_ip) = sscanf($this->SessOpen[0],'%s %d %s %s %s %d');
/*
%s %d %s %s %s %d
| | | | | |
| | | | | +- File descriptor number
| | | | +---- Local TCP port ('-' if !$log_local_addr)
| | | +------- Local IPv4/6 address ('-' if !$log_local_addr)
| | +---------- Listen socket (-a argument)
| +------------- Remote TCP port
+---------------- Remote IPv4/6 address
*/
}
return "note over Client: vxid ".$this->vxid."\\n".$remote_ip;
}
/**
* Simple representation of output data.
* @param String $parent_name Name for initiator.
* @return String Simple representation.
*/
function toStringResponse($parent_name=null){
}
/**
* Get a simple name for this transaction.
* @return String The name of the transaction.
*/
function getName(){
return $this->vxid;
}
}
/**
* A request is an HTTP exchange between two entities.
*/
class Request extends DefaultTransaction {
public $query; // Original input data from parent or user
public $response; // Final response data from backend or varnish
public $cache; // Manipulated data to lookup cache (Client) or store into cache (Backend)
public $other; // Other headers
public $vcl_calls; // list of VCL calls and their return values.
private $open_vcl_subs; // Temporary store current VCL call.
/**
* @see parent
*/
function __construct($vxid, $direction, $creation_payload){
parent::__construct($vxid, $direction,$creation_payload);
$this->query = array('headers'=>array());
$this->response = array('headers'=>array());
$this->cache = array('headers'=>array());
$this->other = array();
$this->vcl_calls = array();
$this->open_vcl_subs = array();
}
/**
* Add new information about current transaction.
* Any information is categorized by tag type.
* Many tags are currently ignored...
*
* @param String $tag A varnishlog tag.
* @param String $payload Information provided.
*/
function addInformation($tag, $payload){
switch ($tag) {
/** SPECIFIC */
case 'Link':
// Creation of a child transaction
list($type, $vxid_dest, $tag_dest) = explode(' ',$payload);
$this->addChild($vxid_dest,$tag_dest);
return;
break;
case 'VCL_call':
array_push($this->open_vcl_subs, $payload);
return;
break;
case 'VCL_return':
$last_vcl_call = array_pop($this->open_vcl_subs);
if(is_null($last_vcl_call)){
$last_vcl_call = "RECV";
}
$this->vcl_calls[$last_vcl_call] = $payload;
return;
break;
case 'CLI':
case 'SessOpen':
case 'SessClose':
$this->other[$tag][] = $payload;
return;
break;
case 'VCL_Log':
$this->other[$tag][] = $payload;
return;
break;
/** HEADERS */
case 'ReqHeader':
case 'BereqHeader':
case 'RespHeader':
case 'BerespHeader':
case 'ObjHeader':
$this->addHeader($tag,$payload);
return;
break;
case 'ReqUnset':
case 'BereqUnset':
case 'RespUnset':
case 'BerespUnset':
$this->removeHeader($tag,$payload);
return;
break;
/** MAIN HEADERS */
case 'ReqStart':
case 'ReqMethod':
case 'ReqURL':
case 'ReqProtocol':
case 'BereqMethod':
case 'BereqURL':
case 'BereqProtocol':
$data_type = "query";
$tag = preg_replace('/^(Bereq|Req)/','',$tag);
break;
case 'RespProtocol':
case 'RespStatus':
case 'RespReason':
case 'BerespProtocol':
case 'BerespStatus':
case 'BerespReason':
$data_type = "response";
$tag = preg_replace('/^(Beresp|Resp)/','',$tag);
break;
case 'ObjProtocol':
case 'ObjStatus':
case 'ObjReason':
case 'TTL':
$data_type = "cache";
$tag = preg_replace('/^Obj/','',$tag);
break;
/** TAG IGNORED */
case 'Timestamp':
case 'Begin':
case 'End':
case 'Backend':
case 'BackendClose':
case 'BackendOpen':
case 'BackendReuse':
case 'BereqAcct':
case 'Debug':
case 'Fetch_Body':
case 'Gzip':
case 'Hit':
case 'HitPass':
case 'Length':
case 'ReqAcct':
case 'Storage':
case 'VCL_acl':
// Ignored tag, for now...
return;
break;
default:
trigger_error("Unknown tag '".$tag."'.", E_USER_WARNING);
// Please, complete previous list!
return;
break;
}
if(!isset($this->{$data_type}[$tag]))
$this->{$data_type}[$tag] = $payload;
if($data_type != "cache")
$this->cache[$tag] = $payload;
}
/**
* Store a header, avoiding to erase it
* because first entry is from real query/response
* then any override is for cache (query or store).
*
* @param String $tag Tag name.
* @param String $payload "Header: value".
*/
function addHeader($tag, $payload){
$header_name = $header_value = null;
@list($header_name, $header_value) = sscanf($payload,'%[^:]: %[^[]]');
/*
%s: %s
| |
| +- Header value
+----- Header name
*/
if(!$header_name)
return;
$write_to_cache = true;
$write_to_query = false;
$write_to_response = false;
switch ($tag) {
case 'ReqHeader':
$write_to_query = true;
if(!empty($this->open_vcl_subs))
$write_to_query = false; // because manipulated in VCL_recv
break;
case 'RespHeader':
$write_to_response = true;
if(!empty($this->open_vcl_subs))
$write_to_cache = false; // because manipulated in VCL_deliver
break;
case 'BereqHeader':
$write_to_query = true;
if(!empty($this->open_vcl_subs))
$write_to_cache = false; // because manipulated in VCL_fetch
break;
case 'BerespHeader':
$write_to_response = true;
if(!empty($this->open_vcl_subs))
$write_to_cache = false; // because manipulated in VCL_backend_response
break;
case 'ObjHeader':
default:
$write_to_cache = true;
break;
}
if($write_to_query && !isset($this->query["headers"][$header_name]))
$this->query["headers"][$header_name] = $header_value;
if($write_to_response && !isset($this->response["headers"][$header_name]))
$this->response["headers"][$header_name] = $header_value;
if($write_to_cache)
$this->cache["headers"][$header_name] = $header_value;
}
/**
* Remove header, usually in VCL_recv
* or VCL_backend_response.
*
* @param String $tag Tag name.
* @param String $payload "Header: value"
*/
function removeHeader($tag, $payload){
$header_name = $header_value = null;
@list($header_name, $header_value) = sscanf($payload,'%[^:]: %[^[]]');
if(!$header_name)
return;
$write_to_cache = true;
$write_to_query = false;
$write_to_response = false;
switch ($tag) {
case 'ReqUnset':
$write_to_query = false; // original request cannot remove headers
$write_to_cache = true;
break;
case 'RespUnset':
$write_to_response = true;
if(!empty($this->open_vcl_subs))
$write_to_cache = false; // because cache already queried
break;
case 'BereqUnset':
$write_to_query = true;
$write_to_cache = true; // because manipulated in VCL_fetch
break;
case 'BerespUnset':
$write_to_response = false; // original backend response cannot remove headers
$write_to_cache = true;
break;
default:
$write_to_cache = true;
break;
}
if($write_to_query)
unset($this->query["headers"][$header_name]);
if($write_to_response)
unset($this->response["headers"][$header_name]);
if($write_to_cache)
unset($this->cache["headers"][$header_name]);
}
/**
* Trim a request URL output to a max length.
*
* @param String $url The request.
* @param Integer $max_char Max length.
* @return String Truncated URL.
*/
protected function trimUrl($url, $max_char=70){
$placeholder = '[...]';
if(strlen($url) <= $max_char)
return $url;
$original_url = $url;
// First, explode URL parts to get a simple URL
$parts = array('path' => '','query' => '');
$parts['path'] = parse_url($url,PHP_URL_PATH);
$parts['query'] = parse_url($url,PHP_URL_QUERY);
$url = $this->unparseUrl($parts);
// Return if it's already OK
$counter = 0;
while (strlen($url) > $max_char) {
$counter++;
if($counter > 20){
// In order to prevent any infinite loop
// please post an issue on Github if you see
// the following error!
trigger_error("Failed to trim the following URL : $original_url", E_USER_WARNING);
return $url;
}
// Sort by length
$key_for_long_part = strlen($parts['path']) > strlen($parts['query']) ? 'path' : 'query';
switch ($key_for_long_part) {
case 'path':
$exploded_path = explode('/', $parts['path']);
// Do not count already trimmed path
foreach ($exploded_path as $exploded_path_key => $exploded_path_value) {
// beacause "array_filter" with anonymous function is very slow
if($exploded_path_value == $placeholder)
unset($exploded_path[$exploded_path_key]);
}
unset($exploded_path_key,$exploded_path_value);
// $exploded_path[0] is always empty because paths begin with '/'
if(count($exploded_path) <= 2){
// There is no directory.
// keep extension
$pos = strrpos(end($exploded_path),'.');
if($pos !== FALSE){
$extension = substr($parts['path'],$pos);
$basename = substr($parts['path'],0,$pos);
$parts['path'] = substr($basename,0,20-strlen($placeholder)).$placeholder.$extension;
}
else {
// No extension
$parts['path'] = substr($parts['path'],0,20).$placeholder;
}
}
else {
// Replace the last but one directory with placeholder
$keys = array_keys($exploded_path);
$exploded_path[$keys[count($keys)-2]] = $placeholder;
$parts['path'] = implode('/', $exploded_path);
}
break;
case 'query':
if(strpos($parts['query'], '=') !== FALSE
&& strpos($parts['query'], $placeholder) === FALSE){
$parts['query'] = preg_replace('/^([^=]+)=.*$/', '$1='.$placeholder, $parts['query']);
}
else {
$parts['query'] = substr($parts['query'],0,6).$placeholder;
}
break;
default: // Bug ?
return $url;
break;
}
$url = $this->unparseUrl($parts);
}
return $url;
}
/**
* Convert the result of "parse_url" into
* the original URL.
* @source :
* http://php.net/manual/fr/function.parse-url.php#106731
* @param Array $parsed_url Parts of URL.
* @return String Complete URL.
*/
protected function unparseUrl($parsed_url=array()) {
$ret = '';
$ret .= isset($parsed_url['path']) ? $parsed_url['path'] : '';
$ret .= isset($parsed_url['query']) ? '?' . $parsed_url['query'] : '';
return $ret;
}
}
/**
* A client request is a request emited by
* a user or an ESI tag in HTML page.
*/
class ClientRequest extends Request {
/**
* Did Varnish looked up in its cache?
* @return Boolean
*/
private function isCacheQueried(){
return isset($this->vcl_calls['RECV']) && $this->vcl_calls['RECV'] == "hash";
}
/**
* What was the cache response?
* @return Boolean "HIT" or "PASS"
* @todo handle "Hit For Pass" objects!
*/
private function getCacheResponse(){
if(isset($this->vcl_calls['HIT']))
return "HIT";
if(isset($this->vcl_calls['PASS']))
return "PASS";
if(isset($this->vcl_calls['MISS']))
return "MISS";
return "???";
}
/**
* Request and subrequests representation
* as string.
* @see parent.
*/
function toStringRequest($parent_name=null){
$ret = '';
if(is_null($parent_name))
$parent_name = $this->parent;
$ret .= $parent_name."->Varnish:".$this->query['Method']." ".$this->trimUrl($this->query['URL'])." (".$this->vxid.")";
if($this->isCacheQueried()){
$ret .= "\n";
$ret .= "Varnish->Cache:".$this->cache['Method']." ".$this->trimUrl($this->cache['URL']);
$ret .= "\n";
$ret .= "Cache->Varnish:".$this->getCacheResponse();
$ret .= "\n";
}
return $ret;
}
/**
* Request and subrequests responses
* representation as string.
* @see parent.
*/
function toStringResponse($parent_name=null){
if(is_null($parent_name))
$parent_name = $this->parent;
return "Varnish->".$parent_name.":".$this->cache['Status']." ".$this->cache['Reason']." (".$this->vxid.")";
}
/**
* Get simple name for this transaction.
* @return String
*/
function getName(){
if($this->creation_reason == "restart")
return "Varnish";
if($this->creation_reason == "esi")
return "Varnish";
return "Client";
}
}
/**
* A backend request is a request emited
* by Varnish to a backend.
* @todo handle multiple backends.
*/
class BackendRequest extends Request {
/**
* @see parent
*/
function toStringRequest($parent_name=null){
if($this->creation_reason == "bgfetch")
$line = "-->";
else
$line = "->";
return "Varnish".$line."Backend:".$this->query['Method']." ".$this->trimUrl($this->query['URL'])." (".$this->vxid.")";
}
/**
* @see parent
*/
function toStringResponse($parent_name=null){
if($this->creation_reason == "bgfetch")
$line = "-->";
else
$line = "->";
return "Backend".$line."Varnish:".$this->response['Status']." ".$this->response['Reason']." (".$this->vxid.")";
}
/**
* @see parent
*/
function getName(){
return "Varnish";
}
}