@@ -3,6 +3,7 @@ package sigsci
3
3
import (
4
4
"bytes"
5
5
"fmt"
6
+ "io"
6
7
"io/ioutil"
7
8
"log"
8
9
"net"
@@ -225,8 +226,13 @@ func (m *Module) inspectorPreRequest(req *http.Request) (inspin2 RPCMsgIn2, out
225
226
req .Body = ioutil .NopCloser (bytes .NewBuffer (reqbody ))
226
227
}
227
228
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
+ }
230
236
m .extractHeaders (req , inspin )
231
237
232
238
if m .config .Debug () {
@@ -372,6 +378,58 @@ func NewRPCMsgIn(r *http.Request, postbody []byte, code int, size int64, dur tim
372
378
}
373
379
}
374
380
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
+
375
433
// stripPort removes any port from an address (e.g., the client port from the RemoteAddr)
376
434
func stripPort (ipdots string ) string {
377
435
host , _ , err := net .SplitHostPort (ipdots )
0 commit comments