@@ -2,15 +2,17 @@ package main
22
33import (
44 "bufio"
5+ "context"
56 "flag"
67 "fmt"
78 "net/http"
89 "os"
9- "strings"
10+
11+ accesslog "github.com/nekrassov01/access-log-parser"
1012)
1113
1214func main () {
13- inputFile := flag .String ("input-file" , "" , "Path to ELF log file" )
15+ inputFile := flag .String ("input-file" , "" , "Path to CLF log file" )
1416 httpHost := flag .String ("http_host" , "" , "Target HTTP host, e.g. localhost:8983" )
1517 flag .Parse ()
1618
@@ -26,32 +28,43 @@ func main() {
2628 }
2729 defer file .Close ()
2830
29- scanner := bufio .NewScanner (file )
30- for scanner .Scan () {
31- line := scanner .Text ()
32- if strings .HasPrefix (line , "#" ) || line == "" {
33- continue // skip comments and empty lines
31+ handler := func (labels , values []string , isFirst bool ) (string , error ) {
32+ var method , path string
33+ for i , label := range labels {
34+ if label == "method" {
35+ method = values [i ]
36+ }
37+ if label == "request_uri" {
38+ path = values [i ]
39+ }
3440 }
35-
36- fields := strings .Fields (line )
37- if len (fields ) < 5 {
38- continue // skip malformed lines
39- }
40-
41- method := fields [3 ]
42- url := fields [4 ]
43- if method != "GET" {
44- continue // only replay GET requests
41+ fmt .Printf ("DEBUG: method='%s', path='%s'\n " , method , path )
42+ if method != "GET" || path == "" {
43+ fmt .Println ("DEBUG: Not a GET request or missing path, skipping" )
44+ return "" , nil
4545 }
46-
47- fullURL := "http://" + * httpHost + url
46+ fullURL := "http://" + * httpHost + path
47+ fmt . Printf ( "DEBUG: Sending GET to %s \n " , fullURL )
4848 resp , err := http .Get (fullURL )
4949 if err != nil {
5050 fmt .Printf ("Request to %s failed: %v\n " , fullURL , err )
51- continue
51+ return "" , nil
5252 }
5353 fmt .Printf ("%s -> %d\n " , fullURL , resp .StatusCode )
5454 resp .Body .Close ()
55+ return "" , nil
56+ }
57+
58+ opt := accesslog.Option {LineHandler : handler }
59+ parser := accesslog .NewApacheCLFRegexParser (context .Background (), os .Stdout , opt )
60+ scanner := bufio .NewScanner (file )
61+ for scanner .Scan () {
62+ line := scanner .Text ()
63+ if line == "" {
64+ continue // skip empty lines
65+ }
66+ // ParseString will invoke the handler for each line
67+ _ , _ = parser .ParseString (line )
5568 }
5669
5770 if err := scanner .Err (); err != nil {
0 commit comments