1
votes

A very generic title I know, my problem is that I am coding in lua and I'm trying to do a string.gsub but it just seems to not be finding my pattern. Below find all the details. Any help is appreciated.

Msg Variable contains:

{
    "started_at":1456170948017,
    "response":{
        "status":0,
        "size":"0",
        "headers":{
            "connection":"keep-alive"
        }
    },
    "authenticated_entity":{
        "consumer_id":"4be1d8e5-0270-489d-9538-42f05e00e2e6",
        "id":"39f1dbae-45ba-419a-abaa-d0750b5a2f4c"
    },
    "request":{
        "method":"POST",
        "uri":"\/SecureApi\/Logon",
        "size":"565",
        "request_uri":"http:\/\/192.168.10.122:8000\/SecureApi\/Logon",
        "querystring":{

        },
        "headers":{
            "x-consumer-id":"4be1d8e5-0270-489d-9538-42f05e00e2e6",
            "accept-language":"en-US,en;q=0.8",
            "content-type":"application\/json",
            "connection":"keep-alive",
            "content-length":"78",
            "x-consumer-username":"kylec",
            "cache-control":"no-cache",
            "host":"192.168.10.122:8000",
            "cookie":"ASP.NET_SessionId=gwbujeb4k43c0gsscgmwaynh",
            "accept-encoding":"gzip, deflate",
            "origin":"chrome-extension:\/\/fhbjgbiflinjbdggehcddcbncdddomop",
            "accept":"*\/*",
            "apikey":"test",
            "postman-token":"524e7363-a5fc-a2d4-53f4-97a425f5ae30",
            "user-agent":"Mozilla\/5.0 (Windows NT 10.0; WOW64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/48.0.2564.116 Safari\/537.36"
        }
    },
    "client_ip":"192.168.10.143",
    "api":{
        "upstream_url":"https:\/\/stage.site.com",
        "request_path":"\/SecureApi",
        "id":"90ba1710-1e50-4070-95d0-deb76d33e312",
        "created_at":1454625397000,
        "name":"secure_account_stage",
        "request_host":"stage.site.com"
    },
    "latencies":{
        "request":0,
        "kong":0,
        "proxy":-1
    }
}

Code:

string.gsub(msg, '"querystring":{}', '"querystring":{'..BuildURLParameters()..'}')
string.gsub(msg, '},"headers":{','},"body":{'..BuildBodyParameters()..'},"headers":{')

Expected Result:

{
    "started_at":1456170948017,
    "response":{
        "status":0,
        "size":"0",
        "headers":{
            "connection":"keep-alive"
        }
    },
    "authenticated_entity":{
        "consumer_id":"4be1d8e5-0270-489d-9538-42f05e00e2e6",
        "id":"39f1dbae-45ba-419a-abaa-d0750b5a2f4c"
    },
    "request":{
        "method":"POST",
        "uri":"\/SecureApi\/Logon",
        "size":"565",
        "request_uri":"http:\/\/192.168.10.122:8000\/SecureApi\/Logon",
        "querystring":{ somejsoninhere },
        "body":{ somejsoninhere },
        "headers":{
            "x-consumer-id":"4be1d8e5-0270-489d-9538-42f05e00e2e6",
            "accept-language":"en-US,en;q=0.8",
            "content-type":"application\/json",
            "connection":"keep-alive",
            "content-length":"78",
            "x-consumer-username":"kylec",
            "cache-control":"no-cache",
            "host":"192.168.10.122:8000",
            "cookie":"ASP.NET_SessionId=gwbujeb4k43c0gsscgmwaynh",
            "accept-encoding":"gzip, deflate",
            "origin":"chrome-extension:\/\/fhbjgbiflinjbdggehcddcbncdddomop",
            "accept":"*\/*",
            "apikey":"test",
            "postman-token":"524e7363-a5fc-a2d4-53f4-97a425f5ae30",
            "user-agent":"Mozilla\/5.0 (Windows NT 10.0; WOW64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/48.0.2564.116 Safari\/537.36"
        }
    },
    "client_ip":"192.168.10.143",
    "api":{
        "upstream_url":"https:\/\/stage.site.com",
        "request_path":"\/SecureApi",
        "id":"90ba1710-1e50-4070-95d0-deb76d33e312",
        "created_at":1454625397000,
        "name":"secure_account_stage",
        "request_host":"stage.site.com"
    },
    "latencies":{
        "request":0,
        "kong":0,
        "proxy":-1
    }
}

Result:

{
    "started_at":1456170948017,
    "response":{
        "status":0,
        "size":"0",
        "headers":{
            "connection":"keep-alive"
        }
    },
    "authenticated_entity":{
        "consumer_id":"4be1d8e5-0270-489d-9538-42f05e00e2e6",
        "id":"39f1dbae-45ba-419a-abaa-d0750b5a2f4c"
    },
    "request":{
        "method":"POST",
        "uri":"\/SecureApi\/Logon",
        "size":"565",
        "request_uri":"http:\/\/192.168.10.122:8000\/SecureApi\/Logon",
        "querystring":{

        },
        "headers":{
            "x-consumer-id":"4be1d8e5-0270-489d-9538-42f05e00e2e6",
            "accept-language":"en-US,en;q=0.8",
            "content-type":"application\/json",
            "connection":"keep-alive",
            "content-length":"78",
            "x-consumer-username":"kylec",
            "cache-control":"no-cache",
            "host":"192.168.10.122:8000",
            "cookie":"ASP.NET_SessionId=gwbujeb4k43c0gsscgmwaynh",
            "accept-encoding":"gzip, deflate",
            "origin":"chrome-extension:\/\/fhbjgbiflinjbdggehcddcbncdddomop",
            "accept":"*\/*",
            "apikey":"test",
            "postman-token":"524e7363-a5fc-a2d4-53f4-97a425f5ae30",
            "user-agent":"Mozilla\/5.0 (Windows NT 10.0; WOW64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/48.0.2564.116 Safari\/537.36"
        }
    },
    "client_ip":"192.168.10.143",
    "api":{
        "upstream_url":"https:\/\/stage.site.com",
        "request_path":"\/SecureApi",
        "id":"90ba1710-1e50-4070-95d0-deb76d33e312",
        "created_at":1454625397000,
        "name":"secure_account_stage",
        "request_host":"stage.site.com"
    },
    "latencies":{
        "request":0,
        "kong":0,
        "proxy":-1
    }
}
1
First of all: It would be better to just JSON decode it, add the data and JSON encode it again. Second of all: string.gsub does not ignore whitespace (like newlines) as you seem to think. e.g. '"querystring":{}' should be '"querystring":{%s*}' to make it also count newlines. - EinsteinK
@EinsteinK ok, I'm getting it. Although I'd have to look at how the data is structured (table, etc) if it isn't in JSON. Naively this data is coming from nginx. - Kyle

1 Answers

0
votes

As @EinsteinK pointed out, you are querying a string literal, that won't take whitespace into account.

The simplest way to get it working is exactly what he suggested: use [["querystring":{%s*}]].

To leave out redundantly writing "querystring", you can write:

string.gsub(msg, '("querystring"):{%s*}', '%1:{'..BuildURLParameters()..'}')

The parentheses put whatever matched that part of the expression into a capture and %n inserts the n-th of the captures.

You could use captures to indent it, but if pretty output is not a concern, you shouldn't bother with that.

Repl.it sample

Output:

{
    "started_at":1456170948017,
    "response":{
        "status":0,
        "size":"0",
        "headers":{
            "connection":"keep-alive"
        }
    },
    "authenticated_entity":{
        "consumer_id":"4be1d8e5-0270-489d-9538-42f05e00e2e6",
        "id":"39f1dbae-45ba-419a-abaa-d0750b5a2f4c"
    },
    "request":{
        "method":"POST",
        "uri":"\/SecureApi\/Logon",
        "size":"565",
        "request_uri":"http:\/\/192.168.10.122:8000\/SecureApi\/Logon",
        "querystring":{somejson},
        "headers":{
            "x-consumer-id":"4be1d8e5-0270-489d-9538-42f05e00e2e6",
            "accept-language":"en-US,en;q=0.8",
            "content-type":"application\/json",
            "connection":"keep-alive",
            "content-length":"78",
            "x-consumer-username":"kylec",
            "cache-control":"no-cache",
            "host":"192.168.10.122:8000",
            "cookie":"ASP.NET_SessionId=gwbujeb4k43c0gsscgmwaynh",
            "accept-encoding":"gzip, deflate",
            "origin":"chrome-extension:\/\/fhbjgbiflinjbdggehcddcbncdddomop",
            "accept":"*\/*",
            "apikey":"test",
            "postman-token":"524e7363-a5fc-a2d4-53f4-97a425f5ae30",
            "user-agent":"Mozilla\/5.0 (Windows NT 10.0; WOW64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/48.0.2564.116 Safari\/537.36"
        }
    },
    "client_ip":"192.168.10.143",
    "api":{
        "upstream_url":"https:\/\/stage.site.com",
        "request_path":"\/SecureApi",
        "id":"90ba1710-1e50-4070-95d0-deb76d33e312",
        "created_at":1454625397000,
        "name":"secure_account_stage",
        "request_host":"stage.site.com"
    },
    "latencies":{
        "request":0,
        "kong":0,
        "proxy":-1
    }
}   1