func (this *ServerType) CollectionServerHandler(w http.ResponseWriter, r *http.Request) {
	var err error
	var taxiiHeader headers.HttpHeaderType

	if this.SysConfig.Logging.LogLevel >= 3 {
		log.Printf("DEBUG-3: Found Message on Collection Server Handler from %s", r.RemoteAddr)
	}

	// We need to put this first so that during debugging we can see problems
	// that will generate errors below.
	if this.SysConfig.Logging.LogLevel >= 5 {
		taxiiHeader.DebugHttpRequest(r)
	}

	// --------------------------------------------------
	// Check HTTP Headers for correct TAXII values
	// --------------------------------------------------
	// Send a Status Message on error

	err = taxiiHeader.VerifyHttpTaxiiHeaderValues(r)
	if err != nil {
		if this.SysConfig.Logging.LogLevel >= 3 {
			log.Print(err)
		}

		// If the headers are not right we will not attempt to read the message.
		// This also means that we will not have an InReponseTo ID for the
		// createTaxiiStatusMessage function
		statusMessageData := this.CreateTaxiiStatusMessage("", "BAD_MESSAGE", err.Error())
		if this.SysConfig.Logging.LogLevel >= 1 {
			log.Println("DEBUG-1: BAD_MESSAGE", err.Error())
		}
		w.Header().Set("Content-Type", "application/json; charset=utf-8")
		w.Write(statusMessageData)
		return
	}

	// --------------------------------------------------
	// Decode incoming request message
	// --------------------------------------------------
	// Use decoder instead of unmarshal so we can handle stream data
	decoder := json.NewDecoder(r.Body)
	var incomingMessageData collectionMessage.CollectionRequestMessageType
	err = decoder.Decode(&incomingMessageData)

	if err != nil {
		statusMessageData := this.CreateTaxiiStatusMessage("", "BAD_MESSAGE", "Can not decode Collection Request")
		if this.SysConfig.Logging.LogLevel >= 1 {
			log.Println("DEBUG-1: BAD_MESSAGE, can not decode Collection Request")
		}
		w.Header().Set("Content-Type", "application/json; charset=utf-8")
		w.Write(statusMessageData)
		return
	}

	// Check to make sure their is a message ID in the request message
	if incomingMessageData.Id == "" {
		statusMessageData := this.CreateTaxiiStatusMessage("", "BAD_MESSAGE", "Collection Request message did not include an ID")
		if this.SysConfig.Logging.LogLevel >= 1 {
			log.Println("DEBUG-1: BAD_MESSAGE, Collection Request message did not include an ID")
		}
		w.Header().Set("Content-Type", "application/json; charset=utf-8")
		w.Write(statusMessageData)
		return
	}

	if this.SysConfig.Logging.LogLevel >= 1 {
		log.Printf("DEBUG-1: Collection Request from %s with ID: %s", r.RemoteAddr, incomingMessageData.Id)
	}

	// Get a list of valid collections for this collection request
	validCollections := this.SysConfig.GetValidCollections()

	data := this.createCollectionResponse(incomingMessageData.Id, validCollections)
	if this.SysConfig.Logging.LogLevel >= 1 {
		log.Println("DEBUG-1: Sending Collection Response to", r.RemoteAddr)
	}
	w.Header().Set("Content-Type", "application/json; charset=utf-8")
	w.Write(data)
}
Exemplo n.º 2
0
func (this *ServerType) PollServerHandler(w http.ResponseWriter, r *http.Request) {
	var err error
	var taxiiHeader headers.HttpHeaderType

	// Log notice of incoming TAXII message
	if this.SysConfig.Logging.LogLevel >= 3 {
		log.Printf("DEBUG-3: Found Message on Poll Server Handler from %s", r.RemoteAddr)
	}

	// We need to put this first so that during debugging we can see problems
	// that will generate errors below.
	if this.SysConfig.Logging.LogLevel >= 5 {
		taxiiHeader.DebugHttpRequest(r)
	}

	// --------------------------------------------------
	// Check HTTP Headers for correct TAXII values
	// --------------------------------------------------
	// Send a Status Message on error

	err = taxiiHeader.VerifyHttpTaxiiHeaderValues(r)
	if err != nil {
		if this.SysConfig.Logging.LogLevel >= 3 {
			log.Print(err)
		}

		// If the headers are not right we will not attempt to read the message.
		// This also means that we will not have an InReponseTo ID for the
		// createTaxiiStatusMessage function
		statusMessageData := this.CreateTaxiiStatusMessage("", "BAD_MESSAGE", err.Error())
		if this.SysConfig.Logging.LogLevel >= 1 {
			log.Println("DEBUG-1: BAD_MESSAGE", err.Error())
		}
		w.Header().Set("Content-Type", "application/json; charset=utf-8")
		w.Write(statusMessageData)
		return
	}

	// --------------------------------------------------
	// Decode incoming request message
	// --------------------------------------------------
	// Use decoder instead of unmarshal so we can handle stream data

	decoder := json.NewDecoder(r.Body)
	var incomingMessageData pollMessage.PollRequestMessageType
	err = decoder.Decode(&incomingMessageData)

	if err != nil {
		statusMessageData := this.CreateTaxiiStatusMessage("", "BAD_MESSAGE", "Can not decode Poll Request")
		if this.SysConfig.Logging.LogLevel >= 1 {
			log.Println("DEBUG-1: BAD_MESSAGE, can not decode Poll Request")
		}
		w.Header().Set("Content-Type", "application/json; charset=utf-8")
		w.Write(statusMessageData)
		return
	}

	// Check to make sure there is a message ID in the request message
	if incomingMessageData.Id == "" {
		statusMessageData := this.CreateTaxiiStatusMessage("", "BAD_MESSAGE", "Poll Request message did not include an ID")
		if this.SysConfig.Logging.LogLevel >= 1 {
			log.Println("DEBUG-1: BAD_MESSAGE, Poll Request message did not include an ID")
		}
		w.Header().Set("Content-Type", "application/json; charset=utf-8")
		w.Write(statusMessageData)
		return
	}

	// Log notice of incomming Poll Request
	if this.SysConfig.Logging.LogLevel >= 1 {
		log.Printf("DEBUG-1: Poll Request from %s for %s with ID: %s", r.RemoteAddr, incomingMessageData.CollectionName, incomingMessageData.Id)
	}

	// --------------------------------------------------
	// Check for valid collection
	// --------------------------------------------------

	currentlyValidCollections := this.SysConfig.GetValidCollections()

	// TODO First check to make sure the value the requested is something they can actually get by their username / subscription / avaliable
	// Based on the collection they are requesting, create a response that contains just the values for that collection

	if _, ok := currentlyValidCollections[incomingMessageData.CollectionName]; ok {
		data := this.createPollResponse(incomingMessageData.Id, incomingMessageData.CollectionName)

		if this.SysConfig.Logging.LogLevel >= 1 {
			log.Println("DEBUG-1: Sending Poll Response to", r.RemoteAddr)
		}
		w.Header().Set("Content-Type", "application/json; charset=utf-8")
		w.Write(data)
	} else {
		errmsg := "The requested collection \"" + incomingMessageData.CollectionName + "\" does not exist"
		statusMessageData := this.CreateTaxiiStatusMessage("", "DESTINATION_COLLECTION_ERROR", errmsg)
		if this.SysConfig.Logging.LogLevel >= 1 {
			log.Println("DEBUG-1: DESTINATION_COLLECTION_ERROR, Poll Request asked for a collection that does not exist")
		}

		w.Header().Set("Content-Type", "application/json; charset=utf-8")
		w.Write(statusMessageData)
	}

}