func IndexWS(access *Access, errlog *log.Logger, minperiod flags.Period, w http.ResponseWriter, req *http.Request) { // Upgrader.Upgrade() has Origin check if .CheckOrigin is nil upgrader := gorillawebsocket.Upgrader{ HandshakeTimeout: 5 * time.Second, } wsconn, err := upgrader.Upgrade(w, req, nil) if err != nil { // Upgrade() does http.Error() to the client return } // req.Method == "GET" asserted by the mux req.Form = nil // reset reused later .Form c := &conn{ Conn: wsconn, requestOrigin: req, receive: make(chan *received, 2), pushch: make(chan *IndexUpdate, 2), para: params.NewParams(minperiod), access: access, } Register <- c defer func() { unregister <- c c.Conn.Close() }() stop := make(chan struct{}, 1) go c.receiveLoop(stop) // read from the client c.updateLoop(errlog, stop) // write to the client }
func indexData(minperiod flags.Period, req *http.Request) (IndexData, error) { if Connections.Len() == 0 { // collect when there're no active connections, so Loop does not collect lastInfo.collect(&Machine{}) } para := params.NewParams(minperiod) updates, err := getUpdates(req, para, true) if err != nil { return IndexData{}, err } data := IndexData{ DISTRIB: DISTRIB, // value set in init() VERSION: VERSION, // value from server.go Params: updates.Params, Generic: updates.Generic, ExpandableDF: updates.ExpandableDF, ExpandtextDF: updates.ExpandtextDF, ExpandableIF: updates.ExpandableIF, ExpandtextIF: updates.ExpandtextIF, } if updates.CPU != nil { data.CPU = *updates.CPU } if updates.MEM != nil { data.MEM = *updates.MEM } if updates.PStable != nil { data.PStable = *updates.PStable } if updates.DFbytes != nil { data.DFbytes = *updates.DFbytes } if updates.DFinodes != nil { data.DFinodes = *updates.DFinodes } if updates.IFbytes != nil { data.IFbytes = *updates.IFbytes } if updates.IFerrors != nil { data.IFerrors = *updates.IFerrors } if updates.IFpackets != nil { data.IFpackets = *updates.IFpackets } data.VagrantMachines = updates.VagrantMachines data.VagrantError = updates.VagrantError data.VagrantErrord = updates.VagrantErrord return data, nil }
func FormRedirectFunc(minperiod flags.Period, wrap func(http.HandlerFunc) http.Handler) func(http.ResponseWriter, *http.Request, httprouter.Params) { return func(w http.ResponseWriter, r *http.Request, muxpara httprouter.Params) { wrap(func(w http.ResponseWriter, r *http.Request) { where := "/" if q := muxpara.ByName("Q"); q != "" { r.URL.RawQuery = r.Form.Encode() + "&" + strings.TrimPrefix(q, "?") r.Form = nil // reset the .Form for .ParseForm() to parse new r.URL.RawQuery. para := params.NewParams(minperiod) DecodeParam(para, r) // OR err.Error() where = "/?" + para.Query.ValuesEncode(nil) } http.Redirect(w, r, where, http.StatusFound) }).ServeHTTP(w, r) } }