Varnish - double quotes in conditionals - varnish-vcl

Running Varnish 4.x - trying to test for "-" including the double quotes.
if (req.url == **'"-"'**) {
return (synth(200, "ok"));
}
Can't find the right escape character to include the double quotes in the conditional statement. Any ideas ?

Did you try escaping with %22?
if (req.url == "%22-%22") {
return (synth(200, "ok"));
}

I just ran into this issue. From 6.x on (at least) you should be able to use the {"..."} notation:
if (req.url == **{""-""}**) {
return (synth(200, "ok"));
}
See https://varnish-cache.org/docs/6.6/reference/vcl.html#strings

Related

How to use Logical operator OR " || " in ASP .NET

I've followed this question How to use IF statement in asp.net using C#, but I really can make it out with my code.
if ((txtNome.Value == null) || (txtNome.Value == (""))
{
}
Here is the error
The "||" operator cannot be applied a bool and string operands
I've tried all the possible solutions in the question above, but still not working. Some ideas?
Thank you
Solution 1
if ((txtNome.Value == null) || (txtNome.Value == ""))
{
}
Solution 2
The same as above but without the extra round brackets.
These are unnecessary for single logical statements.
if (txtNome.Value == null || txtNome.Value == "")
{
}
Solution 3
Built in function for the above in C#
if (string.IsNullOrEmpty(txtNome.Value))
{
}

Premature end of file lex

When I tried to compile it using make keyword it is giving me an error of:
premature end of file in lex.l file in line no 17.
%option noyywrap
%{
#include "grammer.tab.h"
%}
name ([0-9])
whitespace [ \r\t\v\f]
linefeed \n
%%
{name} { return NAME; }
":" { return COLON; }
"->" { return RIGHT_ARROW; }
"{" { return LEFT_BRACE;}
"}" { return RIGHT_BRACE;}
";" { return SEMICOLON;}
{whitespace}
{linefeed} ++yylineno;
%%
So someone kindly help me.
Error:-
Tail:-
enter image description here
You usually get this error from lex (or flex) when the last line is not terminated by a newline.
To resolve the error just put a blank line at the end of the file.
(The same is also true for yacc/bison)
I also note you have a missing action for the pattern {whitespace}. I suggest you might try:
{whitespace} ; /* No action */
%%
/* End of the file */

Using jsonPath looking for a string

I'm trying to use jsonPath and the pick function to determine if a rule needs to run or not based on the current domain. A simplified version of what I'm doing is here:
global
{
dataset shopscotchMerchants <- "https://s3.amazonaws.com/app-files/dev/merchantJson.json" cachable for 2 seconds
}
rule checkdataset is active
{
select when pageview ".*" setting ()
pre
{
merchantData = shopscotchMerchants.pick("$.merchants[?(#.merchant=='Telefora')]");
}
emit
<|
console.log(merchantData);
|>
}
The console output I expect is the telefora object, instead I get all three objects from the json file.
If instead of merchant=='Telefora' I use merchantID==16 then it works great. I thought jsonPath could do matches to strings as well. Although the example above isn't searching against the merchantDomain part of the json, I'm experiencing the same problem with that.
Your problem comes from the fact that, as stated in the documentation, the string equality operators are eq, neq, and like. == is only for numbers. In your case, you want to test if one string is equal to another string, which is the job of the eq string equality operator.
Simply swap == for eq in you JSONpath filter expression and you will be good to go:
global
{
dataset shopscotchMerchants <- "https://s3.amazonaws.com/app-files/dev/merchantJson.json" cachable for 2 seconds
}
rule checkdataset is active
{
select when pageview ".*" setting ()
pre
{
merchantData = shopscotchMerchants.pick("$.merchants[?(#.merchant eq 'Telefora')]"); // replace == with eq
}
emit
<|
console.log(merchantData);
|>
}
I put this to the test in my own test ruleset, the source for which is below:
ruleset a369x175 {
meta {
name "test-json-filtering"
description <<
>>
author "AKO"
logging on
}
dispatch {
domain "exampley.com"
}
global {
dataset merchant_dataset <- "https://s3.amazonaws.com/app-files/dev/merchantJson.json" cachable for 2 seconds
}
rule filter_some_delicous_json {
select when pageview "exampley.com"
pre {
merchant_data = merchant_dataset.pick("$.merchants[?(#.merchant eq 'Telefora')]");
}
{
emit <|
try { console.log(merchant_data); } catch(e) { }
|>;
}
}
}

how to break out of "if" block in VB.NET

How can I break out of an if statement?
Exit only works for "for", "sub", etc.
In VB.net:
if i > 0 then
do stuff here!
end if
In C#:
if (i > 0)
{
do stuff here!
}
You can't 'break out' of an if statement. If you are attempting this, your logic is wrong and you are approaching it from the wrong angle.
An example of what you are trying to achieve would help clarify, but I suspect you are structuring it incorrectly.
There isn't such an equivalent but you should't really need to with an If statement. You might want to look into using Select Case (VB) or Switch (C#) statements.
In C# .NET:
if (x > y)
{
if (x > z)
{
return;
}
Console.Writeline("cool");
}
Or you could use the goto statement.
You can use
bool result = false;
if (i < 10)
{
if (i == 7)
{
result = true;
break;
}
}
return result;
I have to admit, that in some cases you really wanna have something like an exit sub or a break. On a rare occasion is I use "Goto End" and jump over the "End If" with the def. End:
I know this is an old post but I have been looking for the same answer then eventually I figured it out
try{
if (i > 0) // the outer if condition
{
Console.WriteLine("Will work everytime");
if (i == 10)//inner if condition.when its true it will break out of the outer if condition
{
throw new Exception();
}
Console.WriteLine("Will only work when the inner if is not true");
}
}
catch (Exception ex)
{
// you can add something if you want
}
`

C# alternative for javascript escape function

what is an alternative for javascript escape function in c# for e.g suppose a string:"Hi Foster's i'm missing /you" will give "Hi%20Foster%27s%20i%27m%20missing%20/you" if we use javascript escape function, but what is the alternative for c#. i have searched for it but no use.
You can use:
string encoded = HttpUtility.JavaScriptStringEncode(str);
Note: You need at least ASP.NET 4.0 to run the above code.
var unescapedString = Microsoft.JScript.GlobalObject.unescape(yourEscapedString);
var escapedString = Microsoft.JScript.GlobalObject.escape(yourUnescapedString);
The best solution I've seen is mentioned on this blog - C#: Equivalent of JavaScript escape function by Kaushik Chakraborti. There is more to escaping javascript than simply url-encoding or replacing spaces with entities.
Following is the escape function implementation that you will find in Microsoft.JScript.dll...
[NotRecommended("escape"), JSFunction(JSFunctionAttributeEnum.None, JSBuiltin.Global_escape)]
public static string escape(string str)
{
string str2 = "0123456789ABCDEF";
int length = str.Length;
StringBuilder builder = new StringBuilder(length * 2);
int num3 = -1;
while (++num3 < length)
{
char ch = str[num3];
int num2 = ch;
if ((((0x41 > num2) || (num2 > 90)) &&
((0x61 > num2) || (num2 > 0x7a))) &&
((0x30 > num2) || (num2 > 0x39)))
{
switch (ch)
{
case '#':
case '*':
case '_':
case '+':
case '-':
case '.':
case '/':
goto Label_0125;
}
builder.Append('%');
if (num2 < 0x100)
{
builder.Append(str2[num2 / 0x10]);
ch = str2[num2 % 0x10];
}
else
{
builder.Append('u');
builder.Append(str2[(num2 >> 12) % 0x10]);
builder.Append(str2[(num2 >> 8) % 0x10]);
builder.Append(str2[(num2 >> 4) % 0x10]);
ch = str2[num2 % 0x10];
}
}
Label_0125:
builder.Append(ch);
}
return builder.ToString();
}
Code taken from Reflector.
The best solution I've seen is mentioned on this blog - C#: Equivalent of JavaScript escape function by Kaushik Chakraborti. There is more to escaping javascript than simply url-encoding or replacing spaces with entities.
I noticed another solution in the comments in KodeSharp article that may be better. The comment says it is more compatible with UTF-8 and does not require the reference to JScript. Is this better?
(Dependent on System.Web.Extensions.dll)
using System.Web.Script.Serialization;
JavaScriptSerializer serialiser = new JavaScriptSerializer();
serialiser.Serialize("some \"string\"")
string myString = "Hello my friend";
myString = myString.Replace(" ", "%20");
This would replace all " " with "%20".
Is this what you wanted?
You can try this
Uri.EscapeDataString(Obj);

Resources