Example #1
0
func handleRequest(w http.ResponseWriter, req *http.Request) {
	fmt.Println("Handling Request:", req.URL.Path)
	m := NewMessage(req.URL.Path[1:])
	json := json.NewEncoder(w)
	w.Header().Set("Content-Type", "application/json")
	json.Encode(m)
}
Example #2
0
// Replaces the object of the given type and ID with the new object in the Request. Returns
// the Response object from TripIt.
// supports: air, activity, car, cruise, directions, lodging, map, note, rail, restaurant, transport, trip
func (t *TripIt) Replace(objectType string, objectId uint, r *Request) (*Response, error) {
	b := new(bytes.Buffer)
	json := json.NewEncoder(b)
	err := json.Encode(r)
	if err != nil {
		return nil, err
	}
	req, err := http.NewRequest("POST", fmt.Sprintf("%s/%s/replace/%s/id/%d/format/json", t.baseUrl, t.version, objectType, objectId), b)
	if err != nil {
		return nil, err
	}
	t.credentials.Authorize(req, nil)
	return t.makeRequest(req)
}
Example #3
0
// HandleSTHPollination handles requests POSTed to .../sth-pollination.
// It attempts to store the provided pollination info, and returns a random set of
// pollination data from the last 14 days (i.e. "fresh" by the definition of the gossip RFC.)
func (h *Handler) HandleSTHPollination(rw http.ResponseWriter, req *http.Request) {
	if req.Method != "POST" {
		writeWrongMethodResponse(&rw, "POST")
		return
	}

	decoder := json.NewDecoder(req.Body)
	var p STHPollination
	if err := decoder.Decode(&p); err != nil {
		writeErrorResponse(&rw, http.StatusBadRequest, fmt.Sprintf("Invalid STH Pollination received: %v", err))
		return
	}

	sthToKeep := make([]ct.SignedTreeHead, 0, len(p.STHs))
	for _, sth := range p.STHs {
		v, found := h.verifiers[sth.LogID]
		if !found {
			log.Printf("Pollination entry for unknown logID: %s", sth.LogID.Base64String())
			continue
		}
		if err := v.VerifySTHSignature(sth); err != nil {
			log.Printf("Failed to verify STH, dropping: %v", err)
			continue
		}
		sthToKeep = append(sthToKeep, sth)
	}
	p.STHs = sthToKeep

	err := h.storage.AddSTHPollination(p)
	if err != nil {
		writeErrorResponse(&rw, http.StatusInternalServerError, fmt.Sprintf("Couldn't store pollination: %v", err))
		return
	}

	freshTime := h.clock.Now().AddDate(0, 0, -14)
	rp, err := h.storage.GetRandomSTHPollination(freshTime, *defaultNumPollinationsToReturn)
	if err != nil {
		writeErrorResponse(&rw, http.StatusInternalServerError, fmt.Sprintf("Couldn't fetch pollination to return: %v", err))
		return
	}

	json := json.NewEncoder(rw)
	if err := json.Encode(*rp); err != nil {
		writeErrorResponse(&rw, http.StatusInternalServerError, fmt.Sprintf("Couldn't encode pollination to return: %v", err))
		return
	}
}
func (s *Socket) writeSocket() {

	ticker := time.NewTicker(pingPeriod)

	defer func() {
		ticker.Stop()
		s.connection.Close()
	}()

	for {
		select {
		case message, ok := <-s.write:
			if !ok {
				s.writeMessage(websocket.CloseMessage, []byte{})
				return
			}

			json := parseOutgoing(message)
			payload, err := json.Encode()

			if err != nil {
				log.Fatal("! Json Encode Error:", err)
				break
			}

			if err := s.writeMessage(websocket.TextMessage, payload); err != nil {
				log.Fatal(err)
				return
			}

			log.Println("# Sending Message: ", message)

		case <-ticker.C:
			if err := s.writeMessage(websocket.PingMessage, []byte{}); err != nil {
				return
			}
		}
	}

}