Created
September 27, 2013 21:12
-
-
Save apage43/6735260 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| package main | |
| import ( | |
| "bufio" | |
| "bytes" | |
| "fmt" | |
| "os" | |
| ) | |
| type State int | |
| const ( | |
| INSIDE State = iota | |
| OUTSIDE | |
| ESCAPED | |
| ) | |
| type Machine struct { | |
| state State | |
| Stringbuf bytes.Buffer | |
| Output *bufio.Writer | |
| } | |
| func (m *Machine) Tick(inchar rune) { | |
| incomingState := m.state | |
| if m.state == ESCAPED { | |
| m.state = INSIDE | |
| } else { | |
| switch inchar { | |
| case '"': | |
| switch m.state { | |
| case OUTSIDE: | |
| m.state = INSIDE | |
| case INSIDE: | |
| // Do stuff with content of Stringbuf | |
| m.state = OUTSIDE | |
| } | |
| case '\\': | |
| if m.state == INSIDE { | |
| m.state = ESCAPED | |
| } | |
| } | |
| } | |
| if (m.state == INSIDE || m.state == ESCAPED) && (incomingState == INSIDE || incomingState == ESCAPED) { | |
| m.Stringbuf.WriteRune(inchar) | |
| } | |
| if m.state == OUTSIDE || incomingState == OUTSIDE { | |
| m.Output.WriteRune(inchar) | |
| } | |
| } | |
| func main() { | |
| test := "[1,2,\"three \\\"thirty\", 4, \"four\", {\"what\":\"yeah\"}]" | |
| machine := Machine{OUTSIDE, bytes.Buffer{}, bufio.NewWriter(os.Stdout)} | |
| for _, char := range test { | |
| machine.Tick(char) | |
| } | |
| fmt.Printf("stringbuf: %v\n", machine.Stringbuf.String()) | |
| machine.Output.Flush() | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Applied: https://github.com/couchbaselabs/consolio/blob/master/tools/confsed/confsed.go#L39
You point this thing to a URL and give it a JSON map and it issues a new request that looks just like the original, but to the destination you want, and then rewrites the results, retaining headers and stuff. Probably not the most correct behavior for a proxy, but it does the thing I want.