http-log-to-test 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #!/usr/bin/env manticore-executor
  2. <?php declare(strict_types=1);
  3. // Copyright (c) 2017-2026, Manticore Software LTD (https://manticoresearch.com)
  4. // Copyright (c) 2001-2016, Andrew Aksyonoff
  5. // Copyright (c) 2008-2016, Sphinx Technologies Inc
  6. // All rights reserved
  7. //
  8. // This program is free software; you can redistribute it and/or modify
  9. // it under the terms of the GNU General Public License. You should have
  10. // received a copy of the GPL license along with this program; if you
  11. // did not, you can find it at http://www.gnu.org/
  12. function parseLogFile(string $filename): array {
  13. $result = [];
  14. $currentEntry = null;
  15. $isRequest = false;
  16. $isResponse = false;
  17. // Open file for reading
  18. $handle = fopen($filename, 'r');
  19. if (!$handle) {
  20. throw new Exception("Unable to open file: $filename");
  21. }
  22. while (($line = fgets($handle)) !== false) {
  23. // Remove trailing whitespace
  24. $line = rtrim($line);
  25. // Check for new entry
  26. if (preg_match('/^\[.*\] \[Request-ID: (\d+)\] - (Incoming HTTP Request|Outgoing HTTP Response):/', $line, $matches)) {
  27. // Start new entry if it's a request
  28. if (strpos($line, 'Incoming HTTP Request') !== false) {
  29. if ($currentEntry && shouldIncludeEntry($currentEntry)) {
  30. $result[] = $currentEntry;
  31. }
  32. $currentEntry = [
  33. 'request_id' => $matches[1],
  34. 'timestamp' => extractTimestamp($line),
  35. 'request' => [],
  36. 'response' => []
  37. ];
  38. $isRequest = true;
  39. $isResponse = false;
  40. } elseif (strpos($line, 'Outgoing HTTP Response') !== false) {
  41. $isRequest = false;
  42. $isResponse = true;
  43. }
  44. continue;
  45. }
  46. // Skip delimiter lines
  47. if (preg_match('/^[<>]{10,}$/', $line)) {
  48. continue;
  49. }
  50. // Add content to current entry
  51. if ($currentEntry && !empty($line)) {
  52. if ($isRequest) {
  53. $row = parseHttpRequestLine($line);
  54. $currentEntry['request'] = mergeEntry($currentEntry['request'], $row);
  55. } elseif ($isResponse) {
  56. $row = parseHttpResponseLine($line);
  57. $currentEntry['response'] = mergeEntry($currentEntry['response'], $row);
  58. }
  59. }
  60. }
  61. // Process last entry from the list
  62. if ($currentEntry && shouldIncludeEntry($currentEntry)) {
  63. $result[] = $currentEntry;
  64. }
  65. fclose($handle);
  66. return $result;
  67. }
  68. /**
  69. * Parse a single HTTP request line into a struct
  70. * @param string $line
  71. * @return array
  72. */
  73. function parseHttpRequestLine(string $line): array {
  74. static $isBody = false;
  75. $line = trim($line);
  76. // Return empty if line is empty
  77. if (empty($line)) {
  78. $isBody = true;
  79. return [];
  80. }
  81. // Check if it's the request line (first line)
  82. if (preg_match('/^(GET|POST|PUT|DELETE|HEAD|OPTIONS|PATCH)\s+(\S+)\s+HTTP\/([\d.]+)$/i', $line, $matches)) {
  83. return [
  84. 'http:method' => $matches[1],
  85. 'http:path' => $matches[2],
  86. 'http:version' => $matches[3],
  87. ];
  88. }
  89. // Header line check
  90. if (!$isBody && strpos($line, ':') !== false && preg_match('/^[\w\-]+:\s*.+$/', $line)) {
  91. list($key, $value) = array_map('trim', explode(':', $line, 2));
  92. return ["header:$key" => $value];
  93. }
  94. // If it's not a header and not the first line, treat it as body
  95. return [
  96. 'body' => $line
  97. ];
  98. }
  99. /**
  100. * Parse a single HTTP response line into a struct
  101. * @param string $line
  102. * @return array<string,string>
  103. */
  104. function parseHttpResponseLine(string $line): array {
  105. static $isBody = false;
  106. $line = trim($line);
  107. // Empty line check
  108. if (empty($line)) {
  109. $isBody = true;
  110. return [];
  111. }
  112. // First line check (HTTP status line)
  113. if (preg_match('/^HTTP\/(\d\.\d)\s+(\d{3})\s+(.*)$/', $line, $matches)) {
  114. $isBody = false;
  115. return [
  116. 'http:version' => $matches[1],
  117. 'http:code' => (int)$matches[2],
  118. 'http:message' => $matches[3],
  119. ];
  120. }
  121. // Header line check
  122. if (!$isBody && strpos($line, ':') !== false && preg_match('/^[\w\-]+:\s*.+$/', $line)) {
  123. list($key, $value) = array_map('trim', explode(':', $line, 2));
  124. return ["header:$key" => $value];
  125. }
  126. // If none of the above, return the line as is
  127. return ['body' => $line];
  128. }
  129. function mergeEntry(array $entry, array $newEntry): array {
  130. foreach ($newEntry as $key => $value) {
  131. if ($key === 'body') {
  132. $entry[$key] = ($entry[$key] ?? '') . $value;
  133. } else {
  134. $entry[$key] = $value;
  135. }
  136. }
  137. return $entry;
  138. }
  139. /**
  140. * Check if the current entry should be included in the output
  141. * We are excluding all requests from Buddy that are still logged into the file
  142. * @param array $entry
  143. * @return bool
  144. */
  145. function shouldIncludeEntry(array $entry): bool {
  146. return !str_starts_with($entry['request']['header:User-Agent'], 'Manticore Buddy');
  147. }
  148. function extractTimestamp($line) {
  149. if (preg_match('/^\[(.*?)\]/', $line, $matches)) {
  150. return $matches[1];
  151. }
  152. return null;
  153. }
  154. function printCLTTest(array $logEntries) {
  155. foreach ($logEntries as $entry) {
  156. $url = escapeshellarg($entry['request']['header:Host'] . $entry['request']['http:path']);
  157. unset($entry['request']['header:Host']);
  158. $args = [];
  159. foreach ($entry['request'] as $key => $value) {
  160. if (!str_starts_with($key, 'header:')) {
  161. continue;
  162. }
  163. $args[] = '-H ' . escapeshellarg(substr($key, 7) . ': ' . $value);
  164. }
  165. $method = $entry['request']['http:method'];
  166. if ($method === 'POST') {
  167. $args[] = '-d ' . escapeshellarg($entry['request']['body']);
  168. }
  169. $argLine = implode(' ', $args);
  170. echo "––– input –––\n";
  171. echo "curl -X {$entry['request']['http:method']} {$argLine} {$url}\n";
  172. echo "––– output –––\n";
  173. echo "{$entry['response']['body']}\n";
  174. }
  175. }
  176. try {
  177. if (!isset($argv[1])) {
  178. die("Usage: http-log-to-test <log-file>\n");
  179. }
  180. $logFile = $argv[1];
  181. if (!file_exists($logFile)) {
  182. die("Error: Log file '$logFile' not found.\n");
  183. }
  184. $logEntries = parseLogFile($logFile);
  185. printCLTTest($logEntries);
  186. } catch (Exception $e) {
  187. fwrite(STDERR, "Error: " . $e->getMessage() . "\n");
  188. exit(1);
  189. }