Commit c17eb094 authored by Ikram Haq's avatar Ikram Haq
Browse files

Fix valid address attribute and query parameter issues in distance subscription

parent 9148382e
Loading
Loading
Loading
Loading
+229 −183
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ import (
	"encoding/json"
	"errors"
	"fmt"
	"net"
	"net/http"
	"net/url"
	"os"
@@ -640,6 +641,9 @@ func sendReadyConfirmation(appInstanceId string) error {
	}
	return nil
}
func isValidIPAddress(ip string) bool {
	return net.ParseIP(ip) != nil
}

// sendTerminationConfirmation sends a termination confirmation message for the given application instance ID
// to indicate that the application is terminating to the App Support API.
@@ -2143,6 +2147,7 @@ func sendNotification_1(subsId int, zoneId string, userId string, eventType Loca
// Finally, it constructs the response containing the user list and sends it as a JSON response.
//
// Parameters:
//
//	w: http.ResponseWriter - an interface used to manipulate the HTTP response.
//	r: *http.Request - a pointer to the HTTP request received.
//
@@ -2295,6 +2300,7 @@ func populateUserList(key string, jsonInfo string, userData interface{}) error {
// apGet retrieves information about access points within a specific zone and returns it as a JSON response.
//
// Parameters:
//
//	w: http.ResponseWriter - an interface used to manipulate the HTTP response.
//	r: *http.Request - a pointer to the HTTP request received.
//
@@ -2444,6 +2450,7 @@ func apByIdGet(w http.ResponseWriter, r *http.Request) {
// the JSON response to the HTTP response writer.
//
// Parameters:
//
//	w: http.ResponseWriter - an interface used to manipulate the HTTP response.
//	r: *http.Request - a pointer to the HTTP request received.
func zonesGet(w http.ResponseWriter, r *http.Request) {
@@ -2516,6 +2523,7 @@ func buildResourceURL(baseURL, basePath string, zoneIDs []string) string {
// the JSON response to the HTTP response writer.
//
// Parameters:
//
//	w: http.ResponseWriter - an interface used to manipulate the HTTP response.
//	r: *http.Request - a pointer to the HTTP request received.
func zonesByIdGet(w http.ResponseWriter, r *http.Request) {
@@ -2621,6 +2629,7 @@ func populateApList(key string, jsonInfo string, userData interface{}) error {
// distanceSubDelete deletes a distance subscription identified by its ID and returns a JSON response with no content.
//
// Parameters:
//
//	w: http.ResponseWriter - an interface used to manipulate the HTTP response.
//	r: *http.Request - a pointer to the HTTP request received.
//
@@ -2659,6 +2668,7 @@ func distanceSubDelete(w http.ResponseWriter, r *http.Request) {
// distanceSubListGet retrieves a list of distance subscriptions and returns them as a JSON response.
//
// Parameters:
//
//	w: http.ResponseWriter - an interface used to manipulate the HTTP response.
//	r: *http.Request - a pointer to the HTTP request received.
//
@@ -2678,7 +2688,11 @@ func distanceSubDelete(w http.ResponseWriter, r *http.Request) {
// If there's an error during JSON marshaling, it returns a 500 Internal Server Error status code using errHandlerProblemDetails.
func distanceSubListGet(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json; charset=UTF-8")

	// Check for query parameters
	if len(r.URL.Query()) > 0 {
		errHandlerProblemDetails(w, "Query parameters are not allowed", http.StatusBadRequest)
		return
	}
	var response InlineNotificationSubscriptionList
	var subscriptions []Subscription

@@ -2711,6 +2725,7 @@ func distanceSubListGet(w http.ResponseWriter, r *http.Request) {
// distanceSubGet retrieves a distance subscription identified by its ID and returns it as a JSON response.
//
// Parameters:
//
//	w: http.ResponseWriter - an interface used to manipulate the HTTP response.
//	r: *http.Request - a pointer to the HTTP request received.
//
@@ -2762,6 +2777,7 @@ func distanceSubGet(w http.ResponseWriter, r *http.Request) {
// distanceSubPost creates a new distance subscription based on the provided data in the request body and returns the created subscription as a JSON response.
//
// Parameters:
//
//	w: http.ResponseWriter - an interface used to manipulate the HTTP response.
//	r: *http.Request - a pointer to the HTTP request received.
//
@@ -2863,6 +2879,7 @@ func distanceSubPost(w http.ResponseWriter, r *http.Request) {
// distanceSubPut updates an existing distance subscription with the provided data in the request body and returns the updated subscription as a JSON response.
//
// Parameters:
//
//	w: http.ResponseWriter - an interface used to manipulate the HTTP response.
//	r: *http.Request - a pointer to the HTTP request received.
//
@@ -3048,6 +3065,7 @@ func populateDistanceList(key string, jsonInfo string, userData interface{}) err
// areaSubDELETE deletes a subscription for a specific area or circle identified by its ID and returns a JSON response.
//
// Parameters:
//
//	w: http.ResponseWriter - an interface used to manipulate the HTTP response.
//	r: *http.Request - a pointer to the HTTP request received.
//
@@ -3085,6 +3103,7 @@ func areaSubDELETE(w http.ResponseWriter, r *http.Request) {
// areaSubListGET retrieves a list of area or circle subscriptions and returns it as a JSON response.
//
// Parameters:
//
//	w: http.ResponseWriter - an interface used to manipulate the HTTP response.
//	r: *http.Request - a pointer to the HTTP request received.
//
@@ -3136,6 +3155,7 @@ func areaSubListGET(w http.ResponseWriter, r *http.Request) {
// areaSubGET retrieves information about a subscription for a specific area or circle identified by its ID and returns it as a JSON response.
//
// Parameters:
//
//	w: http.ResponseWriter - an interface used to manipulate the HTTP response.
//	r: *http.Request - a pointer to the HTTP request received.
//
@@ -3188,6 +3208,7 @@ func areaSubGET(w http.ResponseWriter, r *http.Request) {
// areaSubPOST creates a new subscription for a specific area or circle and returns the created subscription as a JSON response.
//
// Parameters:
//
//	w: http.ResponseWriter - an interface used to manipulate the HTTP response.
//	r: *http.Request - a pointer to the HTTP request received.
//
@@ -3309,6 +3330,7 @@ func areaSubPOST(w http.ResponseWriter, r *http.Request) {
// areaSubPUT updates an existing subscription for a specific area or circle and returns the updated subscription as a JSON response.
//
// Parameters:
//
//	w: http.ResponseWriter - an interface used to manipulate the HTTP response.
//	r: *http.Request - a pointer to the HTTP request received.
//
@@ -3509,6 +3531,7 @@ func populateUserAreaList(key string, jsonInfo string, userData interface{}) err
// It returns the list of subscriptions as a JSON response.
//
// Parameters:
//
//	w: http.ResponseWriter - an interface used to manipulate the HTTP response.
//	r: *http.Request - a pointer to the HTTP request received.
//
@@ -3624,6 +3647,7 @@ func userSubListGET(w http.ResponseWriter, r *http.Request) {
// If a subscription is found in either type, it deletes the subscription entry and deregisters it from the respective tracking system.
//
// Parameters:
//
//	w: http.ResponseWriter - an interface used to manipulate the HTTP response.
//	r: *http.Request - a pointer to the HTTP request received.
//
@@ -3693,6 +3717,7 @@ func userSubDELETE(w http.ResponseWriter, r *http.Request) {
// If no subscription is found for the given ID, it returns a 404 Not Found response.
//
// Parameters:
//
//	w: http.ResponseWriter - an interface used to manipulate the HTTP response.
//	r: *http.Request - a pointer to the HTTP request received.
//
@@ -3761,6 +3786,7 @@ func userSubGET(w http.ResponseWriter, r *http.Request) {
// Otherwise, it delegates the handling to the appropriate function based on the type of subscription.
//
// Parameters:
//
//	w: http.ResponseWriter - an interface used to manipulate the HTTP response.
//	r: *http.Request - a pointer to the HTTP request received.
//
@@ -3816,6 +3842,7 @@ func userSubPOST(w http.ResponseWriter, r *http.Request) {
// If successful, it returns a 201 Created response with the created subscription details.
//
// Parameters:
//
//	w: http.ResponseWriter - an interface used to manipulate the HTTP response.
//	requestBody: map[string]interface{} - a map containing the request body decoded from JSON.
//
@@ -3852,6 +3879,11 @@ func handleUserLocationEventSubscription(w http.ResponseWriter, requestBody map[
			errHandlerProblemDetails(w, "Mandatory Address parameter not present", http.StatusBadRequest)
			return
		}
		if !isValidIPAddress(userSubBody.Address) {
			log.Error("Invalid Address parameter")
			errHandlerProblemDetails(w, "Invalid Address parameter", http.StatusBadRequest)
			return
		}
		if userSubBody.SubscriptionType != "UserLocationEventSubscription" {
			log.Error("Mandatory SubscriptionType parameter not present or invalid")
			errHandlerProblemDetails(w, "Mandatory SubscriptionType parameter not present or invalid", http.StatusBadRequest)
@@ -3901,6 +3933,7 @@ func handleUserLocationEventSubscription(w http.ResponseWriter, requestBody map[
// If successful, it returns a 201 Created response with the created subscription details.
//
// Parameters:
//
//	w: http.ResponseWriter - an interface used to manipulate the HTTP response.
//	requestBody: map[string]interface{} - a map containing the request body decoded from JSON.
//
@@ -3991,6 +4024,7 @@ func handleUserLocationPeriodicSubscription(w http.ResponseWriter, requestBody m
// If neither type of subscription is found in the request body, it constructs and returns a Bad Request response.
//
// Parameters:
//
//	w: http.ResponseWriter - an interface used to manipulate the HTTP response.
//	r: *http.Request - a pointer to the HTTP request received.
func zoneSubPUT(w http.ResponseWriter, r *http.Request) {
@@ -4035,6 +4069,7 @@ func zoneSubPUT(w http.ResponseWriter, r *http.Request) {
// If only one type of subscription is present, it calls the corresponding handler function to process the update.
//
// Parameters:
//
//	w: http.ResponseWriter - an interface used to manipulate the HTTP response.
//	r: *http.Request - a pointer to the HTTP request received.
//
@@ -4097,6 +4132,7 @@ func userSubPUT(w http.ResponseWriter, r *http.Request) {
// registers the updated subscription, constructs the response, marshals it into JSON format, and writes it to the HTTP response.
//
// Parameters:
//
//	w: http.ResponseWriter - an interface used to manipulate the HTTP response.
//	requestBody: map[string]interface{} - a map containing the subscription details extracted from the request body.
//	subscriptionID: string - the ID of the subscription extracted from the endpoint URL.
@@ -4208,6 +4244,7 @@ func handlezoneLocationEventSubscriptionPut(w http.ResponseWriter, requestBody m
// registers the updated subscription, constructs the response, marshals it into JSON format, and writes it to the HTTP response.
//
// Parameters:
//
//	w: http.ResponseWriter - an interface used to manipulate the HTTP response.
//	requestBody: map[string]interface{} - a map containing the subscription details extracted from the request body.
//	subscriptionID: string - the ID of the subscription extracted from the endpoint URL.
@@ -4309,6 +4346,7 @@ func handlezoneStatusSubscriptionPut(w http.ResponseWriter, requestBody map[stri
// and performs the update operation.
//
// Parameters:
//
//	w: http.ResponseWriter - an interface used to manipulate the HTTP response.
//	requestBody: map[string]interface{} - the request body containing the event-based subscription information.
//	subscriptionID: string - the ID of the subscription to be updated.
@@ -4412,6 +4450,7 @@ func handleUserLocationEventSubscriptionPut(w http.ResponseWriter, requestBody m
// and performs the update operation.
//
// Parameters:
//
//	w: http.ResponseWriter - an interface used to manipulate the HTTP response.
//	requestBody: map[string]interface{} - the request body containing the periodic-based subscription information.
//	subscriptionID: string - the ID of the subscription to be updated.
@@ -4608,6 +4647,7 @@ func populateUserSubList(data []byte, address string, subscriptions *[]Subscript
// Finally, it constructs the response containing the subscription list and sends it as a JSON response.
//
// Parameters:
//
//	w: http.ResponseWriter - an interface used to manipulate the HTTP response.
//	r: *http.Request - a pointer to the HTTP request received.
//
@@ -4720,6 +4760,7 @@ func zoneSubListGET(w http.ResponseWriter, r *http.Request) {
// If a zone status subscription is found, it performs similar deletion and deregistration steps as above.
//
// Parameters:
//
//	w: http.ResponseWriter - an interface used to manipulate the HTTP response.
//	r: *http.Request - a pointer to the HTTP request received.
func zoneSubDELETE(w http.ResponseWriter, r *http.Request) {
@@ -4770,6 +4811,7 @@ func zoneSubDELETE(w http.ResponseWriter, r *http.Request) {
// Finally, it marshals the response into JSON format and writes it to the HTTP response body.
//
// Parameters:
//
//	w: http.ResponseWriter - an interface used to manipulate the HTTP response.
//	r: *http.Request - a pointer to the HTTP request received.
func zoneSubGET(w http.ResponseWriter, r *http.Request) {
@@ -4821,6 +4863,7 @@ func zoneSubGET(w http.ResponseWriter, r *http.Request) {
// If both types of subscriptions are present in the request body, it returns a 400 Bad Request response.
//
// Parameters:
//
//	w: http.ResponseWriter - an interface used to manipulate the HTTP response.
//	r: *http.Request - a pointer to the HTTP request received.
func zoneSubPOST(w http.ResponseWriter, r *http.Request) {
@@ -4867,6 +4910,7 @@ func zoneSubPOST(w http.ResponseWriter, r *http.Request) {
// Otherwise, it registers the subscription and returns a 201 Created response with the subscription details.
//
// Parameters:
//
//	w: http.ResponseWriter - an interface used to manipulate the HTTP response.
//	requestBody: map[string]interface{} - the request body containing the subscription details.
func handleZoneStatusSubscription(w http.ResponseWriter, requestBody map[string]interface{}) {
@@ -4945,6 +4989,7 @@ func handleZoneStatusSubscription(w http.ResponseWriter, requestBody map[string]
// Otherwise, it registers the subscription and returns a 201 Created response with the subscription details.
//
// Parameters:
//
//	w: http.ResponseWriter - an interface used to manipulate the HTTP response.
//	requestBody: map[string]interface{} - the request body containing the subscription details.
func handleZoneLocationEventSubscription(w http.ResponseWriter, requestBody map[string]interface{}) {
@@ -5673,6 +5718,7 @@ func userLocationPeriodicReInit() {
// distanceGet calculates the distance between two geographical points or addresses and returns the result as a JSON response.
//
// Parameters:
//
//	w: http.ResponseWriter - an interface used to manipulate the HTTP response.
//	r: *http.Request - a pointer to the HTTP request received.
//