zrhoffman commented on code in PR #7161:
URL: https://github.com/apache/trafficcontrol/pull/7161#discussion_r1009733005


##########
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 {

Review Comment:
   Why `32`? Any reason not to make `32` a constant?



##########
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)
+                       }
+               */

Review Comment:
   Can these commented lines be removed?



##########
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()
+               alog.Printf("%.3f %s \"%s\" %s %d b=%d ttms=%d uas=\"%s\" 
rr=\"%s\"\n",
+                       float64(timeStart)/float64(1.e9),
+                       r.Method,
+                       r.URL.String(),
+                       tlsstr,
+                       rec.Status,
+                       rec.ContentBytes,
+                       int64((timeStop-timeStart)/1000000),

Review Comment:
   `1000000` can be `time.Second` (and will not require a typecast once 
`timeStop` and `timeStart` are `time.Duration` type)



##########
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()
+}
+*/
+
+func (h DTPHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
+       var cmds []string
+
+       /*
+               if GlobalConfig.Drop {
+                       DebugLogf("Dropping request for '%s'\n", r.URL.String())
+                       timeoutAndDrop(w, r, "60s")
+                       return
+               }
+       */

Review Comment:
   Same question about whether this is part of a future enhancement



##########
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 {
+               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:
   Another place to use a constant instead of a magic number



##########
test/fakeOrigin/httpService/httpService.go:
##########
@@ -242,7 +287,14 @@ func StartHTTPListener(conf endpoint.Config, routes 
map[string]EndpointRoutes) e
                return errors.New("registering routes: " + err.Error())
        }
        fmt.Println("Serving HTTP on " + conf.ServerConf.BindingAddress + ":" + 
strconv.Itoa(conf.ServerConf.HTTPListeningPort))
-       return 
http.ListenAndServe(conf.ServerConf.BindingAddress+":"+strconv.Itoa(conf.ServerConf.HTTPListeningPort),
 mux)
+       srv := &http.Server{
+               Addr:         conf.ServerConf.BindingAddress + ":" + 
strconv.Itoa(conf.ServerConf.HTTPListeningPort),
+               Handler:      mux,
+               ReadTimeout:  conf.ServerConf.ReadTimeout * time.Second,
+               WriteTimeout: conf.ServerConf.ReadTimeout * time.Second,
+       }
+       //return 
http.ListenAndServe(conf.ServerConf.BindingAddress+":"+strconv.Itoa(conf.ServerConf.HTTPListeningPort),
 mux)

Review Comment:
   Does this commented line need to be here?



##########
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()

Review Comment:
   `timeStart` should be `time.Duration` type



-- 
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]

Reply via email to