zrhoffman commented on code in PR #7161: URL: https://github.com/apache/trafficcontrol/pull/7161#discussion_r1010109462
########## test/fakeOrigin/dtp/dtp.go: ########## @@ -0,0 +1,130 @@ +package dtp + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import ( + "crypto/tls" + "fmt" + "log" + "net/http" + "strings" + "time" +) + +type LogRecorder struct { + http.ResponseWriter + + Status int + HeaderBytes int64 + ContentBytes int64 +} + +func (rec *LogRecorder) WriteHeader(code int) { + rec.Status = code + rec.ResponseWriter.WriteHeader(code) +} + +func (rec *LogRecorder) Write(bytes []byte) (int, error) { + rec.ContentBytes += int64(len(bytes)) + return rec.ResponseWriter.Write(bytes) +} + +// this is mostly for hijack +func isHandlerType(r *http.Request) bool { + if strings.Contains(r.URL.EscapedPath(), "~h.") { + return true + } else if strings.Contains(r.URL.RawQuery, "~h.") { + return true + } else { + for _, part := range r.Header[`X-Dtp`] { + if strings.Contains(part, "~h.") { + return true + } + } + } + + return false +} + +func Logger(alog *log.Logger, next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + timeStart := time.Now().UnixNano() + + // the logger interferes with hijacking + if isHandlerType(r) { + next.ServeHTTP(w, r) + alog.Printf("%.3f %s \"%s\" %d b=%d ttms=%d uas=\"%s\" rr=\"%s\"\n", + float64(timeStart)/float64(1.e9), + r.Method, + r.URL.String(), + 42, // status code -- why not? + 0, // bytes + 0, // ttms + r.UserAgent(), + r.Header.Get("Range"), + ) + return + } + + tlsstr := "-" + if r.TLS != nil { + tlsstr = tls.CipherSuiteName(r.TLS.CipherSuite) + } + /* + if nil != r.TLS { + tlsstr = fmt.Sprintf("0x%x", r.TLS.CipherSuite) + } + */ + + rec := LogRecorder{w, 200, 0, 0} + next.ServeHTTP(&rec, r) + timeStop := time.Now().UnixNano() Review Comment: `timeStop` should be `time.Duration` type ########## test/fakeOrigin/dtp/type_txt.go: ########## @@ -0,0 +1,77 @@ +package dtp + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import ( + "encoding/binary" + "fmt" + "hash/fnv" + "strconv" +) + +func init() { + GlobalGeneratorFuncs[`txt`] = NewTxtGen +} + +type TxtGen struct { + Size int64 + Pos int64 + Seed int64 + Rnd int64 +} + +func (s *TxtGen) ContentType() string { + return "text/plain" +} + +func (s *TxtGen) Read(p []byte) (n int, err error) { + sz, err := ReadBlock(p, s.Pos, s.Size, 32, func(ct int64) []byte { Review Comment: Another place where `32` can be a constant ########## test/fakeOrigin/dtp/handler.go: ########## @@ -0,0 +1,360 @@ +package dtp + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import ( + "crypto/md5" + "encoding/base64" + "encoding/hex" + "fmt" + "io" + "net/http" + "net/url" + "strconv" + "strings" + "time" +) + +const XDtpHdrStr = `X-Dtp` +const XDtpCcHdrStr = `X-Dtp-Cc` + +type DTPHandler struct{} + +func NewDTPHandler() DTPHandler { + return DTPHandler{} +} + +// This is for content generation +type Generator interface { + ContentType() string + io.ReadSeeker +} + +type NewGeneratorFunc func( + map[string]string, + int64, +) Generator + +var GlobalGeneratorFuncs = map[string]NewGeneratorFunc{} + +// No content generation +type HandlerFunc func( + http.ResponseWriter, + *http.Request, + map[string]string, +) + +var GlobalHandlerFuncs = map[string]HandlerFunc{} + +type NewForwardFunc func( + http.ResponseWriter, + *http.Request, + Generator, + map[string]string, + int64, +) Generator + +var GlobalForwarderFuncs = map[string]NewForwardFunc{} + +/* +func timeoutAndDrop(w http.ResponseWriter, r *http.Request, durstr string) { + hj, ok := w.(http.Hijacker) + if !ok { + http.Error(w, "webserver doesn't support hijacking", http.StatusInternalServerError) + DebugLogf("Can't get a hijack\n") + return + } + + conn, buf, err := hj.Hijack() + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + DebugLogf("hijack failed: %s\n", err) + return + } + + delaydur, err := time.ParseDuration(durstr) + if err == nil { + if 0 < delaydur { + time.Sleep(delaydur) + } + } + + conn.Close() +} +*/ Review Comment: Is `timeoutAndDrop()` part of a future enhancement? ########## test/fakeOrigin/dtp/text.go: ########## @@ -0,0 +1,86 @@ +package dtp + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import ( + "encoding/binary" + "errors" + "fmt" + "hash/fnv" + "io" + "net/http" + "strconv" + "time" +) + +type TextStampSeeker struct { + Size int64 + Pos int64 + Seed int64 + Rnd int64 +} + +func (s *TextStampSeeker) Read(p []byte) (n int, err error) { + return ReadBlock(p, s.Pos, s.Size, 32, func(ct int64) []byte { + if s.Rnd == 0 { + return []byte(fmt.Sprintf("%31d\n", ct+s.Seed)) + } else { + h := fnv.New64() + b := make([]byte, 8) + binary.LittleEndian.PutUint64(b, uint64(ct)) + h.Write(b) + binary.LittleEndian.PutUint64(b, uint64(s.Seed)) + h.Write(b) + binary.LittleEndian.PutUint64(b, uint64(s.Rnd)) + h.Write(b) + return []byte(fmt.Sprintf("%31d\n", h.Sum64()&^0x8000000000000000)) Review Comment: > 0x8000000000000000 This magic number should be a constant to make it obvious what it's for ########## test/fakeOrigin/dtp/bin.go: ########## @@ -0,0 +1,86 @@ +package dtp + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import ( + "encoding/binary" + "errors" + "hash/fnv" + "io" + "net/http" + "strconv" + "time" +) + +type BinStampSeeker struct { + Size int64 + Pos int64 + Seed int64 + Rnd int64 +} + +func (s *BinStampSeeker) Read(p []byte) (n int, err error) { + return ReadBlock(p, s.Pos, s.Size, 8, func(ct int64) []byte { + ct += s.Seed + b := make([]byte, 8) + if s.Rnd == 0 { + for i := 0; i < len(b); i++ { + b[i] = byte(ct & 0xf) + ct >>= 8 + } + } else { + h := fnv.New64() + binary.LittleEndian.PutUint64(b, uint64(ct)) + h.Write(b) + binary.LittleEndian.PutUint64(b, uint64(s.Rnd)) + h.Write(b) + return h.Sum(nil) + } + return b + }) +} + +func (s *BinStampSeeker) Seek(off int64, whence int) (int64, error) { + var newPos int64 + switch whence { + case io.SeekStart: + newPos = off + case io.SeekEnd: + newPos = s.Size - off + case io.SeekCurrent: + newPos += off + if newPos > s.Size { + newPos = s.Size + } + } + if newPos < 0 { + return s.Pos, errors.New(`unable to seek before file`) + } + s.Pos = newPos + return s.Pos, nil +} + +func BinStamp(w http.ResponseWriter, r *http.Request, reqdat map[string]string) { + seed, _ := strconv.ParseInt(reqdat[`rnd`], 10, 64) + lastmod, _ := strconv.ParseInt(reqdat[`lm`], 10, 64) + sz, _ := strconv.ParseInt(reqdat[`sz`], 10, 64) Review Comment: Why `10`? Can `10` be a constant? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
