使用PHP调用Httpwatch.controller 来分析httpwatch的log文件
------ httpwatch.log.php -----------
$ct = new COM("HttpWatch.Controller");
$log = $ct->OpenLog($argv[1]);
echo "The log file contains " . $log->Entries->Count . " entries\n";
$entries = $log->Entries;
$cnt = $log->Entries->Count;
for ($i = 0 ; $i < $cnt; $i++) {
$entry = $entries->item($i); // item is a method , not a array
echo $entry->ClientIp .":" . $entry->ClientPort. "n";
echo "=========== Request Stream =============n";
echo decode($entry->Request->Stream);
echo "\n";
echo "\n";
echo $entry->ServerIp .":". $entry->ServerPort ."n";
echo "=========== Response Stream =============n";
$arrResponse = getResponse($entry);
echo $arrResponse["header"];
echo "\n\n";
echo $arrResponse["content"];
echo "\n";
echo "\n";
echo "\n";
}
function getResponse($entry) {
$stream = decode($entry->Response->Stream);
$pos = strpos($stream, "\r\n\r\n");
$header = substr($stream, 0, $pos);
$content = substr($stream, $pos + 4);
$arrHeaders = getHeader($entry, "Response");
if ($arrHeaders["Transfer-Encoding"] == "chunked") {
$content = getChunkedContent($content);
}
if ($arrHeaders["Content-Encoding"] == "gzip") {
$content = gzuncompress($content); // 目前这里是有问题的,不知道为什么
}
return array("header"=>$header, "content"=>$content);
}
function getChunkedContent($content) {
$start = 0;
$result = "";
while(1) {
$pos = strpos($content, "\r\n", $start);
$size = hexdec(substr($content, $start, $pos - $start));
if ($size == 0) break;
$result .= substr($content, $pos + 2, $size);
$start = $pos + 2 + $size + 2;
}
return $result;
}
function getHeader($entry, $type) {
$cnt = $entry->$type->Headers->Count;
for ($i = 0; $i < $cnt; $i++) {
$header = $entry->$type->Headers->item($i);
$arrResult[$header->Name] = $header->Value;
}
return $arrResult;
}
function decode($stream) {
$cnt = count($stream); // 非常注意: 这里用strlen是不行的
for( $i = 0 ; $i < $cnt ; $i ++) {
$result .= chr($stream[$i]);
}
return $result;
}
?>
运行: php httpwatch.log.php a.hwl
扫一扫订阅我的微信号:IT技术博客大学习
- 作者:phpor 来源: PHPor 的Blog
- 标签: httpwatch
- 发布时间:2010-04-25 21:53:24
- [55] IOS安全–浅谈关于IOS加固的几种方法
- [53] android 开发入门
- [52] 如何拿下简短的域名
- [52] 图书馆的世界纪录
- [50] Go Reflect 性能
- [50] Oracle MTS模式下 进程地址与会话信
- [48] 【社会化设计】自我(self)部分――欢迎区
- [47] 读书笔记-壹百度:百度十年千倍的29条法则
- [37] 程序员技术练级攻略
- [27] 视觉调整-设计师 vs. 逻辑