// checkMethod check rule request method name for problems func checkMethod(r *rules.Rule) []*Problem { var result []*Problem if !sliceutil.Contains([]string{"OPTIONS", "GET", "HEAD", "POST", "PUT", "DELETE", "TRACE", "CONNECT", "PATCH"}, r.Request.Method) { result = append(result, &Problem{ Type: PROBLEM_ERR, Info: "Unknown HTTP method", Desc: fmtc.Sprintf("You define unsupported HTTP method \"%s\". Valid methods is OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT and PATCH.", r.Request.Method), }, ) } return result }
// getLineType return data source type func getLineType(line string) int { if line == "" { return TYPE_EMPTY_LINE } if strutil.Head(line, 3) == "-- " { return TYPE_SEPARATOR } if strutil.Head(line, 2) == "+ " { if sliceutil.Contains(headers, strutil.Substr(line, 2, 99)) { return TYPE_HEADER } } if strutil.Head(line, 2) == " " && strings.Contains(line, ":") { return TYPE_RECORD } return TYPE_DATA }
// filterFile read file and show records only between given time range func filterFile(file string) { fd, err := os.OpenFile(file, os.O_RDONLY, 0644) if err != nil { printError("Can't open file %s: %v", file, err) os.Exit(1) } defer fd.Close() reader := bufio.NewReader(fd) var showSection = false var currentSection = "" var dataSections = []string{"REQUEST BODY", "RESPONSE BODY"} fromDate := time.Date(2016, 1, 1, 0, 0, 0, 0, time.Local) toDate := time.Now() if arg.Has(ARG_DATE_FROM) { fromDate, err = parseRangeDate(arg.GetS(ARG_DATE_FROM)) if err != nil { printError("Can't parse range start: %v", err) os.Exit(1) } } if arg.Has(ARG_DATE_TO) { toDate, err = parseRangeDate(arg.GetS(ARG_DATE_TO)) if err != nil { printError("Can't parse range end: %v", err) os.Exit(1) } } for { line, err := reader.ReadString('\n') if err != nil { break } line = strings.TrimRight(line, "\n") line = strings.TrimRight(line, "\r") rt := getLineType(line) if rt == TYPE_SEPARATOR { recDateStr := extractTimeFromSeparator(line) recDate, _ := time.Parse(separatorTimeLayout, recDateStr) showSection = recDate.Unix() >= fromDate.Unix() && recDate.Unix() <= toDate.Unix() } if !showSection { continue } if rt == TYPE_HEADER { currentSection = extractHeaderName(line) renderLine(line, rt) continue } if sliceutil.Contains(dataSections, currentSection) { fmtc.Println(line) continue } renderLine(line, rt) } }
// readFile starts file reading loop func readFile(file string) { fd, err := os.OpenFile(file, os.O_RDONLY, 0644) if err != nil { printError("Can't open file %s: %v", file, err) os.Exit(1) } defer fd.Close() stat, err := fd.Stat() if err != nil { printError("Can't read file stats %s: %v", file, err) os.Exit(1) } if stat.Size() > 2048 { fd.Seek(-2048, 2) } reader := bufio.NewReader(fd) nearRecordFound := false var currentSection = "" var dataSections = []string{"REQUEST BODY", "RESPONSE BODY"} for { line, err := reader.ReadString('\n') if err != nil { time.Sleep(time.Millisecond * 250) continue } line = strings.TrimRight(line, "\n") line = strings.TrimRight(line, "\r") if !nearRecordFound { if strutil.Head(line, 3) != "-- " { continue } nearRecordFound = true } rt := getLineType(line) if rt == TYPE_HEADER { currentSection = extractHeaderName(line) renderLine(line, rt) continue } if sliceutil.Contains(dataSections, currentSection) { fmtc.Println(line) continue } renderLine(line, rt) } }