-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #48 from alphagov/binary_file_format
- Loading branch information
Showing
3 changed files
with
53 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,48 @@ | ||
package replay | ||
|
||
import ( | ||
"bufio" | ||
"log" | ||
"os" | ||
"bytes" | ||
"strconv" | ||
"bytes" | ||
"encoding/gob" | ||
"io" | ||
"io/ioutil" | ||
"log" | ||
|
||
"fmt" | ||
"github.com/buger/gor/utils" | ||
) | ||
|
||
type ParsedRequest struct { | ||
Request []byte | ||
Timestamp int64 | ||
} | ||
|
||
func (self ParsedRequest) String() string { | ||
return fmt.Sprintf("Request: %v, timestamp: %v", string(self.Request), self.Timestamp) | ||
} | ||
|
||
func parseReplayFile() (requests []ParsedRequest, err error) { | ||
requests, err = readLines(Settings.FileToReplayPath) | ||
func parseReplayFile() (requests []utils.RawRequest, err error) { | ||
requests, err = readLines(Settings.FileToReplayPath) | ||
|
||
if err != nil { | ||
log.Fatalf("readLines: %s", err) | ||
} | ||
if err != nil { | ||
log.Fatalf("readLines: %s", err) | ||
} | ||
|
||
return | ||
return | ||
} | ||
|
||
// readLines reads a whole file into memory | ||
// and returns a slice of its lines. | ||
func readLines(path string) (requests []ParsedRequest, err error) { | ||
file, err := os.Open(path) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
defer file.Close() | ||
|
||
scanner := bufio.NewScanner(file) | ||
scanner.Split(scanLinesFunc) | ||
// and returns a slice of request+timestamps. | ||
func readLines(path string) (requests []utils.RawRequest, err error) { | ||
file, err := ioutil.ReadFile(path) | ||
|
||
for scanner.Scan() { | ||
if len(scanner.Text()) > 5 { | ||
buf := append([]byte(nil), scanner.Bytes()...) | ||
i := bytes.IndexByte(buf, '\n') | ||
timestamp, _ := strconv.Atoi(string(buf[:i])) | ||
pr := ParsedRequest{buf[i + 1:], int64(timestamp)} | ||
|
||
requests = append(requests, pr) | ||
} | ||
} | ||
|
||
return requests, scanner.Err() | ||
} | ||
|
||
// scanner spliting logic | ||
func scanLinesFunc(data []byte, atEOF bool) (advance int, token []byte, err error) { | ||
if atEOF && len(data) == 0 { | ||
return 0, nil, nil | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
delimiter := []byte{'\r', '\n', '\r', '\n', '\n'} | ||
fileBuf := bytes.NewBuffer(file) | ||
fileDec := gob.NewDecoder(fileBuf) | ||
|
||
// We have a http request end: \r\n\r\n | ||
if i := bytes.Index(data, delimiter); i >= 0 { | ||
return (i + len(delimiter)), data[0:(i + len(delimiter))], nil | ||
} | ||
for err == nil { | ||
var reqBuf utils.RawRequest | ||
err = fileDec.Decode(&reqBuf) | ||
|
||
if err == io.EOF { | ||
err = nil | ||
break | ||
} | ||
|
||
// If we're at EOF, we have a final, non-terminated line. Return it. | ||
if atEOF { | ||
return len(data), data, nil | ||
requests = append(requests, reqBuf) | ||
} | ||
|
||
// Request more data. | ||
return 0, nil, nil | ||
return requests, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package utils | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
type RawRequest struct { | ||
Timestamp int64 | ||
Request []byte | ||
} | ||
|
||
func (self RawRequest) String() string { | ||
return fmt.Sprintf("Request: %v, timestamp: %v", string(self.Request), self.Timestamp) | ||
} |