Perl URL Control and validation - perl

How can I validate a URL in perl. I mean, the query string has a specific order and all params have specific values (int or string or operator (%2B(+) ect..)).
example : www.mypage/question?name=john&age=21&operation=%2B
Can I control the order (name->age->operation) and the specific values of them?
Thanks

Yes and no.
No, you can't control the order of the parameters as they're submitted to you. Browsers may submit them in a different order, or the user may not be using a browser to submit the query to you.
Yes, you can control the order of the parameters you submit. But, don't rely on this.
You can use Data::Validate::URI to validate a URL.
You shouldn't use a complicated RegEx for controlling parameter values. Either the parameter matches or it doesn't. Either the parameter value is one of the expected options, or it isn't. If it matches, do something. If it doesn't match anything, don't do anything. (Or do a 404, but that can be leveraged against you.) Using anything more complex than "this parameter value should be all numbers" is asking for trouble.

Related

IBM Watson Assistant: Regular expressions with context variables

I am gathering some context variables with slots, and they work just fine.
So I decided to do in another node of the conversation, check if one of these context variables is a specific number:
I was thinking on enabling multi-responses and check if, for example $dni:1 (it is an integer, pattern of 1 integer only), or if it is 2 or 3:
But this is not working. I was trying to solve it for some days with different approaches but I really cannot find a way through it.
My guess is that a context variable has a value, and you can print it to use it like responding with the user's name and stuff like that (which indeed is useful!), but comparing values is not possible.
Any insights on this I can receive?
Watson Assistant uses a short-hand syntax but also supports the more complex expressions. What you could do is to edit the condition in the JSON editor. There, for the condition, use a function like matches() on the value of the context variable.
Note that it is not recommended to check for context variables in the slot conditions. You can use multi-responses. An alternative way is to put the check into the response itself. There, you can use predicates to generate the answer.
<? context.dni==1 ? 'Very well' : 'Your number is not 1' ?>
You can nest the evaluation to have three different answers. Another way is to build an array of responses and use dni as key.
Instead of matching to specific integers, you could consider using the Numbers system entity. Watson Assistant supports several languages. As a benefit, users could answer "the first one", "the 2nd option", etc., and the bot still would understand and your logic could still route to the correct answer.

DialogFlow: Alphanumeric (fixed length) parameter in Intents and #sys.any type

I have an intent in DialogFlow that accepts an input/parameter from the user for validation purposes. The parameter is of fixed length and is alphanumberic (e.g. ABC1234). It is a random ID different for different users. The data input is validated at the backend using a webhook call.
In order for the input value to be accepted, I have to set the type of the parameter as #sys.any. It does work and the value is passed through to webhook for validation. But since this intent has no "Input contexts", the next intents are never triggered because each input from the user is evaluated to#sys.any.
What I need is a type that accepts custom value. But I can't find any.
Here is screenshot for the intent:
Also note that the next intent that doesn't get triggered has the "In context" = authentication
And user says expressions like book me. Due to #sys.any in the intent above, book me causes the above intent to trigger.
This question has similar requirements but no answers: DialogFlow - Improve entity/parameter recognition for alphanumeric parameters
Also: https://discuss.api.ai/t/alphanumeric-support/2572
So after some digging, I had to change the approach. That is, #sys.any is not the way to go. Instead of alpha-numeric, I opted to use the numeric part of the input param (last four digits) and handled any duplication at the server end/webhook. The type I used instead is #sys.number-sequence.
I still had to handle the contexts in a way that any input of number-sequence doesn't trigger this intent.
This is not possible on Dialogflow. You can achieve that in your webhook with a regular expression for example.

Make my google home verify an oral code

I would like to build an app with a oral code verification.
i could just set my cde in dialogflow before then, juste verify it.
GH : "For continue, give me the code"
Me : " 1 2 3 4"
GH " Access granted" / "Access denied"
But how can do an input a get this code on dialogflow?
First of all - consider if you really want to do this. Having someone say a passcode out loud isn't really very secure and adds very little additional security in a multi-user environment.
There are two stages to this - the first is setting up an Intent to handle this, specifically in the format you want, and the second would be handling and verifying this is the correct code.
Setting up the Intent
We'll need two intents - one that prompts and sets a context so we know we're expecting the validation code, and one that checks for the code.
The prompting intent might look something like this:
The notable part here is that it is setting an output context. We'll see why that matters in a moment.
The one to handle numeric input might look like this:
There is a lot more to this one. First note that we're requiring an input context that matches the output context from the last Intent. This means that this Intent should only match if that Context has been set. This lets us talk about numbers elsewhere in our conversation without triggering this validation.
Next we're looking for sequences of numbers that match the #sys.number-sequence built-in Entity type. There are other entity types that may be useful for you - see the documentation for details and pick one that makes sense or experiment to find what works best in your case.
Finally, we're going to use a webhook for fulfillment to verify if the code is correct. Which is the next session...
Verifying the code
While there are ways to do the verification without a webhook, this is really the most straightforward way to do it. If you're using Google's library to handle input from Dialogflow, you can get the value with something like
var code = app.getArgument('number-sequence');
using whatever the parameter name is. If you're not using the library, you can find this in the JSON at result.parameters.number-sequence.
You would then verify this code, however you want, and return a message indicating if it is correct or not.
If you want to use a sequence of numbers as your code you can use the #sys.number-sequence entity to recognize it and then check the code in your webhook.
Another way would be to simply make a custom entity 'code' that has an entry of '1234'.

G-WAN: how to get rid of the "?" in URL and how to set default language?

In G-WAN the default URL is in the form mydomain.com/?hello.c
I want to get rid of the ? to have URLs that look like mydomain.com/hello
The user manual mentions substituting a different character such as ' for ?. In that case the URL would look like mydomain.com/'hello.c
But I don't want to use a different character, I want to get rid of the special character completely. Is that possible?
The default language for G-WAN is C. So mydomain.com/?hello means mydomain.com/?hello.c
How do I change the default to a different language, say Java, so that mydomain.com/?hello now means mydomain.com/?hello.java
Can I set different default languages for different virtual hosts?
Finally, how do I change the URL format for passing parameters? According to the user manual the default format is:
mydomain.com/?hello.c&name=Eva
I want to change it to:
mydomain.com/hello?name=Eva
Is that possible?
This has already been asked many times, and a few solutions are found here:
G-WAN handler rewriting solution
You should note, however, that the way you mean to pass arguments as ?something=answer instead of & only applies to the first argument passed. You can't do ?this=that?somethingelse=this because only the first can be ? and the rest must be &. In fact you can ignore using ? completely and only use & with virtually unlimited arguments so it's in fact better to stick to only using &.
It's important to note for future reference to anyone asking similar questions, G-WAN gives you the entire headers through multiple steps of the HTTP transaction and being that you can modify them with c/c++, you can change anything at all that you want before the requests are handled by the server or sent back to the client. The only limitation is your knowledge and imagination.

Does the locale belong on the path or as a request parameter on the URI?

I have seen the locale added to an URI as a path variable:
/en-US/blah/
or
/blah/en-US
and I have seen it as a request parameter:
/blah?lang=en-US
Is there a standard way that I should be doing it? If not what are the pros and cons of each approach?
I kind of like the request parameter approach because it doesn't require you to pass the locale with every URI (unless you use the second approach and add the locale to the end of the path). Any additional thoughts?
I believe the "standard way" is to use an HTTP "accept language" header. Other than that, if you think it ought to be a parameter (because it's a system-system call or you might request a language other than the browser locale) then it's just a parameter the same as anything else and you should handle it in a similar fashion.
Fun fact: even with this notation "/blah/en-US" it could still be a request parameter. Magic of mod_rewrite and friends.
If you need it as request parameter or part of the url depends of what you want to achieve. If you want to serve static content, you should have it be part of the path. If you want to act dynamically on the chosen locale, you should use it as request parameter, since you don't want to have your scripts replicated several times over different paths just to add different locales.