Skip to content

Commit 82e5412

Browse files
author
vcarvajal
committed
add new constructor, NewRPCMsgInWithModuleConfig
1 parent 4ebcb92 commit 82e5412

File tree

1 file changed

+60
-2
lines changed

1 file changed

+60
-2
lines changed

module.go

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package sigsci
33
import (
44
"bytes"
55
"fmt"
6+
"io"
67
"io/ioutil"
78
"log"
89
"net"
@@ -225,8 +226,13 @@ func (m *Module) inspectorPreRequest(req *http.Request) (inspin2 RPCMsgIn2, out
225226
req.Body = ioutil.NopCloser(bytes.NewBuffer(reqbody))
226227
}
227228

228-
inspin := NewRPCMsgIn(req, reqbody, -1, -1, -1, m.config.ModuleIdentifier(), m.config.ServerIdentifier())
229-
inspin.ServerFlavor = m.config.ServerFlavor()
229+
inspin, err := NewRPCMsgInWithModuleConfig(m.config, req, bytes.NewReader(reqbody))
230+
if err != nil {
231+
if m.config.Debug() {
232+
log.Printf("DEBUG: PreRequest call error (%s %s): %s", req.Method, req.RequestURI, err)
233+
}
234+
return
235+
}
230236
m.extractHeaders(req, inspin)
231237

232238
if m.config.Debug() {
@@ -372,6 +378,58 @@ func NewRPCMsgIn(r *http.Request, postbody []byte, code int, size int64, dur tim
372378
}
373379
}
374380

381+
// NewRPCMsgInWithModuleConfig creates a message from a ModuleConfig object
382+
// End-users of the golang module never need to use this
383+
// directly and it is only exposed for performance testing
384+
func NewRPCMsgInWithModuleConfig(mcfg *ModuleConfig, r *http.Request, postbody io.Reader) (*RPCMsgIn, error) {
385+
386+
now := time.Now()
387+
388+
// assemble a message to send to inspector
389+
tlsProtocol := ""
390+
tlsCipher := ""
391+
scheme := "http"
392+
if r.TLS != nil {
393+
// convert golang/spec integers into something human readable
394+
scheme = "https"
395+
tlsProtocol = tlstext.Version(r.TLS.Version)
396+
tlsCipher = tlstext.CipherSuite(r.TLS.CipherSuite)
397+
}
398+
399+
// golang removes Host header from req.Header map and
400+
// promotes it to r.Host field. Add it back as the first header.
401+
hin := convertHeaders(r.Header)
402+
if len(r.Host) > 0 {
403+
hin = append([][2]string{{"Host", r.Host}}, hin...)
404+
}
405+
406+
body, err := ioutil.ReadAll(postbody)
407+
if err != nil {
408+
return nil, err
409+
}
410+
411+
return &RPCMsgIn{
412+
ModuleVersion: mcfg.ModuleIdentifier(),
413+
ServerVersion: mcfg.ServerIdentifier(),
414+
ServerFlavor: mcfg.ServerFlavor(),
415+
ServerName: r.Host,
416+
Timestamp: now.Unix(),
417+
NowMillis: now.UnixNano() / 1e6,
418+
RemoteAddr: stripPort(r.RemoteAddr),
419+
Method: r.Method,
420+
Scheme: scheme,
421+
URI: r.RequestURI,
422+
Protocol: r.Proto,
423+
TLSProtocol: tlsProtocol,
424+
TLSCipher: tlsCipher,
425+
ResponseCode: -1,
426+
ResponseMillis: 0,
427+
ResponseSize: -1,
428+
PostBody: string(body),
429+
HeadersIn: hin,
430+
}, nil
431+
}
432+
375433
// stripPort removes any port from an address (e.g., the client port from the RemoteAddr)
376434
func stripPort(ipdots string) string {
377435
host, _, err := net.SplitHostPort(ipdots)

0 commit comments

Comments
 (0)